1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2021 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.helpers;
|
21
|
|
22
|
import com.google.firebase.crashlytics.FirebaseCrashlytics;
|
23
|
|
24
|
import org.distorted.library.message.EffectMessageSender;
|
25
|
import org.distorted.main.BuildConfig;
|
26
|
|
27
|
import java.lang.ref.WeakReference;
|
28
|
import java.util.Timer;
|
29
|
import java.util.TimerTask;
|
30
|
|
31
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
32
|
|
33
|
public class BlockController
|
34
|
{
|
35
|
public static final int RUBIK_PLACE_0 =0;
|
36
|
public static final int RUBIK_PLACE_1 =1;
|
37
|
public static final int RUBIK_PLACE_2 =2;
|
38
|
public static final int RUBIK_PLACE_3 =3;
|
39
|
public static final int RUBIK_PLACE_4 =4;
|
40
|
public static final int TUTORIAL_PLACE_0 =10;
|
41
|
public static final int TUTORIAL_PLACE_1 =11;
|
42
|
public static final int TUTORIAL_PLACE_2 =12;
|
43
|
public static final int TUTORIAL_PLACE_3 =13;
|
44
|
public static final int TUTORIAL_PLACE_4 =14;
|
45
|
public static final int CONTROL_PLACE_0 =20;
|
46
|
public static final int CONTROL_PLACE_1 =21;
|
47
|
public static final int MOVES_PLACE_0 =30;
|
48
|
|
49
|
private static final long THRESHHOLD_0 = 3000;
|
50
|
private static final long THRESHHOLD_1 = 25000;
|
51
|
private static final long THRESHHOLD_2 = 5000;
|
52
|
private static final long THRESHHOLD_3 = 45000;
|
53
|
|
54
|
private static long mPauseTime, mResumeTime;
|
55
|
|
56
|
private long mTouchBlockTime, mUIBlockTime;
|
57
|
private int mLastTouchPlace, mLastUIPlace;
|
58
|
|
59
|
private final WeakReference<TwistyActivity> mAct;
|
60
|
|
61
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
public static void onPause()
|
64
|
{
|
65
|
mPauseTime = System.currentTimeMillis();
|
66
|
}
|
67
|
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
|
70
|
public static void onResume()
|
71
|
{
|
72
|
mResumeTime = System.currentTimeMillis();
|
73
|
}
|
74
|
|
75
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
76
|
|
77
|
public BlockController(TwistyActivity act)
|
78
|
{
|
79
|
mAct = new WeakReference<>(act);
|
80
|
|
81
|
Timer timer = new Timer();
|
82
|
|
83
|
timer.scheduleAtFixedRate(new TimerTask()
|
84
|
{
|
85
|
@Override
|
86
|
public void run()
|
87
|
{
|
88
|
act.runOnUiThread(new Runnable()
|
89
|
{
|
90
|
@Override
|
91
|
public void run()
|
92
|
{
|
93
|
checkingThread();
|
94
|
}
|
95
|
});
|
96
|
}
|
97
|
}, 0, 1000);
|
98
|
}
|
99
|
|
100
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
101
|
// RUBIK_PLACE_3 and TUTORIAL_PLACE_3 are scrambles, those can take up to 20 seconds.
|
102
|
// RUBIK_PLACE_4 and TUTORIAL_PLACE_4 are solves, those can (maybe) sometimes take more than 3 seconds.
|
103
|
// CONTROL_PLACE_* are the visual tutorials, could take up to 45 seconds.
|
104
|
|
105
|
private long getThreshhold(int last)
|
106
|
{
|
107
|
switch(last)
|
108
|
{
|
109
|
case RUBIK_PLACE_3 :
|
110
|
case TUTORIAL_PLACE_3: return THRESHHOLD_1;
|
111
|
case RUBIK_PLACE_4 :
|
112
|
case TUTORIAL_PLACE_4: return THRESHHOLD_2;
|
113
|
case CONTROL_PLACE_0 :
|
114
|
case CONTROL_PLACE_1 : return THRESHHOLD_3;
|
115
|
default : return THRESHHOLD_0;
|
116
|
}
|
117
|
}
|
118
|
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
|
121
|
private void checkingThread()
|
122
|
{
|
123
|
long now = System.currentTimeMillis();
|
124
|
long touchThreshhold = getThreshhold(mLastTouchPlace);
|
125
|
long touchBlocked = now-mTouchBlockTime;
|
126
|
|
127
|
if( mTouchBlockTime>mPauseTime && touchBlocked>touchThreshhold )
|
128
|
{
|
129
|
TwistyActivity act = mAct.get();
|
130
|
boolean reallyBlocked = true;
|
131
|
|
132
|
if( act!=null )
|
133
|
{
|
134
|
TwistyPreRender pre = act.getTwistyPreRender();
|
135
|
if( pre!=null )
|
136
|
{
|
137
|
reallyBlocked = pre.isTouchBlocked();
|
138
|
pre.unblockTouch();
|
139
|
}
|
140
|
}
|
141
|
|
142
|
reportTouchProblem(touchBlocked, reallyBlocked);
|
143
|
}
|
144
|
|
145
|
long uiThreshhold = getThreshhold(mLastUIPlace);
|
146
|
long uiBlocked = now-mUIBlockTime;
|
147
|
|
148
|
if( mUIBlockTime>mPauseTime && uiBlocked>uiThreshhold )
|
149
|
{
|
150
|
TwistyActivity act = mAct.get();
|
151
|
boolean reallyBlocked = true;
|
152
|
|
153
|
if( act!=null )
|
154
|
{
|
155
|
TwistyPreRender pre = act.getTwistyPreRender();
|
156
|
if( pre!=null )
|
157
|
{
|
158
|
reallyBlocked = !pre.isUINotBlocked();
|
159
|
pre.unblockUI();
|
160
|
}
|
161
|
}
|
162
|
|
163
|
reportUIProblem(uiBlocked, reallyBlocked);
|
164
|
}
|
165
|
}
|
166
|
|
167
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168
|
|
169
|
private void reportUIProblem(long time, boolean reallyBlocked)
|
170
|
{
|
171
|
String error = "UI BLOCK "+mLastUIPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
|
172
|
|
173
|
if( BuildConfig.DEBUG )
|
174
|
{
|
175
|
android.util.Log.e("D", error);
|
176
|
}
|
177
|
else
|
178
|
{
|
179
|
Exception ex = new Exception(error);
|
180
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
181
|
crashlytics.setCustomKey("pause" , mPauseTime );
|
182
|
crashlytics.setCustomKey("resume", mResumeTime );
|
183
|
crashlytics.recordException(ex);
|
184
|
}
|
185
|
}
|
186
|
|
187
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
188
|
|
189
|
private void reportTouchProblem(long time, boolean reallyBlocked)
|
190
|
{
|
191
|
String error = "TOUCH BLOCK "+mLastTouchPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
|
192
|
|
193
|
if( BuildConfig.DEBUG )
|
194
|
{
|
195
|
android.util.Log.e("D", error);
|
196
|
}
|
197
|
else
|
198
|
{
|
199
|
Exception ex = new Exception(error);
|
200
|
FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
|
201
|
crashlytics.setCustomKey("pause" , mPauseTime );
|
202
|
crashlytics.setCustomKey("resume", mResumeTime );
|
203
|
crashlytics.setCustomKey("thread", EffectMessageSender.reportState() );
|
204
|
crashlytics.recordException(ex);
|
205
|
}
|
206
|
}
|
207
|
|
208
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
209
|
|
210
|
public void touchBlocked(int place)
|
211
|
{
|
212
|
mTouchBlockTime = System.currentTimeMillis();
|
213
|
mLastTouchPlace = place;
|
214
|
}
|
215
|
|
216
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
217
|
|
218
|
public void uiBlocked(int place)
|
219
|
{
|
220
|
mUIBlockTime = System.currentTimeMillis();
|
221
|
mLastUIPlace = place;
|
222
|
}
|
223
|
|
224
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
225
|
|
226
|
public void touchUnblocked()
|
227
|
{
|
228
|
mTouchBlockTime = 0;
|
229
|
}
|
230
|
|
231
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
232
|
|
233
|
public void uiUnblocked()
|
234
|
{
|
235
|
mUIBlockTime = 0;
|
236
|
}
|
237
|
}
|