Project

General

Profile

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

magiccube / src / main / java / org / distorted / uistate / RubikStateSolving.java @ d18993ac

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
import org.distorted.scores.RubikScores;
32

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

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

    
38
public class RubikStateSolving extends RubikStateAbstract
39
  {
40
  private TextView mTime;
41
  private Timer mTimer;
42
  private long mStartTime;
43
  private boolean mRunning;
44
  private RubikScores mScores;
45
  private Button mBack;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  RubikStateSolving()
50
    {
51
    mScores = RubikScores.getInstance();
52
    }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  void leaveState(RubikActivity act)
57
    {
58
    stopCounting(act);
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  void enterState(RubikActivity act, RubikState prev)
64
    {
65
    LayoutInflater inflater = act.getLayoutInflater();
66

    
67
    // TOP ////////////////////////////
68
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
69
    layoutTop.removeAllViews();
70
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
71
    mTime.setText(R.string.ready);
72
    layoutTop.addView(mTime);
73

    
74
    // BOT ////////////////////////////
75
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
76
    layoutLeft.removeAllViews();
77
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
78
    layoutRight.removeAllViews();
79

    
80
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
81
    float scale = metrics.density;
82
    int padding = (int)(5*scale + 0.5f);
83
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
84

    
85
    mBack = new Button(act);
86
    mBack.setLayoutParams(params);
87
    mBack.setId(BUTTON_ID_BACK);
88
    mBack.setPadding(padding,0,padding,0);
89
    mBack.setText(R.string.back);
90
    mBack.setOnClickListener(act);
91
    layoutRight.addView(mBack);
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  public void savePreferences(SharedPreferences.Editor editor)
97
    {
98
    mScores.savePreferences(editor);
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  public void restorePreferences(SharedPreferences preferences)
104
    {
105
    mScores.restorePreferences(preferences);
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

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

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

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  public long stopCounting(RubikActivity act)
140
    {
141
    if( mRunning )
142
      {
143
      act.runOnUiThread(new Runnable()
144
        {
145
        @Override
146
        public void run()
147
          {
148
          mTime.setText(R.string.solved);
149
          mBack.setClickable(false);
150
          }
151
        });
152

    
153
      if( mTimer!=null )
154
        {
155
        mTimer.cancel();
156
        mTimer = null;
157
        }
158
      mRunning = false;
159

    
160
      long timeTaken = System.currentTimeMillis()-mStartTime;
161

    
162
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
163
      int object  = play.getObject();
164
      int size    = play.getSize();
165
      int scramble= play.getPicker();
166

    
167
      boolean isNew = mScores.setRecord(object, size, scramble, timeTaken);
168

    
169
      return isNew ? timeTaken : -timeTaken;
170
      }
171

    
172
    return 0;
173
    }
174
  }
(5-5/5)