Project

General

Profile

« Previous | Next » 

Revision 88a3e972

Added by Leszek Koltunski over 2 years ago

Move more code to objectlib.

View differences:

src/main/java/org/distorted/control/RubikControl.java
19 19

  
20 20
package org.distorted.control;
21 21

  
22
import org.distorted.helpers.BlockController;
22
import org.distorted.objectlib.helpers.BlockController;
23 23
import org.distorted.library.main.DistortedNode;
24 24
import org.distorted.library.main.DistortedScreen;
25 25
import org.distorted.library.message.EffectListener;
src/main/java/org/distorted/effects/EffectController.java
21 21

  
22 22
import org.distorted.library.message.EffectListener;
23 23
import org.distorted.objectlib.main.TwistyObject;
24
import org.distorted.helpers.MovesFinished;
24
import org.distorted.objectlib.helpers.MovesFinished;
25 25

  
26 26
///////////////////////////////////////////////////////////////////////////////////////////////////
27 27

  
src/main/java/org/distorted/effects/scramble/ScrambleEffect.java
32 32

  
33 33
import org.distorted.effects.BaseEffect;
34 34
import org.distorted.effects.EffectController;
35
import org.distorted.helpers.MovesFinished;
35
import org.distorted.objectlib.helpers.MovesFinished;
36 36

  
37 37
///////////////////////////////////////////////////////////////////////////////////////////////////
38 38

  
src/main/java/org/distorted/helpers/BlockController.java
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 java.lang.ref.WeakReference;
25
import java.util.Timer;
26
import java.util.TimerTask;
27

  
28
import org.distorted.library.message.EffectMessageSender;
29
import org.distorted.main.BuildConfig;
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
  private static final long THRESHHOLD_0 =  3000;
50
  private static final long THRESHHOLD_1 = 25000;
51
  private static final long THRESHHOLD_2 =  5000;
52
  private static final long THRESHHOLD_3 = 45000;
53

  
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
// RUBIK_PLACE_4 and TUTORIAL_PLACE_4 are solves, those can (maybe) sometimes take more than 3 seconds.
103
// 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
      case RUBIK_PLACE_4   :
112
      case TUTORIAL_PLACE_4: return THRESHHOLD_2;
113
      case CONTROL_PLACE_0 :
114
      case CONTROL_PLACE_1 : return THRESHHOLD_3;
115
      default              : return THRESHHOLD_0;
116
      }
117
    }
118

  
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

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

  
127
    if( mTouchBlockTime>mPauseTime && touchBlocked>touchThreshhold )
128
      {
129
      boolean running = EffectMessageSender.isRunning();
130

  
131
      if( !running )
132
        {
133
        reportThreadProblem();
134
        EffectMessageSender.restartThread();
135
        }
136
      else
137
        {
138
        TwistyActivity act = mAct.get();
139
        boolean reallyBlocked = true;
140

  
141
        if( act!=null )
142
          {
143
          TwistyPreRender pre = act.getTwistyPreRender();
144
          if( pre!=null )
145
            {
146
            reallyBlocked = pre.isTouchBlocked();
147
            pre.unblockTouch();
148
            }
149
          }
150

  
151
        reportTouchProblem(touchBlocked, reallyBlocked);
152
        }
153
      }
154

  
155
    long uiThreshhold = getThreshhold(mLastUIPlace);
156
    long uiBlocked = now-mUIBlockTime;
157

  
158
    if( mUIBlockTime>mPauseTime && uiBlocked>uiThreshhold )
159
      {
160
      boolean running = EffectMessageSender.isRunning();
161

  
162
      if( !running )
163
        {
164
        reportThreadProblem();
165
        EffectMessageSender.restartThread();
166
        }
167
      else
168
        {
169
        TwistyActivity act = mAct.get();
170
        boolean reallyBlocked = true;
171

  
172
        if( act!=null )
173
          {
174
          TwistyPreRender pre = act.getTwistyPreRender();
175
          if( pre!=null )
176
            {
177
            reallyBlocked = !pre.isUINotBlocked();
178
            pre.unblockUI();
179
            }
180
          }
181

  
182
        reportUIProblem(uiBlocked, reallyBlocked);
183
        }
184
      }
