Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / helpers / BlockController.java @ 198c5bf0

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

    
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
      boolean running = EffectMessageSender.isRunning();
130

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

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

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

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

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

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

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

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

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private void reportUIProblem(long time, boolean reallyBlocked)
190
    {
191
    String error = "UI BLOCK "+mLastUIPlace+" 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.recordException(ex);
204
      }
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

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

    
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
      crashlytics.setCustomKey("pause" , mPauseTime );
222
      crashlytics.setCustomKey("resume", mResumeTime );
223
      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
      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
  }
(1-1/11)