Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / BlockController.java @ 967b79dc

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
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 48acd7c6 Leszek Koltunski
  private static final long THRESHHOLD_0 =  3000;
49 809c3432 Leszek Koltunski
  private static final long THRESHHOLD_1 = 25000;
50 967b79dc Leszek Koltunski
  private static final long THRESHHOLD_2 = 45000;
51 809c3432 Leszek Koltunski
52
  private static long mPauseTime, mResumeTime;
53
54
  private long mTouchBlockTime, mUIBlockTime;
55
  private int mLastTouchPlace, mLastUIPlace;
56
57
  private final WeakReference<TwistyActivity> mAct;
58
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61
  public static void onPause()
62
    {
63
    mPauseTime = System.currentTimeMillis();
64
    }
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68
  public static void onResume()
69
    {
70
    mResumeTime = System.currentTimeMillis();
71
    }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75
  public BlockController(TwistyActivity act)
76
    {
77
    mAct = new WeakReference<>(act);
78
79
    Timer timer = new Timer();
80
81
    timer.scheduleAtFixedRate(new TimerTask()
82
      {
83
      @Override
84
      public void run()
85
        {
86
        act.runOnUiThread(new Runnable()
87
          {
88
          @Override
89
          public void run()
90
            {
91
            checkingThread();
92
            }
93
          });
94
        }
95
      }, 0, 1000);
96
    }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
// RUBIK_PLACE_3 and TUTORIAL_PLACE_3 are scrambles, those can take up to 20 seconds.
100 967b79dc Leszek Koltunski
// 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
      case RUBIK_PLACE_3   :
107
      case TUTORIAL_PLACE_3: return THRESHHOLD_1;
108
      case CONTROL_PLACE_0 :
109
      case CONTROL_PLACE_1 : return THRESHHOLD_2;
110
      default              : return THRESHHOLD_0;
111
      }
112
    }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115 809c3432 Leszek Koltunski
116
  private void checkingThread()
117
    {
118
    long now = System.currentTimeMillis();
119
120 967b79dc Leszek Koltunski
    long touchThreshhold = getThreshhold(mLastTouchPlace);
121 809c3432 Leszek Koltunski
122
    if( mTouchBlockTime>mPauseTime && now-mTouchBlockTime>touchThreshhold )
123
      {
124
      TwistyActivity act = mAct.get();
125
126
      if( act!=null )
127
        {
128
        TwistyPreRender pre = act.getTwistyPreRender();
129
        if( pre!=null ) pre.unblockTouch();
130
        }
131
132
      reportTouchProblem(touchThreshhold);
133
      }
134
135 967b79dc Leszek Koltunski
    long uiThreshhold = getThreshhold(mLastUIPlace);
136 809c3432 Leszek Koltunski
137
    if( mUIBlockTime>mPauseTime && now-mUIBlockTime>uiThreshhold )
138
      {
139
      TwistyActivity act = mAct.get();
140
141
      if( act!=null )
142
        {
143
        TwistyPreRender pre = act.getTwistyPreRender();
144
        if( pre!=null ) pre.unblockUI();
145
        }
146
147
      reportUIProblem(uiThreshhold);
148
      }
149
    }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
  private void reportTouchProblem(long time)
154
    {
155
    String error = "TOUCH BLOCK "+mLastTouchPlace+" blocked for "+time+" milliseconds!";
156
157
    if( BuildConfig.DEBUG )
158
       {
159
       android.util.Log.e("D", error);
160
       }
161
    else
162
      {
163
      Exception ex = new Exception(error);
164
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
165
      crashlytics.setCustomKey("type"  , "Touch" );
166
      crashlytics.setCustomKey("place" , mLastTouchPlace );
167
      crashlytics.recordException(ex);
168
      }
169
    }
170
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173
  private void reportUIProblem(long time)
174
    {
175
    String error = "UI BLOCK "+mLastUIPlace+" blocked for "+time+" milliseconds!";
176
177
    if( BuildConfig.DEBUG )
178
       {
179
       android.util.Log.e("D", error);
180
       }
181
    else
182
      {
183
      Exception ex = new Exception(error);
184
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
185
      crashlytics.setCustomKey("type"  , "UI" );
186
      crashlytics.setCustomKey("place" , mLastUIPlace );
187
      crashlytics.recordException(ex);
188
      }
189
    }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193
  public void touchBlocked(int place)
194
    {
195
    mTouchBlockTime = System.currentTimeMillis();
196
    mLastTouchPlace = place;
197
    }
198
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201
  public void uiBlocked(int place)
202
    {
203
    mUIBlockTime = System.currentTimeMillis();
204
    mLastUIPlace = place;
205
    }
206
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
209
  public void touchUnblocked()
210
    {
211
    mTouchBlockTime = 0;
212
    }
213
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
216
  public void uiUnblocked()
217
    {
218
    mUIBlockTime = 0;
219
    }
220
  }