Project

General

Profile

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

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

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