Project

General

Profile

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

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

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.util.Timer;
25
import java.util.TimerTask;
26

    
27
import org.distorted.library.message.EffectMessageSender;
28
import org.distorted.objectlib.BuildConfig;
29
import org.distorted.objectlib.main.ObjectPreRender;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
public class BlockController
34
  {
35
  public static final int PLACE_0 =0;
36
  public static final int PLACE_1 =1;
37
  public static final int PLACE_2 =2;
38
  public static final int PLACE_3 =3;
39
  public static final int PLACE_4 =4;
40
  public static final int CONTROL_PLACE_0 =20;
41
  public static final int CONTROL_PLACE_1 =21;
42
  public static final int MOVES_PLACE_0 =30;
43

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

    
49
  private static long mPauseTime, mResumeTime;
50

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

    
54
  private final ObjectPreRender mPre;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  public BlockController(TwistyActivity act, ObjectPreRender pre)
73
    {
74
    mPre = pre;
75

    
76
    Timer timer = new Timer();
77

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

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

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

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

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

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

    
124
      if( !running )
125
        {
126
        reportThreadProblem();
127
        EffectMessageSender.restartThread();
128
        }
129
      else
130
        {
131
        boolean reallyBlocked = mPre.isTouchBlocked();
132
        mPre.unblockTouch();
133
        reportTouchProblem(touchBlocked, reallyBlocked);
134
        }
135
      }
136

    
137
    long uiThreshhold = getThreshhold(mLastUIPlace);
138
    long uiBlocked = now-mUIBlockTime;
139

    
140
    if( mUIBlockTime>mPauseTime && uiBlocked>uiThreshhold )
141
      {
142
      boolean running = EffectMessageSender.isRunning();
143

    
144
      if( !running )
145
        {
146
        reportThreadProblem();
147
        EffectMessageSender.restartThread();
148
        }
149
      else
150
        {
151
        boolean reallyBlocked =  !mPre.isUINotBlocked();
152
        mPre.unblockUI();
153
        reportUIProblem(uiBlocked, reallyBlocked);
154
        }
155
      }
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
  private void reportUIProblem(long time, boolean reallyBlocked)
161
    {
162
    String error = "UI BLOCK "+mLastUIPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
163

    
164
    if( BuildConfig.DEBUG )
165
       {
166
       android.util.Log.e("D", error);
167
       }
168
    else
169
      {
170
      Exception ex = new Exception(error);
171
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
172
      crashlytics.setCustomKey("pause" , mPauseTime );
173
      crashlytics.setCustomKey("resume", mResumeTime );
174
      crashlytics.recordException(ex);
175
      }
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
  private void reportTouchProblem(long time, boolean reallyBlocked)
181
    {
182
    String error = "TOUCH BLOCK "+mLastTouchPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
183

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  private void reportThreadProblem()
201
    {
202
    String error = EffectMessageSender.reportState();
203

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

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  public void touchBlocked(int place)
221
    {
222
    mTouchBlockTime = System.currentTimeMillis();
223
    mLastTouchPlace = place;
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  public void uiBlocked(int place)
229
    {
230
    mUIBlockTime = System.currentTimeMillis();
231
    mLastUIPlace = place;
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

    
236
  public void touchUnblocked()
237
    {
238
    mTouchBlockTime = 0;
239
    }
240

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

    
243
  public void uiUnblocked()
244
    {
245
    mUIBlockTime = 0;
246
    }
247
  }
(1-1/11)