Revision 36e2cbdd
Added by Leszek Koltunski over 4 years ago
src/main/java/org/distorted/magic/RubikScoresDownloader.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.magic; |
21 | 21 |
|
22 |
import java.io.IOException; |
|
23 |
import java.io.InputStream; |
|
24 |
import java.net.HttpURLConnection; |
|
25 |
import java.net.URL; |
|
26 |
|
|
22 | 27 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
23 | 28 |
|
24 | 29 |
class RubikScoresDownloader implements Runnable |
... | ... | |
28 | 33 |
void receive(String[][][][] scores); |
29 | 34 |
} |
30 | 35 |
|
31 |
static final int MAX_PLACES = 10;
|
|
36 |
static final int MAX_PLACES = 12;
|
|
32 | 37 |
|
33 | 38 |
private static final int DOWNLOAD = 0; |
34 | 39 |
private static final int SUBMIT = 1; |
... | ... | |
36 | 41 |
|
37 | 42 |
private static final String URL ="http://koltunski.pl/rubik/cgi-bin"; |
38 | 43 |
|
44 |
private final String[] hex = { |
|
45 |
"%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07", |
|
46 |
"%08", "%09", "%0a", "%0b", "%0c", "%0d", "%0e", "%0f", |
|
47 |
"%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17", |
|
48 |
"%18", "%19", "%1a", "%1b", "%1c", "%1d", "%1e", "%1f", |
|
49 |
"%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27", |
|
50 |
"%28", "%29", "%2a", "%2b", "%2c", "%2d", "%2e", "%2f", |
|
51 |
"%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37", |
|
52 |
"%38", "%39", "%3a", "%3b", "%3c", "%3d", "%3e", "%3f", |
|
53 |
"%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47", |
|
54 |
"%48", "%49", "%4a", "%4b", "%4c", "%4d", "%4e", "%4f", |
|
55 |
"%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57", |
|
56 |
"%58", "%59", "%5a", "%5b", "%5c", "%5d", "%5e", "%5f", |
|
57 |
"%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67", |
|
58 |
"%68", "%69", "%6a", "%6b", "%6c", "%6d", "%6e", "%6f", |
|
59 |
"%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77", |
|
60 |
"%78", "%79", "%7a", "%7b", "%7c", "%7d", "%7e", "%7f", |
|
61 |
"%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87", |
|
62 |
"%88", "%89", "%8a", "%8b", "%8c", "%8d", "%8e", "%8f", |
|
63 |
"%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97", |
|
64 |
"%98", "%99", "%9a", "%9b", "%9c", "%9d", "%9e", "%9f", |
|
65 |
"%a0", "%a1", "%a2", "%a3", "%a4", "%a5", "%a6", "%a7", |
|
66 |
"%a8", "%a9", "%aa", "%ab", "%ac", "%ad", "%ae", "%af", |
|
67 |
"%b0", "%b1", "%b2", "%b3", "%b4", "%b5", "%b6", "%b7", |
|
68 |
"%b8", "%b9", "%ba", "%bb", "%bc", "%bd", "%be", "%bf", |
|
69 |
"%c0", "%c1", "%c2", "%c3", "%c4", "%c5", "%c6", "%c7", |
|
70 |
"%c8", "%c9", "%ca", "%cb", "%cc", "%cd", "%ce", "%cf", |
|
71 |
"%d0", "%d1", "%d2", "%d3", "%d4", "%d5", "%d6", "%d7", |
|
72 |
"%d8", "%d9", "%da", "%db", "%dc", "%dd", "%de", "%df", |
|
73 |
"%e0", "%e1", "%e2", "%e3", "%e4", "%e5", "%e6", "%e7", |
|
74 |
"%e8", "%e9", "%ea", "%eb", "%ec", "%ed", "%ee", "%ef", |
|
75 |
"%f0", "%f1", "%f2", "%f3", "%f4", "%f5", "%f6", "%f7", |
|
76 |
"%f8", "%f9", "%fa", "%fb", "%fc", "%fd", "%fe", "%ff" |
|
77 |
}; |
|
78 |
|
|
39 | 79 |
private static boolean mRunning = false; |
40 |
private static Thread mNetworkThrd = null; |
|
41 | 80 |
private static int mMode = IDLE; |
42 | 81 |
private static Receiver mReceiver; |
43 | 82 |
|
... | ... | |
105 | 144 |
} |
106 | 145 |
} |
107 | 146 |
|
147 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
148 |
|
|
149 |
private String URLencode(String s) |
|
150 |
{ |
|
151 |
StringBuilder sbuf = new StringBuilder(); |
|
152 |
int len = s.length(); |
|
153 |
|
|
154 |
for (int i = 0; i < len; i++) |
|
155 |
{ |
|
156 |
int ch = s.charAt(i); |
|
157 |
|
|
158 |
if ('A' <= ch && ch <= 'Z') sbuf.append((char)ch); |
|
159 |
else if ('a' <= ch && ch <= 'z') sbuf.append((char)ch); |
|
160 |
else if ('0' <= ch && ch <= '9') sbuf.append((char)ch); |
|
161 |
else if (ch == ' ' ) sbuf.append('+'); |
|
162 |
else if (ch == '-' || ch == '_' |
|
163 |
|| ch == '.' || ch == '!' |
|
164 |
|| ch == '~' || ch == '*' |
|
165 |
|| ch == '\'' || ch == '(' |
|
166 |
|| ch == ')' ) sbuf.append((char)ch); |
|
167 |
else if (ch <= 0x007f) sbuf.append(hex[ch]); |
|
168 |
else if (ch <= 0x07FF) |
|
169 |
{ |
|
170 |
sbuf.append(hex[0xc0 | (ch >> 6)]); |
|
171 |
sbuf.append(hex[0x80 | (ch & 0x3F)]); |
|
172 |
} |
|
173 |
else |
|
174 |
{ |
|
175 |
sbuf.append(hex[0xe0 | (ch >> 12)]); |
|
176 |
sbuf.append(hex[0x80 | ((ch >> 6) & 0x3F)]); |
|
177 |
sbuf.append(hex[0x80 | (ch & 0x3F)]); |
|
178 |
} |
|
179 |
} |
|
180 |
|
|
181 |
return sbuf.toString(); |
|
182 |
} |
|
183 |
|
|
108 | 184 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
109 | 185 |
|
110 | 186 |
private void doDownload() |
111 | 187 |
{ |
188 |
String veri = "distored"; |
|
189 |
String name = URLencode(veri); |
|
190 |
int numRuns = 1; |
|
191 |
String version = R.string.app_version+"d"; |
|
192 |
String message=URL+"/download.cgi?n="+name+"&r="+numRuns+"&e="+version; |
|
193 |
|
|
194 |
try |
|
195 |
{ |
|
196 |
java.net.URL connectURL = new URL(message); |
|
197 |
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); |
|
198 |
|
|
199 |
conn.setDoInput(true); |
|
200 |
conn.setDoOutput(true); |
|
201 |
conn.setUseCaches(false); |
|
202 |
conn.setRequestMethod("GET"); |
|
203 |
conn.connect(); |
|
204 |
conn.getOutputStream().flush(); |
|
205 |
|
|
206 |
InputStream is = null; |
|
207 |
|
|
208 |
try |
|
209 |
{ |
|
210 |
is = conn.getInputStream(); |
|
211 |
int ch; |
|
212 |
StringBuilder sb = new StringBuilder(); |
|
213 |
while( ( ch = is.read() ) != -1 ) |
|
214 |
{ |
|
215 |
sb.append( (char)ch ); |
|
216 |
} |
|
217 |
mScores = sb.toString(); |
|
218 |
} |
|
219 |
catch(Exception e) |
|
220 |
{ |
|
221 |
android.util.Log.e("downloader", "biffed it getting HTTPResponse"); |
|
222 |
} |
|
223 |
finally |
|
224 |
{ |
|
225 |
try |
|
226 |
{ |
|
227 |
if (is != null) |
|
228 |
is.close(); |
|
229 |
} |
|
230 |
catch (Exception e) {} |
|
231 |
} |
|
232 |
} |
|
233 |
catch( IOException ioe ) |
|
234 |
{ |
|
235 |
android.util.Log.e("downloader", "IOException: "+ioe.getMessage()); |
|
236 |
} |
|
237 |
catch( SecurityException se ) |
|
238 |
{ |
|
239 |
android.util.Log.e("downloader", "SecurityException: "+se.getMessage()); |
|
240 |
} |
|
241 |
|
|
242 |
/* |
|
112 | 243 |
mScores = |
113 | 244 |
|
114 | 245 |
"0 0 0 INEED7X7X7 1 rus" + "\n" + |
... | ... | |
191 | 322 |
"3 3 2 INEED7X7X7 24 rus" + "\n" + |
192 | 323 |
"3 3 3 RUBIK123 30 lit" + "\n" + |
193 | 324 |
"3 3 4 SKY16 31 usa"; |
325 |
|
|
326 |
*/ |
|
194 | 327 |
} |
195 | 328 |
|
196 | 329 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
... | ... | |
208 | 341 |
mScores = ""; |
209 | 342 |
} |
210 | 343 |
|
211 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
212 |
|
|
213 |
void abortNetworkTransaction() |
|
214 |
{ |
|
215 |
mRunning = false; |
|
216 |
} |
|
217 |
|
|
218 | 344 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
219 | 345 |
|
220 | 346 |
void download(Receiver receiver) |
221 | 347 |
{ |
222 | 348 |
mReceiver = receiver; |
223 | 349 |
mMode = DOWNLOAD; |
224 |
mNetworkThrd = new Thread(this);
|
|
225 |
mNetworkThrd.start();
|
|
350 |
Thread networkThrd = new Thread(this);
|
|
351 |
networkThrd.start();
|
|
226 | 352 |
} |
227 | 353 |
|
228 | 354 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
Also available in: Unified diff
Downloading High Scores: actually downloading scores!