Project

General

Profile

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

magiccube / src / main / java / org / distorted / playui / PlayLibInterface.java @ c6e27c65

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.playui;
11

    
12
import static org.distorted.external.RubikScores.RECORD_FIRST;
13
import static org.distorted.external.RubikScores.RECORD_NEW;
14
import static org.distorted.external.RubikScores.RECORD_NOT_NEW;
15

    
16
import android.os.Bundle;
17

    
18
import androidx.annotation.NonNull;
19

    
20
import java.lang.ref.WeakReference;
21

    
22
import com.google.android.play.core.review.ReviewInfo;
23
import com.google.android.play.core.review.ReviewManager;
24
import com.google.android.play.core.review.ReviewManagerFactory;
25
import com.google.android.play.core.tasks.OnCompleteListener;
26
import com.google.android.play.core.tasks.OnFailureListener;
27
import com.google.android.play.core.tasks.Task;
28
import com.google.firebase.analytics.FirebaseAnalytics;
29
import com.google.firebase.crashlytics.FirebaseCrashlytics;
30

    
31
import org.distorted.dialogs.RubikDialogNewRecord;
32
import org.distorted.dialogs.RubikDialogScoresView;
33
import org.distorted.dialogs.RubikDialogSolved;
34
import org.distorted.external.RubikNetwork;
35
import org.distorted.external.RubikScores;
36
import org.distorted.library.message.EffectMessageSender;
37
import org.distorted.objectlib.helpers.BlockController;
38
import org.distorted.objectlib.helpers.ObjectLibInterface;
39
import org.distorted.objectlib.main.ObjectControl;
40
import org.distorted.objectlib.main.TwistyObject;
41
import org.distorted.objects.RubikObject;
42
import org.distorted.objects.RubikObjectList;
43
import org.distorted.main.BuildConfig;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
public class PlayLibInterface implements ObjectLibInterface
48
{
49
  private final WeakReference<PlayActivity> mAct;
50
  private int mIsNewRecord;
51
  private int mNewRecord;
52
  private boolean mReviewAsked;
53
  private int mNumRotations, mNumScrambles;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  PlayLibInterface(PlayActivity act)
58
    {
59
    mAct = new WeakReference<>(act);
60
    mReviewAsked = false;
61
    mNumRotations = 0;
62
    mNumScrambles = 0;
63
    }
64

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

    
67
  public void onObjectCreated(long time) { }
68
  public void onReplaceModeDown(int cubit, int face) { }
69
  public void onReplaceModeUp() { }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

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

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

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  private void reportRecord(PlayActivity act, long startTime, long endTime, String debug, int scrambleNum)
98
    {
99
    RubikScores scores  = RubikScores.getInstance();
100
    int objectOrdinal = act.getObjectOrdinal();
101
    String name = scores.getName();
102
    RubikObject obj = RubikObjectList.getObject(objectOrdinal);
103
    String objName = obj==null ? "NULL" : obj.getUpperName();
104

    
105
    String record = objName+" time "+mNewRecord+" isNew: "+mIsNewRecord+" scrambleNum: "+scrambleNum;
106

    
107
    if( BuildConfig.DEBUG )
108
      {
109
      android.util.Log.e("libInterface", debug);
110
      android.util.Log.e("libInterface", name);
111
      android.util.Log.e("libInterface", record);
112
      }
113
    else
114
      {
115
      if( scrambleNum>=9 && mNewRecord<300*scrambleNum )
116
        {
117
        long timeNow = System.currentTimeMillis();
118
        long elapsed = timeNow - startTime;
119
        String suspicious ="start"+startTime+"end"+endTime+"elapsed"+elapsed+"obj"+objName+"record"+mNewRecord+"scrambles"+scrambleNum+debug;
120
        RubikNetwork network = RubikNetwork.getInstance();
121
        network.suspicious(suspicious,act);
122
        }
123

    
124
      FirebaseAnalytics analytics = act.getAnalytics();
125

    
126
      if( analytics!=null )
127
        {
128
        Bundle bundle = new Bundle();
129
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, debug);
130
        bundle.putString(FirebaseAnalytics.Param.CHARACTER, name);
131
        bundle.putString(FirebaseAnalytics.Param.LEVEL, record);
132
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP, bundle);
133
        }
134
      }
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  private Bundle createDialogBundle()
140
    {
141
    Bundle bundle = new Bundle();
142
    String arg = RubikDialogScoresView.formatRecord(mNewRecord);
143
    bundle.putString("argument", arg );
144
    return bundle;
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  private void requestReview(PlayActivity act)
150
    {
151
    android.util.Log.e("D", "ASKING FOR REVIEW");
152

    
153
    mReviewAsked = true;
154
    final String name = RubikScores.getInstance().getName();
155
    final long timeBegin = System.currentTimeMillis();
156
    final ReviewManager manager = ReviewManagerFactory.create(act);
157
    Task<ReviewInfo> request = manager.requestReviewFlow();
158

    
159
    request.addOnCompleteListener(new OnCompleteListener<ReviewInfo>()
160
      {
161
      @Override
162
      public void onComplete (@NonNull Task<ReviewInfo> task)
163
        {
164
        if (task.isSuccessful())
165
          {
166
          ReviewInfo reviewInfo = task.getResult();
167
          Task<Void> flow = manager.launchReviewFlow(act, reviewInfo);
168

    
169
          flow.addOnFailureListener(new OnFailureListener()
170
            {
171
            @Override
172
            public void onFailure(Exception e)
173
            {
174
            analyticsReport(act,"Failed", name, timeBegin);
175
            }
176
            });
177

    
178
          flow.addOnCompleteListener(new OnCompleteListener<Void>()
179
            {
180
            @Override
181
            public void onComplete(@NonNull Task<Void> task)
182
              {
183
              analyticsReport(act,"Complete", name, timeBegin);
184
              }
185
            });
186
          }
187
        else analyticsReport(act,"Not Successful", name, timeBegin);
188
        }
189
      });
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
  public void onScrambleEffectFinished()
195
    {
196
    if( ScreenList.getCurrentScreen()==ScreenList.SCRA )
197
      {
198
      PlayActivity act = mAct.get();
199
      RubikScores.getInstance().incrementNumPlays();
200

    
201
      act.runOnUiThread(new Runnable()
202
        {
203
        @Override
204
        public void run()
205
          {
206
          ScreenList.switchScreen(act,ScreenList.READ);
207
          ObjectControl control = act.getControl();
208
          control.unblockEverything();
209
          }
210
        });
211
      }
212

    
213
    mNumScrambles++;
214

    
215
    if( mNumScrambles==10 && !mReviewAsked )
216
      {
217
      PlayActivity act = mAct.get();
218
      requestReview(act);
219
      }
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  public void onRemoveRotation(int axis, int row, int angle)
225
    {
226
    mNumRotations++;
227
    PlayActivity act = mAct.get();
228

    
229
    if( act!=null )
230
      {
231
      ScreenList screen = ScreenList.getCurrentScreen();
232

    
233
      if( screen==ScreenList.FREE ||
234
          screen==ScreenList.SOLV  ) ((ScreenBase)screen.getScreenClass()).addMove(act,axis,row,angle);
235
      }
236

    
237
    if( mNumRotations==40 && !mReviewAsked )
238
      {
239
      requestReview(act);
240
      }
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
  public void onBeginRotation()
246
    {
247
    if( ScreenList.getCurrentScreen()==ScreenList.READ )
248
      {
249
      ScreenSolving solving = (ScreenSolving) ScreenList.SOLV.getScreenClass();
250
      solving.resetElapsed();
251
      PlayActivity act = mAct.get();
252
      ScreenList.switchScreen( act,ScreenList.SOLV );
253
      }
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  public void failedToDrag()
259
    {
260
    PlayActivity act = mAct.get();
261

    
262
    if( act!=null )
263
      {
264
      ScreenList screen = ScreenList.getCurrentScreen();
265

    
266
      if( screen==ScreenList.FREE ||
267
          screen==ScreenList.SOLV ||
268
          screen==ScreenList.SCRA  ) ((ScreenBase)screen.getScreenClass()).reddenLock(act);
269
      }
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  public void onSolved()
275
    {
276
    if( ScreenList.getCurrentScreen()==ScreenList.SOLV )
277
      {
278
      PlayActivity act = mAct.get();
279
      int objectOrdinal = act.getObjectOrdinal();
280
      ScreenSolving solving = (ScreenSolving)ScreenList.SOLV.getScreenClass();
281
      mNewRecord = solving.stopTimerAndGetRecord();
282
      mIsNewRecord = solving.setRecord(objectOrdinal);
283
      }
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  public void onWinEffectFinished(long startTime, long endTime, String debug, int scrambleNum)
289
    {
290
    if( ScreenList.getCurrentScreen()==ScreenList.SOLV )
291
      {
292
      PlayActivity act = mAct.get();
293
      reportRecord(act,startTime,endTime,debug,scrambleNum);
294

    
295
      RubikScores scores = RubikScores.getInstance();
296
      int numWins = scores.incrementNumWins();
297
      int numRuns = scores.getNumRuns();
298

    
299
      if( numRuns==3 || numRuns==6 || numWins==4 || numWins==20 || numWins==50 || numWins==80 || numWins==100)
300
        {
301
        requestReview(act);
302
        }
303

    
304
      switch(mIsNewRecord)
305
        {
306
        case RECORD_FIRST  :
307
        case RECORD_NEW    : ObjectControl control = act.getControl();
308
                             TwistyObject obj = control.getObject();
309
                             boolean submittable = obj.isSubmittable();
310

    
311
                             Bundle byes = createDialogBundle();
312

    
313
                             if( submittable )
314
                               {
315
                               RubikDialogNewRecord dyes = new RubikDialogNewRecord();
316
                               dyes.setArguments(byes);
317
                               dyes.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
318
                               }
319
                             else
320
                               {
321
                               RubikDialogSolved dno = new RubikDialogSolved();
322
                               dno.setArguments(byes);
323
                               dno.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
324
                               }
325
                             break;
326
        case RECORD_NOT_NEW: Bundle bno = createDialogBundle();
327
                             RubikDialogSolved dno = new RubikDialogSolved();
328
                             dno.setArguments(bno);
329
                             dno.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
330
        break;
331
        }
332

    
333
      act.runOnUiThread(new Runnable()
334
        {
335
        @Override
336
        public void run()
337
          {
338
          ScreenList.switchScreen( act,ScreenList.DONE );
339
          }
340
        });
341
      }
342
    }
343

    
344
///////////////////////////////////////////////////////////////////////////////////////////////////
345

    
346
  public void reportProblem(String problem, boolean reportException)
347
    {
348
    if( BuildConfig.DEBUG )
349
      {
350
      android.util.Log.e("interface", problem);
351
      }
352
    else
353
      {
354
      if( reportException )
355
        {
356
        Exception ex = new Exception(problem);
357
        FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
358
        crashlytics.setCustomKey("problem" , problem);
359
        crashlytics.recordException(ex);
360
        }
361
      else
362
        {
363
        FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
364
        crashlytics.log(problem);
365
        }
366
      }
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

    
371
  private void reportScramblingProblem(int place, long pause, long resume, long time)
372
    {
373
    String error = "SCRAMBLING BLOCK "+place+" blocked for "+time;
374

    
375
    if( BuildConfig.DEBUG )
376
       {
377
       android.util.Log.e("D", error);
378
       }
379
    else
380
      {
381
      Exception ex = new Exception(error);
382
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
383
      crashlytics.setCustomKey("pause" , pause );
384
      crashlytics.setCustomKey("resume", resume );
385
      crashlytics.recordException(ex);
386
      }
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390

    
391
  private void reportRotationProblem(int place, long pause, long resume, long time)
392
    {
393
    String error = "ROTATION BLOCK "+place+" blocked for "+time;
394

    
395
    if( BuildConfig.DEBUG )
396
       {
397
       android.util.Log.e("D", error);
398
       }
399
    else
400
      {
401
      Exception ex = new Exception(error);
402
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
403
      crashlytics.setCustomKey("pause" , pause );
404
      crashlytics.setCustomKey("resume", resume);
405
      crashlytics.recordException(ex);
406
      }
407
    }
408

    
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410

    
411
  private void reportThreadProblem(int place, long pause, long resume, long time)
412
    {
413
    String error = EffectMessageSender.reportState();
414

    
415
    if( BuildConfig.DEBUG )
416
       {
417
       android.util.Log.e("D", error);
418
       }
419
    else
420
      {
421
      Exception ex = new Exception(error);
422
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
423
      crashlytics.setCustomKey("pause" , pause  );
424
      crashlytics.setCustomKey("resume", resume );
425
      crashlytics.recordException(ex);
426
      }
427
    }
428

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

    
431
  public void reportBlockProblem(int type, int place, long pause, long resume, long time)
432
    {
433
    switch(type)
434
      {
435
      case BlockController.TYPE_SCRAMBLING: reportScramblingProblem(place,pause,resume,time); break;
436
      case BlockController.TYPE_ROTATION  : reportRotationProblem(place,pause,resume,time); break;
437
      case BlockController.TYPE_THREAD    : reportThreadProblem(place,pause,resume,time); break;
438
      }
439
    }
440

    
441
///////////////////////////////////////////////////////////////////////////////////////////////////
442

    
443
  public void reportJSONError(String error, int ordinal)
444
    {
445
    RubikObject object = RubikObjectList.getObject(ordinal);
446
    String name = object==null ? "NULL" : object.getUpperName();
447

    
448
    if( BuildConfig.DEBUG )
449
       {
450
       android.util.Log.e("libInterface", "name="+name+" JSON error: "+error);
451
       }
452
    else
453
      {
454
      Exception ex = new Exception(error);
455
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
456
      crashlytics.setCustomKey("name" , name );
457
      crashlytics.setCustomKey("JSONerror", error );
458
      crashlytics.recordException(ex);
459
      }
460
    }
461
}
(2-2/12)