| 29 |
29 |
|
| 30 |
30 |
public class RubikRememberedSolves
|
| 31 |
31 |
{
|
| 32 |
|
private static final int MAXSOLVES = 5;
|
|
32 |
private static final int MAXSOLVES = 4;
|
| 33 |
33 |
private static RubikRememberedSolves mThis;
|
| 34 |
34 |
|
| 35 |
35 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| ... | ... | |
| 101 |
101 |
return contents.toString();
|
| 102 |
102 |
}
|
| 103 |
103 |
|
|
104 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
105 |
|
|
106 |
public void deleteSolve(Activity act, String objname, int level, long time)
|
|
107 |
{
|
|
108 |
String filename = objname.toLowerCase()+"_solves.json";
|
|
109 |
RubikFiles files = RubikFiles.getInstance();
|
|
110 |
InputStream input= files.openFile(act,filename);
|
|
111 |
|
|
112 |
if( input!=null )
|
|
113 |
{
|
|
114 |
String contents= deleteInfo(input,level,time);
|
|
115 |
File file = new File(act.getFilesDir(), filename);
|
|
116 |
|
|
117 |
try( FileOutputStream fos = new FileOutputStream(file) )
|
|
118 |
{
|
|
119 |
fos.write(contents.getBytes(StandardCharsets.UTF_8));
|
|
120 |
}
|
|
121 |
catch(IOException ex)
|
|
122 |
{
|
|
123 |
android.util.Log.e("D", "deleteSolve: failed to save file "+filename+" : "+ex.getMessage());
|
|
124 |
}
|
|
125 |
}
|
|
126 |
else
|
|
127 |
{
|
|
128 |
android.util.Log.e("D", "deleteSolve: error: file "+filename+" not found");
|
|
129 |
}
|
|
130 |
}
|
|
131 |
|
| 104 |
132 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 105 |
133 |
|
| 106 |
134 |
public void rememberSolve(Activity act, String objname, int level, long time, Static4D rot, int[] quats)
|
| ... | ... | |
| 117 |
145 |
}
|
| 118 |
146 |
catch(IOException ex)
|
| 119 |
147 |
{
|
| 120 |
|
android.util.Log.e("D", "failed to save file "+filename+" : "+ex.getMessage());
|
|
148 |
android.util.Log.e("D", "rememberSolve: failed to save file "+filename+" : "+ex.getMessage());
|
| 121 |
149 |
}
|
| 122 |
150 |
}
|
| 123 |
151 |
|
| ... | ... | |
| 139 |
167 |
"quats: "+quatStr );
|
| 140 |
168 |
}
|
| 141 |
169 |
|
|
170 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
171 |
|
|
172 |
public String deleteInfo(InputStream stream, int level, long time)
|
|
173 |
{
|
|
174 |
try
|
|
175 |
{
|
|
176 |
String contents = readContents(stream);
|
|
177 |
JSONArray levels = new JSONArray(contents);
|
|
178 |
JSONArray lvl = levels.getJSONArray(level);
|
|
179 |
int numSolves = lvl.length();
|
|
180 |
|
|
181 |
for(int s=0; s<numSolves; s++)
|
|
182 |
{
|
|
183 |
JSONObject obj = lvl.getJSONObject(s);
|
|
184 |
long tm = obj.getLong("time");
|
|
185 |
if( tm==time ) { lvl.remove(s); break; }
|
|
186 |
}
|
|
187 |
|
|
188 |
return levels.toString();
|
|
189 |
}
|
|
190 |
catch(IOException iex) { android.util.Log.e("D", "addInfo: failed to read file: "+iex.getMessage() ); }
|
|
191 |
catch(JSONException jex) { android.util.Log.e("D", "addInfo: failed to parse file: "+jex.getMessage()); }
|
|
192 |
|
|
193 |
return null;
|
|
194 |
}
|
|
195 |
|
| 142 |
196 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 143 |
197 |
|
| 144 |
198 |
public String addInfo(InputStream stream, int level, long time, Static4D rot, int[] quats)
|
| ... | ... | |
| 151 |
205 |
JSONArray levels = new JSONArray(contents);
|
| 152 |
206 |
JSONArray lvl = levels.getJSONArray(level);
|
| 153 |
207 |
JSONObject data = createData(time,rot,quats);
|
| 154 |
|
if( lvl.length()>MAXSOLVES ) lvl.remove(0);
|
|
208 |
if( lvl.length()>=MAXSOLVES ) lvl.remove(0);
|
| 155 |
209 |
lvl.put(data);
|
| 156 |
210 |
return levels.toString();
|
| 157 |
211 |
}
|
progress with remembering solves.