Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / BlockController.java @ 3f7a4363

1 809c3432 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 java.lang.ref.WeakReference;
25
import java.util.Timer;
26
import java.util.TimerTask;
27
28 3f7a4363 Leszek Koltunski
import org.distorted.library.message.EffectMessageSender;
29
import org.distorted.main.BuildConfig;
30
31 809c3432 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 48acd7c6 Leszek Koltunski
  private static final long THRESHHOLD_0 =  3000;
50 809c3432 Leszek Koltunski
  private static final long THRESHHOLD_1 = 25000;
51 8fa39aa6 Leszek Koltunski
  private static final long THRESHHOLD_2 =  5000;
52
  private static final long THRESHHOLD_3 = 45000;
53 809c3432 Leszek Koltunski
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 8fa39aa6 Leszek Koltunski
// RUBIK_PLACE_4 and TUTORIAL_PLACE_4 are solves, those can (maybe) sometimes take more than 3 seconds.
103 967b79dc Leszek Koltunski
// 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 8fa39aa6 Leszek Koltunski
      case RUBIK_PLACE_4   :
112
      case TUTORIAL_PLACE_4: return THRESHHOLD_2;
113 967b79dc Leszek Koltunski
      case CONTROL_PLACE_0 :
114 8fa39aa6 Leszek Koltunski
      case CONTROL_PLACE_1 : return THRESHHOLD_3;
115 967b79dc Leszek Koltunski
      default              : return THRESHHOLD_0;
116
      }
117
    }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120 809c3432 Leszek Koltunski
121
  private void checkingThread()
122
    {
123
    long now = System.currentTimeMillis();
124 967b79dc Leszek Koltunski
    long touchThreshhold = getThreshhold(mLastTouchPlace);
125 8fa39aa6 Leszek Koltunski
    long touchBlocked = now-mTouchBlockTime;
126 809c3432 Leszek Koltunski
127 8fa39aa6 Leszek Koltunski
    if( mTouchBlockTime>mPauseTime && touchBlocked>touchThreshhold )
128 809c3432 Leszek Koltunski
      {
129 4c49986e Leszek Koltunski
      boolean running = EffectMessageSender.isRunning();
130 809c3432 Leszek Koltunski
131 4c49986e Leszek Koltunski
      if( !running )
132 809c3432 Leszek Koltunski
        {
133 4c49986e Leszek Koltunski
        reportThreadProblem();
134
        EffectMessageSender.restartThread();
135
        }
136
      else
137
        {
138
        TwistyActivity act = mAct.get();
139
        boolean reallyBlocked = true;
140
141
        if( act!=null )
142 8fa39aa6 Leszek Koltunski
          {
143 4c49986e Leszek Koltunski
          TwistyPreRender pre = act.getTwistyPreRender();
144
          if( pre!=null )
145
            {
146
            reallyBlocked = pre.isTouchBlocked();
147
            pre.unblockTouch();
148
            }
149 8fa39aa6 Leszek Koltunski
          }
150 809c3432 Leszek Koltunski
151 4c49986e Leszek Koltunski
        reportTouchProblem(touchBlocked, reallyBlocked);
152
        }
153 809c3432 Leszek Koltunski
      }
154
155 967b79dc Leszek Koltunski
    long uiThreshhold = getThreshhold(mLastUIPlace);
156 8fa39aa6 Leszek Koltunski
    long uiBlocked = now-mUIBlockTime;
157 809c3432 Leszek Koltunski
158 8fa39aa6 Leszek Koltunski
    if( mUIBlockTime>mPauseTime && uiBlocked>uiThreshhold )
159 809c3432 Leszek Koltunski
      {
160 4c49986e Leszek Koltunski
      boolean running = EffectMessageSender.isRunning();
161 809c3432 Leszek Koltunski
162 4c49986e Leszek Koltunski
      if( !running )
163
        {
164
        reportThreadProblem();
165
        EffectMessageSender.restartThread();
166
        }
167
      else
168 809c3432 Leszek Koltunski
        {
169 4c49986e Leszek Koltunski
        TwistyActivity act = mAct.get();
170
        boolean reallyBlocked = true;
171
172
        if( act!=null )
173 8fa39aa6 Leszek Koltunski
          {
174 4c49986e Leszek Koltunski
          TwistyPreRender pre = act.getTwistyPreRender();
175
          if( pre!=null )
176
            {
177
            reallyBlocked = !pre.isUINotBlocked();
178
            pre.unblockUI();
179
            }
180 8fa39aa6 Leszek Koltunski
          }
181 809c3432 Leszek Koltunski
182 4c49986e Leszek Koltunski
        reportUIProblem(uiBlocked, reallyBlocked);
183
        }
184 809c3432 Leszek Koltunski
      }
185
    }
186
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189 8fa39aa6 Leszek Koltunski
  private void reportUIProblem(long time, boolean reallyBlocked)
190 809c3432 Leszek Koltunski
    {
191 8fa39aa6 Leszek Koltunski
    String error = "UI BLOCK "+mLastUIPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
192 809c3432 Leszek Koltunski
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 8fa39aa6 Leszek Koltunski
      crashlytics.setCustomKey("pause" , mPauseTime );
202
      crashlytics.setCustomKey("resume", mResumeTime );
203 809c3432 Leszek Koltunski
      crashlytics.recordException(ex);
204
      }
205
    }
206
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
209 8fa39aa6 Leszek Koltunski
  private void reportTouchProblem(long time, boolean reallyBlocked)
210 809c3432 Leszek Koltunski
    {
211 8fa39aa6 Leszek Koltunski
    String error = "TOUCH BLOCK "+mLastTouchPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
212 809c3432 Leszek Koltunski
213
    if( BuildConfig.DEBUG )
214
       {
215
       android.util.Log.e("D", error);
216
       }
217
    else
218
      {
219
      Exception ex = new Exception(error);
220
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
221 8fa39aa6 Leszek Koltunski
      crashlytics.setCustomKey("pause" , mPauseTime );
222
      crashlytics.setCustomKey("resume", mResumeTime );
223 4c49986e Leszek Koltunski
      crashlytics.recordException(ex);
224
      }
225
    }
226
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
229
  private void reportThreadProblem()
230
    {
231
    String error = EffectMessageSender.reportState();
232
233
    if( BuildConfig.DEBUG )
234
       {
235
       android.util.Log.e("D", error);
236
       }
237
    else
238
      {
239
      Exception ex = new Exception(error);
240
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
241
      crashlytics.setCustomKey("pause" , mPauseTime );
242
      crashlytics.setCustomKey("resume", mResumeTime );
243 809c3432 Leszek Koltunski
      crashlytics.recordException(ex);
244
      }
245
    }
246
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248
249
  public void touchBlocked(int place)
250
    {
251
    mTouchBlockTime = System.currentTimeMillis();
252
    mLastTouchPlace = place;
253
    }
254
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
257
  public void uiBlocked(int place)
258
    {
259
    mUIBlockTime = System.currentTimeMillis();
260
    mLastUIPlace = place;
261
    }
262
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264
265
  public void touchUnblocked()
266
    {
267
    mTouchBlockTime = 0;
268
    }
269
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
272
  public void uiUnblocked()
273
    {
274
    mUIBlockTime = 0;
275
    }
276
  }