Project

General

Profile

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

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

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

    
34
import org.distorted.objectlib.helpers.ObjectLibInterface;
35
import org.distorted.objectlib.main.ObjectType;
36

    
37
import org.distorted.dialogs.RubikDialogNewRecord;
38
import org.distorted.dialogs.RubikDialogSolved;
39
import org.distorted.network.RubikScores;
40
import org.distorted.screens.RubikScreenPlay;
41
import org.distorted.screens.RubikScreenReady;
42
import org.distorted.screens.RubikScreenSolver;
43
import org.distorted.screens.RubikScreenSolving;
44
import org.distorted.screens.ScreenList;
45
import org.distorted.solvers.SolverMain;
46

    
47
import java.lang.ref.WeakReference;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
public class RubikObjectLibInterface implements ObjectLibInterface
52
{
53
  WeakReference<RubikActivity> mAct;
54
  private boolean mIsNewRecord;
55
  private long mNewRecord;
56

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

    
59
  RubikObjectLibInterface(RubikActivity act)
60
    {
61
    mAct = new WeakReference<>(act);
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  private void analyticsReport(RubikActivity act, String message, String name, long timeBegin)
67
    {
68
    long elapsed = System.currentTimeMillis() - timeBegin;
69
    String msg = message+" startTime: "+timeBegin+" elapsed: "+elapsed+" name: "+name;
70

    
71
    if( BuildConfig.DEBUG )
72
       {
73
       android.util.Log.d("pre", msg);
74
       }
75
    else
76
      {
77
      FirebaseAnalytics analytics = act.getAnalytics();
78

    
79
      if( analytics!=null )
80
        {
81
        Bundle bundle = new Bundle();
82
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, msg);
83
        analytics.logEvent(FirebaseAnalytics.Event.SHARE, bundle);
84
        }
85
      }
86
    }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

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

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

    
100
    if( BuildConfig.DEBUG )
101
       {
102
       android.util.Log.e("pre", debug);
103
       android.util.Log.e("pre", name);
104
       android.util.Log.e("pre", record);
105
       }
106
    else
107
      {
108
      FirebaseAnalytics analytics = act.getAnalytics();
109

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

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  private void requestReview(RubikActivity act)
124
    {
125
    final RubikScores scores = RubikScores.getInstance();
126
    int numWins = scores.incrementNumWins();
127

    
128
    if( numWins==7 || numWins==30 || numWins==100 || numWins==200)
129
      {
130
      final long timeBegin = System.currentTimeMillis();
131
      final ReviewManager manager = ReviewManagerFactory.create(act);
132
      Task<ReviewInfo> request = manager.requestReviewFlow();
133

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

    
145
            flow.addOnFailureListener(new OnFailureListener()
146
              {
147
              @Override
148
              public void onFailure(Exception e)
149
                {
150
                analyticsReport(act,"Failed", name, timeBegin);
151
                }
152
              });
153

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

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

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

    
183
      reportRecord(act,debug,scrambleNum);
184
      requestReview(act);
185

    
186
      if( mIsNewRecord )
187
        {
188
        RubikDialogNewRecord dialog = new RubikDialogNewRecord();
189
        dialog.setArguments(bundle);
190
        dialog.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
191
        }
192
      else
193
        {
194
        RubikDialogSolved dialog = new RubikDialogSolved();
195
        dialog.setArguments(bundle);
196
        dialog.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
197
        }
198

    
199
      act.runOnUiThread(new Runnable()
200
        {
201
        @Override
202
        public void run()
203
          {
204
          ScreenList.switchScreen( act, ScreenList.DONE);
205
          }
206
        });
207
      }
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  public void onScrambleEffectFinished()
213
    {
214
    RubikActivity act = mAct.get();
215
    RubikScores.getInstance().incrementNumPlays();
216

    
217
    act.runOnUiThread(new Runnable()
218
      {
219
      @Override
220
      public void run()
221
        {
222
        ScreenList.switchScreen( act, ScreenList.READ);
223
        }
224
      });
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

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

    
245
  public void onBeginRotation()
246
    {
247
    if( ScreenList.getCurrentScreen()== ScreenList.READ )
248
      {
249
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
250
      solving.resetElapsed();
251
      RubikActivity act = mAct.get();
252

    
253
      act.runOnUiThread(new Runnable()
254
        {
255
        @Override
256
        public void run()
257
          {
258
          ScreenList.switchScreen( act, ScreenList.SOLV);
259
          }
260
        });
261
      }
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

    
266
  public void failedToDrag()
267
    {
268
    ScreenList curr = ScreenList.getCurrentScreen();
269

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

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288

    
289
  public void onSolved()
290
    {
291
    if( ScreenList.getCurrentScreen()== ScreenList.SOLV )
292
      {
293
      RubikScreenSolving solving = (RubikScreenSolving) ScreenList.SOLV.getScreenClass();
294
      mNewRecord = solving.getRecord();
295

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

    
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311

    
312
  public void onObjectCreated(long time)
313
    {
314

    
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318

    
319
  public int getCurrentColor()
320
    {
321
    RubikScreenSolver solver = (RubikScreenSolver) ScreenList.SVER.getScreenClass();
322
    return solver.getCurrentColor();
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  public int cubitIsLocked(ObjectType type, int cubit)
328
    {
329
    return SolverMain.cubitIsLocked(type,cubit);
330
    }
331
}
(2-2/4)