Project

General

Profile

« Previous | Next » 

Revision 8838ac87

Added by Leszek Koltunski about 14 hours ago

Progress remembering solves.

View differences:

src/main/java/org/distorted/helpers/RubikRememberedSolves.java
39 39

  
40 40
    }
41 41

  
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
// PUBLIC API
44 42
///////////////////////////////////////////////////////////////////////////////////////////////////
45 43

  
46
  public static RubikRememberedSolves getInstance()
44
  private String readContents(InputStream stream) throws IOException
47 45
    {
48
    if( mThis==null ) mThis = new RubikRememberedSolves();
49
    return mThis;
46
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
47
    StringBuilder contents = new StringBuilder();
48
    String tmp;
49

  
50
    while( (tmp = br.readLine()) != null) contents.append(tmp);
51
    br.close();
52
    stream.close();
53

  
54
    return contents.toString();
50 55
    }
51 56

  
52 57
///////////////////////////////////////////////////////////////////////////////////////////////////
53 58

  
54
  private JSONObject createData(long time, Static4D rot, int[] quats) throws JSONException
59
  private JSONObject createData(int elapsed, Static4D rot, int[] quats) throws JSONException
55 60
    {
56 61
    JSONObject data = new JSONObject();
57
    data.put("time",time);
62
    data.put("time",System.currentTimeMillis());
63
    data.put("elapsed", elapsed);
58 64
    data.put("rot0", rot.get0() );
59 65
    data.put("rot1", rot.get1() );
60 66
    data.put("rot2", rot.get2() );
......
66 72
    return data;
67 73
    }
68 74

  
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

  
77
  private String deleteInfo(InputStream stream, int level, long time)
78
    {
79
    try
80
      {
81
      String contents = readContents(stream);
82
      JSONArray levels = new JSONArray(contents);
83
      JSONArray lvl = levels.getJSONArray(level);
84
      int numSolves = lvl.length();
85

  
86
      for(int s=0; s<numSolves; s++)
87
        {
88
        JSONObject obj = lvl.getJSONObject(s);
89
        long tm = obj.getLong("time");
90
        if( tm==time ) { lvl.remove(s); break; }
91
        }
92

  
93
      return levels.toString();
94
      }
95
    catch(IOException iex)    { android.util.Log.e("D", "addInfo: failed to read file: "+iex.getMessage() ); }
96
    catch(JSONException jex)  { android.util.Log.e("D", "addInfo: failed to parse file: "+jex.getMessage()); }
97

  
98
    return null;
99
    }
100

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

  
103
  private String addInfo(InputStream stream, int level, int elapsed, Static4D rot, int[] quats)
104
    {
105
    if( stream!=null )
106
      {
107
      try
108
        {
109
        String contents = readContents(stream);
110
        JSONArray levels = new JSONArray(contents);
111
        JSONArray lvl = levels.getJSONArray(level);
112
        JSONObject data = createData(elapsed,rot,quats);
113
        if( lvl.length()>=MAXSOLVES ) lvl.remove(0);
114
        lvl.put(data);
115
        return levels.toString();
116
        }
117
      catch(IOException iex)    { android.util.Log.e("D", "addInfo: failed to read file: "+iex.getMessage() ); }
118
      catch(JSONException jex)  { android.util.Log.e("D", "addInfo: failed to parse file: "+jex.getMessage()); }
119
      }
120
    else
121
      {
122
      JSONArray levels = new JSONArray();
123

  
124
      try
125
        {
126
        for(int l=0; l< MainObjectPopup.LEVELS_SHOWN+1; l++)
127
          {
128
          JSONArray save = new JSONArray();
129

  
130
          if( l==level )
131
            {
132
            JSONObject data = createData(elapsed,rot,quats);
133
            save.put(data);
134
            }
135
          levels.put(save);
136
          }
137
        }
138
      catch(JSONException jex)  { android.util.Log.e("D", "addInfo: failed to parse file: "+jex.getMessage()); }
139

  
140
      return levels.toString();
141
      }
142

  
143
    return null;
144
    }
145

  
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
// PUBLIC API
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

  
150
  public static RubikRememberedSolves getInstance()
151
    {
152
    if( mThis==null ) mThis = new RubikRememberedSolves();
153
    return mThis;
154
    }
