Project

General

Profile

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

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

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 android.app.Activity;
23

    
24
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
import org.distorted.objectlib.main.ObjectPreRender;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class BlockController
36
  {
37
  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
  public static final int CONTROL_PLACE_0 =20;
43
  public static final int CONTROL_PLACE_1 =21;
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 ObjectPreRender mPre;
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(Activity act, ObjectPreRender pre)
74
    {
75
    mPre = pre;
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
        boolean reallyBlocked = mPre.isTouchBlocked();
133
        mPre.unblockTouch();
134
        reportTouchProblem(touchBlocked, reallyBlocked);
135
        }
136
      }
137

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

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

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

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

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

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

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

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

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

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

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

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