1818import android .net .ConnectivityManager ;
1919import android .net .Uri ;
2020import android .os .AsyncTask ;
21+ import android .os .Build ;
22+ import android .os .Environment ;
2123import android .widget .Toast ;
24+ import java .io .DataInputStream ;
2225import java .io .File ;
26+ import java .io .FileOutputStream ;
2327import java .io .InputStream ;
2428import java .net .HttpURLConnection ;
2529import 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 ;
2834
2935public final class UpdateChecker
3036{
3137 private static OnUpdateDetectedListener listener ;
3238 private static String updateLogUrl = "" ;
3339 private static boolean autoRun = false , autoInstall = false ;
40+ private static JSONReader jsonReader ;
3441 private static Context ctx ;
3542
3643 /*
@@ -63,7 +70,7 @@ public static final UpdateChecker getInstance(Context ctx)
6370 *@param autoRun
6471 *@return UpdateChecker.class
6572 */
66- public UpdateChecker shouldRunWhenConnected (boolean autoRun )
73+ public UpdateChecker shouldAutoRun (boolean autoRun )
6774 {
6875 this .autoRun = autoRun ;
6976
@@ -113,6 +120,18 @@ public UpdateChecker setOnUpdateDetectedListener(UpdateChecker.OnUpdateDetectedL
113120 return this ;
114121 }
115122
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+
116135 /*
117136 * Runs the update checker
118137 *
@@ -153,6 +172,29 @@ public void installApp(String path)
153172 }
154173 }
155174
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+
156198 public static interface OnUpdateDetectedListener
157199 {
158200 public void onUpdateDetected (NewUpdateInfo info , boolean autoInstall )
@@ -186,9 +228,13 @@ protected void onPreExecute()
186228 {
187229 super .onPreExecute ();
188230
231+ if (jsonReader == null )
232+ jsonReader = new JSONReader ();
233+
189234 dlg = new ProgressDialog (ctx );
190235 dlg .setCancelable (false );
191236 dlg .setCanceledOnTouchOutside (false );
237+ dlg .setProgressDrawable (ctx .getResources ().getDrawable (android .R .drawable .progress_horizontal ));
192238 dlg .setProgressStyle (ProgressDialog .STYLE_SPINNER );
193239 dlg .setMessage ("Checking for new update..." );
194240 dlg .show ();
@@ -221,20 +267,8 @@ protected NewUpdateInfo doInBackground(String... params)
221267 json += new String (buffer , 0 , len );
222268 is .close ();
223269
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 );
238272 }
239273
240274 conn .disconnect ();
@@ -280,6 +314,80 @@ private String sanitizeUrl(String url)
280314 }
281315 }
282316
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+
283391 private static final class ConnectivityReceiver extends BroadcastReceiver
284392 {
285393 @ Override
@@ -295,12 +403,4 @@ public static final boolean isConnected(Context ctx)
295403 return (cm .getActiveNetworkInfo () != null && cm .getActiveNetworkInfo ().getType () == ConnectivityManager .TYPE_WIFI && cm .getActiveNetworkInfo ().isConnected ());
296404 }
297405 }
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- }
306406}
0 commit comments