Project

General

Profile

« Previous | Next » 

Revision 0333d81e

Added by Leszek Koltunski about 4 years ago

Progress with StateSolving

View differences:

src/main/java/org/distorted/uistate/RubikStateSolving.java
29 29
import org.distorted.magic.R;
30 30
import org.distorted.magic.RubikActivity;
31 31

  
32
import java.util.Timer;
33
import java.util.TimerTask;
34

  
32 35
import static android.view.View.INVISIBLE;
33 36

  
34 37
///////////////////////////////////////////////////////////////////////////////////////////////////
35 38

  
36 39
public class RubikStateSolving extends RubikStateAbstract
37 40
  {
41
  private TextView mTime;
42
  private Timer mTimer;
43
  private long mStartTime;
44
  private boolean mRunning;
45

  
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

  
38 48
  public void leaveState(RubikActivity act)
39 49
    {
40

  
50
    stopCounting();
41 51
    }
42 52

  
43 53
///////////////////////////////////////////////////////////////////////////////////////////////////
......
49 59
    // TOP ////////////////////////////
50 60
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
51 61
    layoutTop.removeAllViews();
52
    final TextView text = (TextView)inflater.inflate(R.layout.upper_text, null);
53
    text.setText(R.string.ready);
54
    layoutTop.addView(text);
62
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
63
    mTime.setText(R.string.ready);
64
    layoutTop.addView(mTime);
55 65

  
56 66
    // BOT ////////////////////////////
57 67
    LinearLayout layoutBot = act.findViewById(R.id.mainBar);
......
95 105
    {
96 106

  
97 107
    }
108

  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

  
111
  public void startCounting(final RubikActivity act)
112
    {
113
    if( !mRunning )
114
      {
115
      mRunning = true;
116
      mStartTime = System.currentTimeMillis();
117
      mTimer = new Timer();
118

  
119
      mTimer.scheduleAtFixedRate(new TimerTask()
120
        {
121
        @Override
122
        public void run()
123
          {
124
          act.runOnUiThread(new Runnable()
125
            {
126
            @Override
127
            public void run()
128
              {
129
              int elapsed = (int)(System.currentTimeMillis()-mStartTime)/1000;
130
              mTime.setText(act.getString(R.string.tm_placeholder,elapsed/60,elapsed%60));
131
              }
132
            });
133
          }
134
        }, 0, 1000);
135
      }
136
    }
137

  
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

  
140
  public long stopCounting()
141
    {
142
    if( mRunning )
143
      {
144
      if( mTimer!=null )
145
        {
146
        mTimer.cancel();
147
        mTimer = null;
148
        }
149
      mRunning = false;
150

  
151
      return System.currentTimeMillis()-mStartTime;
152
      }
153

  
154
    return 0;
155
    }
98 156
  }

Also available in: Unified diff