Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolving.java @ 0333d81e

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.uistate;
21

    
22
import android.content.SharedPreferences;
23
import android.util.DisplayMetrics;
24
import android.view.LayoutInflater;
25
import android.widget.Button;
26
import android.widget.LinearLayout;
27
import android.widget.TextView;
28

    
29
import org.distorted.magic.R;
30
import org.distorted.magic.RubikActivity;
31

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

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

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

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

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

    
48
  public void leaveState(RubikActivity act)
49
    {
50
    stopCounting();
51
    }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  public void enterState(RubikActivity act)
56
    {
57
    LayoutInflater inflater = act.getLayoutInflater();
58

    
59
    // TOP ////////////////////////////
60
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
61
    layoutTop.removeAllViews();
62
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
63
    mTime.setText(R.string.ready);
64
    layoutTop.addView(mTime);
65

    
66
    // BOT ////////////////////////////
67
    LinearLayout layoutBot = act.findViewById(R.id.mainBar);
68
    layoutBot.removeAllViews();
69

    
70
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
71
    float scale = metrics.density;
72
    int size = (int)(60*scale +0.5f);
73
    int padding = (int)(5*scale + 0.5f);
74
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,size,0.5f);
75

    
76
    Button buttonL = new Button(act);
77
    buttonL.setLayoutParams(params);
78
    buttonL.setId(BUTTON_ID_BACK);
79
    buttonL.setPadding(padding,0,padding,0);
80
    buttonL.setText(R.string.back);
81
    buttonL.setOnClickListener(act);
82
    layoutBot.addView(buttonL);
83

    
84
    Button buttonR = new Button(act);
85
    buttonR.setLayoutParams(params);
86
    buttonR.setId(BUTTON_ID_BACK);
87
    buttonR.setPadding(padding,0,padding,0);
88
    buttonR.setText(R.string.back);
89
    buttonR.setOnClickListener(act);
90
    layoutBot.addView(buttonR);
91

    
92
    buttonL.setVisibility(INVISIBLE);
93
    }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  public void savePreferences(SharedPreferences.Editor editor)
98
    {
99

    
100
    }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  public void restorePreferences(SharedPreferences preferences)
105
    {
106

    
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
    }
156
  }
(5-5/5)