Revision acabdd83
Added by Leszek Koltunski almost 3 years ago
src/main/java/org/distorted/dialogs/RubikDialogNewRecord.java | ||
---|---|---|
37 | 37 |
|
38 | 38 |
import org.distorted.main.R; |
39 | 39 |
import org.distorted.main.RubikActivity; |
40 |
import org.distorted.network.RubikScores;
|
|
40 |
import org.distorted.external.RubikScores;
|
|
41 | 41 |
import org.distorted.objects.RubikObjectList; |
42 | 42 |
import org.distorted.screens.ScreenList; |
43 |
import org.distorted.screens.RubikScreenPlay; |
|
44 | 43 |
|
45 | 44 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
46 | 45 |
|
src/main/java/org/distorted/dialogs/RubikDialogScoresPagerAdapter.java | ||
---|---|---|
31 | 31 |
import android.widget.LinearLayout; |
32 | 32 |
|
33 | 33 |
import org.distorted.main.R; |
34 |
import org.distorted.network.RubikScores;
|
|
35 |
import org.distorted.network.RubikNetwork;
|
|
34 |
import org.distorted.external.RubikScores;
|
|
35 |
import org.distorted.external.RubikNetwork;
|
|
36 | 36 |
import org.distorted.objects.RubikObjectList; |
37 | 37 |
import org.distorted.screens.RubikScreenPlay; |
38 | 38 |
|
src/main/java/org/distorted/dialogs/RubikDialogScoresView.java | ||
---|---|---|
33 | 33 |
|
34 | 34 |
import org.distorted.main.R; |
35 | 35 |
import org.distorted.main.RubikActivity; |
36 |
import org.distorted.network.RubikScores;
|
|
36 |
import org.distorted.external.RubikScores;
|
|
37 | 37 |
|
38 |
import static org.distorted.network.RubikNetwork.MAX_PLACES;
|
|
38 |
import static org.distorted.external.RubikNetwork.MAX_PLACES;
|
|
39 | 39 |
|
40 | 40 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
41 | 41 |
|
src/main/java/org/distorted/dialogs/RubikDialogSetName.java | ||
---|---|---|
40 | 40 |
|
41 | 41 |
import org.distorted.main.R; |
42 | 42 |
import org.distorted.main.RubikActivity; |
43 |
import org.distorted.network.RubikScores;
|
|
43 |
import org.distorted.external.RubikScores;
|
|
44 | 44 |
import org.distorted.objects.RubikObjectList; |
45 | 45 |
import org.distorted.screens.ScreenList; |
46 |
import org.distorted.screens.RubikScreenPlay; |
|
47 | 46 |
|
48 | 47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
49 | 48 |
|
src/main/java/org/distorted/dialogs/RubikDialogUpdateView.java | ||
---|---|---|
31 | 31 |
import android.widget.TextView; |
32 | 32 |
|
33 | 33 |
import org.distorted.main.R; |
34 |
import org.distorted.network.RubikNetwork; |
|
35 |
import org.distorted.network.RubikUpdates; |
|
36 |
|
|
37 |
import java.io.InputStream; |
|
38 |
|
|
39 |
import static android.view.View.GONE; |
|
40 |
import static android.view.View.VISIBLE; |
|
34 |
import org.distorted.external.RubikNetwork; |
|
35 |
import org.distorted.external.RubikUpdates; |
|
41 | 36 |
|
42 | 37 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
43 | 38 |
|
... | ... | |
80 | 75 |
|
81 | 76 |
if( info.mPercent>=100 ) |
82 | 77 |
{ |
83 |
mBar.setVisibility(GONE); |
|
78 |
mBar.setVisibility(View.GONE);
|
|
84 | 79 |
mButton.setOnClickListener( new View.OnClickListener() |
85 | 80 |
{ |
86 | 81 |
@Override |
... | ... | |
98 | 93 |
} |
99 | 94 |
else |
100 | 95 |
{ |
101 |
mButton.setVisibility(GONE); |
|
96 |
mButton.setVisibility(View.GONE);
|
|
102 | 97 |
mBar.setLayoutParams(pButt); |
103 | 98 |
mBar.setProgress(info.mPercent); |
104 | 99 |
} |
... | ... | |
119 | 114 |
{ |
120 | 115 |
mDescription.setText(R.string.downloading); |
121 | 116 |
mBar.setProgress(20); |
122 |
mButton.setVisibility(GONE); |
|
123 |
mBar.setVisibility(VISIBLE); |
|
117 |
mButton.setVisibility(View.GONE);
|
|
118 |
mBar.setVisibility(View.VISIBLE);
|
|
124 | 119 |
} |
125 | 120 |
|
126 | 121 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
src/main/java/org/distorted/dialogs/RubikDialogUpdates.java | ||
---|---|---|
41 | 41 |
|
42 | 42 |
import org.distorted.main.R; |
43 | 43 |
import org.distorted.main.RubikActivity; |
44 |
import org.distorted.network.RubikNetwork;
|
|
45 |
import org.distorted.network.RubikUpdates;
|
|
44 |
import org.distorted.external.RubikNetwork;
|
|
45 |
import org.distorted.external.RubikUpdates;
|
|
46 | 46 |
|
47 | 47 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
48 | 48 |
|
src/main/java/org/distorted/external/RubikNetwork.java | ||
---|---|---|
1 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
2 |
// Copyright 2019 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.external; |
|
21 |
|
|
22 |
import java.io.BufferedReader; |
|
23 |
import java.io.IOException; |
|
24 |
import java.io.InputStream; |
|
25 |
import java.io.InputStreamReader; |
|
26 |
import java.net.HttpURLConnection; |
|
27 |
import java.net.URL; |
|
28 |
import java.net.UnknownHostException; |
|
29 |
import java.security.MessageDigest; |
|
30 |
import java.security.NoSuchAlgorithmException; |
|
31 |
|
|
32 |
import android.content.pm.PackageInfo; |
|
33 |
import android.content.pm.PackageManager; |
|
34 |
import android.graphics.Bitmap; |
|
35 |
import android.graphics.BitmapFactory; |
|
36 |
|
|
37 |
import androidx.fragment.app.FragmentActivity; |
|
38 |
|
|
39 |
import org.distorted.library.main.DistortedLibrary; |
|
40 |
import org.distorted.objectlib.json.JsonWriter; |
|
41 |
import org.distorted.objects.RubikObjectList; |
|
42 |
|
|
43 |
import static org.distorted.objects.RubikObjectList.MAX_LEVEL; |
|
44 |
|
|
45 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
46 |
|
|
47 |
public class RubikNetwork |
|
48 |
{ |
|
49 |
public interface ScoresReceiver |
|
50 |
{ |
|
51 |
void receive(String[][][] country, String[][][] name, float[][][] time); |
|
52 |
void message(String mess); |
|
53 |
void error(String error); |
|
54 |
} |
|
55 |
|
|
56 |
public interface IconReceiver |
|
57 |
{ |
|
58 |
void iconDownloaded(int ordinal, Bitmap bitmap); |
|
59 |
} |
|
60 |
|
|
61 |
public interface Updatee |
|
62 |
{ |
|
63 |
void receiveUpdate(RubikUpdates update); |
|
64 |
void errorUpdate(); |
|
65 |
} |
|
66 |
|
|
67 |
public interface Downloadee |
|
68 |
{ |
|
69 |
void jsonDownloaded(); |
|
70 |
} |
|
71 |
|
|
72 |
public static final int MAX_PLACES = 10; |
|
73 |
|
|
74 |
private static final int REND_ADRENO= 0; |
|
75 |
private static final int REND_MALI = 1; |
|
76 |
private static final int REND_POWER = 2; |
|
77 |
private static final int REND_OTHER = 3; |
|
78 |
|
|
79 |
private static final int DEBUG_RUNNING = 1; |
|
80 |
private static final int DEBUG_SUCCESS = 2; |
|
81 |
private static final int DEBUG_FAILURE = 3; |
|
82 |
|
|
83 |
private final String[] hex = { |
|
84 |
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", |
|
85 |
"%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f", |
|
86 |
"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17", |
|
87 |
"%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f", |
|
88 |
"%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", |
|
89 |
"%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f", |
|
90 |
"%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37", |
|
91 |
"%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f", |
|
92 |
"%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47", |
|
93 |
"%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f", |
|
94 |
"%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57", |
|
95 |
"%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f", |
|
96 |
"%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67", |
|
97 |
"%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f", |
|
98 |
"%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77", |
|
99 |
"%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f", |
|
100 |
"%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87", |
|
101 |
"%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f", |
|
102 |
"%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97", |
|
103 |
"%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f", |
|
104 |
"%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7", |
|
105 |
"%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af", |
|
106 |
"%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7", |
|
107 |
"%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf", |
|
108 |
"%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7", |
|
109 |
"%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf", |
|
110 |
"%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7", |
|
111 |
"%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df", |
|
112 |
"%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7", |
|
113 |
"%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef", |
|
114 |
"%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7", |
|
115 |
"%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff" |
|
116 |
}; |
|
117 |
|
|
118 |
private static String[][][] mCountry; |
|
119 |
private static String[][][] mName; |
|
120 |
private static float[][][] mTime; |
|
121 |
private static int[][] mPlaces; |
|
122 |
|
|
123 |
private static RubikNetwork mThis; |
|
124 |
private static String mScores = ""; |
|
125 |
private static boolean mRunning = false; |
|
126 |
private static Updatee mUpdatee; |
|
127 |
private static String mVersion; |
|
128 |
private static int mNumObjects; |
|
129 |
private static RubikUpdates mUpdates; |
|
130 |
private static int mDebugState; |
|
131 |
|
|
132 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
133 |
|
|
134 |
private static void initializeStatics() |
|
135 |
{ |
|
136 |
int newNum = RubikObjectList.getNumObjects(); |
|
137 |
|
|
138 |
if( mCountry==null || newNum!=mNumObjects ) mCountry = new String[newNum][MAX_LEVEL][MAX_PLACES]; |
|
139 |
if( mName==null || newNum!=mNumObjects ) mName = new String[newNum][MAX_LEVEL][MAX_PLACES]; |
|
140 |
if( mTime==null || newNum!=mNumObjects ) mTime = new float[newNum][MAX_LEVEL][MAX_PLACES]; |
|
141 |
if( mPlaces==null || newNum!=mNumObjects ) mPlaces = new int[newNum][MAX_LEVEL]; |
|
142 |
|
|
143 |
if( mUpdates==null ) mUpdates = new RubikUpdates(); |
|
144 |
|
|
145 |
mNumObjects = newNum; |
|
146 |
} |
|
147 |
|
|
148 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
149 |
|
|
150 |
private static String computeHash(String stringToHash, byte[] salt) |
|
151 |
{ |
|
152 |
String generatedPassword; |
|
153 |
|
|
154 |
try |
|
155 |
{ |
|
156 |
MessageDigest md = MessageDigest.getInstance("MD5"); |
|
157 |
md.update(salt); |
|
158 |
byte[] bytes = md.digest(stringToHash.getBytes()); |
|
159 |
StringBuilder sb = new StringBuilder(); |
|
160 |
|
|
161 |
for (byte aByte : bytes) |
|
162 |
{ |
|
163 |
sb.append(Integer.toString((aByte & 0xff) + 0x100, 16).substring(1)); |
|
164 |
} |
|
165 |
|
|
166 |
generatedPassword = sb.toString(); |
|
167 |
} |
|
168 |
catch (NoSuchAlgorithmException e) |
|
169 |
{ |
|
170 |
return "NoSuchAlgorithm"; |
|
171 |
} |
|
172 |
|
|
173 |
return generatedPassword; |
|
174 |
} |
|
175 |
|
|
176 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
177 |
|
|
178 |
private boolean fillValuesNormal(ScoresReceiver receiver) |
|
179 |
{ |
|
180 |
int begin=-1 ,end, len = mScores.length(); |
|
181 |
String row; |
|
182 |
|
|
183 |
if( len==0 ) |
|
184 |
{ |
|
185 |
receiver.error("1"); |
|
186 |
return false; |
|
187 |
} |
|
188 |
else if( len<=2 ) |
|
189 |
{ |
|
190 |
receiver.error(mScores); |
|
191 |
return false; |
|
192 |
} |
|
193 |
|
|
194 |
for(int i=0; i<mNumObjects; i++) |
|
195 |
for(int j=0; j<MAX_LEVEL; j++) |
|
196 |
{ |
|
197 |
mPlaces[i][j] = 0; |
|
198 |
} |
|
199 |
|
|
200 |
while( begin<len ) |
|
201 |
{ |
|
202 |
end = mScores.indexOf('\n', begin+1); |
|
203 |
if( end<0 ) end = len; |
|
204 |
|
|
205 |
try |
|
206 |
{ |
|
207 |
row = mScores.substring(begin+1,end); |
|
208 |
fillRow(row); |
|
209 |
} |
|
210 |
catch(Exception ex) |
|
211 |
{ |
|
212 |
// faulty row - ignore |
|
213 |
} |
|
214 |
|
|
215 |
begin = end; |
|
216 |
} |
|
217 |
|
|
218 |
return true; |
|
219 |
} |
|
220 |
|
|
221 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
222 |
|
|
223 |
private void fillRow(String row) |
|
224 |
{ |
|
225 |
int s1 = row.indexOf(' '); |
|
226 |
int s2 = row.indexOf(' ',s1+1); |
|
227 |
int s3 = row.indexOf(' ',s2+1); |
|
228 |
int s4 = row.indexOf(' ',s3+1); |
|
229 |
int s5 = row.length(); |
|
230 |
|
|
231 |
if( s5>s4 && s4>s3 && s3>s2 && s2>s1 && s1>0 ) |
|
232 |
{ |
|
233 |
int object = RubikObjectList.getOrdinal( row.substring(0,s1) ); |
|
234 |
|
|
235 |
if( object>=0 && object<mNumObjects ) |
|
236 |
{ |
|
237 |
int level = Integer.parseInt( row.substring(s1+1,s2) ); |
|
238 |
String name = row.substring(s2+1, s3); |
|
239 |
int time = Integer.parseInt( row.substring(s3+1,s4) ); |
|
240 |
String country = row.substring(s4+1, s5); |
|
241 |
|
|
242 |
if( country.equals("do") ) country = "dm"; // see RubikScores.setCountry() |
|
243 |
|
|
244 |
if(level>=0 && level<MAX_LEVEL) |
|
245 |
{ |
|
246 |
int p = mPlaces[object][level]; |
|
247 |
mPlaces[object][level]++; |
|
248 |
|
|
249 |
mCountry[object][level][p] = country; |
|
250 |
mName [object][level][p] = name; |
|
251 |
mTime [object][level][p] = ((float)(time/10))/100.0f; |
|
252 |
} |
|
253 |
} |
|
254 |
} |
|
255 |
else |
|
256 |
{ |
|
257 |
tryDoCommand(row); |
|
258 |
} |
|
259 |
} |
|
260 |
|
|
261 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
262 |
|
|
263 |
private void tryDoCommand(String row) |
|
264 |
{ |
|
265 |
if( row.startsWith("comm") ) |
|
266 |
{ |
|
267 |
int colon = row.indexOf(':'); |
|
268 |
|
|
269 |
if( colon>0 ) |
|
270 |
{ |
|
271 |
String commandNumber = row.substring(4,colon); |
|
272 |
int number; |
|
273 |
|
|
274 |
try |
|
275 |
{ |
|
276 |
number = Integer.parseInt(commandNumber); |
|
277 |
} |
|
278 |
catch(NumberFormatException ex) |
|
279 |
{ |
|
280 |
number=0; |
|
281 |
} |
|
282 |
|
|
283 |
if(number==1) |
|
284 |
{ |
|
285 |
String country = row.substring(colon+1); |
|
286 |
RubikScores scores = RubikScores.getInstance(); |
|
287 |
scores.setCountry(country); |
|
288 |
} |
|
289 |
} |
|
290 |
} |
|
291 |
} |
|
292 |
|
|
293 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
294 |
|
|
295 |
private int getRendererType(String renderer) |
|
296 |
{ |
|
297 |
if( renderer.contains("Adreno") ) return REND_ADRENO; |
|
298 |
if( renderer.contains("Mali") ) return REND_MALI; |
|
299 |
if( renderer.contains("PowerVR") ) return REND_POWER; |
|
300 |
|
|
301 |
return REND_OTHER; |
|
302 |
} |
|
303 |
|
|
304 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
305 |
|
|
306 |
private String parseRenderer(final int type, String renderer) |
|
307 |
{ |
|
308 |
if( type==REND_ADRENO || type==REND_POWER ) |
|
309 |
{ |
|
310 |
int lastSpace = renderer.lastIndexOf(' '); |
|
311 |
String ret = renderer.substring(lastSpace+1); |
|
312 |
return URLencode(ret); |
|
313 |
} |
|
314 |
|
|
315 |
if( type==REND_MALI ) |
|
316 |
{ |
|
317 |
int firstHyphen = renderer.indexOf('-'); |
|
318 |
String ret = renderer.substring(firstHyphen+1); |
|
319 |
return URLencode(ret); |
|
320 |
} |
|
321 |
|
|
322 |
return "other"; |
|
323 |
} |
|
324 |
|
|
325 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
326 |
|
|
327 |
private String parseVersion(final int type, String version) |
|
328 |
{ |
|
329 |
switch(type) |
|
330 |
{ |
|
331 |
case REND_ADRENO: int aMonkey = version.indexOf('@'); |
|
332 |
int aDot = version.indexOf('.', aMonkey); |
|
333 |
String ret1 = aDot>=3 ? version.substring(aDot-3,aDot) : ""; |
|
334 |
return URLencode(ret1); |
|
335 |
case REND_MALI : int mV1 = version.indexOf("v1"); |
|
336 |
int mHyphen = version.indexOf('-', mV1); |
|
337 |
String ret2 = mHyphen>mV1+3 && mV1>=0 ? version.substring(mV1+3,mHyphen) : ""; |
|
338 |
return URLencode(ret2); |
|
339 |
case REND_POWER : int pMonkey = version.indexOf('@'); |
|
340 |
int pSpace = version.lastIndexOf(' '); |
|
341 |
String ret3 = pSpace>=0 && pMonkey>pSpace+1 ? version.substring(pSpace+1,pMonkey) : ""; |
|
342 |
return URLencode(ret3); |
|
343 |
default : return ""; |
|
344 |
} |
|
345 |
} |
|
346 |
|
|
347 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
348 |
|
|
349 |
private String URLencode(String s) |
|
350 |
{ |
|
351 |
StringBuilder sbuf = new StringBuilder(); |
|
352 |
int len = s.length(); |
|
353 |
|
|
354 |
for (int i = 0; i < len; i++) |
|
355 |
{ |
|
356 |
int ch = s.charAt(i); |
|
357 |
|
|
358 |
if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch); |
|
359 |
else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch); |
|
360 |
else if ('0' <= ch && ch <= '9') sbuf.append((char)ch); |
|
361 |
else if (ch == ' ' ) sbuf.append('+'); |
|
362 |
else if (ch == '-' || ch == '_' |
|
363 |
|| ch == '.' || ch == '!' |
|
364 |
|| ch == '~' || ch == '*' |
|
365 |
|| ch == '\'' || ch == '(' |
|
366 |
|| ch == ')' ) sbuf.append((char)ch); |
|
367 |
else if (ch <= 0x007f) sbuf.append(hex[ch]); |
|
368 |
else if (ch <= 0x07FF) |
|
369 |
{ |
|
370 |
sbuf.append(hex[0xc0 | (ch >> 6)]); |
|
371 |
sbuf.append(hex[0x80 | (ch & 0x3F)]); |
|
372 |
} |
|
373 |
else |
|
374 |
{ |
|
375 |
sbuf.append(hex[0xe0 | (ch >> 12)]); |
|
376 |
sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]); |
|
377 |
sbuf.append(hex[0x80 | (ch & 0x3F)]); |
|
378 |
} |
|
379 |
} |
|
380 |
|
|
381 |
return sbuf.toString(); |
|
382 |
} |
|
383 |
|
|
384 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
385 |
|
|
386 |
private boolean network(String url, ScoresReceiver receiver) |
|
387 |
{ |
|
388 |
try |
|
389 |
{ |
|
390 |
java.net.URL connectURL = new URL(url); |
|
391 |
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); |
|
392 |
|
|
393 |
conn.setDoInput(true); |
|
394 |
conn.setDoOutput(true); |
|
395 |
conn.setUseCaches(false); |
|
396 |
conn.setRequestMethod("GET"); |
|
397 |
conn.connect(); |
|
398 |
conn.getOutputStream().flush(); |
|
399 |
|
|
400 |
InputStream is = conn.getInputStream(); |
|
401 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
|
402 |
StringBuilder total = new StringBuilder(); |
|
403 |
|
|
404 |
for (String line; (line = r.readLine()) != null; ) |
|
405 |
{ |
|
406 |
total.append(line).append('\n'); |
|
407 |
} |
|
408 |
|
|
409 |
mScores = total.toString(); |
|
410 |
conn.disconnect(); |
|
411 |
} |
|
412 |
catch( final UnknownHostException e ) |
|
413 |
{ |
|
414 |
receiver.message("No access to Internet"); |
|
415 |
return false; |
|
416 |
} |
|
417 |
catch( final SecurityException e ) |
|
418 |
{ |
|
419 |
receiver.message("Application not authorized to connect to the Internet"); |
|
420 |
return false; |
|
421 |
} |
|
422 |
catch( final Exception e ) |
|
423 |
{ |
|
424 |
receiver.message(e.getMessage()); |
|
425 |
return false; |
|
426 |
} |
|
427 |
|
|
428 |
if( mScores.length()==0 ) |
|
429 |
{ |
|
430 |
receiver.message("Failed to download scores"); |
|
431 |
return false; |
|
432 |
} |
|
433 |
|
|
434 |
return true; |
|
435 |
} |
|
436 |
|
|
437 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
438 |
|
|
439 |
private String constructSuspiciousURL(String suspURL) |
|
440 |
{ |
|
441 |
RubikScores scores = RubikScores.getInstance(); |
|
442 |
int deviceID= scores.getDeviceID(); |
|
443 |
String suspicious = URLencode(suspURL); |
|
444 |
|
|
445 |
String url="https://distorted.org/magic/cgi-bin/suspicious.cgi"; |
|
446 |
url += "?i="+deviceID+"&d="+suspicious; |
|
447 |
|
|
448 |
return url; |
|
449 |
} |
|
450 |
|
|
451 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
452 |
|
|
453 |
private String constructDebugURL() |
|
454 |
{ |
|
455 |
RubikScores scores = RubikScores.getInstance(); |
|
456 |
String name = URLencode(scores.getName()); |
|
457 |
int numRuns = scores.getNumRuns(); |
|
458 |
int numPlay = scores.getNumPlays(); |
|
459 |
String country = scores.getCountry(); |
|
460 |
String renderer = DistortedLibrary.getDriverRenderer(); |
|
461 |
String version = DistortedLibrary.getDriverVersion(); |
|
462 |
int objectAPI = JsonWriter.VERSION_OBJECT_MAJOR; |
|
463 |
int tutorialAPI = JsonWriter.VERSION_EXTRAS_MAJOR; |
|
464 |
|
|
465 |
renderer = URLencode(renderer); |
|
466 |
version = URLencode(version); |
|
467 |
|
|
468 |
String url="https://distorted.org/magic/cgi-bin/debugs-new.cgi"; |
|
469 |
url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion+"d"; |
|
470 |
url += "&d="+renderer+"&v="+version+"&a="+objectAPI+"&b="+tutorialAPI; |
|
471 |
|
|
472 |
return url; |
|
473 |
} |
|
474 |
|
|
475 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
476 |
|
|
477 |
private String constructDownloadURL() |
|
478 |
{ |
|
479 |
RubikScores scores = RubikScores.getInstance(); |
|
480 |
String name = URLencode(scores.getName()); |
|
481 |
int numRuns = scores.getNumRuns(); |
|
482 |
int numPlay = scores.getNumPlays(); |
|
483 |
String country = scores.getCountry(); |
|
484 |
|
|
485 |
String url="https://distorted.org/magic/cgi-bin/download.cgi"; |
|
486 |
url += "?n="+name+"&r="+numRuns+"&p="+numPlay+"&c="+country+"&e="+mVersion; |
|
487 |
|
|
488 |
return url; |
|
489 |
} |
|
490 |
|
|
491 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
492 |
|
|
493 |
private String constructSubmitURL() |
|
494 |
{ |
|
495 |
RubikScores scores = RubikScores.getInstance(); |
|
496 |
String name = URLencode(scores.getName()); |
|
497 |
String veri = scores.isVerified() ? "1" : ""; |
|
498 |
int numRuns = scores.getNumRuns(); |
|
499 |
int numPlay = scores.getNumPlays(); |
|
500 |
int deviceID= scores.getDeviceID(); |
|
501 |
String reclist = scores.getRecordList("&o=","&l=","&t="); |
|
502 |
String country = scores.getCountry(); |
|
503 |
long epoch = System.currentTimeMillis(); |
|
504 |
String salt = "cuboid"; |
|
505 |
|
|
506 |
String renderer = DistortedLibrary.getDriverRenderer(); |
|
507 |
String version = DistortedLibrary.getDriverVersion(); |
|
508 |
|
|
509 |
int type = getRendererType(renderer); |
|
510 |
renderer = parseRenderer(type,renderer); |
|
511 |
version = parseVersion(type,version); |
|
512 |
|
|
513 |
String url1="https://distorted.org/magic/cgi-bin/submit.cgi"; |
|
514 |
String url2 = "n="+name+"&v="+veri+"&r="+numRuns+"&p="+numPlay+"&i="+deviceID+"&e="+mVersion; |
|
515 |
url2 += "&d="+renderer+"&s="+version+reclist+"&c="+country+"&f="+epoch; |
|
516 |
String hash = computeHash( url2, salt.getBytes() ); |
|
517 |
|
|
518 |
return url1 + "?" + url2 + "&h=" + hash; |
|
519 |
} |
|
520 |
|
|
521 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
522 |
|
|
523 |
private boolean gottaDownload() |
|
524 |
{ |
|
525 |
return ((mScores.length()==0) && !mRunning); |
|
526 |
} |
|
527 |
|
|
528 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
529 |
|
|
530 |
private void figureOutVersion(FragmentActivity act) |
|
531 |
{ |
|
532 |
try |
|
533 |
{ |
|
534 |
PackageInfo pInfo = act.getPackageManager().getPackageInfo( act.getPackageName(), 0); |
|
535 |
mVersion = pInfo.versionName; |
|
536 |
} |
|
537 |
catch (PackageManager.NameNotFoundException e) |
|
538 |
{ |
|
539 |
mVersion = "0.9.2"; |
|
540 |
} |
|
541 |
} |
|
542 |
|
|
543 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
544 |
|
|
545 |
private void downloadThread(ScoresReceiver receiver) |
|
546 |
{ |
|
547 |
try |
|
548 |
{ |
|
549 |
if( gottaDownload() ) |
|
550 |
{ |
|
551 |
mRunning = true; |
|
552 |
boolean receiveValues = network(constructDownloadURL(),receiver); |
|
553 |
|
|
554 |
if( mRunning ) |
|
555 |
{ |
|
556 |
receiveValues = fillValuesNormal(receiver); |
|
557 |
mRunning = false; |
|
558 |
} |
|
559 |
|
|
560 |
if( receiveValues ) receiver.receive(mCountry, mName, mTime); |
|
561 |
} |
|
562 |
} |
|
563 |
catch( Exception e ) |
|
564 |
{ |
|
565 |
receiver.message("Exception downloading records: "+e.getMessage() ); |
|
566 |
} |
|
567 |
} |
|
568 |
|
|
569 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
570 |
|
|
571 |
private void submitThread(ScoresReceiver receiver) |
|
572 |
{ |
|
573 |
try |
|
574 |
{ |
|
575 |
mRunning = true; |
|
576 |
RubikScores scores = RubikScores.getInstance(); |
|
577 |
|
|
578 |
if( scores.thereAreUnsubmittedRecords() ) |
|
579 |
{ |
|
580 |
boolean receiveValues = network(constructSubmitURL(),receiver); |
|
581 |
|
|
582 |
if( mRunning ) |
|
583 |
{ |
|
584 |
receiveValues = fillValuesNormal(receiver); |
|
585 |
mRunning = false; |
|
586 |
} |
|
587 |
|
|
588 |
if( receiveValues ) |
|
589 |
{ |
|
590 |
RubikScores.getInstance().successfulSubmit(); |
|
591 |
receiver.receive(mCountry, mName, mTime); |
|
592 |
} |
|
593 |
} |
|
594 |
} |
|
595 |
catch( Exception e ) |
|
596 |
{ |
|
597 |
receiver.message("Exception submitting records: "+e.getMessage() ); |
|
598 |
} |
|
599 |
} |
|
600 |
|
|
601 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
602 |
|
|
603 |
private void debugThread() |
|
604 |
{ |
|
605 |
String url = constructDebugURL(); |
|
606 |
/* |
|
607 |
try { Thread.sleep(5000); } |
|
608 |
catch( InterruptedException ignored) {} |
|
609 |
*/ |
|
610 |
try |
|
611 |
{ |
|
612 |
java.net.URL connectURL = new URL(url); |
|
613 |
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); |
|
614 |
|
|
615 |
conn.setDoInput(true); |
|
616 |
conn.setDoOutput(true); |
|
617 |
conn.setUseCaches(false); |
|
618 |
conn.setRequestMethod("GET"); |
|
619 |
conn.connect(); |
|
620 |
conn.getOutputStream().flush(); |
|
621 |
|
|
622 |
InputStream is = conn.getInputStream(); |
|
623 |
BufferedReader r = new BufferedReader(new InputStreamReader(is)); |
|
624 |
StringBuilder answer = new StringBuilder(); |
|
625 |
|
|
626 |
for (String line; (line = r.readLine()) != null; ) |
|
627 |
{ |
|
628 |
answer.append(line).append('\n'); |
|
629 |
} |
|
630 |
|
|
631 |
String updates = answer.toString(); |
|
632 |
conn.disconnect(); |
|
633 |
mUpdates.parse(updates); |
|
634 |
|
|
635 |
if( mUpdatee!=null ) mUpdatee.receiveUpdate(mUpdates); |
|
636 |
mDebugState = DEBUG_SUCCESS; |
|
637 |
} |
|
638 |
catch( final Exception e ) |
|
639 |
{ |
|
640 |
if( mUpdatee!=null ) mUpdatee.errorUpdate(); |
|
641 |
mDebugState = DEBUG_FAILURE; |
|
642 |
} |
|
643 |
} |
|
644 |
|
|
645 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
646 |
|
|
647 |
private void suspiciousThread(String suspURL) |
|
648 |
{ |
|
649 |
String url = constructSuspiciousURL(suspURL); |
|
650 |
|
|
651 |
try |
|
652 |
{ |
|
653 |
java.net.URL connectURL = new URL(url); |
|
654 |
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); |
|
655 |
|
|
656 |
conn.setDoInput(true); |
|
657 |
conn.setDoOutput(true); |
|
658 |
conn.setUseCaches(false); |
|
659 |
conn.setRequestMethod("GET"); |
|
660 |
conn.connect(); |
|
661 |
conn.getOutputStream().flush(); |
|
662 |
conn.getInputStream(); |
|
663 |
conn.disconnect(); |
|
664 |
} |
|
665 |
catch( final Exception e ) |
|
666 |
{ |
|
667 |
// ignore |
|
668 |
} |
|
669 |
} |
|
670 |
|
|
671 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
672 |
|
|
673 |
private Bitmap downloadIcon(String url) |
|
674 |
{ |
|
675 |
try |
|
676 |
{ |
|
677 |
java.net.URL connectURL = new URL(url); |
|
678 |
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection(); |
|
679 |
conn.setDoInput(true); |
|
680 |
conn.connect(); |
|
681 |
InputStream input = conn.getInputStream(); |
|
682 |
Bitmap icon = BitmapFactory.decodeStream(input); |
|
683 |
conn.disconnect(); |
|
684 |
return icon; |
|
685 |
} |
|
686 |
catch (IOException e) |
|
687 |
{ |
|
688 |
android.util.Log.e("D", "Failed to download "+url); |
|
689 |
android.util.Log.e("D", e.getMessage() ); |
|
690 |
return null; |
|
691 |
} |
|
692 |
} |
|
693 |
|
|
694 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
695 |
|
|
696 |
private void iconThread(IconReceiver receiver) |
|
697 |
{ |
|
698 |
int numC = mUpdates.getCompletedNumber(); |
|
699 |
int numS = mUpdates.getStartedNumber(); |
|
700 |
|
|
701 |
for(int c=0; c<numC; c++) |
|
702 |
{ |
|
703 |
int iconPresent = mUpdates.getCompletedIconPresent(c); |
|
704 |
|
|
705 |
if( iconPresent!=0 ) |
|
706 |
{ |
|
707 |
Bitmap icon = mUpdates.getCompletedIcon(c); |
|
708 |
|
|
709 |
if( icon==null ) |
|
710 |
{ |
|
711 |
String url = mUpdates.getCompletedURL(c); |
|
712 |
icon = downloadIcon(url); |
|
713 |
} |
|
714 |
if( icon!=null ) |
|
715 |
{ |
|
716 |
mUpdates.setCompletedIcon(c,icon); |
|
717 |
receiver.iconDownloaded(c,icon); |
|
718 |
} |
|
719 |
} |
|
720 |
} |
|
721 |
|
|
722 |
for(int s=0; s<numS; s++) |
|
723 |
{ |
|
724 |
int iconPresent = mUpdates.getStartedIconPresent(s); |
|
725 |
|
|
726 |
if( iconPresent!=0 ) |
|
727 |
{ |
|
728 |
Bitmap icon = mUpdates.getStartedIcon(s); |
|
729 |
|
|
730 |
if( icon==null ) |
|
731 |
{ |
|
732 |
String url = mUpdates.getStartedURL(s); |
|
733 |
icon = downloadIcon(url); |
|
734 |
} |
|
735 |
if( icon!=null ) |
|
736 |
{ |
|
737 |
mUpdates.setStartedIcon(s,icon); |
|
738 |
receiver.iconDownloaded(numC+s,icon); |
|
739 |
} |
|
740 |
} |
|
741 |
} |
|
742 |
} |
|
743 |
|
|
744 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
745 |
|
|
746 |
private InputStream downloadJSON(String name) |
|
747 |
{ |
|
748 |
String url = mUpdates.getURL() + name; |
|
749 |
|
|
750 |
try |
|
751 |
{ |
|
752 |
java.net.URL connectURL = new URL(url); |
|
753 |
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection(); |
|
754 |
conn.setDoInput(true); |
|
755 |
conn.connect(); |
|
756 |
InputStream stream = conn.getInputStream(); |
|
757 |
conn.disconnect(); |
|
758 |
return stream; |
|
759 |
} |
|
760 |
catch (IOException e) |
|
761 |
{ |
|
762 |
android.util.Log.e("D", "Failed to download "+url); |
|
763 |
android.util.Log.e("D", e.getMessage() ); |
|
764 |
return null; |
|
765 |
} |
|
766 |
} |
|
767 |
|
|
768 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
769 |
|
|
770 |
private void jsonThread(final RubikUpdates.UpdateInfo info, Downloadee downloadee) |
|
771 |
{ |
|
772 |
if(info.mUpdateObject) info.mObjectStream = downloadJSON(info.mObjectShortName+"_object.json"); |
|
773 |
if(info.mUpdateExtras) info.mExtrasStream = downloadJSON(info.mObjectShortName+"_extras.json"); |
|
774 |
|
|
775 |
downloadee.jsonDownloaded(); |
|
776 |
} |
|
777 |
|
|
778 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
779 |
|
|
780 |
private RubikNetwork() |
|
781 |
{ |
|
782 |
|
|
783 |
} |
|
784 |
|
|
785 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
786 |
// PUBLIC API |
|
787 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
788 |
|
|
789 |
public static void onPause() |
|
790 |
{ |
|
791 |
mRunning = false; |
|
792 |
} |
|
793 |
|
|
794 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
795 |
|
|
796 |
public static RubikNetwork getInstance() |
|
797 |
{ |
|
798 |
if( mThis==null ) |
|
799 |
{ |
|
800 |
mThis = new RubikNetwork(); |
|
801 |
} |
|
802 |
|
|
803 |
return mThis; |
|
804 |
} |
|
805 |
|
|
806 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
807 |
|
|
808 |
public void download(final ScoresReceiver receiver, final FragmentActivity act) |
|
809 |
{ |
|
810 |
initializeStatics(); |
|
811 |
figureOutVersion(act); |
|
812 |
|
|
813 |
Thread thread = new Thread() |
|
814 |
{ |
|
815 |
public void run() |
|
816 |
{ |
|
817 |
downloadThread(receiver); |
|
818 |
} |
|
819 |
}; |
|
820 |
|
|
821 |
thread.start(); |
|
822 |
} |
|
823 |
|
|
824 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
825 |
|
|
826 |
public void submit(ScoresReceiver receiver, final FragmentActivity act) |
|
827 |
{ |
|
828 |
initializeStatics(); |
|
829 |
figureOutVersion(act); |
|
830 |
|
|
831 |
Thread thread = new Thread() |
|
832 |
{ |
|
833 |
public void run() |
|
834 |
{ |
|
835 |
submitThread(receiver); |
|
836 |
} |
|
837 |
}; |
|
838 |
|
|
839 |
thread.start(); |
|
840 |
} |
|
841 |
|
|
842 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
843 |
|
|
844 |
public void debug() |
|
845 |
{ |
|
846 |
initializeStatics(); |
|
847 |
mDebugState = DEBUG_RUNNING; |
|
848 |
|
|
849 |
Thread thread = new Thread() |
|
850 |
{ |
|
851 |
public void run() |
|
852 |
{ |
|
853 |
debugThread(); |
|
854 |
} |
|
855 |
}; |
|
856 |
|
|
857 |
thread.start(); |
|
858 |
} |
|
859 |
|
|
860 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
861 |
|
|
862 |
public void suspicious(final String suspicious) |
|
863 |
{ |
|
864 |
initializeStatics(); |
|
865 |
|
|
866 |
Thread thread = new Thread() |
|
867 |
{ |
|
868 |
public void run() |
|
869 |
{ |
|
870 |
suspiciousThread(suspicious); |
|
871 |
} |
|
872 |
}; |
|
873 |
|
|
874 |
thread.start(); |
|
875 |
} |
|
876 |
|
|
877 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
878 |
// Yes it can happen that the second Updatee registers before we sent an update to the first one |
|
879 |
// and, as a result, the update never gets sent to the first one. This is not a problem (now, when |
|
880 |
// there are only two updatees - the RubikStatePlay and the UpdateDialog) |
|
881 |
// |
|
882 |
// Yes, there is also a remote possibility that the two threads executing this function and executing |
|
883 |
// the sendDebug() get swapped exactly in unlucky moment and the update never gets to the updatee. |
|
884 |
// We don't care about such remote possibility, then the app simply would signal that there are no |
|
885 |
// updates available. |
|
886 |
|
|
887 |
public void signUpForUpdates(Updatee updatee) |
|
888 |
{ |
|
889 |
if( mDebugState==DEBUG_SUCCESS ) updatee.receiveUpdate(mUpdates); |
|
890 |
else if( mDebugState==DEBUG_FAILURE ) updatee.errorUpdate(); |
|
891 |
else mUpdatee = updatee; |
|
892 |
} |
|
893 |
|
|
894 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
895 |
|
|
896 |
public void downloadIcons(final IconReceiver receiver) |
|
897 |
{ |
|
898 |
initializeStatics(); |
|
899 |
|
|
900 |
Thread thread = new Thread() |
|
901 |
{ |
|
902 |
public void run() |
|
903 |
{ |
|
904 |
iconThread(receiver); |
|
905 |
} |
|
906 |
}; |
|
907 |
|
|
908 |
thread.start(); |
|
909 |
} |
|
910 |
|
|
911 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
912 |
|
|
913 |
public void downloadJSON(final RubikUpdates.UpdateInfo info, final Downloadee downloadee) |
|
914 |
{ |
|
915 |
initializeStatics(); |
|
916 |
|
|
917 |
Thread thread = new Thread() |
|
918 |
{ |
|
919 |
public void run() |
|
920 |
{ |
|
921 |
jsonThread(info,downloadee); |
|
922 |
} |
|
923 |
}; |
|
924 |
|
|
925 |
thread.start(); |
|
926 |
} |
|
927 |
|
|
928 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
929 |
|
|
930 |
public void updateDone(String shortName) |
|
931 |
{ |
|
932 |
mUpdates.updateDone(shortName); |
|
933 |
} |
|
934 |
} |
src/main/java/org/distorted/external/RubikScores.java | ||
---|---|---|
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.external; |
|
21 |
|
|
22 |
import java.util.HashMap; |
|
23 |
import java.util.UUID; |
|
24 |
|
|
25 |
import android.content.Context; |
|
26 |
import android.content.SharedPreferences; |
|
27 |
import android.telephony.TelephonyManager; |
|
28 |
|
|
29 |
import com.google.firebase.crashlytics.FirebaseCrashlytics; |
|
30 |
|
|
31 |
import org.distorted.main.BuildConfig; |
|
32 |
import org.distorted.objects.RubikObject; |
|
33 |
import org.distorted.objects.RubikObjectList; |
|
34 |
|
|
35 |
import static org.distorted.objects.RubikObjectList.MAX_LEVEL; |
|
36 |
|
|
37 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
38 |
// hold my own scores, and some other statistics. |
|
39 |
|
|
40 |
public class RubikScores |
|
41 |
{ |
|
42 |
public static final int MULT = 1000000; |
|
43 |
public static final long NO_RECORD = Long.MAX_VALUE; |
|
44 |
private static RubikScores mThis; |
|
45 |
|
|
46 |
private String mName, mCountry; |
|
47 |
private boolean mNameIsVerified; |
|
48 |
private int mNumRuns; |
|
49 |
private int mNumPlays; |
|
50 |
private int mNumWins; |
|
51 |
private int mDeviceID; |
|
52 |
|
|
53 |
private static class MapValue |
|
54 |
{ |
|
55 |
long record; |
|
56 |
boolean submitted; |
|
57 |
|
|
58 |
MapValue(long rec,int sub) |
|
59 |
{ |
|
60 |
record = rec; |
|
61 |
submitted = sub!=0; |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
private final HashMap<Integer,MapValue> mMap; |
|
66 |
|
|
67 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
68 |
|
|
69 |
private RubikScores() |
|
70 |
{ |
|
71 |
mMap = new HashMap<>(); |
|
72 |
|
|
73 |
mName = ""; |
|
74 |
mCountry = "un"; |
|
75 |
|
|
76 |
mNameIsVerified = false; |
|
77 |
|
|
78 |
mNumPlays= -1; |
|
79 |
mNumRuns = -1; |
|
80 |
mDeviceID= -1; |
|
81 |
mNumWins = 0; |
|
82 |
} |
|
83 |
|
|
84 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
85 |
|
|
86 |
private int mapKey(int object,int level) |
|
87 |
{ |
|
88 |
return object*MULT + level; |
|
89 |
} |
|
90 |
|
|
91 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
92 |
|
|
93 |
private int privateGetDeviceID() |
|
94 |
{ |
|
95 |
int id; |
|
96 |
|
|
97 |
try |
|
98 |
{ |
|
99 |
String s = UUID.randomUUID().toString(); |
|
100 |
id = s.hashCode(); |
|
101 |
} |
|
102 |
catch(Exception ex) |
|
103 |
{ |
|
104 |
id = 0; |
|
105 |
android.util.Log.e("scores", "Exception in getDeviceID()"); |
|
106 |
} |
|
107 |
|
|
108 |
return id<0 ? -id : id; |
|
109 |
} |
|
110 |
|
|
111 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
112 |
|
|
113 |
synchronized void successfulSubmit() |
|
114 |
{ |
|
115 |
mNameIsVerified = true; |
|
116 |
|
|
117 |
for(int key: mMap.keySet()) |
|
118 |
{ |
|
119 |
MapValue value = mMap.get(key); |
|
120 |
if( value!=null ) value.submitted = true; |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
125 |
|
|
126 |
int getDeviceID() |
|
127 |
{ |
|
128 |
return mDeviceID; |
|
129 |
} |
|
130 |
|
|
131 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
132 |
|
|
133 |
synchronized boolean thereAreUnsubmittedRecords() |
|
134 |
{ |
|
135 |
for(int key: mMap.keySet()) |
|
136 |
{ |
|
137 |
MapValue value = mMap.get(key); |
|
138 |
if( value!=null && !value.submitted && value.record<NO_RECORD) return true; |
|
139 |
} |
|
140 |
|
|
141 |
return false; |
|
142 |
} |
|
143 |
|
|
144 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
145 |
|
|
146 |
synchronized String getRecordList(String strObj, String strLvl, String strTim) |
|
147 |
{ |
|
148 |
StringBuilder builderObj = new StringBuilder(); |
|
149 |
StringBuilder builderLvl = new StringBuilder(); |
|
150 |
StringBuilder builderTim = new StringBuilder(); |
|
151 |
boolean first = true; |
|
152 |
|
|
153 |
for(int key: mMap.keySet()) |
|
154 |
{ |
|
155 |
MapValue value = mMap.get(key); |
|
156 |
|
|
157 |
if( value!=null && !value.submitted && value.record<NO_RECORD) |
|
158 |
{ |
|
159 |
if( !first ) |
|
160 |
{ |
|
161 |
builderObj.append(','); |
|
162 |
builderLvl.append(','); |
|
163 |
builderTim.append(','); |
|
164 |
} |
|
165 |
first=false; |
|
166 |
|
|
167 |
RubikObject object = RubikObjectList.getObject(key/MULT); |
|
168 |
|
|
169 |
if( object!=null ) |
|
170 |
{ |
|
171 |
builderObj.append(object.getName()); |
|
172 |
builderLvl.append(key%MULT); |
|
173 |
builderTim.append(value.record); |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
|
|
178 |
return strObj+builderObj.toString()+strLvl+builderLvl.toString()+strTim+builderTim.toString(); |
|
179 |
} |
|
180 |
|
|
181 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
182 |
// Public API |
|
183 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
184 |
|
|
185 |
public boolean isVerified() |
|
186 |
{ |
|
187 |
return mNameIsVerified; |
|
188 |
} |
|
189 |
|
|
190 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
191 |
|
|
192 |
public int getNumPlays() |
|
193 |
{ |
|
194 |
return mNumPlays; |
|
195 |
} |
|
196 |
|
|
197 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
198 |
|
|
199 |
public int getNumRuns() |
|
200 |
{ |
|
201 |
return mNumRuns; |
|
202 |
} |
|
203 |
|
|
204 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
205 |
|
|
206 |
public String getName() |
|
207 |
{ |
|
208 |
return mName; |
|
209 |
} |
|
210 |
|
|
211 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
212 |
|
|
213 |
public String getCountry() |
|
214 |
{ |
|
215 |
return mCountry; |
|
216 |
} |
|
217 |
|
|
218 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
219 |
|
|
220 |
public void incrementNumPlays() |
|
221 |
{ |
|
222 |
mNumPlays++; |
|
223 |
} |
|
224 |
|
|
225 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
226 |
|
|
227 |
public void incrementNumRuns() |
|
228 |
{ |
|
229 |
mNumRuns++; |
|
230 |
} |
|
231 |
|
|
232 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
233 |
|
|
234 |
public int incrementNumWins() |
|
235 |
{ |
|
236 |
mNumWins++; |
|
237 |
return mNumWins; |
|
238 |
} |
|
239 |
|
|
240 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
241 |
|
|
242 |
public void setName(String newName) |
|
243 |
{ |
|
244 |
mName = newName; |
|
245 |
} |
|
246 |
|
|
247 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
248 |
|
|
249 |
public synchronized boolean setRecord(int object, int level, long record) |
|
250 |
{ |
|
251 |
int key = mapKey(object,level)-1; // -1 - historical reasons; previous versions saved it like this. |
|
252 |
MapValue oldValue = mMap.get(key); |
|
253 |
|
|
254 |
if( oldValue==null ) |
|
255 |
{ |
|
256 |
MapValue value = new MapValue(record,0); |
|
257 |
mMap.put(key,value); |
|
258 |
return true; |
|
259 |
} |
|
260 |
|
|
261 |
long oldRecord = oldValue.record; |
|
262 |
|
|
263 |
if( oldRecord>record) |
|
264 |
{ |
|
265 |
MapValue value = new MapValue(record,0); |
|
266 |
mMap.put(key,value); |
|
267 |
return true; |
|
268 |
} |
|
269 |
|
|
270 |
return false; |
|
271 |
} |
|
272 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
273 |
|
|
274 |
public synchronized long getRecord(int object, int level) |
|
275 |
{ |
|
276 |
int key = mapKey(object,level); |
|
277 |
MapValue value = mMap.get(key); |
|
278 |
return value!=null ? value.record : NO_RECORD; |
|
279 |
} |
|
280 |
|
|
281 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
282 |
|
|
283 |
public synchronized boolean isSolved(int object, int level) |
|
284 |
{ |
|
285 |
int key = mapKey(object,level); |
|
286 |
MapValue value = mMap.get(key); |
|
287 |
return value!=null && value.record<NO_RECORD; |
|
288 |
} |
|
289 |
|
|
290 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
291 |
|
|
292 |
public void setCountry(Context context) |
|
293 |
{ |
|
294 |
TelephonyManager tM =((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)); |
|
295 |
|
|
296 |
if( tM!=null ) |
|
297 |
{ |
|
298 |
mCountry = tM.getSimCountryIso(); |
|
299 |
|
|
300 |
if( mCountry==null || mCountry.length()<=1 ) |
|
301 |
{ |
|
302 |
mCountry = tM.getNetworkCountryIso(); |
|
303 |
} |
|
304 |
} |
|
305 |
|
|
306 |
// Special case: Dominicana. Its ISO-3166-alpha-2 country code is 'do' which we can't have here |
|
307 |
// because we later on map this to a resource name (the flag) and 'do' is a reserved Java keyword |
|
308 |
// and can't be a resource name. |
|
309 |
|
|
310 |
if( mCountry.equals("do") ) mCountry = "dm"; |
|
311 |
} |
|
312 |
|
|
313 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
314 |
|
|
315 |
public void setCountry(String country) |
|
316 |
{ |
|
317 |
mCountry = country; |
|
318 |
|
|
319 |
if( mCountry.equals("do") ) mCountry = "dm"; // see above |
|
320 |
} |
|
321 |
|
|
322 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
323 |
|
|
324 |
public static RubikScores getInstance() |
|
325 |
{ |
|
326 |
if( mThis==null ) mThis = new RubikScores(); |
|
327 |
return mThis; |
|
328 |
} |
|
329 |
|
|
330 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
331 |
|
|
332 |
public synchronized void savePreferences(SharedPreferences.Editor editor) |
|
333 |
{ |
|
334 |
int numObjects = RubikObjectList.getNumObjects(); |
|
335 |
StringBuilder builder = new StringBuilder(); |
|
336 |
|
|
337 |
for(int level=0; level<MAX_LEVEL; level++) |
|
338 |
{ |
|
339 |
builder.setLength(0); |
|
340 |
|
|
341 |
for(int object=0; object<numObjects; object++) |
|
342 |
{ |
|
343 |
int key = mapKey(object,level); |
|
344 |
RubikObject obj = RubikObjectList.getObject(object); |
|
345 |
MapValue value = mMap.get(key); |
|
346 |
|
|
347 |
if( obj!=null && value!=null && value.record<NO_RECORD ) |
|
348 |
{ |
|
349 |
builder.append(obj.getName()); |
|
350 |
builder.append("="); |
|
351 |
builder.append(value.record); |
|
352 |
builder.append(","); |
|
353 |
builder.append(value.submitted ? 1:0 ); |
|
354 |
builder.append(" "); |
|
355 |
} |
|
356 |
} |
|
357 |
|
|
358 |
editor.putString("scores_record"+level, builder.toString()); |
|
359 |
} |
|
360 |
|
|
361 |
editor.putString("scores_name" , mName ); |
|
362 |
editor.putBoolean("scores_isVerified", mNameIsVerified); |
|
363 |
editor.putInt("scores_numPlays", mNumPlays); |
|
364 |
editor.putInt("scores_numRuns" , mNumRuns ); |
|
365 |
editor.putInt("scores_deviceid", mDeviceID); |
|
366 |
editor.putInt("scores_review" , mNumWins ); |
|
367 |
} |
|
368 |
|
|
369 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
370 |
|
|
371 |
public synchronized void restorePreferences(SharedPreferences preferences) |
|
372 |
{ |
|
373 |
String recordStr, subStr, nameStr, timeStr, submStr, errorStr=""; |
|
374 |
int start, end, equals, comma, object, subm; |
|
375 |
long time; |
|
376 |
boolean thereWasError = false; |
|
377 |
int numObjects = RubikObjectList.getNumObjects(); |
|
378 |
|
|
379 |
for(int level=0; level<MAX_LEVEL; level++) |
|
380 |
{ |
|
381 |
start = end = 0; |
|
382 |
recordStr = preferences.getString("scores_record"+level, ""); |
|
383 |
|
|
384 |
while( end!=-1 ) |
|
385 |
{ |
|
386 |
end = recordStr.indexOf(" ", start); |
|
387 |
|
|
388 |
if( end==-1 ) subStr = recordStr.substring(start); |
|
389 |
else subStr = recordStr.substring(start,end); |
|
390 |
|
|
391 |
start = end+1; |
|
392 |
|
|
393 |
equals = subStr.indexOf("="); |
|
394 |
comma = subStr.indexOf(","); |
|
395 |
|
|
396 |
if( equals>=0 && comma>=0 ) |
|
397 |
{ |
|
398 |
nameStr = subStr.substring(0,equals); |
|
399 |
timeStr = subStr.substring(equals+1,comma); |
|
400 |
submStr = subStr.substring(comma+1); |
|
401 |
|
|
402 |
object = RubikObjectList.getOrdinal(nameStr); |
|
403 |
|
|
404 |
if( object>=0 && object<numObjects ) |
|
405 |
{ |
|
406 |
time = Long.parseLong(timeStr); |
|
407 |
subm = Integer.parseInt(submStr); |
|
408 |
|
|
409 |
if( subm>=0 && subm<=1 ) |
|
410 |
{ |
|
411 |
MapValue value = new MapValue(time,subm); |
|
412 |
int key = mapKey(object,level); |
|
413 |
mMap.put(key,value); |
|
414 |
} |
|
415 |
else |
|
416 |
{ |
|
417 |
errorStr += ("error1: subm="+subm+" obj: "+nameStr+"\n"); |
|
418 |
thereWasError= true; |
|
419 |
} |
|
420 |
} |
|
421 |
else |
Also available in: Unified diff
Rename the 'network' package to 'external' since it will also deal with writing/reading files from local storage.