Project

General

Profile

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

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

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 PLACE_0 =0;
37
  public static final int PLACE_1 =1;
38
  public static final int PLACE_2 =2;
39
  public static final int PLACE_3 =3;
40
  public static final int PLACE_4 =4;
41
  public static final int CONTROL_PLACE_0 =20;
42
  public static final int CONTROL_PLACE_1 =21;
43
  public static final int MOVES_PLACE_0 =30;
44

    
45
  private static final long THRESHHOLD_0 =  3000;
46
  private static final long THRESHHOLD_1 = 25000;
47
  private static final long THRESHHOLD_2 =  5000;
48
  private static final long THRESHHOLD_3 = 45000;
49

    
50
  private static long mPauseTime, mResumeTime;
51

    
52
  private long mTouchBlockTime, mUIBlockTime;
53
  private int mLastTouchPlace, mLastUIPlace;
54

    
55
  private final WeakReference<TwistyActivity> mAct;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  public static void onPause()
60
    {
61
    mPauseTime = System.currentTimeMillis();
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  public static void onResume()
67
    {
68
    mResumeTime = System.currentTimeMillis();
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  public BlockController(TwistyActivity act)
74
    {
75
    mAct = new WeakReference<>(act);
76

    
77
    Timer timer = new Timer();
78

    
79
    timer.scheduleAtFixedRate(new TimerTask()
80
      {
81
      @Override
82
      public void run()
83
        {
84
        act.runOnUiThread(new Runnable()
85
          {
86
          @Override
87
          public void run()
88
            {
89
            checkingThread();
90
            }
91
          });
92
        }
93
      }, 0, 1000);
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
// RUBIK_PLACE_3 and TUTORIAL_PLACE_3 are scrambles, those can take up to 20 seconds.
98
// RUBIK_PLACE_4 and TUTORIAL_PLACE_4 are solves, those can (maybe) sometimes take more than 3 seconds.
99
// CONTROL_PLACE_* are the visual tutorials, could take up to 45 seconds.
100

    
101
  private long getThreshhold(int last)
102
    {
103
    switch(last)
104
      {
105
      case PLACE_3         :return THRESHHOLD_1;
106
      case PLACE_4         : return THRESHHOLD_2;
107
      case CONTROL_PLACE_0 :
108
      case CONTROL_PLACE_1 : return THRESHHOLD_3;
109
      default              : return THRESHHOLD_0;
110
      }
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
  private void checkingThread()
116
    {
117
    long now = System.currentTimeMillis();
118
    long touchThreshhold = getThreshhold(mLastTouchPlace);
119
    long touchBlocked = now-mTouchBlockTime;
120

    
121
    if( mTouchBlockTime>mPauseTime && touchBlocked>touchThreshhold )
122
      {
123
      boolean running = EffectMessageSender.isRunning();
124

    
125
      if( !running )
126
        {
127
        reportThreadProblem();
128
        EffectMessageSender.restartThread();
129
        }
130
      else
131
        {
132
        TwistyActivity act = mAct.get();
133
        boolean reallyBlocked = true;
134

    
135
        if( act!=null )
136
          {
137
          ObjectPreRender pre = act.getPreRender();
138
          if( pre!=null )
139
            {
140
            reallyBlocked = pre.isTouchBlocked();
141
            pre.unblockTouch();
142
            }
143
          }
144

    
145
        reportTouchProblem(touchBlocked, reallyBlocked);
146
        }
147
      }
148

    
149
    long uiThreshhold = getThreshhold(mLastUIPlace);
150
    long uiBlocked = now-mUIBlockTime;
151

    
152
    if( mUIBlockTime>mPauseTime && uiBlocked>uiThreshhold )
153
      {
154
      boolean running = EffectMessageSender.isRunning();
155

    
156
      if( !running )
157
        {
158
        reportThreadProblem();
159
        EffectMessageSender.restartThread();
160
        }
161
      else
162
        {
163
        TwistyActivity act = mAct.get();
164
        boolean reallyBlocked = true;
165

    
166
        if( act!=null )
167
          {
168
          ObjectPreRender pre = act.getPreRender();
169
          if( pre!=null )
170
            {
171
            reallyBlocked = !pre.isUINotBlocked();
172
            pre.unblockUI();
173
            }
174
          }
175

    
176
        reportUIProblem(uiBlocked, reallyBlocked);
177
        }
178
      }
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  private void reportUIProblem(long time, boolean reallyBlocked)
184
    {
185
    String error = "UI BLOCK "+mLastUIPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
186

    
187
    if( BuildConfig.DEBUG )
188
       {
189
       android.util.Log.e("D", error);
190
       }
191
    else
192
      {
193
      Exception ex = new Exception(error);
194
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
195
      crashlytics.setCustomKey("pause" , mPauseTime );
196
      crashlytics.setCustomKey("resume", mResumeTime );
197
      crashlytics.recordException(ex);
198
      }
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  private void reportTouchProblem(long time, boolean reallyBlocked)
204
    {
205
    String error = "TOUCH BLOCK "+mLastTouchPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
206

    
207
    if( BuildConfig.DEBUG )
208
       {
209
       android.util.Log.e("D", error);
210
       }
211
    else
212
      {
213
      Exception ex = new Exception(error);
214
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
215
      crashlytics.setCustomKey("pause" , mPauseTime );
216
      crashlytics.setCustomKey("resume", mResumeTime );
217
      crashlytics.recordException(ex);
218
      }
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  private void reportThreadProblem()
224
    {
225
    String error = EffectMessageSender.reportState();
226

    
227
    if( BuildConfig.DEBUG )
228
       {
229
       android.util.Log.e("D", error);
230
       }
231
    else
232
      {
233
      Exception ex = new Exception(error);
234
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
235
      crashlytics.setCustomKey("pause" , mPauseTime );
236
      crashlytics.setCustomKey("resume", mResumeTime );
237
      crashlytics.recordException(ex);
238
      }
239
    }
240

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

    
243
  public void touchBlocked(int place)
244
    {
245
    mTouchBlockTime = System.currentTimeMillis();
246
    mLastTouchPlace = place;
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

    
251
  public void uiBlocked(int place)
252
    {
253
    mUIBlockTime = System.currentTimeMillis();
254
    mLastUIPlace = place;
255
    }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
  public void touchUnblocked()
260
    {
261
    mTouchBlockTime = 0;
262
    }
263

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

    
266
  public void uiUnblocked()
267
    {
268
    mUIBlockTime = 0;
269
    }
270
  }
(1-1/12)