18
18
import android .net .ConnectivityManager ;
19
19
import android .net .Uri ;
20
20
import android .os .AsyncTask ;
21
+ import android .os .Build ;
22
+ import android .os .Environment ;
21
23
import android .widget .Toast ;
24
+ import java .io .DataInputStream ;
22
25
import java .io .File ;
26
+ import java .io .FileOutputStream ;
23
27
import java .io .InputStream ;
24
28
import java .net .HttpURLConnection ;
25
29
import java .net .URL ;
26
- import org .json .JSONArray ;
27
- import org .json .JSONObject ;
30
+ import java .net .URLConnection ;
31
+ import java .util .Objects ;
32
+
33
+ import com .cdph .app .json .JSONReader ;
28
34
29
35
public final class UpdateChecker
30
36
{
31
37
private static OnUpdateDetectedListener listener ;
32
38
private static String updateLogUrl = "" ;
33
39
private static boolean autoRun = false , autoInstall = false ;
40
+ private static JSONReader jsonReader ;
34
41
private static Context ctx ;
35
42
36
43
/*
@@ -63,7 +70,7 @@ public static final UpdateChecker getInstance(Context ctx)
63
70
*@param autoRun
64
71
*@return UpdateChecker.class
65
72
*/
66
- public UpdateChecker shouldRunWhenConnected (boolean autoRun )
73
+ public UpdateChecker shouldAutoRun (boolean autoRun )
67
74
{
68
75
this .autoRun = autoRun ;
69
76
@@ -113,6 +120,18 @@ public UpdateChecker setOnUpdateDetectedListener(UpdateChecker.OnUpdateDetectedL
113
120
return this ;
114
121
}
115
122
123
+ /*
124
+ * Sets a custom json reader to suit your needs
125
+ *
126
+ *@param jsonReader
127
+ *@return UpdateChecker.class
128
+ */
129
+ public <T extends JSONReader > UpdateChecker setJsonReader (T jsonReader )
130
+ {
131
+ this .jsonReader = jsonReader ;
132
+ return this ;
133
+ }
134
+
116
135
/*
117
136
* Runs the update checker
118
137
*
@@ -153,6 +172,29 @@ public void installApp(String path)
153
172
}
154
173
}
155
174
175
+ /*
176
+ * Downloads the file from the url
177
+ *
178
+ *@param url - The download url
179
+ *@return file - The downloaded file
180
+ */
181
+ public File downloadUpdate (String url )
182
+ {
183
+ File file = null ;
184
+
185
+ if (!ConnectivityReceiver .isConnected (ctx ))
186
+ return file ;
187
+
188
+ try {
189
+ TaskDownloadUpdate down = new TaskDownloadUpdate ();
190
+ file = down .execute (url ).get ();
191
+ } catch (Exception e ) {
192
+ e .printStackTrace ();
193
+ }
194
+
195
+ return file ;
196
+ }
197
+
156
198
public static interface OnUpdateDetectedListener
157
199
{
158
200
public void onUpdateDetected (NewUpdateInfo info , boolean autoInstall )
@@ -186,9 +228,13 @@ protected void onPreExecute()
186
228
{
187
229
super .onPreExecute ();
188
230
231
+ if (jsonReader == null )
232
+ jsonReader = new JSONReader ();
233
+
189
234
dlg = new ProgressDialog (ctx );
190
235
dlg .setCancelable (false );
191
236
dlg .setCanceledOnTouchOutside (false );
237
+ dlg .setProgressDrawable (ctx .getResources ().getDrawable (android .R .drawable .progress_horizontal ));
192
238
dlg .setProgressStyle (ProgressDialog .STYLE_SPINNER );
193
239
dlg .setMessage ("Checking for new update..." );
194
240
dlg .show ();
@@ -221,20 +267,8 @@ protected NewUpdateInfo doInBackground(String... params)
221
267
json += new String (buffer , 0 , len );
222
268
is .close ();
223
269
224
- //Parse as jsonObject then get the values
225
- JSONObject job = new JSONObject (json );
226
- int versionCode = job .getInt (Config .KEY_VERSION_CODE );
227
- String versionName = job .getString (Config .KEY_VERSION_NAME );
228
- String downloadUrl = job .getString (Config .KEY_DOWNLOAD_URL );
229
- String description = "" ;
230
-
231
- //Parse 'description' as jsonArray then get the values
232
- JSONArray jar = job .getJSONArray (Config .KEY_DESCRIPTION );
233
- for (int i = 0 ; i < jar .length (); i ++)
234
- description += jar .getString (i ) + "\n " ;
235
- description = description .substring (0 , description .length ()-1 );
236
-
237
- info = new NewUpdateInfo (downloadUrl , versionName , description , versionCode );
270
+ //Read json
271
+ info = jsonReader .readJson (json );
238
272
}
239
273
240
274
conn .disconnect ();
@@ -280,6 +314,80 @@ private String sanitizeUrl(String url)
280
314
}
281
315
}
282
316
317
+ private static final class TaskDownloadUpdate extends AsyncTask <String , Void , File >
318
+ {
319
+ private ProgressDialog dlg ;
320
+
321
+ @ Override
322
+ protected void onPreExecute ()
323
+ {
324
+ super .onPreExecute ();
325
+
326
+ dlg = new ProgressDialog (ctx );
327
+ dlg .setCancelable (false );
328
+ dlg .setCanceledOnTouchOutside (false );
329
+ dlg .setProgressDrawable (ctx .getResources ().getDrawable (android .R .drawable .progress_horizontal ));
330
+ dlg .setProgressStyle (ProgressDialog .STYLE_SPINNER );
331
+ dlg .setMessage ("Downloading update..." );
332
+ dlg .show ();
333
+ }
334
+
335
+ @ Override
336
+ protected File doInBackground (String [] params )
337
+ {
338
+ File file = null ;
339
+
340
+ try {
341
+ String str_url = params [0 ];
342
+ String str_dir = "/Android/.temp" ;
343
+
344
+ File downDir = new File (Environment .getExternalStorageDirectory (), str_dir );
345
+ File downApk = new File (downDir , "/update.apk" );
346
+
347
+ if (downApk .exists ())
348
+ downApk .delete ();
349
+
350
+ if (downDir .exists ())
351
+ downDir .delete ();
352
+
353
+ downDir .mkdir ();
354
+ downApk .createNewFile ();
355
+
356
+ URL url = new URL (str_url );
357
+ URLConnection conn = url .openConnection ();
358
+ int len = conn .getContentLength ();
359
+
360
+ DataInputStream dis = new DataInputStream (url .openStream ());
361
+ byte [] buffer = new byte [len ];
362
+ dis .readFully (buffer );
363
+ dis .close ();
364
+
365
+ if (buffer .length > 0 )
366
+ {
367
+ FileOutputStream fos = ctx .openFileOutput (downApk .getAbsolutePath (), Context .MODE_PRIVATE );
368
+ fos .write (buffer );
369
+ fos .flush ();
370
+ fos .close ();
371
+
372
+ file = downApk ;
373
+ }
374
+ } catch (Exception e ) {
375
+ e .printStackTrace ();
376
+ }
377
+
378
+ return file ;
379
+ }
380
+
381
+ @ Override
382
+ protected void onPostExecute (File result )
383
+ {
384
+ super .onPostExecute (result );
385
+
386
+ if (dlg != null )
387
+ dlg .dismiss ();
388
+ }
389
+ }
390
+
283
391
private static final class ConnectivityReceiver extends BroadcastReceiver
284
392
{
285
393
@ Override
@@ -295,12 +403,4 @@ public static final boolean isConnected(Context ctx)
295
403
return (cm .getActiveNetworkInfo () != null && cm .getActiveNetworkInfo ().getType () == ConnectivityManager .TYPE_WIFI && cm .getActiveNetworkInfo ().isConnected ());
296
404
}
297
405
}
298
-
299
- private class Config
300
- {
301
- public static final String KEY_VERSION_CODE = "versionCode" ;
302
- public static final String KEY_VERSION_NAME = "versionName" ;
303
- public static final String KEY_DOWNLOAD_URL = "url" ;
304
- public static final String KEY_DESCRIPTION = "description" ;
305
- }
306
406
}
0 commit comments