Project

General

Profile

Download (11.6 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / main / RubikObjectLibInterface.java @ 0c52f5da

1 1cd323dd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.main;
21
22
import android.os.Bundle;
23
24
import androidx.annotation.NonNull;
25
26
import com.google.android.play.core.review.ReviewInfo;
27
import com.google.android.play.core.review.ReviewManager;
28
import com.google.android.play.core.review.ReviewManagerFactory;
29
import com.google.android.play.core.tasks.OnCompleteListener;
30
import com.google.android.play.core.tasks.OnFailureListener;
31
import com.google.android.play.core.tasks.Task;
32
import com.google.firebase.analytics.FirebaseAnalytics;
33
34 0c52f5da Leszek Koltunski
import org.distorted.library.main.DistortedFramebuffer;
35 e019c70b Leszek Koltunski
import org.distorted.objectlib.helpers.ObjectLibInterface;
36
import org.distorted.objectlib.main.ObjectControl;
37 1cd323dd Leszek Koltunski
import org.distorted.objectlib.main.ObjectType;
38
39
import org.distorted.dialogs.RubikDialogNewRecord;
40
import org.distorted.dialogs.RubikDialogSolved;
41
import org.distorted.network.RubikScores;
42
import org.distorted.screens.RubikScreenPlay;
43 dd1a65c1 Leszek Koltunski
import org.distorted.screens.RubikScreenReady;
44
import org.distorted.screens.RubikScreenSolver;
45 1cd323dd Leszek Koltunski
import org.distorted.screens.RubikScreenSolving;
46
import org.distorted.screens.ScreenList;
47 dd1a65c1 Leszek Koltunski
import org.distorted.solvers.SolverMain;
48 1cd323dd Leszek Koltunski
49 e019c70b Leszek Koltunski
import java.lang.ref.WeakReference;
50
51 1cd323dd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 e019c70b Leszek Koltunski
public class RubikObjectLibInterface implements ObjectLibInterface
54 1cd323dd Leszek Koltunski
{
55 e019c70b Leszek Koltunski
  WeakReference<RubikActivity> mAct;
56 1cd323dd Leszek Koltunski
  private boolean mIsNewRecord;
57
  private long mNewRecord;
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 e019c70b Leszek Koltunski
  RubikObjectLibInterface(RubikActivity act)
62
    {
63
    mAct = new WeakReference<>(act);
64
    }
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68
  private void analyticsReport(RubikActivity act, String message, String name, long timeBegin)
69 1cd323dd Leszek Koltunski
    {
70
    long elapsed = System.currentTimeMillis() - timeBegin;
71
    String msg = message+" startTime: "+timeBegin+" elapsed: "+elapsed+" name: "+name;
72
73
    if( BuildConfig.DEBUG )
74
       {
75
       android.util.Log.d("pre", msg);
76
       }
77
    else
78
      {
79 e019c70b Leszek Koltunski
      FirebaseAnalytics analytics = act.getAnalytics();
80 1cd323dd Leszek Koltunski
81
      if( analytics!=null )
82
        {
83
        Bundle bundle = new Bundle();
84
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, msg);
85
        analytics.logEvent(FirebaseAnalytics.Event.SHARE, bundle);
86
        }
87
      }
88
    }
89
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
92 e019c70b Leszek Koltunski
  private void reportRecord(RubikActivity act, String debug, int scrambleNum)
93 1cd323dd Leszek Koltunski
    {
94 8ab435b9 Leszek Koltunski
    RubikScreenPlay play= (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
95
    RubikScores scores  = RubikScores.getInstance();
96
    ObjectType object   = play.getObject();
97
    int level           = play.getLevel();
98
    String name         = scores.getName();
99
100
    String record = object.name()+" level "+level+" time "+mNewRecord+" isNew: "+mIsNewRecord+" scrambleNum: "+scrambleNum;
101 1cd323dd Leszek Koltunski
102
    if( BuildConfig.DEBUG )
103
       {
104
       android.util.Log.e("pre", debug);
105
       android.util.Log.e("pre", name);
106
       android.util.Log.e("pre", record);
107
       }
108
    else
109
      {
110 e019c70b Leszek Koltunski
      FirebaseAnalytics analytics = act.getAnalytics();
111 1cd323dd Leszek Koltunski
112
      if( analytics!=null )
113
        {
114
        Bundle bundle = new Bundle();
115
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, debug);
116
        bundle.putString(FirebaseAnalytics.Param.CHARACTER, name);
117
        bundle.putString(FirebaseAnalytics.Param.LEVEL, record);
118
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP, bundle);
119
        }
120
      }
121
    }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125 e019c70b Leszek Koltunski
  private void requestReview(RubikActivity act)