155

  
69 156
///////////////////////////////////////////////////////////////////////////////////////////////////
70 157

  
71 158
  public long getTime(JSONObject object) throws JSONException
......
73 160
    return object.getLong("time");
74 161
    }
75 162

  
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

  
165
  public int getElapsed(JSONObject object) throws JSONException
166
    {
167
    return object.getInt("elapsed");
168
    }
169

  
76 170
///////////////////////////////////////////////////////////////////////////////////////////////////
77 171

  
78 172
  public Static4D getRotQuat(JSONObject object) throws JSONException
......
116 210
    return null;
117 211
    }
118 212

  
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

  
121
  private String readContents(InputStream stream) throws IOException
122
    {
123
    BufferedReader br = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8));
124
    StringBuilder contents = new StringBuilder();
125
    String tmp;
126

  
127
    while( (tmp = br.readLine()) != null) contents.append(tmp);
128
    br.close();
129
    stream.close();
130

  
131
    return contents.toString();
132
    }
133

  
134 213
///////////////////////////////////////////////////////////////////////////////////////////////////
135 214

  
136 215
  public void deleteSolve(Activity act, String objname, int level, long time)
......
161 240

  
162 241
///////////////////////////////////////////////////////////////////////////////////////////////////
163 242

  
164
  public void rememberSolve(Activity act, String objname, int level, long time, Static4D rot, int[] quats)
243
  public void rememberSolve(Activity act, String objname, int level, int elapsed, Static4D rot, int[] quats)
165 244
    {
166 245
    String filename  = objname.toLowerCase()+"_solves.json";
167 246
    RubikFiles files = RubikFiles.getInstance();
168 247
    InputStream input= files.openFile(act,filename);
169
    String contents  = addInfo(input,level,time,rot,quats);
248
    String contents  = addInfo(input,level,elapsed,rot,quats);
170 249
    File file        = new File(act.getFilesDir(), filename);
171 250

  
172 251
    try( FileOutputStream fos = new FileOutputStream(file) )
......
178 257
      android.util.Log.e("D", "rememberSolve: failed to save file "+filename+" : "+ex.getMessage());
179 258
      }
180 259
    }
181

  
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

  
184
  private void debug(int level, long time, Static4D rot, int[] quats)
185
    {
186
    StringBuilder quatStr = new StringBuilder();
187

  
188
    for(int c : quats)
189
      {
190
      quatStr.append(c);
191
      quatStr.append(" ");
192
      }
193

  
194
    android.util.Log.e("D",   "level: "+level+"\n"+
195
                                    "time: "+time+"\n"+
196
                                    "rotQuat: " +rot.get0()+" "+rot.get1()+" "+rot.get2()+" "+rot.get3()+"\n"+
197
                                    "quats: "+quatStr );
198
    }
199

  
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

  
202
  public String deleteInfo(InputStream stream, int level, long time)
203
    {
204
    try
205
      {
206
      String contents = readContents(stream);
207
      JSONArray levels = new JSONArray(contents);
208
      JSONArray lvl = levels.getJSONArray(level);
209
      int numSolves = lvl.length();
210

  
211
      for(int s=0; s<numSolves; s++)
212
        {
213
        JSONObject obj = lvl.getJSONObject(s);
214
        long tm = obj.getLong("time");
215
        if( tm==time ) { lvl.remove(s); break; }
216
        }
217

  
218
      return levels.toString();
219
      }
220
    catch(IOException iex)    { android.util.Log.e("D", "addInfo: failed to read file: "+iex.getMessage() ); }
221
    catch(JSONException jex)  { android.util.Log.e("D", "addInfo: failed to parse file: "+jex.getMessage()); }
222

  
223
    return null;
224
    }
225

  
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

  
228
  public String addInfo(InputStream stream, int level, long time, Static4D rot, int[] quats)
