Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikObjectLibInterface.java @ 1fa125c2

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.network.RubikNetwork;
38
import org.distorted.objectlib.BuildConfig;
39
import org.distorted.objectlib.helpers.BlockController;
40
import org.distorted.objectlib.helpers.ObjectLibInterface;
41
import org.distorted.objectlib.main.ObjectControl;
42

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

    
55
import java.lang.ref.WeakReference;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

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

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

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

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  private void reportRecord(RubikActivity act, long startTime, long endTime, String debug, int scrambleNum)
101
    {
102
    RubikScreenPlay play= (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
103
    RubikScores scores  = RubikScores.getInstance();
104
    int object  = RubikObjectList.getCurrObject();
105
    int level   = play.getLevel();
106
    String name = scores.getName();
107
    RubikObject obj = RubikObjectList.getObject(object);
108
    String objName = obj==null ? "NULL" : obj.getName();
109

    
110
    String record = objName+" level "+level+" time "+mNewRecord+" isNew: "+mIsNewRecord+" scrambleNum: "+scrambleNum;
111

    
112
    if( BuildConfig.DEBUG )
113
       {
114
       android.util.Log.e("libInterface", debug);
115
       android.util.Log.e("libInterface", name);
116
       android.util.Log.e("libInterface", record);
117
       }
118
    else
119
      {
120
      long timeNow = System.currentTimeMillis();
121
      long elapsed = timeNow - startTime;
122

    
123
      if( level>=9 && mNewRecord<300*level )
124
        {
125
        String suspicious ="start"+startTime+"end"+endTime+"elapsed"+elapsed+"obj"+objName+"level"+level+"record"+mNewRecord+"scrambles"+scrambleNum+debug;
126
        RubikNetwork network = RubikNetwork.getInstance();
127
        network.suspicious(act,suspicious);
128
        }
129

    
130
      FirebaseAnalytics analytics = act.getAnalytics();
131

    
132
      if( analytics!=null )
133
        {
134
        Bundle bundle = new Bundle();
135
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, debug);
136
        bundle.putString(FirebaseAnalytics.Param.CHARACTER, name);
137
        bundle.putString(FirebaseAnalytics.Param.LEVEL, record);
138
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP, bundle);
139
        }
140
      }
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  private void requestReview(RubikActivity act)
146
    {
147
    final RubikScores scores = RubikScores.getInstance();
148
    int numWins = scores.incrementNumWins();
149

    
150
    if( numWins==7 || numWins==30 || numWins==100 || numWins==200)
151
      {
152
      final long timeBegin = System.currentTimeMillis();
153
      final ReviewManager manager = ReviewManagerFactory.create(act);
154
      Task<ReviewInfo> request = manager.requestReviewFlow();
155

    
156
      request.addOnCompleteListener(new OnCompleteListener<ReviewInfo>()
157
        {
158
        @Override
159
        public void onComplete (@NonNull Task<ReviewInfo> task)
160
          {
161
          if (task.isSuccessful())
162
            {
163
            final String name = scores.getName();
164
            ReviewInfo reviewInfo = task.getResult();
165
            Task<Void> flow = manager.launchReviewFlow(act, reviewInfo);
166

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

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

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
  public void onWinEffectFinished(long startTime, long endTime, String debug, int scrambleNum)
198
    {
199
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
200
      {
201
      RubikActivity act = mAct.get();
202
      Bundle bundle = new Bundle();
203
      bundle.putLong("time", mNewRecord );
204

    
205
      reportRecord(act,startTime,endTime,debug,scrambleNum);
206
      requestReview(act);
207

    
208
      if( mIsNewRecord )
209
        {
210
        RubikDialogNewRecord dialog = new RubikDialogNewRecord();
211
        dialog.setArguments(bundle);
212
        dialog.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
213
        }
214
      else
215
        {
216
        RubikDialogSolved dialog = new RubikDialogSolved();
217
        dialog.setArguments(bundle);
218
        dialog.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
219
        }
220

    
221
      act.runOnUiThread(new Runnable()
222
        {
223
        @Override
224
        public void run()
225
          {
226
          ScreenList.switchScreen( act, ScreenList.DONE);
227
          }
228
        });
229
      }
230
    }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
  public void onScrambleEffectFinished()
235
    {
236
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
237

    
238
    if( play.shouldReactToEndOfScrambling() )
239
      {
240
      RubikActivity act = mAct.get();
241
      RubikScores.getInstance().incrementNumPlays();
242

    
243
      act.runOnUiThread(new Runnable()
244
        {
245
        @Override
246
        public void run()
247
        {
248
        ScreenList.switchScreen( act, ScreenList.READ);
249
        }
250
        });
251
      }
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  public void onFinishRotation(int axis, int row, int angle)
257
    {
258
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
259
      {
260
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
261
      solv.addMove(mAct.get(), axis, row, angle);
262
      }
263
    if( ScreenList.getCurrentScreen()== ScreenList.PLAY )
264
      {
265
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
266
      play.addMove(mAct.get(), axis, row, angle);
267
      }
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  public void onBeginRotation()
273
    {
274
    if( ScreenList.getCurrentScreen()== ScreenList.READ )
275
      {
276
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
277
      solving.resetElapsed();
278
      RubikActivity act = mAct.get();
279

    
280
      act.runOnUiThread(new Runnable()
281
        {
282
        @Override
283
        public void run()
284
          {
285
          ScreenList.switchScreen( act, ScreenList.SOLV);
286
          }
287
        });
288
      }
289
    }
290

    
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

    
293
  public void failedToDrag()
294
    {
295
    ScreenList curr = ScreenList.getCurrentScreen();
296

    
297
    if( curr==ScreenList.PLAY )
298
      {
299
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
300
      play.reddenLock(mAct.get());
301
      }
302
    else if( curr==ScreenList.READ )
303
      {
304
      RubikScreenReady read = (RubikScreenReady) ScreenList.READ.getScreenClass();
305
      read.reddenLock(mAct.get());
306
      }
307
    else if( curr==ScreenList.SOLV )
308
      {
309
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
310
      solv.reddenLock(mAct.get());
311
      }
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  public void onSolved()
317
    {
318
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
319
      {
320
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
321
      mNewRecord = solving.getRecord();
322

    
323
      if( mNewRecord< 0 )
324
        {
325
        mNewRecord = -mNewRecord;
326
        mIsNewRecord = false;
327
        }
328
      else
329
        {
330
        mIsNewRecord = true;
331
        }
332
      }
333
    }
334

    
335
///////////////////////////////////////////////////////////////////////////////////////////////////
336

    
337
  public void onObjectCreated(long time)
338
    {
339

    
340
    }
341

    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343

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

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368

    
369
  private void reportUIProblem(int place, long pause, long resume, long time)
370
    {
371
    String error = "UI BLOCK "+place+" blocked for "+time;
372

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

    
387
///////////////////////////////////////////////////////////////////////////////////////////////////
388

    
389
  private void reportTouchProblem(int place, long pause, long resume, long time)
390
    {
391
    String error = "TOUCH BLOCK "+place+" blocked for "+time;
392

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

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408

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

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

    
427
///////////////////////////////////////////////////////////////////////////////////////////////////
428

    
429
  public void reportBlockProblem(int type, int place, long pause, long resume, long time)
430
    {
431
    switch(type)
432
      {
433
      case BlockController.TYPE_UI    : reportUIProblem(place,pause,resume,time); break;
434
      case BlockController.TYPE_TOUCH : reportTouchProblem(place,pause,resume,time); break;
435
      case BlockController.TYPE_THREAD: reportThreadProblem(place,pause,resume,time); break;
436
      }
437
    }
438

    
439
///////////////////////////////////////////////////////////////////////////////////////////////////
440

    
441
  public void onReplaceModeDown(int cubit, int face)
442
    {
443
    RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
444
    int color = solver.getCurrentColor();
445
    int currObject = RubikObjectList.getCurrObject();
446
    mLastCubitColor = SolverMain.cubitIsLocked(currObject,cubit);
447
    mLastCubit = cubit;
448
    mLastCubitFace = face;
449
    ObjectControl control = mAct.get().getControl();
450
    control.setTextureMap( cubit, face, color );
451
    }
452

    
453
///////////////////////////////////////////////////////////////////////////////////////////////////
454

    
455
  public void onReplaceModeUp()
456
    {
457
    if( mLastCubitColor>=0 )
458
      {
459
      ObjectControl control = mAct.get().getControl();
460
      control.setTextureMap( mLastCubit, mLastCubitFace, mLastCubitColor );
461
      mLastCubitColor = -1;
462
      }
463
    }
464
}
(2-2/4)