Revision 18b0ae9c
Added by Leszek Koltunski 6 months ago
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 |
|
Also available in: Unified diff
Progress with the generic PlayActivity.