126 1cd323dd Leszek Koltunski
    {
127
    final RubikScores scores = RubikScores.getInstance();
128
    int numWins = scores.incrementNumWins();
129
130
    if( numWins==7 || numWins==30 || numWins==100 || numWins==200)
131
      {
132
      final long timeBegin = System.currentTimeMillis();
133
      final ReviewManager manager = ReviewManagerFactory.create(act);
134
      Task<ReviewInfo> request = manager.requestReviewFlow();
135
136
      request.addOnCompleteListener(new OnCompleteListener<ReviewInfo>()
137
        {
138
        @Override
139
        public void onComplete (@NonNull Task<ReviewInfo> task)
140
          {
141
          if (task.isSuccessful())
142
            {
143
            final String name = scores.getName();
144
            ReviewInfo reviewInfo = task.getResult();
145
            Task<Void> flow = manager.launchReviewFlow(act, reviewInfo);
146
147
            flow.addOnFailureListener(new OnFailureListener()
148
              {
149
              @Override
150
              public void onFailure(Exception e)
151
                {
152
                analyticsReport(act,"Failed", name, timeBegin);
153
                }
154
              });
155
156
            flow.addOnCompleteListener(new OnCompleteListener<Void>()
157
              {
158
              @Override
159
              public void onComplete(@NonNull Task<Void> task)
160
                {
161
                analyticsReport(act,"Complete", name, timeBegin);
162
                }
163
              });
164
            }
165
          else
166
            {
167
            String name = scores.getName();
168
            analyticsReport(act,"Not Successful", name, timeBegin);
169
            }
170
          }
171
        });
172
      }
173
    }
174
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
177 e019c70b Leszek Koltunski
  public void onWinEffectFinished(String debug, int scrambleNum)
178
    {
179
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
180
      {
181
      RubikActivity act = mAct.get();
182
      Bundle bundle = new Bundle();
183
      bundle.putLong("time", mNewRecord );
184
185
      reportRecord(act,debug,scrambleNum);
186
      requestReview(act);
187
188
      if( mIsNewRecord )
189
        {
190
        RubikDialogNewRecord dialog = new RubikDialogNewRecord();
191
        dialog.setArguments(bundle);
192
        dialog.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
193
        }
194
      else
195
        {
196
        RubikDialogSolved dialog = new RubikDialogSolved();
197
        dialog.setArguments(bundle);
198
        dialog.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
199
        }
200
201
      act.runOnUiThread(new Runnable()
202
        {
203
        @Override
204
        public void run()
205
          {
206
          ScreenList.switchScreen( act, ScreenList.DONE);
207
          }
208
        });
209
      }
210
    }
211 1cd323dd Leszek Koltunski
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
214 e019c70b Leszek Koltunski
  public void onScrambleEffectFinished()
215
    {
216
    RubikActivity act = mAct.get();
217
    RubikScores.getInstance().incrementNumPlays();
218 1cd323dd Leszek Koltunski
219 e019c70b Leszek Koltunski
    act.runOnUiThread(new Runnable()
220
      {
221
      @Override
222
      public void run()
223
        {
224
        ScreenList.switchScreen( act, ScreenList.READ);
225
        }
226
      });
227
    }
