Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikObjectLibInterface.java @ 400ff34d

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import com.google.firebase.crashlytics.FirebaseCrashlytics;
34

    
35
import org.distorted.library.message.EffectMessageSender;
36

    
37
import org.distorted.objectlib.BuildConfig;
38
import org.distorted.objectlib.helpers.BlockController;
39
import org.distorted.objectlib.helpers.ObjectLibInterface;
40
import org.distorted.objectlib.main.ObjectControl;
41

    
42
import org.distorted.dialogs.RubikDialogNewRecord;
43
import org.distorted.dialogs.RubikDialogSolved;
44
import org.distorted.network.RubikScores;
45
import org.distorted.objects.RubikObject;
46
import org.distorted.objects.RubikObjectList;
47
import org.distorted.screens.RubikScreenPlay;
48
import org.distorted.screens.RubikScreenReady;
49
import org.distorted.screens.RubikScreenSolver;
50
import org.distorted.screens.RubikScreenSolving;
51
import org.distorted.screens.ScreenList;
52
import org.distorted.solvers.SolverMain;
53

    
54
import java.lang.ref.WeakReference;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
public class RubikObjectLibInterface implements ObjectLibInterface
59
{
60
  private final WeakReference<RubikActivity> mAct;
61
  private boolean mIsNewRecord;
62
  private long mNewRecord;
63
  private int mLastCubitColor, mLastCubit, mLastCubitFace;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
  RubikObjectLibInterface(RubikActivity act)
68
    {
69
    mAct = new WeakReference<>(act);
70
    mLastCubitColor = -1;
71
    }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  private void analyticsReport(RubikActivity act, String message, String name, long timeBegin)
76
    {
77
    long elapsed = System.currentTimeMillis() - timeBegin;
78
    String msg = message+" startTime: "+timeBegin+" elapsed: "+elapsed+" name: "+name;
79

    
80
    if( BuildConfig.DEBUG )
81
       {
82
       android.util.Log.d("pre", msg);
83
       }
84
    else
85
      {
86
      FirebaseAnalytics analytics = act.getAnalytics();
87

    
88
      if( analytics!=null )
89
        {
90
        Bundle bundle = new Bundle();
91
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, msg);
92
        analytics.logEvent(FirebaseAnalytics.Event.SHARE, bundle);
93
        }
94
      }
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  private void reportRecord(RubikActivity act, String debug, int scrambleNum)
100
    {
101
    RubikScreenPlay play= (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
102
    RubikScores scores  = RubikScores.getInstance();
103
    int object  = RubikObjectList.getCurrObject();
104
    int level   = play.getLevel();
105
    String name = scores.getName();
106
    RubikObject obj = RubikObjectList.getObject(object);
107

    
108
    String record = obj+" level "+level+" time "+mNewRecord+" isNew: "+mIsNewRecord+" scrambleNum: "+scrambleNum;
109

    
110
    if( BuildConfig.DEBUG )
111
       {
112
       android.util.Log.e("pre", debug);
113
       android.util.Log.e("pre", name);
114
       android.util.Log.e("pre", record);
115
       }
116
    else
117
      {
118
      FirebaseAnalytics analytics = act.getAnalytics();
119

    
120
      if( analytics!=null )
121
        {
122
        Bundle bundle = new Bundle();
123
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, debug);
124
        bundle.putString(FirebaseAnalytics.Param.CHARACTER, name);
125
        bundle.putString(FirebaseAnalytics.Param.LEVEL, record);
126
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP, bundle);
127
        }
128
      }
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  private void requestReview(RubikActivity act)
134
    {
135
    final RubikScores scores = RubikScores.getInstance();
136
    int numWins = scores.incrementNumWins();
137

    
138
    if( numWins==7 || numWins==30 || numWins==100 || numWins==200)
139
      {
140
      final long timeBegin = System.currentTimeMillis();
141
      final ReviewManager manager = ReviewManagerFactory.create(act);
142
      Task<ReviewInfo> request = manager.requestReviewFlow();
143

    
144
      request.addOnCompleteListener(new OnCompleteListener<ReviewInfo>()
145
        {
146
        @Override
147
        public void onComplete (@NonNull Task<ReviewInfo> task)
148
          {
149
          if (task.isSuccessful())
150
            {
151
            final String name = scores.getName();
152
            ReviewInfo reviewInfo = task.getResult();
153
            Task<Void> flow = manager.launchReviewFlow(act, reviewInfo);
154

    
155
            flow.addOnFailureListener(new OnFailureListener()
156
              {
157
              @Override
158
              public void onFailure(Exception e)
159
                {
160
                analyticsReport(act,"Failed", name, timeBegin);
161
                }
162
              });
163

    
164
            flow.addOnCompleteListener(new OnCompleteListener<Void>()
165
              {
166
              @Override
167
              public void onComplete(@NonNull Task<Void> task)
168
                {
169
                analyticsReport(act,"Complete", name, timeBegin);
170
                }
171
              });
172
            }
173
          else
174
            {
175
            String name = scores.getName();
176
            analyticsReport(act,"Not Successful", name, timeBegin);
177
            }
178
          }
179
        });
180
      }
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  public void onWinEffectFinished(String debug, int scrambleNum)
186
    {
187
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
188
      {
189
      RubikActivity act = mAct.get();
190
      Bundle bundle = new Bundle();
191
      bundle.putLong("time", mNewRecord );
192

    
193
      reportRecord(act,debug,scrambleNum);
194
      requestReview(act);
195

    
196
      if( mIsNewRecord )
197
        {
198
        RubikDialogNewRecord dialog = new RubikDialogNewRecord();
199
        dialog.setArguments(bundle);
200
        dialog.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
201
        }
202
      else
203
        {
204
        RubikDialogSolved dialog = new RubikDialogSolved();
205
        dialog.setArguments(bundle);
206
        dialog.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
207
        }
208

    
209
      act.runOnUiThread(new Runnable()
210
        {
211
        @Override
212
        public void run()
213
          {
214
          ScreenList.switchScreen( act, ScreenList.DONE);
215
          }
216
        });
217
      }
218
    }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
  public void onScrambleEffectFinished()
223
    {
224
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
225

    
226
    if( play.shouldReactToEndOfScrambling() )
227
      {
228
      RubikActivity act = mAct.get();
229
      RubikScores.getInstance().incrementNumPlays();
230

    
231
      act.runOnUiThread(new Runnable()
232
        {
233
        @Override
234
        public void run()
235
        {
236
        ScreenList.switchScreen( act, ScreenList.READ);
237
        }
238
        });
239
      }
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  public void onFinishRotation(int axis, int row, int angle)
245
    {
246
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
247
      {
248
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
249
      solv.addMove(mAct.get(), axis, row, angle);
250
      }
251
    if( ScreenList.getCurrentScreen()== ScreenList.PLAY )
252
      {
253
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
254
      play.addMove(mAct.get(), axis, row, angle);
255
      }
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  public void onBeginRotation()
261
    {
262
    if( ScreenList.getCurrentScreen()== ScreenList.READ )
263
      {
264
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
265
      solving.resetElapsed();
266
      RubikActivity act = mAct.get();
267

    
268
      act.runOnUiThread(new Runnable()
269
        {
270
        @Override
271
        public void run()
272
          {
273
          ScreenList.switchScreen( act, ScreenList.SOLV);
274
          }
275
        });
276
      }
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  public void failedToDrag()
282
    {
283
    ScreenList curr = ScreenList.getCurrentScreen();
284

    
285
    if( curr==ScreenList.PLAY )
286
      {
287
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
288
      play.reddenLock(mAct.get());
289
      }
290
    else if( curr==ScreenList.READ )
291
      {
292
      RubikScreenReady read = (RubikScreenReady) ScreenList.READ.getScreenClass();
293
      read.reddenLock(mAct.get());
294
      }
295
    else if( curr==ScreenList.SOLV )
296
      {
297
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
298
      solv.reddenLock(mAct.get());
299
      }
300
    }
301

    
302
///////////////////////////////////////////////////////////////////////////////////////////////////
303

    
304
  public void onSolved()
305
    {
306
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
307
      {
308
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
309
      mNewRecord = solving.getRecord();
310

    
311
      if( mNewRecord< 0 )
312
        {
313
        mNewRecord = -mNewRecord;
314
        mIsNewRecord = false;
315
        }
316
      else
317
        {
318
        mIsNewRecord = true;
319
        RubikScreenPlay play= (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
320
        play.adjustSolvedIcons();
321
        }
322
      }
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  public void onObjectCreated(long time)
328
    {
329

    
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
  public void reportProblem(String problem, boolean recordException)
335
    {
336
    if( BuildConfig.DEBUG )
337
      {
338
      android.util.Log.e("interface", problem);
339
      }
340
    else
341
      {
342
      if( recordException )
343
        {
344
        Exception ex = new Exception(problem);
345
        FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
346
        crashlytics.setCustomKey("problem" , problem);
347
        crashlytics.recordException(ex);
348
        }
349
      else
350
        {
351
        FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
352
        crashlytics.log(problem);
353
        }
354
      }
355
    }
356

    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358

    
359
  private void reportUIProblem(int place, long pause, long resume, long time)
360
    {
361
    String error = "UI BLOCK "+place+" blocked for "+time;
362

    
363
    if( BuildConfig.DEBUG )
364
       {
365
       android.util.Log.e("D", error);
366
       }
367
    else
368
      {
369
      Exception ex = new Exception(error);
370
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
371
      crashlytics.setCustomKey("pause" , pause );
372
      crashlytics.setCustomKey("resume", resume );
373
      crashlytics.recordException(ex);
374
      }
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
  private void reportTouchProblem(int place, long pause, long resume, long time)
380
    {
381
    String error = "TOUCH BLOCK "+place+" blocked for "+time;
382

    
383
    if( BuildConfig.DEBUG )
384
       {
385
       android.util.Log.e("D", error);
386
       }
387
    else
388
      {
389
      Exception ex = new Exception(error);
390
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
391
      crashlytics.setCustomKey("pause" , pause );
392
      crashlytics.setCustomKey("resume", resume);
393
      crashlytics.recordException(ex);
394
      }
395
    }
396

    
397
///////////////////////////////////////////////////////////////////////////////////////////////////
398

    
399
  private void reportThreadProblem(int place, long pause, long resume, long time)
400
    {
401
    String error = EffectMessageSender.reportState();
402

    
403
    if( BuildConfig.DEBUG )
404
       {
405
       android.util.Log.e("D", error);
406
       }
407
    else
408
      {
409
      Exception ex = new Exception(error);
410
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
411
      crashlytics.setCustomKey("pause" , pause  );
412
      crashlytics.setCustomKey("resume", resume );
413
      crashlytics.recordException(ex);
414
      }
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418

    
419
  public void reportBlockProblem(int type, int place, long pause, long resume, long time)
420
    {
421
    switch(type)
422
      {
423
      case BlockController.TYPE_UI    : reportUIProblem(place,pause,resume,time); break;
424
      case BlockController.TYPE_TOUCH : reportTouchProblem(place,pause,resume,time); break;
425
      case BlockController.TYPE_THREAD: reportThreadProblem(place,pause,resume,time); break;
426
      }
427
    }
428

    
429
///////////////////////////////////////////////////////////////////////////////////////////////////
430

    
431
  public void onReplaceModeDown(int cubit, int face)
432
    {
433
    RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
434
    int color = solver.getCurrentColor();
435
    int currObject = RubikObjectList.getCurrObject();
436
    mLastCubitColor = SolverMain.cubitIsLocked(currObject,cubit);
437
    mLastCubit = cubit;
438
    mLastCubitFace = face;
439
    ObjectControl control = mAct.get().getControl();
440
    control.setTextureMap( cubit, face, color );
441
    }
442

    
443
///////////////////////////////////////////////////////////////////////////////////////////////////
444

    
445
  public void onReplaceModeUp()
446
    {
447
    if( mLastCubitColor>=0 )
448
      {
449
      ObjectControl control = mAct.get().getControl();
450
      control.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
451
      mLastCubitColor = -1;
452
      }
453
    }
454
}
(2-2/4)