Project

General

Profile

Download (9.12 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / helpers / BlockController.java @ 7c111294

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.objectlib.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
import org.distorted.library.message.EffectMessageSender;
29
import org.distorted.objectlib.BuildConfig;
30
import org.distorted.objectlib.main.ObjectPreRender;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public class BlockController
35
  {
36
  public static final int RUBIK_PLACE_0 =0;
37
  public static final int RUBIK_PLACE_1 =1;
38
  public static final int RUBIK_PLACE_2 =2;
39
  public static final int RUBIK_PLACE_3 =3;
40
  public static final int RUBIK_PLACE_4 =4;
41
  public static final int TUTORIAL_PLACE_0 =10;
42
  public static final int TUTORIAL_PLACE_1 =11;
43
  public static final int TUTORIAL_PLACE_2 =12;
44
  public static final int TUTORIAL_PLACE_3 =13;
45
  public static final int TUTORIAL_PLACE_4 =14;
46
  public static final int CONTROL_PLACE_0 =20;
47
  public static final int CONTROL_PLACE_1 =21;
48
  public static final int MOVES_PLACE_0 =30;
49

    
50
  private static final long THRESHHOLD_0 =  3000;
51
  private static final long THRESHHOLD_1 = 25000;
52
  private static final long THRESHHOLD_2 =  5000;
53
  private static final long THRESHHOLD_3 = 45000;
54

    
55
  private static long mPauseTime, mResumeTime;
56

    
57
  private long mTouchBlockTime, mUIBlockTime;
58
  private int mLastTouchPlace, mLastUIPlace;
59

    
60
  private final WeakReference<TwistyActivity> mAct;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  public static void onPause()
65
    {
66
    mPauseTime = System.currentTimeMillis();
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  public static void onResume()
72
    {
73
    mResumeTime = System.currentTimeMillis();
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  public BlockController(TwistyActivity act)
79
    {
80
    mAct = new WeakReference<>(act);
81

    
82
    Timer timer = new Timer();
83

    
84
    timer.scheduleAtFixedRate(new TimerTask()
85
      {
86
      @Override
87
      public void run()
88
        {
89
        act.runOnUiThread(new Runnable()
90
          {
91
          @Override
92
          public void run()
93
            {
94
            checkingThread();
95
            }
96
          });
97
        }
98
      }, 0, 1000);
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
// RUBIK_PLACE_3 and TUTORIAL_PLACE_3 are scrambles, those can take up to 20 seconds.
103
// RUBIK_PLACE_4 and TUTORIAL_PLACE_4 are solves, those can (maybe) sometimes take more than 3 seconds.
104
// CONTROL_PLACE_* are the visual tutorials, could take up to 45 seconds.
105

    
106
  private long getThreshhold(int last)
107
    {
108
    switch(last)
109
      {
110
      case RUBIK_PLACE_3   :
111
      case TUTORIAL_PLACE_3: return THRESHHOLD_1;
112
      case RUBIK_PLACE_4   :
113
      case TUTORIAL_PLACE_4: return THRESHHOLD_2;
114
      case CONTROL_PLACE_0 :
115
      case CONTROL_PLACE_1 : return THRESHHOLD_3;
116
      default              : return THRESHHOLD_0;
117
      }
118
    }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
  private void checkingThread()
123
    {
124
    long now = System.currentTimeMillis();
125
    long touchThreshhold = getThreshhold(mLastTouchPlace);
126
    long touchBlocked = now-mTouchBlockTime;
127

    
128
    if( mTouchBlockTime>mPauseTime && touchBlocked>touchThreshhold )
129
      {
130
      boolean running = EffectMessageSender.isRunning();
131

    
132
      if( !running )
133
        {
134
        reportThreadProblem();
135
        EffectMessageSender.restartThread();
136
        }
137
      else
138
        {
139
        TwistyActivity act = mAct.get();
140
        boolean reallyBlocked = true;
141

    
142
        if( act!=null )
143
          {
144
          ObjectPreRender pre = act.getPreRender();
145
          if( pre!=null )
146
            {
147
            reallyBlocked = pre.isTouchBlocked();
148
            pre.unblockTouch();
149
            }
150
          }
151

    
152
        reportTouchProblem(touchBlocked, reallyBlocked);
153
        }
154
      }
155

    
156
    long uiThreshhold = getThreshhold(mLastUIPlace);
157
    long uiBlocked = now-mUIBlockTime;
158

    
159
    if( mUIBlockTime>mPauseTime && uiBlocked>uiThreshhold )
160
      {
161
      boolean running = EffectMessageSender.isRunning();
162

    
163
      if( !running )
164
        {
165
        reportThreadProblem();
166
        EffectMessageSender.restartThread();
167
        }
168
      else
169
        {
170
        TwistyActivity act = mAct.get();
171
        boolean reallyBlocked = true;
172

    
173
        if( act!=null )
174
          {
175
          ObjectPreRender pre = act.getPreRender();
176
          if( pre!=null )
177
            {
178
            reallyBlocked = !pre.isUINotBlocked();
179
            pre.unblockUI();
180
            }
181
          }
182

    
183
        reportUIProblem(uiBlocked, reallyBlocked);
184
        }
185
      }
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
  private void reportUIProblem(long time, boolean reallyBlocked)
191
    {
192
    String error = "UI BLOCK "+mLastUIPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
193

    
194
    if( BuildConfig.DEBUG )
195
       {
196
       android.util.Log.e("D", error);
197
       }
198
    else
199
      {
200
      Exception ex = new Exception(error);
201
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
202
      crashlytics.setCustomKey("pause" , mPauseTime );
203
      crashlytics.setCustomKey("resume", mResumeTime );
204
      crashlytics.recordException(ex);
205
      }
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
  private void reportTouchProblem(long time, boolean reallyBlocked)
211
    {
212
    String error = "TOUCH BLOCK "+mLastTouchPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
213

    
214
    if( BuildConfig.DEBUG )
215
       {
216
       android.util.Log.e("D", error);
217
       }
218
    else
219
      {
220
      Exception ex = new Exception(error);
221
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
222
      crashlytics.setCustomKey("pause" , mPauseTime );
223
      crashlytics.setCustomKey("resume", mResumeTime );
224
      crashlytics.recordException(ex);
225
      }
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  private void reportThreadProblem()
231
    {
232
    String error = EffectMessageSender.reportState();
233

    
234
    if( BuildConfig.DEBUG )
235
       {
236
       android.util.Log.e("D", error);
237
       }
238
    else
239
      {
240
      Exception ex = new Exception(error);
241
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
242
      crashlytics.setCustomKey("pause" , mPauseTime );
243
      crashlytics.setCustomKey("resume", mResumeTime );
244
      crashlytics.recordException(ex);
245
      }
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249

    
250
  public void touchBlocked(int place)
251
    {
252
    mTouchBlockTime = System.currentTimeMillis();
253
    mLastTouchPlace = place;
254
    }
255

    
256
///////////////////////////////////////////////////////////////////////////////////////////////////
257

    
258
  public void uiBlocked(int place)
259
    {
260
    mUIBlockTime = System.currentTimeMillis();
261
    mLastUIPlace = place;
262
    }
263

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

    
266
  public void touchUnblocked()
267
    {
268
    mTouchBlockTime = 0;
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  public void uiUnblocked()
274
    {
275
    mUIBlockTime = 0;
276
    }
277
  }
(1-1/12)