229
    {
230
    if( stream!=null )
231
      {
232
      try
233
        {
234
        String contents = readContents(stream);
235
        JSONArray levels = new JSONArray(contents);
236
        JSONArray lvl = levels.getJSONArray(level);
237
        JSONObject data = createData(time,rot,quats);
238
        if( lvl.length()>=MAXSOLVES ) lvl.remove(0);
239
        lvl.put(data);
240
        return levels.toString();
241
        }
242
      catch(IOException iex)    { android.util.Log.e("D", "addInfo: failed to read file: "+iex.getMessage() ); }
243
      catch(JSONException jex)  { android.util.Log.e("D", "addInfo: failed to parse file: "+jex.getMessage()); }
244
      }
245
    else
246
      {
247
      JSONArray levels = new JSONArray();
248

  
249
      try
250
        {
251
        for(int l=0; l< MainObjectPopup.LEVELS_SHOWN+1; l++)
252
          {
253
          JSONArray save = new JSONArray();
254

  
255
          if( l==level )
256
            {
257
            JSONObject data = createData(time,rot,quats);
258
            save.put(data);
259
            }
260
          levels.put(save);
261
          }
262
        }
263
      catch(JSONException jex)  { android.util.Log.e("D", "addInfo: failed to parse file: "+jex.getMessage()); }
264

  
265
      return levels.toString();
266
      }
267

  
268
    return null;
269
    }
270 260
}
src/main/java/org/distorted/main/MainActivity.java
338 338

  
339 339
///////////////////////////////////////////////////////////////////////////////////////////////////
340 340

  
341
    public void switchToPlay(RubikObject object, int ordinal, int scrambles, int level, long time, Static4D rot, int[] quats)
341
    public void switchToPlay(RubikObject object, int ordinal, int scrambles, int level, int elapsed, Static4D rot, int[] quats)
342 342
      {
343 343
      boolean local = object.isLocal();
344 344
      String name = local ? object.getLowerName() : object.getUpperName();
......
349 349
      intent.putExtra("scrambles", scrambles);
350 350
      intent.putExtra("local", local );
351 351
      intent.putExtra("ordinal", ordinal );
352
      intent.putExtra("time", time);
352
      intent.putExtra("elapsed", elapsed);
353 353

  
354 354
      if( rot!=null )
355 355
        {
src/main/java/org/distorted/main/MainSolvesPopup.java
88 88
        {
89 89
        JSONObject object = array.getJSONObject(s);
90 90
        long time = solves.getTime(object);
91
        int elapsed = solves.getElapsed(object);
91 92
        Static4D rotQuat = solves.getRotQuat(object);
92 93
        int[] quats = solves.getQuats(object);
93
        View pane = createOldPane(act, s, time, rotQuat, quats, pL, pT);
94
        View pane = createOldPane(act, s, time, elapsed, rotQuat, quats, pL, pT);
94 95
        mLayout.addView(pane);
95 96
        }
96 97
      }
......
121 122

  
122 123
///////////////////////////////////////////////////////////////////////////////////////////////////
123 124

  
124
  private View createOldPane(MainActivity act, int index, long time, Static4D rot, int[] quats, LinearLayout.LayoutParams pView, LinearLayout.LayoutParams pText)
125
  private View createOldPane(MainActivity act, int index, long time, int elapsed, Static4D rot,
126
                             int[] quats, LinearLayout.LayoutParams pView, LinearLayout.LayoutParams pText)
125 127
    {
126 128
    View view = act.getLayoutInflater().inflate(R.layout.dialog_solve_old_pane, null);
127 129
    TextView title = view.findViewById(R.id.solves_pane_title);
......
166 168
        public void onClick(View v)
167 169
          {
168 170
          mPopup.dismiss();
169
          act.switchToPlay(mObject,mObjectOrdinal,mNumScrambles,mLevel, time, rot, quats);
171
          act.switchToPlay(mObject,mObjectOrdinal,mNumScrambles,mLevel, elapsed, rot, quats);
170 172
          }
171 173
        });
172 174

  
src/main/java/org/distorted/play/PlayActivity.java
53 53

  
54 54
    private String mObjectName;
55 55
    private int mNumScrambles;
56
    private long mTime;
56
    private int mElapsed;
57 57
    private Static4D mRotQuat;
58 58
    private int[] mQuats;
59
    private boolean mObjectLocal;
59
    private boolean mObjectLocal, mModeResume;
60 60
    private int mObjectOrdinal;
61 61
    private int mLevel;
62 62
    private boolean mModeFree;
