Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for user cancel update dialog #121

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ appUpdate.checkAppUpdate(onSuccess, onFail, updateUrl, {
})
```

- User can cancel update
```js
appUpdate.checkAppUpdate(onSuccess, onFail, updateUrl, {
'userCanCancel' : true
})
```

### versionCode

You can simply get the versionCode from typing those code in `Console`
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<description>Cordova App Update</description>
<license>Apache 2.0</license>
<keywords>cordova,update,app,auto,updater</keywords>
<dependency id="cordova-plugin-appversion" version="1.0.0" />
<js-module src="www/AppUpdate.js" name="AppUpdate">
<clobbers target="AppUpdate" />
</js-module>
<platform name="android">
<dependency id="cordova-plugin-appversion" version="1.0.0" />
<config-file parent="/*" target="res/xml/config.xml">
<feature name="AppUpdate">
<param name="android-package" value="com.vaenow.appupdate.android.CheckAppUpdate"/>
Expand Down Expand Up @@ -67,7 +67,7 @@
<source-file src="res/values-ko/appupdate_strings.xml" target-dir="res/values-ko"/>
<source-file src="res/layout/appupdate_progress.xml" target-dir="res/layout"/>
<source-file src="res/xml/appupdate_paths.xml" target-dir="res/xml" />

<framework src="com.android.support:support-v4:24.1.1+" />
</platform>
</plugin>
53 changes: 46 additions & 7 deletions src/android/AuthenticationOptions.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
package android;

package com.vaenow.appupdate.android;

import android.util.Base64;

import org.json.JSONException;
import org.json.JSONObject;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public class AuthenticationOptions {

private static final String[] VALID_AUTH_TYPE = new String[] {"BASIC", "TOKEN"};

private String authType;
private String username;
private String password;
private String token;
private String tokenPrefix;

public AuthenticationOptions(JSONObject options) {
try {
this.setAuthType(options.getString("authType"));
this.setUsername(options.getString("username"));
this.setPassword(options.getString("password"));
if (options.has("username")) {
this.setUsername(options.getString("username"));
}
if (options.has("password")) {
this.setPassword(options.getString("password"));
}
if (options.has("token")) {
this.setToken(options.getString("token"));
}
if (options.has("tokenPrefix")) {
this.setTokenPrefix(options.getString("tokenPrefix"));
}
} catch (JSONException e){
// If there is any error then ensure that auth type is unset
this.setAuthType("");
Expand All @@ -31,12 +45,27 @@ public AuthenticationOptions(JSONObject options) {
* @return boolean flag indicating if there are authentication credentials
*/
public boolean hasCredentials() {
return authType.equals("basic");
return Arrays.asList(VALID_AUTH_TYPE).contains(authType.toUpperCase());
}

public String getEncodedAuthorization() {
return "Basic " + Base64.encodeToString((this.username + ":" + this.password)
.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
String header = "";

switch (authType.toUpperCase()) {
case "TOKEN":
if (this.tokenPrefix != null && !this.tokenPrefix.isEmpty()) {
header = this.tokenPrefix + " ";
}

header += this.token;
break;
case "BASIC":
header = "Basic " + Base64.encodeToString((this.username + ":" + this.password)
.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
break;
}

return header;
}

public String getAuthType() {
Expand All @@ -62,4 +91,14 @@ public String getPassword() {
public void setPassword(String password) {
this.password = password;
}

public void setToken(String token) {
this.token = token;
}

public String getToken() { return this.token; }

public void setTokenPrefix(String tokenPrefix) {
this.tokenPrefix = tokenPrefix;
}
}
33 changes: 12 additions & 21 deletions src/android/CheckUpdateThread.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vaenow.appupdate.android;

import android.AuthenticationOptions;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
Expand All @@ -9,13 +8,10 @@
import org.json.JSONObject;
import org.json.JSONException;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;

import java.nio.charset.StandardCharsets;

Expand All @@ -34,6 +30,8 @@ public class CheckUpdateThread implements Runnable {
private AuthenticationOptions authentication;
private Handler mHandler;

private boolean sslCheck = true;

private void setMHashMap(HashMap<String, String> mHashMap) {
this.mHashMap = mHashMap;
}
Expand All @@ -49,6 +47,11 @@ public CheckUpdateThread(Context mContext, Handler mHandler, List<Version> queue
this.updateXmlUrl = updateXmlUrl;
this.authentication = new AuthenticationOptions(options);
this.mHandler = mHandler;
try {
this.sslCheck = options.has("sslCheck") ? options.getBoolean("sslCheck") : true;
} catch (JSONException e){
// If there is any error then sslCheck is to true by default
}
}

@Override
Expand All @@ -73,28 +76,16 @@ public void run() {
* @return
*/
private InputStream returnFileIS(String path) {
LOG.d(TAG, "returnFileIS..");
LOG.d(TAG, "returnFileIS.." + (this.sslCheck ? "Secure" : "Unsecure"));

URL url = null;
HttpURLConnection conn = Utils.openConnection(path, this.authentication, this.sslCheck);
InputStream is = null;

try {
url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();//利用HttpURLConnection对象,我们可以从网络中获取网页数据.

if(this.authentication.hasCredentials()){
conn.setRequestProperty("Authorization", this.authentication.getEncodedAuthorization());
}

conn.setDoInput(true);
conn.connect();
is = conn.getInputStream(); //得到网络返回的输入流
} catch (FileNotFoundException e) {
e.printStackTrace();
mHandler.sendEmptyMessage(Constants.REMOTE_FILE_NOT_FOUND);
is = conn.getInputStream();
} catch (IOException e) {
e.printStackTrace();
mHandler.sendEmptyMessage(Constants.NETWORK_ERROR);
mHandler.sendEmptyMessage(Constants.UNKNOWN_ERROR);
}

return is;
Expand Down Expand Up @@ -150,4 +141,4 @@ private int getVersionCodeRemote() {

return versionCodeRemote;
}
}
}
21 changes: 8 additions & 13 deletions src/android/DownloadApkThread.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.vaenow.appupdate.android;

import android.AuthenticationOptions;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Environment;
Expand All @@ -16,7 +15,6 @@
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import java.nio.charset.StandardCharsets;
Expand All @@ -39,18 +37,23 @@ public class DownloadApkThread implements Runnable {
private DownloadHandler downloadHandler;
private Handler mHandler;
private AuthenticationOptions authentication;
private boolean sslCheck = true;

public DownloadApkThread(Context mContext, Handler mHandler, ProgressBar mProgress, AlertDialog mDownloadDialog, HashMap<String, String> mHashMap, JSONObject options) {
this.mDownloadDialog = mDownloadDialog;
this.mHashMap = mHashMap;
this.mHandler = mHandler;
this.authentication = new AuthenticationOptions(options);
try {
this.sslCheck = options.has("sslCheck") ? options.getBoolean("sslCheck") : true;
} catch (JSONException e){
// If there is any error then sslCheck is to true by default
}

this.mSavePath = Environment.getExternalStorageDirectory() + "/" + "download"; // SD Path
this.downloadHandler = new DownloadHandler(mContext, mProgress, mDownloadDialog, this.mSavePath, mHashMap);
}


@Override
public void run() {
downloadAndInstall();
Expand All @@ -66,16 +69,8 @@ private void downloadAndInstall() {
try {
// 判断SD卡是否存在,并且是否具有读写权限
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// 获得存储卡的路径
URL url = new URL(mHashMap.get("url"));
// 创建连接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();

if(this.authentication.hasCredentials()){
conn.setRequestProperty("Authorization", this.authentication.getEncodedAuthorization());
}
HttpURLConnection conn = Utils.openConnection(mHashMap.get("url"), mHandler, this.authentication, this.sslCheck);

conn.connect();
// 获取文件大小
int length = conn.getContentLength();
// 创建输入流
Expand Down Expand Up @@ -120,4 +115,4 @@ private void downloadAndInstall() {
}

}
}
}
11 changes: 10 additions & 1 deletion src/android/MsgBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,23 @@ public MsgBox(Context mContext) {
*
* @param onClickListener
*/
public Dialog showNoticeDialog(OnClickListener onClickListener) {
public Dialog showNoticeDialog(OnClickListener onClickListener, boolean userCanCancel) {
if (noticeDialog == null) {
LOG.d(TAG, "showNoticeDialog");
// 构造对话框
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(msgHelper.getString(MsgHelper.UPDATE_TITLE));
builder.setMessage(msgHelper.getString(MsgHelper.UPDATE_MESSAGE));
// 更新
if (userCanCancel) {
builder.setNegativeButton(msgHelper.getString(MsgHelper.UPDATE_CANCEL_BTN), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Juste dismiss
dialog.dismiss();
}
});
}
builder.setPositiveButton(msgHelper.getString(MsgHelper.UPDATE_UPDATE_BTN), onClickListener);
noticeDialog = builder.create();
}
Expand Down
1 change: 1 addition & 0 deletions src/android/MsgHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class MsgHelper {
public static String UPDATE_TITLE = "update_title";
public static String UPDATE_MESSAGE = "update_message";
public static String UPDATE_UPDATE_BTN = "update_update_btn";
public static String UPDATE_CANCEL_BTN = "update_cancel";
public static String APPUPDATE_PROGRESS = "appupdate_progress";
public static String UPDATE_PROGRESS = "update_progress";
public static String UPDATING = "updating";
Expand Down
14 changes: 9 additions & 5 deletions src/android/UpdateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public UpdateManager(Context context, CordovaInterface cordova) {
msgBox = new MsgBox(mContext);
}

public UpdateManager(JSONArray args, CallbackContext callbackContext, Context context, JSONObject options) {
/*public UpdateManager(JSONArray args, CallbackContext callbackContext, Context context, JSONObject options) {
this(args, callbackContext, context, "http://192.168.3.102:8080/update_apk/version.xml", options);
}

Expand All @@ -65,7 +65,7 @@ public UpdateManager(JSONArray args, CallbackContext callbackContext, Context co
this.mContext = context;
packageName = mContext.getPackageName();
msgBox = new MsgBox(mContext);
}
}*/

public UpdateManager options(JSONArray args, CallbackContext callbackContext)
throws JSONException {
Expand Down Expand Up @@ -159,8 +159,12 @@ private void compareVersions() {
skipProgressDialog = options.getBoolean("skipProgressDialog");
} catch (JSONException e) {}

//比对版本号
//检查软件是否有更新版本
boolean userCanCancel = false;
try {
userCanCancel = options.getBoolean("userCanCancel");
} catch (JSONException ignore) {}

LOG.d(TAG, "local : " + versionCodeLocal + " / remote :" + versionCodeRemote);
if (versionCodeLocal < versionCodeRemote) {
if (isDownloading) {
msgBox.showDownloadDialog(null, null, null, !skipProgressDialog);
Expand All @@ -171,7 +175,7 @@ private void compareVersions() {
mHandler.sendEmptyMessage(Constants.DOWNLOAD_CLICK_START);
} else {
// 显示提示对话框
msgBox.showNoticeDialog(noticeDialogOnClick);
msgBox.showNoticeDialog(noticeDialogOnClick, userCanCancel);
mHandler.sendEmptyMessage(Constants.VERSION_NEED_UPDATE);
}
}
Expand Down
Loading