228 1cd323dd Leszek Koltunski
229 e019c70b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
230
231
  public void onFinishRotation(int axis, int row, int angle)
232
    {
233
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
234
      {
235
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
236
      solv.addMove(mAct.get(), axis, row, angle);
237
      }
238
    if( ScreenList.getCurrentScreen()== ScreenList.PLAY )
239
      {
240
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
241
      play.addMove(mAct.get(), axis, row, angle);
242
      }
243
    }
244 1cd323dd Leszek Koltunski
245 dd1a65c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
246
247 e019c70b Leszek Koltunski
  public void onBeginRotation()
248
    {
249
    if( ScreenList.getCurrentScreen()== ScreenList.READ )
250
      {
251
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
252
      solving.resetElapsed();
253
      RubikActivity act = mAct.get();
254
255
      act.runOnUiThread(new Runnable()
256
        {
257
        @Override
258
        public void run()
259
          {
260
          ScreenList.switchScreen( act, ScreenList.SOLV);
261
          }
262
        });
263
      }
264
    }
265 dd1a65c1 Leszek Koltunski
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267
268 e019c70b Leszek Koltunski
  public void failedToDrag()
269
    {
270
    ScreenList curr = ScreenList.getCurrentScreen();
271
272
    if( curr==ScreenList.PLAY )
273
      {
274
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
275
      play.reddenLock(mAct.get());
276
      }
277
    else if( curr==ScreenList.READ )
278
      {
279
      RubikScreenReady read = (RubikScreenReady) ScreenList.READ.getScreenClass();
280
      read.reddenLock(mAct.get());
281
      }
282
    else if( curr==ScreenList.SOLV )
283
      {
284
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
285
      solv.reddenLock(mAct.get());
286
      }
287
    }
288 dd1a65c1 Leszek Koltunski
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290
291 e019c70b Leszek Koltunski
  public void onSolved()
292
    {
293
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
294 dd1a65c1 Leszek Koltunski
      {
295 e019c70b Leszek Koltunski
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
296
      mNewRecord = solving.getRecord();
297 dd1a65c1 Leszek Koltunski
298 e019c70b Leszek Koltunski
      if( mNewRecord< 0 )
299 dd1a65c1 Leszek Koltunski
        {
300 e019c70b Leszek Koltunski
        mNewRecord = -mNewRecord;
301
        mIsNewRecord = false;
302 dd1a65c1 Leszek Koltunski
        }
303 e019c70b Leszek Koltunski
      else
304 dd1a65c1 Leszek Koltunski
        {
305 e019c70b Leszek Koltunski
        mIsNewRecord = true;
306 dd1a65c1 Leszek Koltunski
        }
307
      }
308 e019c70b Leszek Koltunski
    }
309 dd1a65c1 Leszek Koltunski
310 1cd323dd Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
311
312 e019c70b Leszek Koltunski
  public int getCurrentColor()
313
    {
314
    RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
315
    return solver.getCurrentColor();
316
    }
317 1cd323dd Leszek Koltunski
318 e019c70b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
319
320
  public int cubitIsLocked(ObjectType type, int cubit)
321
    {
322
    return SolverMain.cubitIsLocked(type,cubit);
323
    }
324 dd1a65c1 Leszek Koltunski
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
327 e019c70b Leszek Koltunski
  public ObjectControl getControl()
328
    {
329
    RubikActivity act = mAct.get();
330
    return act.getControl();
331
    }
332 dd1a65c1 Leszek Koltunski
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
335 0c52f5da Leszek Koltunski
  public DistortedFramebuffer getFramebuffer()
336 e019c70b Leszek Koltunski
    {
337
    RubikActivity act = mAct.get();
338
    return act.getScreen();
339
    }
340 1cd323dd Leszek Koltunski
}