Skip to content

Commit fd4e119

Browse files
committed
Changed the json parsing behaviour and added gson library for a more easier json parsing.
1 parent 20b5bdc commit fd4e119

File tree

7 files changed

+189
-143
lines changed

7 files changed

+189
-143
lines changed

README.md

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,78 @@
1-
### JSON-based Update Checker
2-
[![DepShield Badge](https://depshield.sonatype.org/badges/SnoopyCodeX/repository/depshield.svg)](https://depshield.github.io)
3-
4-
> ☑ Customizeable
5-
---
6-
> ☑ Easy to implement
7-
---
8-
> ☑ JSON-based checker
9-
---
10-
> ☑ Can download app
11-
---
12-
> ☑ Can auto install app
13-
---
14-
> ☑ Automatically check for updates
1+
# JSON-based Update Checker
2+
![DepShield Badge](https://depshield.sonatype.org/badges/SnoopyCodeX/repository/depshield.svg)
153
---
16-
> ☑ Lightweight library
4+
- [x] Customizeable
5+
- [x] Easy to implement
6+
- [x] JSON-based checker
7+
- [x] Can download app
8+
- [x] Can auto install app
9+
- [x] Automatically check for updates
10+
- [x] Lightweight library
11+
- [x] Uses Google's [Gson Library](https://github.com/google/gson) for json parsing
1712
---
18-
Setup
13+
# Setup
1914
---
20-
> Initialize UpdateChecker
15+
### Initialize UpdateChecker
2116
```java
2217
UpdateChecker checker = UpdateChecker.getInstance(context);
23-
2418
```
2519
---
26-
> Set custom json reader
20+
### Set custom json reader
2721
```java
28-
checker.setJsonReader(new MyCustomJsonReader());
22+
checker.setJsonModel(MyModel.class);
2923

30-
private class MyCustomJsonReader extends JSONReader
24+
public class MyModel
3125
{
32-
@Override
33-
public NewUpdateInfo readJson(String json) throws Exception
34-
{
35-
JSONObject job = new JSONObject(json);
36-
int vcode = job.getInt("versionCode");
37-
String vname = job.getString("versionName");
38-
String url = job.getString("downloadUrl");
39-
String desc = job.getString("description");
40-
41-
return (new NewUpdateInfo(url, desc, vname, vcode));
42-
}
26+
@SerializedName("description")
27+
List<String> description;
28+
29+
@SerializedName("name")
30+
String name;
31+
32+
@SerializedName("version")
33+
String version;
34+
35+
@SerializedName("downloadUrl")
36+
String downloadUrl;
4337
}
4438
```
4539
---
46-
> Enable auto update
40+
### Enable auto update
4741
```java
4842
checker.shouldAutoRun(true);
4943
```
5044
---
51-
> Enable update on both mobile networks
45+
### Enable update on both mobile networks
5246
```java
5347
checker.shouldCheckUpdateOnWifiOnly(false);
5448
```
5549
---
56-
> Enable auto installation
50+
### Enable auto installation
5751
```java
5852
checker.shouldAutoInstall(true);
5953
```
6054
---
61-
> Set the url of the json file
55+
### Set the url of the json file
6256
```java
6357
checker.setUpdateLogsUrl("https://urlhere");
6458
```
6559
---
66-
> Downloading the app
60+
### Downloading the app
6761
```java
6862
//Returns the filepath of the downloaded app
6963
UpdateChecker.downloadUpdate("https://urlHere");
7064
```
7165
---
72-
> Installing the app manually
66+
### Installing the app manually
7367
```java
7468
UpdateChecker.installApp(file);
7569
```
7670
----
77-
> Add listener
71+
### Add listener
7872
```java
7973
checker.setOnUpdateDetectedListener(new UpdateChecker.OnUpdateDetectedListener() {
8074
@Override
81-
public void onUpdateDetected(NewUpdateInfo info)
75+
public void onUpdateDetected(Object info)
8276
{}
8377
});
8478
```
85-
---
86-
Support
87-
88-
---

cdph_updatechecker_app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ android {
2121

2222
dependencies {
2323
compile project(':cdph_updatechecker_lib')
24+
compile 'com.google.code.gson:gson:2.8.5'
2425
compile fileTree(dir: 'libs', include: ['*.jar'])
2526
}
Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,80 @@
11
package com.cdph.updatechecker;
22

3-
import android.app.*;
4-
import android.content.*;
5-
import android.os.*;
6-
import org.json.*;
3+
import android.app.Activity;
4+
import android.app.AlertDialog;
5+
import android.content.Context;
6+
import android.os.Bundle;
7+
import android.widget.TextView;
8+
import android.widget.Toast;
9+
import java.util.List;
710

11+
import com.google.gson.annotations.SerializedName;
812
import com.cdph.app.UpdateChecker;
9-
import com.cdph.app.UpdateChecker.NewUpdateInfo;
10-
import com.cdph.app.json.JSONReader;
1113

1214
public class MainActivity extends Activity
1315
{
16+
private TextView tv;
17+
1418
@Override
1519
protected void onCreate(Bundle savedInstanceState)
1620
{
1721
super.onCreate(savedInstanceState);
1822
setContentView(R.layout.main);
19-
23+
24+
tv = findViewById(R.id.mainTextView);
25+
2026
UpdateChecker.getInstance(this)
21-
.setUpdateLogsUrl("https://pastebin.com/raw/e3q1h4iQ")
27+
.setUpdateLogsUrl("https://pastebin.com/raw/x9JufEML")
2228
.shouldAutoRun(true)
2329
.shouldAutoInstall(true)
24-
.setJsonReader(new MyCustomJsonReader())
30+
.setJsonModel(Model.class)
2531
.setOnUpdateDetectedListener(new UpdateChecker.OnUpdateDetectedListener() {
2632
@Override
27-
public void onUpdateDetected(final UpdateChecker.NewUpdateInfo info)
33+
public void onUpdateDetected(Object info)
2834
{
29-
final AlertDialog dlg = new AlertDialog.Builder(MainActivity.this).create();
30-
31-
String msg = "";
32-
msg += info.app_version + "\n";
33-
msg += info.app_versionName + "\n";
34-
msg += info.app_updateUrl + "\n";
35-
msg += info.app_description;
36-
37-
dlg.setTitle("New Update Detected");
38-
dlg.setMessage(msg);
39-
dlg.setButton(AlertDialog.BUTTON1, "Update now", new DialogInterface.OnClickListener() {
40-
@Override
41-
public void onClick(DialogInterface di, int btn)
35+
try {
36+
Model model = (Model) info;
37+
String str_curVer = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
38+
String str_newVer = model.version;
39+
40+
if(UpdateChecker.compareVersion(str_curVer, str_newVer))
4241
{
43-
dlg.dismiss();
44-
UpdateChecker.downloadUpdate("https://github.com/SnoopyCodeX/binarymatrixandroid/raw/master/binarymatrix_lwp/app/build/bin/app.apk", "cdph_updatechecker_app.apk");
42+
String txt = String.format("Name: %s\nVersion: %s\nDownload: %s\nDescription: %s",
43+
model.name,
44+
model.version,
45+
model.downloadUrl,
46+
model.description.get(0)
47+
);
48+
49+
AlertDialog dlg = new AlertDialog.Builder(MainActivity.this).create();
50+
dlg.setCancelable(true);
51+
dlg.setCanceledOnTouchOutside(false);
52+
dlg.setMessage(txt);
53+
dlg.setTitle("Update Available");
54+
dlg.show();
4555
}
46-
});
47-
dlg.show();
56+
else
57+
Toast.makeText(MainActivity.this, "You have the latest version!", Toast.LENGTH_LONG).show();
58+
} catch(Exception e) {
59+
e.printStackTrace();
60+
}
4861
}
49-
});
62+
})
63+
.runUpdateChecker();
5064
}
51-
52-
private class MyCustomJsonReader extends JSONReader
65+
66+
public static final class Model
5367
{
54-
@Override
55-
public NewUpdateInfo readJson(String json) throws Exception
56-
{
57-
//Parse as jsonObject then get the values
58-
JSONObject job = new JSONObject(json);
59-
int versionCode = job.getInt("versionCode");
60-
String versionName = job.getString("versionName");
61-
String downloadUrl = job.getString("url");
62-
String description = "";
68+
@SerializedName("description")
69+
List<String> description;
70+
71+
@SerializedName("version")
72+
String version;
6373

64-
//Parse 'description' as jsonArray then get the values
65-
JSONArray jar = job.getJSONArray("description");
66-
for(int i = 0; i < jar.length(); i++)
67-
description += jar.getString(i) + "\n";
68-
description = description.substring(0, description.length()-1);
74+
@SerializedName("name")
75+
String name;
6976

70-
return (new NewUpdateInfo(downloadUrl, versionName, description, versionCode));
71-
}
77+
@SerializedName("downloadUrl")
78+
String downloadUrl;
7279
}
7380
}

cdph_updatechecker_app/src/main/res/layout/main.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
android:gravity="center">
55

66
<TextView
7+
android:id="@+id/mainTextView"
78
android:text="@string/hello_world"
9+
android:gravity="center"
810
android:layout_width="wrap_content"
911
android:layout_height="wrap_content" />
1012

cdph_updatechecker_lib/build.gradle

Lines changed: 3 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 21
12-
versionName "21.1.0w8y20a2"
11+
versionCode 22
12+
versionName "22.0.0"
1313
}
1414
buildTypes {
1515
release {
@@ -20,5 +20,6 @@ android {
2020
}
2121

2222
dependencies {
23+
compile 'com.google.code.gson:gson:2.8.5'
2324
compile fileTree(dir: 'libs', include: ['*.jar'])
2425
}

0 commit comments

Comments
 (0)