Project

General

Profile

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

magiccube / src / main / java / org / distorted / helpers / BlockController.java @ 48acd7c6

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

    
51
  private static long mPauseTime, mResumeTime;
52

    
53
  private long mTouchBlockTime, mUIBlockTime;
54
  private int mLastTouchPlace, mLastUIPlace;
55

    
56
  private final WeakReference<TwistyActivity> mAct;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

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

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

    
78
    Timer timer = new Timer();
79

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

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// RUBIK_PLACE_3 and TUTORIAL_PLACE_3 are scrambles, those can take up to 20 seconds.
99

    
100
  private void checkingThread()
101
    {
102
    long now = System.currentTimeMillis();
103

    
104
    long touchThreshhold = (mLastTouchPlace==RUBIK_PLACE_3 || mLastTouchPlace==TUTORIAL_PLACE_3) ? THRESHHOLD_1 : THRESHHOLD_0;
105

    
106
    if( mTouchBlockTime>mPauseTime && now-mTouchBlockTime>touchThreshhold )
107
      {
108
      TwistyActivity act = mAct.get();
109

    
110
      if( act!=null )
111
        {
112
        TwistyPreRender pre = act.getTwistyPreRender();
113
        if( pre!=null ) pre.unblockTouch();
114
        }
115

    
116
      reportTouchProblem(touchThreshhold);
117
      }
118

    
119
    long uiThreshhold = (mLastUIPlace==RUBIK_PLACE_3 || mLastUIPlace==TUTORIAL_PLACE_3) ? THRESHHOLD_1 : THRESHHOLD_0;
120

    
121
    if( mUIBlockTime>mPauseTime && now-mUIBlockTime>uiThreshhold )
122
      {
123
      TwistyActivity act = mAct.get();
124

    
125
      if( act!=null )
126
        {
127
        TwistyPreRender pre = act.getTwistyPreRender();
128
        if( pre!=null ) pre.unblockUI();
129
        }
130

    
131
      reportUIProblem(uiThreshhold);
132
      }
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
  private void reportTouchProblem(long time)
138
    {
139
    String error = "TOUCH BLOCK "+mLastTouchPlace+" blocked for "+time+" milliseconds!";
140

    
141
    if( BuildConfig.DEBUG )
142
       {
143
       android.util.Log.e("D", error);
144
       }
145
    else
146
      {
147
      Exception ex = new Exception(error);
148
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
149
      crashlytics.setCustomKey("type"  , "Touch" );
150
      crashlytics.setCustomKey("place" , mLastTouchPlace );
151
      crashlytics.recordException(ex);
152
      }
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
  private void reportUIProblem(long time)
158
    {
159
    String error = "UI BLOCK "+mLastUIPlace+" blocked for "+time+" milliseconds!";
160

    
161
    if( BuildConfig.DEBUG )
162
       {
163
       android.util.Log.e("D", error);
164
       }
165
    else
166
      {
167
      Exception ex = new Exception(error);
168
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
169
      crashlytics.setCustomKey("type"  , "UI" );
170
      crashlytics.setCustomKey("place" , mLastUIPlace );
171
      crashlytics.recordException(ex);
172
      }
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
  public void touchBlocked(int place)
178
    {
179
    mTouchBlockTime = System.currentTimeMillis();
180
    mLastTouchPlace = place;
181
    }
182

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184

    
185
  public void uiBlocked(int place)
186
    {
187
    mUIBlockTime = System.currentTimeMillis();
188
    mLastUIPlace = place;
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
  public void touchUnblocked()
194
    {
195
    mTouchBlockTime = 0;
196
    }
197

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

    
200
  public void uiUnblocked()
201
    {
202
    mUIBlockTime = 0;
203
    }
204
  }
(3-3/11)