185
    }
186

  
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

  
189
  private void reportUIProblem(long time, boolean reallyBlocked)
190
    {
191
    String error = "UI BLOCK "+mLastUIPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
192

  
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
      crashlytics.setCustomKey("pause" , mPauseTime );
202
      crashlytics.setCustomKey("resume", mResumeTime );
203
      crashlytics.recordException(ex);
204
      }
205
    }
206

  
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

  
209
  private void reportTouchProblem(long time, boolean reallyBlocked)
210
    {
211
    String error = "TOUCH BLOCK "+mLastTouchPlace+" blocked for "+time+" milliseconds ("+reallyBlocked+")";
212

  
213
    if( BuildConfig.DEBUG )
214
       {
215
       android.util.Log.e("D", error);
216
       }
217
    else
218
      {
219
      Exception ex = new Exception(error);
220
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
221
      crashlytics.setCustomKey("pause" , mPauseTime );
222
      crashlytics.setCustomKey("resume", mResumeTime );
223
      crashlytics.recordException(ex);
224
      }
225
    }
226

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

  
229
  private void reportThreadProblem()
230
    {
231
    String error = EffectMessageSender.reportState();
232

  
233
    if( BuildConfig.DEBUG )
234
       {
235
       android.util.Log.e("D", error);
236
       }
237
    else
238
      {
239
      Exception ex = new Exception(error);
240
      FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
241
      crashlytics.setCustomKey("pause" , mPauseTime );
242
      crashlytics.setCustomKey("resume", mResumeTime );
243
      crashlytics.recordException(ex);
244
      }
245
    }
246

  
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

  
249
  public void touchBlocked(int place)
250
    {
251
    mTouchBlockTime = System.currentTimeMillis();
252
    mLastTouchPlace = place;
253
    }
254

  
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256

  
257
  public void uiBlocked(int place)
258
    {
259
    mUIBlockTime = System.currentTimeMillis();
260
    mLastUIPlace = place;
261
    }
262

  
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264

  
265
  public void touchUnblocked()
266
    {
267
    mTouchBlockTime = 0;
268
    }
269

  
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

  
272
  public void uiUnblocked()
273
    {
274
    mUIBlockTime = 0;
275
    }
276
  }
src/main/java/org/distorted/helpers/MovesAndLockController.java
29 29

  
30 30
import org.distorted.main.R;
31 31
import org.distorted.main.RubikActivity;
32
import org.distorted.objectlib.helpers.BlockController;
33
import org.distorted.objectlib.helpers.MovesFinished;
34
import org.distorted.objectlib.helpers.TwistyActivity;
35
import org.distorted.objectlib.helpers.TwistyPreRender;
32 36

  
33 37
///////////////////////////////////////////////////////////////////////////////////////////////////
34 38

  
src/main/java/org/distorted/helpers/MovesFinished.java
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
///////////////////////////////////////////////////////////////////////////////////////////////////
23

  
24
public interface MovesFinished
25
  {
26
  void onActionFinished(long effectID);
27
  }
src/main/java/org/distorted/helpers/TwistyActivity.java
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 androidx.appcompat.app.AppCompatActivity;
23

  
24
import org.distorted.screens.ScreenList;
25

  
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

  
28
abstract public class TwistyActivity extends AppCompatActivity
29
  {
30
  boolean mIsLocked, mRemLocked;
31

  
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

  
34
  public abstract TwistyPreRender getTwistyPreRender();
35

  
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

  
38
  public boolean retLocked()
39
      {
40
      return mIsLocked;
41
      }
42

  
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

  
45
  public void toggleLock()
46
      {
47
      mIsLocked = !mIsLocked;
48
      }
49

  
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

  
52
  public void unlock()
53
    {
54
    mIsLocked = false;
55
    }
56

  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

  
59
  public boolean isLocked()
60
    {
61
    ScreenList state = ScreenList.getCurrentScreen();
62

  
63
    if( state== ScreenList.PLAY || state== ScreenList.READ || state== ScreenList.SOLV )
64
      {
65
      return mIsLocked;
66
      }
67

  
68
    return false;
69
    }
70

  
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

  
73
  public void setLock()
74
    {
75
    mRemLocked = mIsLocked;
76
    mIsLocked = true;
77
    }
78

  
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

  
81
  public void unsetLock()
82
    {
83
    mIsLocked = mRemLocked;
84
    }
85
  }
