Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikObjectLibInterface.java @ dd874ae8

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
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
220

    
221
    if( play.shouldReactToEndOfScrambling() )
222
      {
223
      RubikActivity act = mAct.get();
224
      RubikScores.getInstance().incrementNumPlays();
225

    
226
      act.runOnUiThread(new Runnable()
227
        {
228
        @Override
229
        public void run()
230
        {
231
        ScreenList.switchScreen( act, ScreenList.READ);
232
        }
233
        });
234
      }
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  public void onFinishRotation(int axis, int row, int angle)
240
    {
241
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
242
      {
243
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
244
      solv.addMove(mAct.get(), axis, row, angle);
245
      }
246
    if( ScreenList.getCurrentScreen()== ScreenList.PLAY )
247
      {
248
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
249
      play.addMove(mAct.get(), axis, row, angle);
250
      }
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
  public void onBeginRotation()
256
    {
257
    if( ScreenList.getCurrentScreen()== ScreenList.READ )
258
      {
259
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
260
      solving.resetElapsed();
261
      RubikActivity act = mAct.get();
262

    
263
      act.runOnUiThread(new Runnable()
264
        {
265
        @Override
266
        public void run()
267
          {
268
          ScreenList.switchScreen( act, ScreenList.SOLV);
269
          }
270
        });
271
      }
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
  public void failedToDrag()
277
    {
278
    ScreenList curr = ScreenList.getCurrentScreen();
279

    
280
    if( curr==ScreenList.PLAY )
281
      {
282
      RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
283
      play.reddenLock(mAct.get());
284
      }
285
    else if( curr==ScreenList.READ )
286
      {
287
      RubikScreenReady read = (RubikScreenReady) ScreenList.READ.getScreenClass();
288
      read.reddenLock(mAct.get());
289
      }
290
    else if( curr==ScreenList.SOLV )
291
      {
292
      RubikScreenSolving solv = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
293
      solv.reddenLock(mAct.get());
294
      }
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

    
299
  public void onSolved()
300
    {
301
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
302
      {
303
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
304
      mNewRecord = solving.getRecord();
305

    
306
      if( mNewRecord< 0 )
307
        {
308
        mNewRecord = -mNewRecord;
309
        mIsNewRecord = false;
310
        }
311
      else
312
        {
313
        mIsNewRecord = true;
314
        RubikScreenPlay play= (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
315
        play.adjustSolvedIcons();
316
        }
317
      }
318
    }
319

    
320
///////////////////////////////////////////////////////////////////////////////////////////////////
321

    
322
  public void onObjectCreated(long time)
323
    {
324

    
325
    }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328

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

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

    
346
  private void reportUIProblem(int place, long pause, long resume, long time)
347
    {
348
    String error = "UI BLOCK "+place+" blocked for "+time;
349

    
350
    if( BuildConfig.DEBUG )
351
       {
352
       android.util.Log.e("D", error);
353
       }
354
    else
355
      {
356
      Exception ex = new Exception(error);
357
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
358
      crashlytics.setCustomKey("pause" , pause );
359
      crashlytics.setCustomKey("resume", resume );
360
      crashlytics.recordException(ex);
361
      }
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
  private void reportTouchProblem(int place, long pause, long resume, long time)
367
    {
368
    String error = "TOUCH BLOCK "+place+" blocked for "+time;
369

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

    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385

    
386
  private void reportThreadProblem(int place, long pause, long resume, long time)
387
    {
388
    String error = EffectMessageSender.reportState();
389

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

    
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405

    
406
  public void reportBlockProblem(int type, int place, long pause, long resume, long time)
407
    {
408
    switch(type)
409
      {
410
      case BlockController.TYPE_UI    : reportUIProblem(place,pause,resume,time); break;
411
      case BlockController.TYPE_TOUCH : reportTouchProblem(place,pause,resume,time); break;
412
      case BlockController.TYPE_THREAD: reportThreadProblem(place,pause,resume,time); break;
413
      }
414
    }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417

    
418
  public int getCurrentColor()
419
    {
420
    RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
421
    return solver.getCurrentColor();
422
    }
423

    
424
///////////////////////////////////////////////////////////////////////////////////////////////////
425

    
426
  public int cubitIsLocked(int cubit)
427
    {
428
    RubikScreenPlay play = (RubikScreenPlay) ScreenList.PLAY.getScreenClass();
429
    ObjectType currObject = play.getObject();
430
    return SolverMain.cubitIsLocked(currObject,cubit);
431
    }
432
}
(2-2/4)