Skip to content

Commit

Permalink
Added Download & Custom JSONReader support
Browse files Browse the repository at this point in the history
  • Loading branch information
SnoopyCodeX committed Apr 24, 2020
1 parent d0e37db commit 1791be1
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import android.app.*;
import android.os.*;
import org.json.*;

import com.cdph.app.UpdateChecker;
import com.cdph.app.UpdateChecker.NewUpdateInfo;
import com.cdph.app.json.JSONReader;
import com.cdph.app.util.Config;

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

UpdateChecker.getInstance(this)
.setUpdateLogsUrl("https://pastebin.com/raw/SFpLs0De")
.shouldRunWhenConnected(true)
.shouldAutoRun(true)
.shouldAutoInstall(true)
.setJsonReader(new MyCustomJsonReader())
.setOnUpdateDetectedListener(new UpdateChecker.OnUpdateDetectedListener() {
@Override
public void onUpdateDetected(UpdateChecker.NewUpdateInfo info, boolean autoInstall)
Expand All @@ -35,4 +40,26 @@ public void onUpdateDetected(UpdateChecker.NewUpdateInfo info, boolean autoInsta
}
});
}

private class MyCustomJsonReader extends JSONReader
{
@Override
public NewUpdateInfo readJson(String json) throws Exception
{
//Parse as jsonObject then get the values
JSONObject job = new JSONObject(json);
int versionCode = job.getInt(Config.KEY_VERSION_CODE);
String versionName = job.getString(Config.KEY_VERSION_NAME);
String downloadUrl = job.getString(Config.KEY_DOWNLOAD_URL);
String description = "";

//Parse 'description' as jsonArray then get the values
JSONArray jar = job.getJSONArray(Config.KEY_DESCRIPTION);
for(int i = 0; i < jar.length(); i++)
description += jar.getString(i) + "\n";
description = description.substring(0, description.length()-1);

return (new NewUpdateInfo(downloadUrl, versionName, description, versionCode));
}
}
}
4 changes: 2 additions & 2 deletions cdph_updatechecker_lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.cdph.app"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
versionCode 21
versionName "21.1.0 - alpha8"
}
buildTypes {
release {
Expand Down
150 changes: 125 additions & 25 deletions cdph_updatechecker_lib/src/main/java/com/cdph/app/UpdateChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Environment;
import android.widget.Toast;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONArray;
import org.json.JSONObject;
import java.net.URLConnection;
import java.util.Objects;

import com.cdph.app.json.JSONReader;

public final class UpdateChecker
{
private static OnUpdateDetectedListener listener;
private static String updateLogUrl = "";
private static boolean autoRun = false, autoInstall = false;
private static JSONReader jsonReader;
private static Context ctx;

/*
Expand Down Expand Up @@ -63,7 +70,7 @@ public static final UpdateChecker getInstance(Context ctx)
*@param autoRun
*@return UpdateChecker.class
*/
public UpdateChecker shouldRunWhenConnected(boolean autoRun)
public UpdateChecker shouldAutoRun(boolean autoRun)
{
this.autoRun = autoRun;

Expand Down Expand Up @@ -113,6 +120,18 @@ public UpdateChecker setOnUpdateDetectedListener(UpdateChecker.OnUpdateDetectedL
return this;
}

/*
* Sets a custom json reader to suit your needs
*
*@param jsonReader
*@return UpdateChecker.class
*/
public <T extends JSONReader> UpdateChecker setJsonReader(T jsonReader)
{
this.jsonReader = jsonReader;
return this;
}

/*
* Runs the update checker
*
Expand Down Expand Up @@ -153,6 +172,29 @@ public void installApp(String path)
}
}

/*
* Downloads the file from the url
*
*@param url - The download url
*@return file - The downloaded file
*/
public File downloadUpdate(String url)
{
File file = null;

if(!ConnectivityReceiver.isConnected(ctx))
return file;

try {
TaskDownloadUpdate down = new TaskDownloadUpdate();
file = down.execute(url).get();
} catch(Exception e) {
e.printStackTrace();
}

return file;
}

public static interface OnUpdateDetectedListener
{
public void onUpdateDetected(NewUpdateInfo info, boolean autoInstall)
Expand Down Expand Up @@ -186,9 +228,13 @@ protected void onPreExecute()
{
super.onPreExecute();

if(jsonReader == null)
jsonReader = new JSONReader();

dlg = new ProgressDialog(ctx);
dlg.setCancelable(false);
dlg.setCanceledOnTouchOutside(false);
dlg.setProgressDrawable(ctx.getResources().getDrawable(android.R.drawable.progress_horizontal));
dlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dlg.setMessage("Checking for new update...");
dlg.show();
Expand Down Expand Up @@ -221,20 +267,8 @@ protected NewUpdateInfo doInBackground(String... params)
json += new String(buffer, 0, len);
is.close();

//Parse as jsonObject then get the values
JSONObject job = new JSONObject(json);
int versionCode = job.getInt(Config.KEY_VERSION_CODE);
String versionName = job.getString(Config.KEY_VERSION_NAME);
String downloadUrl = job.getString(Config.KEY_DOWNLOAD_URL);
String description = "";

//Parse 'description' as jsonArray then get the values
JSONArray jar = job.getJSONArray(Config.KEY_DESCRIPTION);
for(int i = 0; i < jar.length(); i++)
description += jar.getString(i) + "\n";
description = description.substring(0, description.length()-1);

info = new NewUpdateInfo(downloadUrl, versionName, description, versionCode);
//Read json
info = jsonReader.readJson(json);
}

conn.disconnect();
Expand Down Expand Up @@ -280,6 +314,80 @@ private String sanitizeUrl(String url)
}
}

private static final class TaskDownloadUpdate extends AsyncTask<String, Void, File>
{
private ProgressDialog dlg;

@Override
protected void onPreExecute()
{
super.onPreExecute();

dlg = new ProgressDialog(ctx);
dlg.setCancelable(false);
dlg.setCanceledOnTouchOutside(false);
dlg.setProgressDrawable(ctx.getResources().getDrawable(android.R.drawable.progress_horizontal));
dlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dlg.setMessage("Downloading update...");
dlg.show();
}

@Override
protected File doInBackground(String[] params)
{
File file = null;

try {
String str_url = params[0];
String str_dir = "/Android/.temp";

File downDir = new File(Environment.getExternalStorageDirectory(), str_dir);
File downApk = new File(downDir, "/update.apk");

if(downApk.exists())
downApk.delete();

if(downDir.exists())
downDir.delete();

downDir.mkdir();
downApk.createNewFile();

URL url = new URL(str_url);
URLConnection conn = url.openConnection();
int len = conn.getContentLength();

DataInputStream dis = new DataInputStream(url.openStream());
byte[] buffer = new byte[len];
dis.readFully(buffer);
dis.close();

if(buffer.length > 0)
{
FileOutputStream fos = ctx.openFileOutput(downApk.getAbsolutePath(), Context.MODE_PRIVATE);
fos.write(buffer);
fos.flush();
fos.close();

file = downApk;
}
} catch(Exception e) {
e.printStackTrace();
}

return file;
}

@Override
protected void onPostExecute(File result)
{
super.onPostExecute(result);

if(dlg != null)
dlg.dismiss();
}
}

private static final class ConnectivityReceiver extends BroadcastReceiver
{
@Override
Expand All @@ -295,12 +403,4 @@ public static final boolean isConnected(Context ctx)
return (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI && cm.getActiveNetworkInfo().isConnected());
}
}

private class Config
{
public static final String KEY_VERSION_CODE = "versionCode";
public static final String KEY_VERSION_NAME = "versionName";
public static final String KEY_DOWNLOAD_URL = "url";
public static final String KEY_DESCRIPTION = "description";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.cdph.app.json;

import org.json.JSONArray;
import org.json.JSONObject;

import com.cdph.app.UpdateChecker.NewUpdateInfo;
import com.cdph.app.util.Config;

public class JSONReader
{
public NewUpdateInfo readJson(String json) throws Exception
{
//Parse as jsonObject then get the values
JSONObject job = new JSONObject(json);
int versionCode = job.getInt(Config.KEY_VERSION_CODE);
String versionName = job.getString(Config.KEY_VERSION_NAME);
String downloadUrl = job.getString(Config.KEY_DOWNLOAD_URL);
String description = "";

//Parse 'description' as jsonArray then get the values
JSONArray jar = job.getJSONArray(Config.KEY_DESCRIPTION);
for(int i = 0; i < jar.length(); i++)
description += jar.getString(i) + "\n";
description = description.substring(0, description.length()-1);

return (new NewUpdateInfo(downloadUrl, versionName, description, versionCode));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cdph.app.util;

public final class Config
{
public static final String KEY_VERSION_CODE = "versionCode";
public static final String KEY_VERSION_NAME = "versionName";
public static final String KEY_DOWNLOAD_URL = "url";
public static final String KEY_DESCRIPTION = "description";
}

0 comments on commit 1791be1

Please sign in to comment.