Project

General

Profile

« Previous | Next » 

Revision 18b0ae9c

Added by Leszek Koltunski about 1 year ago

Progress with the generic PlayActivity.

View differences:

src/main/java/org/distorted/main/MainScrollGrid.java
58 58
        @Override
59 59
        public void onClick(View v)
60 60
          {
61
          android.util.Log.e("D", "clicked on child "+ordinal);
62

  
63 61
          int w = displaymetrics.widthPixels;
64 62
          int h = displaymetrics.heightPixels;
65 63
          MainObjectPopup popup = new MainObjectPopup(act,ordinal,w,h);
src/main/java/org/distorted/playui/PlayActivity.java
24 24
import androidx.appcompat.app.AppCompatActivity;
25 25
import androidx.preference.PreferenceManager;
26 26

  
27
import com.google.firebase.analytics.FirebaseAnalytics;
28

  
27 29
import org.distorted.library.main.DistortedLibrary;
28 30
import org.distorted.objectlib.main.InitAssets;
29 31
import org.distorted.objectlib.main.ObjectControl;
......
57 59
    private boolean mObjectLocal;
58 60
    private int mObjectOrdinal;
59 61
    private boolean mModeFree;
62
    private FirebaseAnalytics mFirebaseAnalytics;
60 63

  
61 64
///////////////////////////////////////////////////////////////////////////////////////////////////
62 65

  
......
68 71
      setTheme(R.style.MaterialThemeNoActionBar);
69 72
      setContentView(R.layout.play);
70 73

  
74
      mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
75

  
71 76
      Bundle b = getIntent().getExtras();
72 77

  
73 78
      if( b!=null )
......
208 213
      PlayView view = findViewById(R.id.playView);
209 214
      view.onResume();
210 215

  
211
      ScreenList.switchScreen(this,ScreenList.FREE);
216
      ScreenList.switchScreen(this, mModeFree ? ScreenList.FREE : ScreenList.SCRA );
212 217

  
213 218
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
214 219
      restorePreferences(preferences);
......
218 223
        {
219 224
        changeIfDifferent(mObjectName,mObjectLocal,mObjectOrdinal,view.getObjectControl());
220 225
        }
226

  
227
      if( !mModeFree )
228
        {
229
        android.util.Log.e("D", "scrambles: "+mNumScrambles);
230
        ObjectControl control = getControl();
231
        control.scrambleObject(mNumScrambles);
232
        }
221 233
      }
222 234

  
223 235
///////////////////////////////////////////////////////////////////////////////////////////////////
......
325 337

  
326 338
///////////////////////////////////////////////////////////////////////////////////////////////////
327 339
// PUBLIC API
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341

  
342
    public FirebaseAnalytics getAnalytics()
343
      {
344
      return mFirebaseAnalytics;
345
      }
346

  
328 347
///////////////////////////////////////////////////////////////////////////////////////////////////
329 348

  
330 349
    public int getScreenWidthInPixels()
