Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikObjectLibInterface.java @ 95472aca

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
import org.distorted.objectlib.BuildConfig;
37
import org.distorted.objectlib.helpers.BlockController;
38
import org.distorted.objectlib.helpers.ObjectLibInterface;
39
import org.distorted.objectlib.main.ObjectType;
40

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

    
51
import java.lang.ref.WeakReference;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

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

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

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

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

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

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

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

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

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

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

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

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

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

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

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

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

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

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

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

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

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

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

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

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

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

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

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

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

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

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

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

    
316
  public void onObjectCreated(long time)
317
    {
318

    
319
    }
320

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322

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

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

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

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

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359

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

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

    
378
///////////////////////////////////////////////////////////////////////////////////////////////////
379

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

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

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399

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

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411

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

    
418
///////////////////////////////////////////////////////////////////////////////////////////////////
419

    
420
  public int cubitIsLocked(ObjectType type, int cubit)
421
    {
422
    return SolverMain.cubitIsLocked(type,cubit);
423
    }
424
}
(2-2/4)