From 2240532f0c0ef07099448edf1d2e940158742eee Mon Sep 17 00:00:00 2001 From: SnoopyCodeX Date: Sat, 25 Apr 2020 10:24:15 +0800 Subject: [PATCH] Removed Config class --- .../com/cdph/updatechecker/MainActivity.java | 9 +++---- cdph_updatechecker_lib/build.gradle | 2 +- .../main/java/com/cdph/app/UpdateChecker.java | 25 +++++++++++-------- .../java/com/cdph/app/json/JSONReader.java | 21 ++-------------- .../main/java/com/cdph/app/util/Config.java | 9 ------- 5 files changed, 21 insertions(+), 45 deletions(-) delete mode 100644 cdph_updatechecker_lib/src/main/java/com/cdph/app/util/Config.java diff --git a/cdph_updatechecker_app/src/main/java/com/cdph/updatechecker/MainActivity.java b/cdph_updatechecker_app/src/main/java/com/cdph/updatechecker/MainActivity.java index e00bc66..d438f9d 100644 --- a/cdph_updatechecker_app/src/main/java/com/cdph/updatechecker/MainActivity.java +++ b/cdph_updatechecker_app/src/main/java/com/cdph/updatechecker/MainActivity.java @@ -7,7 +7,6 @@ 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 { @@ -48,13 +47,13 @@ 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); + int versionCode = job.getInt("versionCode"); + String versionName = job.getString("versionName"); + String downloadUrl = job.getString("url"); String description = ""; //Parse 'description' as jsonArray then get the values - JSONArray jar = job.getJSONArray(Config.KEY_DESCRIPTION); + JSONArray jar = job.getJSONArray("description"); for(int i = 0; i < jar.length(); i++) description += jar.getString(i) + "\n"; description = description.substring(0, description.length()-1); diff --git a/cdph_updatechecker_lib/build.gradle b/cdph_updatechecker_lib/build.gradle index bee92f8..ee87c51 100644 --- a/cdph_updatechecker_lib/build.gradle +++ b/cdph_updatechecker_lib/build.gradle @@ -9,7 +9,7 @@ android { minSdkVersion 14 targetSdkVersion 21 versionCode 21 - versionName "21.1.0 - alpha8" + versionName "21.1.0w8y20a2" } buildTypes { release { diff --git a/cdph_updatechecker_lib/src/main/java/com/cdph/app/UpdateChecker.java b/cdph_updatechecker_lib/src/main/java/com/cdph/app/UpdateChecker.java index 2f5179b..db6ec8d 100644 --- a/cdph_updatechecker_lib/src/main/java/com/cdph/app/UpdateChecker.java +++ b/cdph_updatechecker_lib/src/main/java/com/cdph/app/UpdateChecker.java @@ -67,7 +67,7 @@ public static final UpdateChecker getInstance(Context ctx) * new updates when connected to the internet. * Default is false. * - *@param autoRun + *@param autoRun - Default is false *@return UpdateChecker.class */ public UpdateChecker shouldAutoRun(boolean autoRun) @@ -84,7 +84,7 @@ public UpdateChecker shouldAutoRun(boolean autoRun) * The url where the new info of the app * will be read to. * - *@param updateLogsUrl + *@param updateLogsUrl - The url of the json-encoded info of the new update *@return UpdateChecker.class */ public UpdateChecker setUpdateLogsUrl(String updateLogsUrl) @@ -98,7 +98,7 @@ public UpdateChecker setUpdateLogsUrl(String updateLogsUrl) * install the new app after it has been * downloaded. * - *@param autoInstall + *@param autoInstall - Default is false *@return UpdateChecker.class */ public UpdateChecker shouldAutoInstall(boolean autoInstall) @@ -111,7 +111,7 @@ public UpdateChecker shouldAutoInstall(boolean autoInstall) * Sets an OnUpdateDetectedListener, when a new update * is detected, this listener will be triggered. * - *@param listener + *@param listener - The listener to be triggered *@return UpdateChecker.class */ public UpdateChecker setOnUpdateDetectedListener(UpdateChecker.OnUpdateDetectedListener listener) @@ -123,7 +123,7 @@ public UpdateChecker setOnUpdateDetectedListener(UpdateChecker.OnUpdateDetectedL /* * Sets a custom json reader to suit your needs * - *@param jsonReader + *@param jsonReader - A custom class that extends {com.cdph.app.json.JSONReader} *@return UpdateChecker.class */ public UpdateChecker setJsonReader(T jsonReader) @@ -153,7 +153,7 @@ public void runUpdateChecker() /* * Installs the application * - *@param filePath + *@param filePath - The path of the apk to be installed *@return null */ public void installApp(String path) @@ -190,6 +190,7 @@ public File downloadUpdate(String url) file = down.execute(url).get(); } catch(Exception e) { e.printStackTrace(); + Toast.makeText(ctx, String.format("[ERROR]: %s", e.getMessage()), Toast.LENGTH_LONG).show(); } return file; @@ -229,9 +230,6 @@ protected void onPreExecute() { super.onPreExecute(); - if(jsonReader == null) - jsonReader = new JSONReader(); - dlg = new ProgressDialog(ctx); dlg.setCancelable(false); dlg.setCanceledOnTouchOutside(false); @@ -294,10 +292,10 @@ protected void onPostExecute(NewUpdateInfo result) if(ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0).versionCode < result.app_version) listener.onUpdateDetected(result, autoInstall); else - Toast.makeText(ctx, errMsg, Toast.LENGTH_LONG).show(); + Toast.makeText(ctx, String.format("[ERROR]: %s", errMsg), Toast.LENGTH_LONG).show(); } catch(Exception e) { e.printStackTrace(); - Toast.makeText(ctx, e.getMessage(), Toast.LENGTH_LONG).show(); + Toast.makeText(ctx, String.format("[ERROR]: %s", e.getMessage()), Toast.LENGTH_LONG).show(); } } } @@ -305,6 +303,7 @@ protected void onPostExecute(NewUpdateInfo result) private static final class TaskDownloadUpdate extends AsyncTask { private ProgressDialog dlg; + private String errMsg; @Override protected void onPreExecute() @@ -361,6 +360,7 @@ protected File doInBackground(String[] params) } } catch(Exception e) { e.printStackTrace(); + errMsg += e.getMessage(); } return file; @@ -373,6 +373,9 @@ protected void onPostExecute(File result) if(dlg != null) dlg.dismiss(); + + if(errMsg != null) + Toast.makeText(ctx, String.format("[ERROR]: %s", errMsg), Toast.LENGTH_LONG).show(); } } diff --git a/cdph_updatechecker_lib/src/main/java/com/cdph/app/json/JSONReader.java b/cdph_updatechecker_lib/src/main/java/com/cdph/app/json/JSONReader.java index 44da129..d23e5c1 100644 --- a/cdph_updatechecker_lib/src/main/java/com/cdph/app/json/JSONReader.java +++ b/cdph_updatechecker_lib/src/main/java/com/cdph/app/json/JSONReader.java @@ -4,25 +4,8 @@ import org.json.JSONObject; import com.cdph.app.UpdateChecker.NewUpdateInfo; -import com.cdph.app.util.Config; -public class JSONReader +public abstract 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)); - } + public abstract NewUpdateInfo readJson(String json) throws Exception } diff --git a/cdph_updatechecker_lib/src/main/java/com/cdph/app/util/Config.java b/cdph_updatechecker_lib/src/main/java/com/cdph/app/util/Config.java deleted file mode 100644 index dd02d95..0000000 --- a/cdph_updatechecker_lib/src/main/java/com/cdph/app/util/Config.java +++ /dev/null @@ -1,9 +0,0 @@ -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"; -}