Project

General

Profile

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

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

1 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 f0e87514 Leszek Koltunski
import org.distorted.scores.RubikScores;
32 ad9e8bb3 Leszek Koltunski
33 0333d81e Leszek Koltunski
import java.util.Timer;
34
import java.util.TimerTask;
35
36 ad9e8bb3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
37
38
public class RubikStateSolving extends RubikStateAbstract
39
  {
40 0333d81e Leszek Koltunski
  private TextView mTime;
41
  private Timer mTimer;
42
  private long mStartTime;
43
  private boolean mRunning;
44 f0e87514 Leszek Koltunski
  private RubikScores mScores;
45 e41e7dc3 Leszek Koltunski
  private Button mBack;
46 329c0aeb Leszek Koltunski
47 0333d81e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 329c0aeb Leszek Koltunski
  RubikStateSolving()
50
    {
51 714292f1 Leszek Koltunski
    mScores = RubikScores.getInstance();
52 329c0aeb Leszek Koltunski
    }
53
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56
  void leaveState(RubikActivity act)
57 ad9e8bb3 Leszek Koltunski
    {
58 e41e7dc3 Leszek Koltunski
    stopCounting(act);
59 ad9e8bb3 Leszek Koltunski
    }
60
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 d18993ac Leszek Koltunski
  void enterState(RubikActivity act, RubikState prev)
64 ad9e8bb3 Leszek Koltunski
    {
65
    LayoutInflater inflater = act.getLayoutInflater();
66
67
    // TOP ////////////////////////////
68
    LinearLayout layoutTop = act.findViewById(R.id.mainTitle);
69
    layoutTop.removeAllViews();
70 0333d81e Leszek Koltunski
    mTime = (TextView)inflater.inflate(R.layout.upper_text, null);
71
    mTime.setText(R.string.ready);
72
    layoutTop.addView(mTime);
73 ad9e8bb3 Leszek Koltunski
74
    // BOT ////////////////////////////
75 4c0cd600 Leszek Koltunski
    LinearLayout layoutLeft = act.findViewById(R.id.mainBarLeft);
76
    layoutLeft.removeAllViews();
77
    LinearLayout layoutRight = act.findViewById(R.id.mainBarRight);
78
    layoutRight.removeAllViews();
79 ad9e8bb3 Leszek Koltunski
80
    DisplayMetrics metrics = act.getResources().getDisplayMetrics();
81
    float scale = metrics.density;
82
    int padding = (int)(5*scale + 0.5f);
83 4c0cd600 Leszek Koltunski
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
84 ad9e8bb3 Leszek Koltunski
85 e41e7dc3 Leszek Koltunski
    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 4c0cd600 Leszek Koltunski
    layoutRight.addView(mBack);
92 ad9e8bb3 Leszek Koltunski
    }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96
  public void savePreferences(SharedPreferences.Editor editor)
97
    {
98 f0e87514 Leszek Koltunski
    mScores.savePreferences(editor);
99 ad9e8bb3 Leszek Koltunski
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103
  public void restorePreferences(SharedPreferences preferences)
104
    {
105 f0e87514 Leszek Koltunski
    mScores.restorePreferences(preferences);
106 ad9e8bb3 Leszek Koltunski
    }
107 0333d81e Leszek Koltunski
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 e41e7dc3 Leszek Koltunski
  public long stopCounting(RubikActivity act)
140 0333d81e Leszek Koltunski
    {
141
    if( mRunning )
142
      {
143 e41e7dc3 Leszek Koltunski
      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 0333d81e Leszek Koltunski
      if( mTimer!=null )
154
        {
155
        mTimer.cancel();
156
        mTimer = null;
157
        }
158
      mRunning = false;
159
160 329c0aeb Leszek Koltunski
      long timeTaken = System.currentTimeMillis()-mStartTime;
161
162
      RubikStatePlay play = (RubikStatePlay)RubikState.PLAY.getStateClass();
163 4888e97c Leszek Koltunski
      int object  = play.getObject();
164
      int size    = play.getSize();
165 329c0aeb Leszek Koltunski
      int scramble= play.getPicker();
166
167 e41e7dc3 Leszek Koltunski
      boolean isNew = mScores.setRecord(object, size, scramble, timeTaken);
168
169
      return isNew ? timeTaken : -timeTaken;
170 0333d81e Leszek Koltunski
      }
171
172
    return 0;
173
    }
174 ad9e8bb3 Leszek Koltunski
  }