Project

General

Profile

Download (6.63 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / helpers / BlockController.java @ 45e0065d

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

    
22
import java.util.Timer;
23
import java.util.TimerTask;
24

    
25
import android.app.Activity;
26

    
27
import org.distorted.library.message.EffectMessageSender;
28
import org.distorted.objectlib.main.ObjectPreRender;
29

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

    
32
public class BlockController
33
  {
34
  public static final int TYPE_UI     = 0;
35
  public static final int TYPE_TOUCH  = 1;
36
  public static final int TYPE_THREAD = 2;
37

    
38
  public static final int PLACE_0 =0;
39
  public static final int PLACE_1 =1;
40
  public static final int PLACE_2 =2;
41
  public static final int PLACE_3 =3;
42
  public static final int PLACE_4 =4;
43
  public static final int CONTROL_PLACE_0 =20;
44
  public static final int CONTROL_PLACE_1 =21;
45

    
46
  private static final long THRESHHOLD_0 =  6000;
47
  private static final long THRESHHOLD_1 = 50000;
48
  private static final long THRESHHOLD_2 = 10000;
49
  private static final long THRESHHOLD_3 = 90000;
50

    
51
  private static long mPauseTime, mResumeTime;
52

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

    
56
  private final ObjectPreRender mPre;
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(Activity act, ObjectPreRender pre)
75
    {
76
    mPre = pre;
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
// RUBIK_PLACE_4 and TUTORIAL_PLACE_4 are solves, those can (maybe) sometimes take more than 3 seconds.
100
// 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 PLACE_3         : return THRESHHOLD_1;
107
      case PLACE_4         : return THRESHHOLD_2;
108
      case CONTROL_PLACE_0 :
109
      case CONTROL_PLACE_1 : return THRESHHOLD_3;
110
      default              : return THRESHHOLD_0;
111
      }
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  private void checkingThread()
117
    {
118
    long now = System.currentTimeMillis();
119
    long touchThreshhold = getThreshhold(mLastTouchPlace);
120
    long touchBlocked = now-mTouchBlockTime;
121

    
122
    if( mTouchBlockTime>mPauseTime && touchBlocked>touchThreshhold )
123
      {
124
      boolean running = EffectMessageSender.isRunning();
125
      ObjectLibInterface libInterface = mPre.getInterface();
126

    
127
      if( !running )
128
        {
129
        libInterface.reportBlockProblem(TYPE_THREAD,0,mPauseTime,mResumeTime,0);
130
        EffectMessageSender.restartThread();
131
        }
132
      else
133
        {
134
        mPre.unblockTouch();
135
        libInterface.reportBlockProblem(TYPE_TOUCH,mLastTouchPlace,mPauseTime,mResumeTime,mTouchBlockTime);
136
        }
137
      }
138

    
139
    long uiThreshhold = getThreshhold(mLastUIPlace);
140
    long uiBlocked = now-mUIBlockTime;
141

    
142
    if( mUIBlockTime>mPauseTime && uiBlocked>uiThreshhold )
143
      {
144
      boolean running = EffectMessageSender.isRunning();
145
      ObjectLibInterface libInterface = mPre.getInterface();
146

    
147
      if( !running )
148
        {
149
        libInterface.reportBlockProblem(TYPE_THREAD,0,mPauseTime,mResumeTime,0);
150
        EffectMessageSender.restartThread();
151
        }
152
      else
153
        {
154
        mPre.unblockUI();
155
        libInterface.reportBlockProblem(TYPE_UI,mLastUIPlace,mPauseTime,mResumeTime,mUIBlockTime);
156
        }
157
      }
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  public void touchBlocked(int place)
163
    {
164
    mTouchBlockTime = System.currentTimeMillis();
165
    mLastTouchPlace = place;
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169

    
170
  public void uiBlocked(int place)
171
    {
172
    mUIBlockTime = System.currentTimeMillis();
173
    mLastUIPlace = place;
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  public void touchUnblocked()
179
    {
180
    mTouchBlockTime = 0;
181
    }
182

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

    
185
  public void uiUnblocked()
186
    {
187
    mUIBlockTime = 0;
188
    }
189
  }
(1-1/10)