src/main/java/org/distorted/helpers/TwistyPreRender.java
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
///////////////////////////////////////////////////////////////////////////////////////////////////
23

  
24
public interface TwistyPreRender
25
  {
26
  boolean isTouchBlocked();
27
  boolean isUINotBlocked();
28
  void blockTouch(int place);
29
  void unblockTouch();
30
  void blockEverything(int place);
31
  void unblockEverything();
32
  void unblockUI();
33
  void addRotation(MovesFinished listener, int axis, int rowBitmap, int angle, long duration);
34
  void solveObject();
35
  }
src/main/java/org/distorted/main/RubikActivity.java
41 41

  
42 42
import org.distorted.objectlib.main.TwistyObject;
43 43
import org.distorted.objectlib.main.ObjectType;
44
import org.distorted.objectlib.helpers.BlockController;
45
import org.distorted.objectlib.helpers.TwistyPreRender;
44 46

  
45 47
import org.distorted.dialogs.RubikDialogError;
46 48
import org.distorted.dialogs.RubikDialogPrivacy;
47 49
import org.distorted.effects.BaseEffect;
48
import org.distorted.helpers.BlockController;
49
import org.distorted.helpers.TwistyActivity;
50
import org.distorted.helpers.TwistyPreRender;
50
import org.distorted.objectlib.helpers.TwistyActivity;
51 51
import org.distorted.network.RubikScores;
52 52
import org.distorted.network.RubikNetwork;
53 53
import org.distorted.screens.ScreenList;
......
499 499
      pre.changeObject(newObject);
500 500
      }
501 501

  
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503

  
504
  public boolean isLocked()
505
    {
506
    ScreenList state = ScreenList.getCurrentScreen();
507

  
508
    if( state== ScreenList.PLAY || state== ScreenList.READ || state== ScreenList.SOLV )
509
      {
510
      return retLocked();
511
      }
512

  
513
    return false;
514
    }
515

  
502 516
///////////////////////////////////////////////////////////////////////////////////////////////////
503 517

  
504 518
    public void setupObject(ObjectType object, int[][] moves)
src/main/java/org/distorted/main/RubikPreRender.java
42 42
import org.distorted.effects.BaseEffect;
43 43
import org.distorted.effects.EffectController;
44 44
import org.distorted.effects.scramble.ScrambleEffect;
45
import org.distorted.helpers.BlockController;
46
import org.distorted.helpers.MovesFinished;
47
import org.distorted.helpers.TwistyPreRender;
45
import org.distorted.objectlib.helpers.BlockController;
46
import org.distorted.objectlib.helpers.MovesFinished;
47
import org.distorted.objectlib.helpers.TwistyPreRender;
48 48
import org.distorted.network.RubikScores;
49 49
import org.distorted.screens.RubikScreenPlay;
50 50
import org.distorted.screens.ScreenList;
......
459 459

  
460 460
  void setScreenSize(int width)
461 461
    {
462
    if( mNewObject!=null ) mNewObject.recomputeScaleFactor(width);
462
    if( mNewObject!=null )
463
      {
464
      mNewObject.createTexture();
465
      mNewObject.recomputeScaleFactor(width);
466
      }
463 467
    mScreenWidth = width;
464 468
    }