......
84 84
        mNumScrambles  = b.getInt("scrambles");
85 85
        mObjectLocal   = b.getBoolean("local");
86 86
        mObjectOrdinal = b.getInt("ordinal");
87
        mTime          = b.getLong("time");
87
        mElapsed       = b.getInt("elapsed");
88 88

  
89
        if( mTime>0 )
89
        if( mElapsed>0 )
90 90
          {
91 91
          float r0 = b.getFloat("rot0");
92 92
          float r1 = b.getFloat("rot1");
93 93
          float r2 = b.getFloat("rot2");
94 94
          float r3 = b.getFloat("rot3");
95 95
          mRotQuat = new Static4D(r0,r1,r2,r3);
96
          mQuats = b.getIntArray("quqts");
96
          mQuats = b.getIntArray("quats");
97 97
          }
98 98
        }
99 99
      else
......
103 103
        mNumScrambles = 0;
104 104
        mObjectLocal = true;
105 105
        mObjectOrdinal = 0;
106
        mTime = 0;
106
        mElapsed = 0;
107 107
        mRotQuat = null;
108 108
        mQuats = null;
109 109
        }
110 110

  
111 111
      mModeFree = (mLevel<0);
112
      mModeResume = (mElapsed>0);
112 113

  
113 114
      computeScreenDimensions();
114 115
      hideNavigationBar();
......
163 164
      SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
164 165
      restorePreferences(preferences);
165 166

  
166
      ScreenList sl =  mJustStarted ?
167
                      (mModeFree ? ScreenList.FREE : ScreenList.SCRA) :
168
                      ScreenList.getCurrentScreen();
169

  
170
      ScreenList.switchScreen(this,sl);
167
      ScreenList sl = mJustStarted ? (mModeFree ? ScreenList.FREE : ScreenList.SCRA) : ScreenList.getCurrentScreen();
168
      ScreenList.switchScreen(this, sl);
171 169

  
172 170
      if( !mJustStarted ) restoreMoves(preferences);
173 171

  
174 172
      if( !mObjectName.isEmpty() )
175 173
        {
176
        changeIfDifferent(mObjectName,mObjectLocal,mObjectOrdinal,control);
174
        changeIfDifferent(mObjectName, mObjectLocal, mObjectOrdinal, control);
177 175
        }
178 176

  
179
      if( mJustStarted && !mModeFree )
177
      if( !mModeResume )
178
        {
179
        if( mJustStarted && !mModeFree )
180
          {
181
          control.scrambleObject(mNumScrambles);
182
          }
183
        }
184
      else
180 185
        {
181
        control.scrambleObject(mNumScrambles);
186
        control.rotateNow(mRotQuat);
187
        TwistyObject object = control.getObject();
188
        object.setCubitQuats(mQuats);
182 189
        }
183 190

  
184 191
      mJustStarted = false;
......
327 334
      TwistyObject object = control.getObject();
328 335

  
329 336
      int level = mLevel==LEVELS_SHOWN ? mLevel : mLevel+1;
330
      long time = System.currentTimeMillis();
337
      ScreenSolving solving = (ScreenSolving)ScreenList.SOLV.getScreenClass();
338
      int time = solving.stopTimerAndGetRecord();
331 339
      String name = object.getShortName();
332 340
      int numCubits = object.getNumCubits();
333 341
      int[] quats = new int[numCubits];
src/main/java/org/distorted/play/ScreenScrambling.java
24 24

  
25 25
///////////////////////////////////////////////////////////////////////////////////////////////////
26 26

  
27
  void leaveScreen(PlayActivity act)
28
    {
29

  
30
    }
27
  void leaveScreen(PlayActivity act) { }
28
  public void savePreferences(SharedPreferences.Editor editor) { }
29
  public void restorePreferences(SharedPreferences preferences) { }
31 30

  
32 31
///////////////////////////////////////////////////////////////////////////////////////////////////
33 32

  
......
72 71
        }
73 72
      });
74 73
    }
75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

  
78
  public void savePreferences(SharedPreferences.Editor editor)
79
    {
80

  
81
    }
82

  
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

  
85
  public void restorePreferences(SharedPreferences preferences)
86
    {
87

  
88
    }
89 74
  }

Also available in: Unified diff