Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikObjectLibInterface.java @ 8723caee

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.ObjectType;
41

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

    
52
import java.lang.ref.WeakReference;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
public class RubikObjectLibInterface implements ObjectLibInterface
57
{
58
  WeakReference<RubikActivity> mAct;
59
  private boolean mIsNewRecord;
60
  private long mNewRecord;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  RubikObjectLibInterface(RubikActivity act)
65
    {
66
    mAct = new WeakReference<>(act);
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

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

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

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  private void reportRecord(RubikActivity act, String debug, int scrambleNum)
96
    {
97
    RubikScreenPlay play= (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
98
    RubikScores scores  = RubikScores.getInstance();
99
    ObjectType object   = play.getObject();
100
    int level           = play.getLevel();
101
    String name         = scores.getName();
102

    
103
    String record = object.name()+" level "+level+" time "+mNewRecord+" isNew: "+mIsNewRecord+" scrambleNum: "+scrambleNum;
104

    
105
    if( BuildConfig.DEBUG )
106
       {
107
       android.util.Log.e("pre", debug);
108
       android.util.Log.e("pre", name);
109
       android.util.Log.e("pre", record);
110
       }
111
    else
112
      {
113
      FirebaseAnalytics analytics = act.getAnalytics();
114

    
115
      if( analytics!=null )
116
        {
117
        Bundle bundle = new Bundle();
118
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, debug);
119
        bundle.putString(FirebaseAnalytics.Param.CHARACTER, name);
120
        bundle.putString(FirebaseAnalytics.Param.LEVEL, record);
121
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP, bundle);
122
        }
123
      }
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  private void requestReview(RubikActivity act)
129
    {
130
    final RubikScores scores = RubikScores.getInstance();
131
    int numWins = scores.incrementNumWins();
132

    
133
    if( numWins==7 || numWins==30 || numWins==100 || numWins==200)
134
      {
135
      final long timeBegin = System.currentTimeMillis();
136
      final ReviewManager manager = ReviewManagerFactory.create(act);
137
      Task<ReviewInfo> request = manager.requestReviewFlow();
138

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

    
150
            flow.addOnFailureListener(new OnFailureListener()
151
              {
152
              @Override
153
              public void onFailure(Exception e)
154
                {
155
                analyticsReport(act,"Failed", name, timeBegin);
156
                }
157
              });
158

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

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  public void onWinEffectFinished(String debug, int scrambleNum)
181
    {
182
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
183
      {
184
      RubikActivity act = mAct.get();
185
      Bundle bundle = new Bundle();
186
      bundle.putLong("time", mNewRecord );
187

    
188
      reportRecord(act,debug,scrambleNum);
189
      requestReview(act);
190

    
191
      if( mIsNewRecord )
192
        {
193
        RubikDialogNewRecord dialog = new RubikDialogNewRecord();
194
        dialog.setArguments(bundle);
195
        dialog.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
196
        }
197
      else
198
        {
199
        RubikDialogSolved dialog = new RubikDialogSolved();
200
        dialog.setArguments(bundle);
201
        dialog.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
202
        }
203

    
204
      act.runOnUiThread(new Runnable()
205
        {
206
        @Override
207
        public void run()
208
          {
209
          ScreenList.switchScreen( act, ScreenList.DONE);
210
          }
211
        });
212
      }
213
    }
214

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

    
217
  public void onScrambleEffectFinished()
218
    {
219
    RubikActivity act = mAct.get();
220
    RubikScores.getInstance().incrementNumPlays();
221

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

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

    
234
  public void onFinishRotation(int axis, int row, int angle)
235
    {
236
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
237
      {
238
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
239
      solv.addMove(mAct.get(), axis, row, angle);
240
      }
241
    if( ScreenList.getCurrentScreen()== ScreenList.PLAY )
242
      {
243
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
244
      play.addMove(mAct.get(), axis, row, angle);
245
      }
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public void onBeginRotation()
251
    {
252
    if( ScreenList.getCurrentScreen()== ScreenList.READ )
253
      {
254
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
255
      solving.resetElapsed();
256
      RubikActivity act = mAct.get();
257

    
258
      act.runOnUiThread(new Runnable()
259
        {
260
        @Override
261
        public void run()
262
          {
263
          ScreenList.switchScreen( act, ScreenList.SOLV);
264
          }
265
        });
266
      }
267
    }
268

    
269
///////////////////////////////////////////////////////////////////////////////////////////////////
270

    
271
  public void failedToDrag()
272
    {
273
    ScreenList curr = ScreenList.getCurrentScreen();
274

    
275
    if( curr==ScreenList.PLAY )
276
      {
277
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
278
      play.reddenLock(mAct.get());
279
      }
280
    else if( curr==ScreenList.READ )
281
      {
282
      RubikScreenReady read = (RubikScreenReady) ScreenList.READ.getScreenClass();
283
      read.reddenLock(mAct.get());
284
      }
285
    else if( curr==ScreenList.SOLV )
286
      {
287
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
288
      solv.reddenLock(mAct.get());
289
      }
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

    
294
  public void onSolved()
295
    {
296
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
297
      {
298
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
299
      mNewRecord = solving.getRecord();
300

    
301
      if( mNewRecord< 0 )
302
        {
303
        mNewRecord = -mNewRecord;
304
        mIsNewRecord = false;
305
        }
306
      else
307
        {
308
        mIsNewRecord = true;
309
        RubikScreenPlay play= (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
310
        play.adjustSolvedIcons();
311
        }
312
      }
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  public void onObjectCreated(long time)
318
    {
319

    
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  public void reportProblem(String problem)
325
    {
326
    if( BuildConfig.DEBUG )
327
      {
328
      android.util.Log.e("interface", problem);
329
      }
330
    else
331
      {
332
      Exception ex = new Exception(problem);
333
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
334
      crashlytics.setCustomKey("problem" , problem);
335
      crashlytics.recordException(ex);
336
      }
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340

    
341
  private void reportUIProblem(int place, long pause, long resume, long time)
342
    {
343
    String error = "UI BLOCK "+place+" blocked for "+time;
344

    
345
    if( BuildConfig.DEBUG )
346
       {
347
       android.util.Log.e("D", error);
348
       }
349
    else
350
      {
351
      Exception ex = new Exception(error);
352
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
353
      crashlytics.setCustomKey("pause" , pause );
354
      crashlytics.setCustomKey("resume", resume );
355
      crashlytics.recordException(ex);
356
      }
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  private void reportTouchProblem(int place, long pause, long resume, long time)
362
    {
363
    String error = "TOUCH BLOCK "+place+" blocked for "+time;
364

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

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
  private void reportThreadProblem(int place, long pause, long resume, long time)
382
    {
383
    String error = EffectMessageSender.reportState();
384

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

    
399
///////////////////////////////////////////////////////////////////////////////////////////////////
400

    
401
  public void reportBlockProblem(int type, int place, long pause, long resume, long time)
402
    {
403
    switch(type)
404
      {
405
      case BlockController.TYPE_UI    : reportUIProblem(place,pause,resume,time); break;
406
      case BlockController.TYPE_TOUCH : reportTouchProblem(place,pause,resume,time); break;
407
      case BlockController.TYPE_THREAD: reportThreadProblem(place,pause,resume,time); break;
408
      }
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412

    
413
  public int getCurrentColor()
414
    {
415
    RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
416
    return solver.getCurrentColor();
417
    }
418

    
419
///////////////////////////////////////////////////////////////////////////////////////////////////
420

    
421
  public int cubitIsLocked(int cubit)
422
    {
423
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
424
    ObjectType currObject = play.getObject();
425
    return SolverMain.cubitIsLocked(currObject,cubit);
426
    }
427
}
(2-2/4)