src/main/java/org/distorted/playui/PlayLibInterface.java
9 9

  
10 10
package org.distorted.playui;
11 11

  
12
import static org.distorted.external.RubikScores.RECORD_FIRST;
13
import static org.distorted.external.RubikScores.RECORD_NEW;
14
import static org.distorted.external.RubikScores.RECORD_NOT_NEW;
15

  
16
import android.os.Bundle;
17

  
18
import androidx.annotation.NonNull;
19

  
12 20
import java.lang.ref.WeakReference;
13 21

  
22
import com.google.android.play.core.review.ReviewInfo;
23
import com.google.android.play.core.review.ReviewManager;
24
import com.google.android.play.core.review.ReviewManagerFactory;
25
import com.google.android.play.core.tasks.OnCompleteListener;
26
import com.google.android.play.core.tasks.OnFailureListener;
27
import com.google.android.play.core.tasks.Task;
28
import com.google.firebase.analytics.FirebaseAnalytics;
14 29
import com.google.firebase.crashlytics.FirebaseCrashlytics;
15 30

  
31
import org.distorted.dialogs.RubikDialogNewRecord;
32
import org.distorted.dialogs.RubikDialogScoresView;
33
import org.distorted.dialogs.RubikDialogSolved;
34
import org.distorted.external.RubikNetwork;
35
import org.distorted.external.RubikScores;
16 36
import org.distorted.library.message.EffectMessageSender;
17 37
import org.distorted.objectlib.helpers.BlockController;
18 38
import org.distorted.objectlib.helpers.ObjectLibInterface;
39
import org.distorted.objectlib.main.ObjectControl;
19 40
import org.distorted.objects.RubikObject;
20 41
import org.distorted.objects.RubikObjectList;
21 42
import org.distorted.main.BuildConfig;
......
25 46
public class PlayLibInterface implements ObjectLibInterface
26 47
{
27 48
  private final WeakReference<PlayActivity> mAct;
49
  private int mIsNewRecord;
50
  private int mNewRecord;
51
  private boolean mReviewAsked;
52
  private int mNumRotations, mNumScrambles;
28 53

  
29 54
///////////////////////////////////////////////////////////////////////////////////////////////////
30 55

  
31 56
  PlayLibInterface(PlayActivity act)
32 57
    {
33 58
    mAct = new WeakReference<>(act);
59
    mReviewAsked = false;
60
    mNumRotations = 0;
61
    mNumScrambles = 0;
34 62
    }
35 63

  
36 64
///////////////////////////////////////////////////////////////////////////////////////////////////
37 65

  
38
  public void onWinEffectFinished(long startTime, long endTime, String debug, int scrambleNum) { }
39
  public void onScrambleEffectFinished() { }
40
  public void onBeginRotation() { }
41
  public void onSolved() { }
42 66
  public void onObjectCreated(long time) { }
43 67
  public void onReplaceModeDown(int cubit, int face) { }
44 68
  public void onReplaceModeUp() { }
45 69

  
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

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

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

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

  
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

  
96
  private void reportRecord(PlayActivity act, long startTime, long endTime, String debug, int scrambleNum)
97
    {
98
    RubikScores scores  = RubikScores.getInstance();
99
    int object  = RubikObjectList.getCurrObject();
100
    String name = scores.getName();
101
    RubikObject obj = RubikObjectList.getObject(object);
102
    String objName = obj==null ? "NULL" : obj.getUpperName();
103

  
104
    String record = objName+" time "+mNewRecord+" isNew: "+mIsNewRecord+" scrambleNum: "+scrambleNum;
105

  
106
    if( BuildConfig.DEBUG )
107
      {
108
      android.util.Log.e("libInterface", debug);
109
      android.util.Log.e("libInterface", name);
110
      android.util.Log.e("libInterface", record);
111
      }
112
    else
113
      {
114
      if( scrambleNum>=9 && mNewRecord<300*scrambleNum )
115
        {
116
        long timeNow = System.currentTimeMillis();
117
        long elapsed = timeNow - startTime;
118
        String suspicious ="start"+startTime+"end"+endTime+"elapsed"+elapsed+"obj"+objName+"record"+mNewRecord+"scrambles"+scrambleNum+debug;
119
        RubikNetwork network = RubikNetwork.getInstance();
120
        network.suspicious(suspicious,act);
121
        }
122

  
123
      FirebaseAnalytics analytics = act.getAnalytics();
124

  
125
      if( analytics!=null )
126
        {
127
        Bundle bundle = new Bundle();
128
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, debug);
129
        bundle.putString(FirebaseAnalytics.Param.CHARACTER, name);
130
        bundle.putString(FirebaseAnalytics.Param.LEVEL, record);
131
        analytics.logEvent(FirebaseAnalytics.Event.LEVEL_UP, bundle);
132
        }
133
      }
134
    }
135

  
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

  
138
  private Bundle createDialogBundle()
