-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the json parsing behaviour and added gson library for a more …
…easier json parsing.
- Loading branch information
1 parent
20b5bdc
commit fd4e119
Showing
7 changed files
with
189 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,78 @@ | ||
### JSON-based Update Checker | ||
[](https://depshield.github.io) | ||
|
||
> ☑ Customizeable | ||
--- | ||
> ☑ Easy to implement | ||
--- | ||
> ☑ JSON-based checker | ||
--- | ||
> ☑ Can download app | ||
--- | ||
> ☑ Can auto install app | ||
--- | ||
> ☑ Automatically check for updates | ||
# JSON-based Update Checker | ||
 | ||
--- | ||
> ☑ Lightweight library | ||
- [x] Customizeable | ||
- [x] Easy to implement | ||
- [x] JSON-based checker | ||
- [x] Can download app | ||
- [x] Can auto install app | ||
- [x] Automatically check for updates | ||
- [x] Lightweight library | ||
- [x] Uses Google's [Gson Library](https://github.com/google/gson) for json parsing | ||
--- | ||
Setup | ||
# Setup | ||
--- | ||
> Initialize UpdateChecker | ||
### Initialize UpdateChecker | ||
```java | ||
UpdateChecker checker = UpdateChecker.getInstance(context); | ||
|
||
``` | ||
--- | ||
> Set custom json reader | ||
### Set custom json reader | ||
```java | ||
checker.setJsonReader(new MyCustomJsonReader()); | ||
checker.setJsonModel(MyModel.class); | ||
|
||
private class MyCustomJsonReader extends JSONReader | ||
public class MyModel | ||
{ | ||
@Override | ||
public NewUpdateInfo readJson(String json) throws Exception | ||
{ | ||
JSONObject job = new JSONObject(json); | ||
int vcode = job.getInt("versionCode"); | ||
String vname = job.getString("versionName"); | ||
String url = job.getString("downloadUrl"); | ||
String desc = job.getString("description"); | ||
return (new NewUpdateInfo(url, desc, vname, vcode)); | ||
} | ||
@SerializedName("description") | ||
List<String> description; | ||
@SerializedName("name") | ||
String name; | ||
@SerializedName("version") | ||
String version; | ||
@SerializedName("downloadUrl") | ||
String downloadUrl; | ||
} | ||
``` | ||
--- | ||
> Enable auto update | ||
### Enable auto update | ||
```java | ||
checker.shouldAutoRun(true); | ||
``` | ||
--- | ||
> Enable update on both mobile networks | ||
### Enable update on both mobile networks | ||
```java | ||
checker.shouldCheckUpdateOnWifiOnly(false); | ||
``` | ||
--- | ||
> Enable auto installation | ||
### Enable auto installation | ||
```java | ||
checker.shouldAutoInstall(true); | ||
``` | ||
--- | ||
> Set the url of the json file | ||
### Set the url of the json file | ||
```java | ||
checker.setUpdateLogsUrl("https://urlhere"); | ||
``` | ||
--- | ||
> Downloading the app | ||
### Downloading the app | ||
```java | ||
//Returns the filepath of the downloaded app | ||
UpdateChecker.downloadUpdate("https://urlHere"); | ||
``` | ||
--- | ||
> Installing the app manually | ||
### Installing the app manually | ||
```java | ||
UpdateChecker.installApp(file); | ||
``` | ||
---- | ||
> Add listener | ||
### Add listener | ||
```java | ||
checker.setOnUpdateDetectedListener(new UpdateChecker.OnUpdateDetectedListener() { | ||
@Override | ||
public void onUpdateDetected(NewUpdateInfo info) | ||
public void onUpdateDetected(Object info) | ||
{} | ||
}); | ||
``` | ||
--- | ||
Support | ||
|
||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 53 additions & 46 deletions
99
cdph_updatechecker_app/src/main/java/com/cdph/updatechecker/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,80 @@ | ||
package com.cdph.updatechecker; | ||
|
||
import android.app.*; | ||
import android.content.*; | ||
import android.os.*; | ||
import org.json.*; | ||
import android.app.Activity; | ||
import android.app.AlertDialog; | ||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
import java.util.List; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import com.cdph.app.UpdateChecker; | ||
import com.cdph.app.UpdateChecker.NewUpdateInfo; | ||
import com.cdph.app.json.JSONReader; | ||
|
||
public class MainActivity extends Activity | ||
{ | ||
private TextView tv; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) | ||
{ | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.main); | ||
|
||
|
||
tv = findViewById(R.id.mainTextView); | ||
|
||
UpdateChecker.getInstance(this) | ||
.setUpdateLogsUrl("https://pastebin.com/raw/e3q1h4iQ") | ||
.setUpdateLogsUrl("https://pastebin.com/raw/x9JufEML") | ||
.shouldAutoRun(true) | ||
.shouldAutoInstall(true) | ||
.setJsonReader(new MyCustomJsonReader()) | ||
.setJsonModel(Model.class) | ||
.setOnUpdateDetectedListener(new UpdateChecker.OnUpdateDetectedListener() { | ||
@Override | ||
public void onUpdateDetected(final UpdateChecker.NewUpdateInfo info) | ||
public void onUpdateDetected(Object info) | ||
{ | ||
final AlertDialog dlg = new AlertDialog.Builder(MainActivity.this).create(); | ||
|
||
String msg = ""; | ||
msg += info.app_version + "\n"; | ||
msg += info.app_versionName + "\n"; | ||
msg += info.app_updateUrl + "\n"; | ||
msg += info.app_description; | ||
|
||
dlg.setTitle("New Update Detected"); | ||
dlg.setMessage(msg); | ||
dlg.setButton(AlertDialog.BUTTON1, "Update now", new DialogInterface.OnClickListener() { | ||
@Override | ||
public void onClick(DialogInterface di, int btn) | ||
try { | ||
Model model = (Model) info; | ||
String str_curVer = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; | ||
String str_newVer = model.version; | ||
|
||
if(UpdateChecker.compareVersion(str_curVer, str_newVer)) | ||
{ | ||
dlg.dismiss(); | ||
UpdateChecker.downloadUpdate("https://github.com/SnoopyCodeX/binarymatrixandroid/raw/master/binarymatrix_lwp/app/build/bin/app.apk", "cdph_updatechecker_app.apk"); | ||
String txt = String.format("Name: %s\nVersion: %s\nDownload: %s\nDescription: %s", | ||
model.name, | ||
model.version, | ||
model.downloadUrl, | ||
model.description.get(0) | ||
); | ||
|
||
AlertDialog dlg = new AlertDialog.Builder(MainActivity.this).create(); | ||
dlg.setCancelable(true); | ||
dlg.setCanceledOnTouchOutside(false); | ||
dlg.setMessage(txt); | ||
dlg.setTitle("Update Available"); | ||
dlg.show(); | ||
} | ||
}); | ||
dlg.show(); | ||
else | ||
Toast.makeText(MainActivity.this, "You have the latest version!", Toast.LENGTH_LONG).show(); | ||
} catch(Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
}); | ||
}) | ||
.runUpdateChecker(); | ||
} | ||
private class MyCustomJsonReader extends JSONReader | ||
|
||
public static final class Model | ||
{ | ||
@Override | ||
public NewUpdateInfo readJson(String json) throws Exception | ||
{ | ||
//Parse as jsonObject then get the values | ||
JSONObject job = new JSONObject(json); | ||
int versionCode = job.getInt("versionCode"); | ||
String versionName = job.getString("versionName"); | ||
String downloadUrl = job.getString("url"); | ||
String description = ""; | ||
@SerializedName("description") | ||
List<String> description; | ||
|
||
@SerializedName("version") | ||
String version; | ||
|
||
//Parse 'description' as jsonArray then get the values | ||
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); | ||
@SerializedName("name") | ||
String name; | ||
|
||
return (new NewUpdateInfo(downloadUrl, versionName, description, versionCode)); | ||
} | ||
@SerializedName("downloadUrl") | ||
String downloadUrl; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.