Project

General

Profile

Download (8.12 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / helpers / BlockController.java @ f5e7bb94

1 809c3432 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.helpers;
21
22
import com.google.firebase.crashlytics.FirebaseCrashlytics;
23
24 f5e7bb94 Leszek Koltunski
import org.distorted.library.message.EffectMessageSender;
25 809c3432 Leszek Koltunski
import org.distorted.main.BuildConfig;
26
27
import java.lang.ref.WeakReference;
28
import java.util.Timer;
29
import java.util.TimerTask;
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 48acd7c6 Leszek Koltunski
  private static final long THRESHHOLD_0 =  3000;
50 809c3432 Leszek Koltunski
  private static final long THRESHHOLD_1 = 25000;
51 8fa39aa6 Leszek Koltunski
  private static final long THRESHHOLD_2 =  5000;
52
  private static final long THRESHHOLD_3 = 45000;
53 809c3432 Leszek Koltunski
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 8fa39aa6 Leszek Koltunski
// RUBIK_PLACE_4 and TUTORIAL_PLACE_4 are solves, those can (maybe) sometimes take more than 3 seconds.
103 967b79dc Leszek Koltunski
// 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 8fa39aa6 Leszek Koltunski
      case RUBIK_PLACE_4   :
112
      case TUTORIAL_PLACE_4: return THRESHHOLD_2;
113 967b79dc Leszek Koltunski
      case CONTROL_PLACE_0 :
114 8fa39aa6 Leszek Koltunski
      case CONTROL_PLACE_1 : return THRESHHOLD_3;
115 967b79dc Leszek Koltunski
      default              : return THRESHHOLD_0;
116
      }
117
    }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120 809c3432 Leszek Koltunski
121
  private void checkingThread()
122
    {
123
    long now = System.currentTimeMillis();
124 967b79dc Leszek Koltunski
    long touchThreshhold = getThreshhold(mLastTouchPlace);
125 8fa39aa6 Leszek Koltunski
    long touchBlocked = now-mTouchBlockTime;
126 809c3432 Leszek Koltunski
127 8fa39aa6 Leszek Koltunski
    if( mTouchBlockTime>mPauseTime && touchBlocked>touchThreshhold )
128 809c3432 Leszek Koltunski
      {
129
      TwistyActivity act = mAct.get();
130 8fa39aa6 Leszek Koltunski
      boolean reallyBlocked = true;
131 809c3432 Leszek Koltunski
132
      if( act!=null )
133
        {
134
        TwistyPreRender pre = act.getTwistyPreRender();
135 8fa39aa6 Leszek Koltunski
        if( pre!=null )
136
          {
137
          reallyBlocked = pre.isTouchBlocked();
138
          pre.unblockTouch();
139
          }
140 809c3432 Leszek Koltunski
        }
141
142 8fa39aa6 Leszek Koltunski
      reportTouchProblem(touchBlocked, reallyBlocked);
143 809c3432 Leszek Koltunski
      }
144
145 967b79dc Leszek Koltunski
    long uiThreshhold = getThreshhold(mLastUIPlace);
146 8fa39aa6 Leszek Koltunski
    long uiBlocked = now-mUIBlockTime;
147 809c3432 Leszek Koltunski
148 8fa39aa6 Leszek Koltunski
    if( mUIBlockTime>mPauseTime && uiBlocked>uiThreshhold )
149 809c3432 Leszek Koltunski
      {
150
      TwistyActivity act = mAct.get();
151 8fa39aa6 Leszek Koltunski
      boolean reallyBlocked = true;
152 809c3432 Leszek Koltunski
153
      if( act!=null )
154
        {
155
        TwistyPreRender pre = act.getTwistyPreRender();
156 8fa39aa6 Leszek Koltunski
        if( pre!=null )
157
          {
158
          reallyBlocked = !pre.isUINotBlocked();
159
          pre.unblockUI();
160
          }
161 809c3432 Leszek Koltunski
        }
162
163 8fa39aa6 Leszek Koltunski
      reportUIProblem(uiBlocked, reallyBlocked);
164 809c3432 Leszek Koltunski
      }
165
    }
166
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
169 8fa39aa6 Leszek Koltunski
  private void reportUIProblem(long time, boolean reallyBlocked)
170 809c3432 Leszek Koltunski
    {
171 8fa39aa6 Leszek Koltunski
    String error = "UI BLOCK "+mLastUIPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
172 809c3432 Leszek Koltunski
173
    if( BuildConfig.DEBUG )
174
       {
175
       android.util.Log.e("D", error);
176
       }
177
    else
178
      {
179
      Exception ex = new Exception(error);
180
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
181 8fa39aa6 Leszek Koltunski
      crashlytics.setCustomKey("pause" , mPauseTime );
182
      crashlytics.setCustomKey("resume", mResumeTime );
183 809c3432 Leszek Koltunski
      crashlytics.recordException(ex);
184
      }
185
    }
186
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189 8fa39aa6 Leszek Koltunski
  private void reportTouchProblem(long time, boolean reallyBlocked)
190 809c3432 Leszek Koltunski
    {
191 8fa39aa6 Leszek Koltunski
    String error = "TOUCH BLOCK "+mLastTouchPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
192 809c3432 Leszek Koltunski
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 8fa39aa6 Leszek Koltunski
      crashlytics.setCustomKey("pause" , mPauseTime );
202
      crashlytics.setCustomKey("resume", mResumeTime );
203 f5e7bb94 Leszek Koltunski
      crashlytics.setCustomKey("thread", EffectMessageSender.reportState() );
204 809c3432 Leszek Koltunski
      crashlytics.recordException(ex);
205
      }
206
    }
207
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210
  public void touchBlocked(int place)
211
    {
212
    mTouchBlockTime = System.currentTimeMillis();
213
    mLastTouchPlace = place;
214
    }
215
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217
218
  public void uiBlocked(int place)
219
    {
220
    mUIBlockTime = System.currentTimeMillis();
221
    mLastUIPlace = place;
222
    }
223
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225
226
  public void touchUnblocked()
227
    {
228
    mTouchBlockTime = 0;
229
    }
230
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232
233
  public void uiUnblocked()
234
    {
235
    mUIBlockTime = 0;
236
    }
237
  }