139
    {
140
    Bundle bundle = new Bundle();
141
    String arg = RubikDialogScoresView.formatRecord(mNewRecord);
142
    bundle.putString("argument", arg );
143
    return bundle;
144
    }
145

  
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

  
148
  private void requestReview(PlayActivity act)
149
    {
150
    android.util.Log.e("D", "ASKING FOR REVIEW");
151

  
152
    mReviewAsked = true;
153
    final String name = RubikScores.getInstance().getName();
154
    final long timeBegin = System.currentTimeMillis();
155
    final ReviewManager manager = ReviewManagerFactory.create(act);
156
    Task<ReviewInfo> request = manager.requestReviewFlow();
157

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

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

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

  
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

  
193
  public void onScrambleEffectFinished()
194
    {
195
    if( ScreenList.getCurrentScreen()==ScreenList.SCRA )
196
      {
197
      PlayActivity act = mAct.get();
198
      RubikScores.getInstance().incrementNumPlays();
199

  
200
      act.runOnUiThread(new Runnable()
201
        {
202
        @Override
203
        public void run()
204
          {
205
          ScreenScrambling screen = (ScreenScrambling)ScreenList.SCRA.getScreenClass();
206
          screen.setReady();
207
          ObjectControl control = act.getControl();
208
          control.unblockEverything();
209
          }
210
        });
211
      }
212

  
213
    mNumScrambles++;
214

  
215
    if( mNumScrambles==10 && !mReviewAsked )
216
      {
217
      PlayActivity act = mAct.get();
218
      requestReview(act);
219
      }
220
    }
221

  
46 222
///////////////////////////////////////////////////////////////////////////////////////////////////
47 223

  
48 224
  public void onFinishRotation(int axis, int row, int angle)
49 225
    {
226
    mNumRotations++;
50 227
    PlayActivity act = mAct.get();
51 228

  
52 229
    if( act!=null )
53 230
      {
54 231
      ScreenList screen = ScreenList.getCurrentScreen();
55 232

  
56
      if( screen==ScreenList.FREE ) ((ScreenFree   )screen.getScreenClass()).addMove(act,axis,row,angle);
57
      if( screen==ScreenList.SOLV ) ((ScreenSolving)screen.getScreenClass()).addMove(act,axis,row,angle);
233
      if( screen==ScreenList.FREE ||
234
          screen==ScreenList.SOLV  ) ((ScreenBase)screen.getScreenClass()).addMove(act,axis,row,angle);
235
      }
236

  
237
    if( mNumRotations==40 && !mReviewAsked )
238
      {
239
      requestReview(act);
240
      }
241
    }
242

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

  
245
  public void onBeginRotation()
246
    {
247
    if( ScreenList.getCurrentScreen()==ScreenList.SCRA )
248
      {
249
      ScreenSolving solving = (ScreenSolving) ScreenList.SOLV.getScreenClass();
250
      solving.resetElapsed();
251
      PlayActivity 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
        });
58 261
      }
59 262
    }
60 263

  
......
68 271
      {
69 272
      ScreenList screen = ScreenList.getCurrentScreen();
70 273

  
71
      if( screen==ScreenList.FREE ) ((ScreenFree   )screen.getScreenClass()).reddenLock(act);
72
      if( screen==ScreenList.SOLV ) ((ScreenSolving)screen.getScreenClass()).reddenLock(act);
274
      if( screen==ScreenList.FREE ||
275
          screen==ScreenList.SOLV ||
276
          screen==ScreenList.SCRA  ) ((ScreenBase)screen.getScreenClass()).reddenLock(act);
277
      }
278
    }
279

  
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

  
282
  public void onSolved()
283
    {
284
    if( ScreenList.getCurrentScreen()==ScreenList.SOLV )
285
      {
286
      ScreenSolving solving = (ScreenSolving)ScreenList.SOLV.getScreenClass();
287
      mNewRecord = solving.stopTimerAndGetRecord();
288
      mIsNewRecord = solving.setRecord();
289
      }
290
    }
