From a8b20c6b2d1532a55d8cccdf82477fbec92ef68e Mon Sep 17 00:00:00 2001 From: shutup Date: Sat, 6 Aug 2016 08:48:31 +0800 Subject: [PATCH] update kcptun to 20160725 change version name to v0.2 optimize kcptun bin file update logic --- .../com/shutup/kcptun_android/Constants.java | 23 ++++++++++--------- .../shutup/kcptun_android/MainActivity.java | 23 ++++++++++++++++--- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/shutup/kcptun_android/Constants.java b/app/src/main/java/com/shutup/kcptun_android/Constants.java index 5867e8c..1642cfb 100644 --- a/app/src/main/java/com/shutup/kcptun_android/Constants.java +++ b/app/src/main/java/com/shutup/kcptun_android/Constants.java @@ -4,17 +4,18 @@ * Created by shutup on 16/7/20. */ public interface Constants { - public static final String SettingChanged = "SettingChanged"; + String AppVersionCode = "AppVersionCode"; + String SettingChanged = "SettingChanged"; - public static final String RemoteServerIp = "RemoteServerIp"; - public static final String RemoteServerPort = "RemoteServerPort"; - public static final String LocalServerPort = "LocalServerPort"; - public static final String ServerKey = "ServerKey"; - public static final String ServerMode = "ServerMode"; + String RemoteServerIp = "RemoteServerIp"; + String RemoteServerPort = "RemoteServerPort"; + String LocalServerPort = "LocalServerPort"; + String ServerKey = "ServerKey"; + String ServerMode = "ServerMode"; - public static final int SET_INFO_CONTENT = 0; - public static final int APPEND_INFO_CONTENT = 1; - public static final int CHANGE_START_BTN_NAME = 2; - public static final int CHANGE_START_BTN_ENABLE = 3; - public static final int CHANGE_SETTING_BTN_ENABLE = 5; + int SET_INFO_CONTENT = 0; + int APPEND_INFO_CONTENT = 1; + int CHANGE_START_BTN_NAME = 2; + int CHANGE_START_BTN_ENABLE = 3; + int CHANGE_SETTING_BTN_ENABLE = 5; } diff --git a/app/src/main/java/com/shutup/kcptun_android/MainActivity.java b/app/src/main/java/com/shutup/kcptun_android/MainActivity.java index a48b24d..279239f 100644 --- a/app/src/main/java/com/shutup/kcptun_android/MainActivity.java +++ b/app/src/main/java/com/shutup/kcptun_android/MainActivity.java @@ -3,6 +3,8 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; @@ -179,13 +181,13 @@ private int detectCpuArchInfo() { * @param filename * @return */ - private static String installBinary(Context ctx, int resId, String filename) { + private String installBinary(Context ctx, int resId, String filename) { try { File f = new File(ctx.getDir("bin", 0), filename); if (f.exists()) { - + handleKcptunUpdate(ctx, resId, f); } else { - copyRawFile(ctx, resId, f, "0755"); + handleKcptunUpdate(ctx, resId, f); } return f.getCanonicalPath(); } catch (Exception e) { @@ -194,6 +196,21 @@ private static String installBinary(Context ctx, int resId, String filename) { } } + private void handleKcptunUpdate(Context ctx, int resId, File f) throws PackageManager.NameNotFoundException, IOException, InterruptedException { + PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0); + int currentVersionCode = pInfo.versionCode; + int localVersionCode = mSharedPreferences.getInt(AppVersionCode,0); + if (localVersionCode < currentVersionCode) { + copyRawFile(ctx,resId,f,"0755"); + SharedPreferences.Editor editor = mSharedPreferences.edit(); + editor.putInt(AppVersionCode,currentVersionCode); + editor.commit(); + } + else{ + if (BuildConfig.DEBUG) Log.d(TAG, "no update"); + } + } + private static void copyRawFile(Context ctx, int resid, File file, String mode) throws IOException, InterruptedException { final String abspath = file.getAbsolutePath(); final FileOutputStream out = new FileOutputStream(file);