465 469

  
src/main/java/org/distorted/patterns/RubikPattern.java
22 22
import java.util.ArrayList;
23 23
import java.util.List;
24 24

  
25
import org.distorted.helpers.MovesFinished;
25
import org.distorted.objectlib.helpers.MovesFinished;
26 26
import org.distorted.main.RubikPreRender;
27 27

  
28 28
import static org.distorted.patterns.RubikPatternList.NUM_OBJECTS;
src/main/java/org/distorted/screens/RubikScreenBase.java
23 23
import android.widget.LinearLayout;
24 24

  
25 25
import org.distorted.helpers.MovesAndLockController;
26
import org.distorted.helpers.TwistyActivity;
26
import org.distorted.objectlib.helpers.TwistyActivity;
27 27
import org.distorted.main.R;
28 28
import org.distorted.main.RubikActivity;
29 29

  
src/main/java/org/distorted/screens/RubikScreenSolution.java
30 30

  
31 31
import org.distorted.objectlib.main.TwistyObject;
32 32

  
33
import org.distorted.helpers.MovesFinished;
33
import org.distorted.objectlib.helpers.MovesFinished;
34 34
import org.distorted.helpers.TransparentImageButton;
35 35
import org.distorted.main.R;
36 36
import org.distorted.main.RubikActivity;
src/main/java/org/distorted/tutorials/TutorialActivity.java
36 36
import org.distorted.objectlib.main.TwistyObject;
37 37

  
38 38
import org.distorted.dialogs.RubikDialogError;
39
import org.distorted.helpers.BlockController;
40
import org.distorted.helpers.TwistyActivity;
41
import org.distorted.helpers.TwistyPreRender;
39
import org.distorted.objectlib.helpers.BlockController;
40
import org.distorted.objectlib.helpers.TwistyActivity;
41
import org.distorted.objectlib.helpers.TwistyPreRender;
42 42
import org.distorted.main.R;
43
import org.distorted.screens.ScreenList;
43 44

  
44 45
import static org.distorted.main.RubikRenderer.BRIGHTNESS;
45 46

  
......
286 287
      return view.getPreRender();
287 288
      }
288 289

  
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291

  
292
  public boolean isLocked()
293
    {
294
    return retLocked();
295
    }
296

  
289 297
///////////////////////////////////////////////////////////////////////////////////////////////////
290 298

  
291 299
    public static int getDrawableSize()
src/main/java/org/distorted/tutorials/TutorialPreRender.java
24 24

  
25 25
import org.distorted.objectlib.main.ObjectType;
26 26
import org.distorted.objectlib.main.TwistyObject;
27
import org.distorted.objectlib.helpers.BlockController;
28
import org.distorted.objectlib.helpers.MovesFinished;
29
import org.distorted.objectlib.helpers.TwistyPreRender;
27 30

  
28 31
import org.distorted.effects.BaseEffect;
29 32
import org.distorted.effects.EffectController;
30
import org.distorted.helpers.BlockController;
31
import org.distorted.helpers.MovesFinished;
32
import org.distorted.helpers.TwistyPreRender;
33 33

  
34 34
///////////////////////////////////////////////////////////////////////////////////////////////////
35 35

  
......
262 262

  
263 263
  void setScreenSize(int width)
264 264
    {
265
    if( mNewObject!=null ) mNewObject.recomputeScaleFactor(width);
265
    if( mNewObject!=null )
266
      {
267
      mNewObject.createTexture();
268
      mNewObject.recomputeScaleFactor(width);
269
      }
270

  
266 271
    mScreenWidth = width;
267 272
    }
268 273

  
src/main/java/org/distorted/tutorials/TutorialState.java
26 26
import org.distorted.objectlib.main.ObjectType;
27 27

  
28 28
import org.distorted.helpers.MovesAndLockController;
29
import org.distorted.helpers.TwistyActivity;
30
import org.distorted.helpers.TwistyPreRender;
29
import org.distorted.objectlib.helpers.TwistyActivity;
30
import org.distorted.objectlib.helpers.TwistyPreRender;
31 31
import org.distorted.main.R;
32 32
import org.distorted.main.RubikActivity;
33 33
import org.distorted.screens.RubikScreenPlay;

Also available in: Unified diff