Skip to content

Commit 1791be1

Browse files
committed
Added Download & Custom JSONReader support
1 parent d0e37db commit 1791be1

File tree

5 files changed

+192
-28
lines changed

5 files changed

+192
-28
lines changed

cdph_updatechecker_app/src/main/java/com/cdph/updatechecker/MainActivity.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
import android.app.*;
44
import android.os.*;
5+
import org.json.*;
56

67
import com.cdph.app.UpdateChecker;
8+
import com.cdph.app.UpdateChecker.NewUpdateInfo;
9+
import com.cdph.app.json.JSONReader;
10+
import com.cdph.app.util.Config;
711

812
public class MainActivity extends Activity
913
{
@@ -15,8 +19,9 @@ protected void onCreate(Bundle savedInstanceState)
1519

1620
UpdateChecker.getInstance(this)
1721
.setUpdateLogsUrl("https://pastebin.com/raw/SFpLs0De")
18-
.shouldRunWhenConnected(true)
22+
.shouldAutoRun(true)
1923
.shouldAutoInstall(true)
24+
.setJsonReader(new MyCustomJsonReader())
2025
.setOnUpdateDetectedListener(new UpdateChecker.OnUpdateDetectedListener() {
2126
@Override
2227
public void onUpdateDetected(UpdateChecker.NewUpdateInfo info, boolean autoInstall)
@@ -35,4 +40,26 @@ public void onUpdateDetected(UpdateChecker.NewUpdateInfo info, boolean autoInsta
3540
}
3641
});
3742
}
43+
44+
private class MyCustomJsonReader extends JSONReader
45+
{
46+
@Override
47+
public NewUpdateInfo readJson(String json) throws Exception
48+
{
49+
//Parse as jsonObject then get the values
50+
JSONObject job = new JSONObject(json);
51+
int versionCode = job.getInt(Config.KEY_VERSION_CODE);
52+
String versionName = job.getString(Config.KEY_VERSION_NAME);
53+
String downloadUrl = job.getString(Config.KEY_DOWNLOAD_URL);
54+
String description = "";
55+
56+
//Parse 'description' as jsonArray then get the values
57+
JSONArray jar = job.getJSONArray(Config.KEY_DESCRIPTION);
58+
for(int i = 0; i < jar.length(); i++)
59+
description += jar.getString(i) + "\n";
60+
description = description.substring(0, description.length()-1);
61+
62+
return (new NewUpdateInfo(downloadUrl, versionName, description, versionCode));
63+
}
64+
}
3865
}

cdph_updatechecker_lib/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ android {
88
applicationId "com.cdph.app"
99
minSdkVersion 14
1010
targetSdkVersion 21
11-
versionCode 1
12-
versionName "1.0"
11+
versionCode 21
12+
versionName "21.1.0 - alpha8"
1313
}
1414
buildTypes {
1515
release {

cdph_updatechecker_lib/src/main/java/com/cdph/app/UpdateChecker.java

Lines changed: 125 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,26 @@
1818
import android.net.ConnectivityManager;
1919
import android.net.Uri;
2020
import android.os.AsyncTask;
21+
import android.os.Build;
22+
import android.os.Environment;
2123
import android.widget.Toast;
24+
import java.io.DataInputStream;
2225
import java.io.File;
26+
import java.io.FileOutputStream;
2327
import java.io.InputStream;
2428
import java.net.HttpURLConnection;
2529
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;
2834

2935
public 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
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.cdph.app.json;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
import com.cdph.app.UpdateChecker.NewUpdateInfo;
7+
import com.cdph.app.util.Config;
8+
9+
public class JSONReader
10+
{
11+
public NewUpdateInfo readJson(String json) throws Exception
12+
{
13+
//Parse as jsonObject then get the values
14+
JSONObject job = new JSONObject(json);
15+
int versionCode = job.getInt(Config.KEY_VERSION_CODE);
16+
String versionName = job.getString(Config.KEY_VERSION_NAME);
17+
String downloadUrl = job.getString(Config.KEY_DOWNLOAD_URL);
18+
String description = "";
19+
20+
//Parse 'description' as jsonArray then get the values
21+
JSONArray jar = job.getJSONArray(Config.KEY_DESCRIPTION);
22+
for(int i = 0; i < jar.length(); i++)
23+
description += jar.getString(i) + "\n";
24+
description = description.substring(0, description.length()-1);
25+
26+
return (new NewUpdateInfo(downloadUrl, versionName, description, versionCode));
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.cdph.app.util;
2+
3+
public final class Config
4+
{
5+
public static final String KEY_VERSION_CODE = "versionCode";
6+
public static final String KEY_VERSION_NAME = "versionName";
7+
public static final String KEY_DOWNLOAD_URL = "url";
8+
public static final String KEY_DESCRIPTION = "description";
9+
}

0 commit comments

Comments
 (0)