291

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

  
294
  public void onWinEffectFinished(long startTime, long endTime, String debug, int scrambleNum)
295
    {
296
    if( ScreenList.getCurrentScreen()==ScreenList.SOLV )
297
      {
298
      PlayActivity act = mAct.get();
299
      reportRecord(act,startTime,endTime,debug,scrambleNum);
300

  
301
      RubikScores scores = RubikScores.getInstance();
302
      int numWins = scores.incrementNumWins();
303
      int numRuns = scores.getNumRuns();
304

  
305
      if( numRuns==3 || numRuns==6 || numWins==4 || numWins==20 || numWins==50 || numWins==80 || numWins==100)
306
        {
307
        requestReview(act);
308
        }
309

  
310
      switch(mIsNewRecord)
311
        {
312
        case RECORD_FIRST  :
313
        case RECORD_NEW    : Bundle byes = createDialogBundle();
314
                             RubikDialogNewRecord dyes = new RubikDialogNewRecord();
315
                             dyes.setArguments(byes);
316
                             dyes.show( act.getSupportFragmentManager(), RubikDialogNewRecord.getDialogTag() );
317
                             break;
318
        case RECORD_NOT_NEW: Bundle bno = createDialogBundle();
319
                             RubikDialogSolved dno = new RubikDialogSolved();
320
                             dno.setArguments(bno);
321
                             dno.show( act.getSupportFragmentManager(), RubikDialogSolved.getDialogTag() );
322
        break;
323
        }
324

  
325
      act.runOnUiThread(new Runnable()
326
        {
327
        @Override
328
        public void run()
329
          {
330
          ScreenList.switchScreen( act,ScreenList.DONE );
331
          }
332
        });
73 333
      }
74 334
    }
75 335

  
src/main/java/org/distorted/playui/ScreenScrambling.java
1 1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
2
// Copyright 2023 Leszek Koltunski                                                               //
3 3
//                                                                                               //
4 4
// This file is part of Magic Cube.                                                              //
5 5
//                                                                                               //
......
21 21

  
22 22
///////////////////////////////////////////////////////////////////////////////////////////////////
23 23

  
24
public class ScreenScrambling extends ScreenBase
24
public class ScreenScrambling extends ScreenAbstract
25 25
  {
26 26
  private TransparentImageButton mBackButton;
27
  private TextView mLabel;
27 28

  
28 29
///////////////////////////////////////////////////////////////////////////////////////////////////
29 30

  
......
43 44
    // TOP ////////////////////////////
44 45
    LinearLayout layoutTop = act.findViewById(R.id.upperBar);
45 46
    layoutTop.removeAllViews();
46
    TextView label = (TextView)inflater.inflate(R.layout.upper_text, null);
47
    label.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
48
    label.setText(R.string.ready);
49
    layoutTop.addView(label);
47
    mLabel = (TextView)inflater.inflate(R.layout.upper_text, null);
48
    mLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleSize);
49
    layoutTop.addView(mLabel);
50

  
51
    // BOT ////////////////////////////
52
    LinearLayout layoutBot = act.findViewById(R.id.lowerBar);
53
    layoutBot.removeAllViews();
54

  
55
    LinearLayout.LayoutParams paramsL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,1);
56
    LinearLayout.LayoutParams paramsR = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT,2);
57

  
58
    LinearLayout layoutLeft = new LinearLayout(act);
59
    layoutLeft.setLayoutParams(paramsL);
60
    LinearLayout layoutRight = new LinearLayout(act);
61
    layoutRight.setLayoutParams(paramsR);
50 62

  
51 63
    setupBackButton(act);
52
    createBottomPane(act,mBackButton);
64

  
65
    layoutRight.addView(mBackButton);
66
    layoutBot.addView(layoutLeft);
67
    layoutBot.addView(layoutRight);
68
    }
69

  
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

  
72
  void setReady()
73
    {
74
    mLabel.setText(R.string.ready);
53 75
    }
54 76

  
55 77
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff