Skip to content

Commit

Permalink
Changed the json parsing behaviour and added gson library for a more …
Browse files Browse the repository at this point in the history
…easier json parsing.
  • Loading branch information
SnoopyCodeX committed Sep 20, 2020
1 parent 20b5bdc commit fd4e119
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 143 deletions.
78 changes: 34 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,88 +1,78 @@
### JSON-based Update Checker
[![DepShield Badge](https://depshield.sonatype.org/badges/SnoopyCodeX/repository/depshield.svg)](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
![DepShield Badge](https://depshield.sonatype.org/badges/SnoopyCodeX/repository/depshield.svg)
---
> ☑ 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

---
1 change: 1 addition & 0 deletions cdph_updatechecker_app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ android {

dependencies {
compile project(':cdph_updatechecker_lib')
compile 'com.google.code.gson:gson:2.8.5'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
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;
}
}
2 changes: 2 additions & 0 deletions cdph_updatechecker_app/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
android:gravity="center">

<TextView
android:id="@+id/mainTextView"
android:text="@string/hello_world"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Expand Down
5 changes: 3 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 21
versionName "21.1.0w8y20a2"
versionCode 22
versionName "22.0.0"
}
buildTypes {
release {
Expand All @@ -20,5 +20,6 @@ android {
}

dependencies {
compile 'com.google.code.gson:gson:2.8.5'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Loading

0 comments on commit fd4e119

Please sign in to comment.