Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / BlockController.java @ 8fa39aa6

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.helpers;
21

    
22
import com.google.firebase.crashlytics.FirebaseCrashlytics;
23

    
24
import org.distorted.main.BuildConfig;
25

    
26
import java.lang.ref.WeakReference;
27
import java.util.Timer;
28
import java.util.TimerTask;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
public class BlockController
33
  {
34
  public static final int RUBIK_PLACE_0 =0;
35
  public static final int RUBIK_PLACE_1 =1;
36
  public static final int RUBIK_PLACE_2 =2;
37
  public static final int RUBIK_PLACE_3 =3;
38
  public static final int RUBIK_PLACE_4 =4;
39
  public static final int TUTORIAL_PLACE_0 =10;
40
  public static final int TUTORIAL_PLACE_1 =11;
41
  public static final int TUTORIAL_PLACE_2 =12;
42
  public static final int TUTORIAL_PLACE_3 =13;
43
  public static final int TUTORIAL_PLACE_4 =14;
44
  public static final int CONTROL_PLACE_0 =20;
45
  public static final int CONTROL_PLACE_1 =21;
46
  public static final int MOVES_PLACE_0 =30;
47

    
48
  private static final long THRESHHOLD_0 =  3000;
49
  private static final long THRESHHOLD_1 = 25000;
50
  private static final long THRESHHOLD_2 =  5000;
51
  private static final long THRESHHOLD_3 = 45000;
52

    
53
  private static long mPauseTime, mResumeTime;
54

    
55
  private long mTouchBlockTime, mUIBlockTime;
56
  private int mLastTouchPlace, mLastUIPlace;
57

    
58
  private final WeakReference<TwistyActivity> mAct;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  public static void onPause()
63
    {
64
    mPauseTime = System.currentTimeMillis();
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
  public static void onResume()
70
    {
71
    mResumeTime = System.currentTimeMillis();
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  public BlockController(TwistyActivity act)
77
    {
78
    mAct = new WeakReference<>(act);
79

    
80
    Timer timer = new Timer();
81

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

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

    
104
  private long getThreshhold(int last)
105
    {
106
    switch(last)
107
      {
108
      case RUBIK_PLACE_3   :
109
      case TUTORIAL_PLACE_3: return THRESHHOLD_1;
110
      case RUBIK_PLACE_4   :
111
      case TUTORIAL_PLACE_4: return THRESHHOLD_2;
112
      case CONTROL_PLACE_0 :
113
      case CONTROL_PLACE_1 : return THRESHHOLD_3;
114
      default              : return THRESHHOLD_0;
115
      }
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
  private void checkingThread()
121
    {
122
    long now = System.currentTimeMillis();
123
    long touchThreshhold = getThreshhold(mLastTouchPlace);
124
    long touchBlocked = now-mTouchBlockTime;
125

    
126
    if( mTouchBlockTime>mPauseTime && touchBlocked>touchThreshhold )
127
      {
128
      TwistyActivity act = mAct.get();
129
      boolean reallyBlocked = true;
130

    
131
      if( act!=null )
132
        {
133
        TwistyPreRender pre = act.getTwistyPreRender();
134
        if( pre!=null )
135
          {
136
          reallyBlocked = pre.isTouchBlocked();
137
          pre.unblockTouch();
138
          }
139
        }
140

    
141
      reportTouchProblem(touchBlocked, reallyBlocked);
142
      }
143

    
144
    long uiThreshhold = getThreshhold(mLastUIPlace);
145
    long uiBlocked = now-mUIBlockTime;
146

    
147
    if( mUIBlockTime>mPauseTime && uiBlocked>uiThreshhold )
148
      {
149
      TwistyActivity act = mAct.get();
150
      boolean reallyBlocked = true;
151

    
152
      if( act!=null )
153
        {
154
        TwistyPreRender pre = act.getTwistyPreRender();
155
        if( pre!=null )
156
          {
157
          reallyBlocked = !pre.isUINotBlocked();
158
          pre.unblockUI();
159
          }
160
        }
161

    
162
      reportUIProblem(uiBlocked, reallyBlocked);
163
      }
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  private void reportUIProblem(long time, boolean reallyBlocked)
169
    {
170
    String error = "UI BLOCK "+mLastUIPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
171

    
172
    if( BuildConfig.DEBUG )
173
       {
174
       android.util.Log.e("D", error);
175
       }
176
    else
177
      {
178
      Exception ex = new Exception(error);
179
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
180
      crashlytics.setCustomKey("pause" , mPauseTime );
181
      crashlytics.setCustomKey("resume", mResumeTime );
182
      crashlytics.recordException(ex);
183
      }
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

    
188
  private void reportTouchProblem(long time, boolean reallyBlocked)
189
    {
190
    String error = "TOUCH BLOCK "+mLastTouchPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
191

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
  public void touchBlocked(int place)
209
    {
210
    mTouchBlockTime = System.currentTimeMillis();
211
    mLastTouchPlace = place;
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  public void uiBlocked(int place)
217
    {
218
    mUIBlockTime = System.currentTimeMillis();
219
    mLastUIPlace = place;
220
    }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
  public void touchUnblocked()
225
    {
226
    mTouchBlockTime = 0;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  public void uiUnblocked()
232
    {
233
    mUIBlockTime = 0;
234
    }
235
  }
(3-3/12)