Преглед на файлове

非正式包添加自动更新的操作

zengjiebin преди 7 години
родител
ревизия
9f37ad7254

+ 25 - 0
app/src/main/java/com/sheep/gamegroup/util/StringUtils.java

@@ -14,6 +14,7 @@ import org.json.JSONObject;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.GregorianCalendar;
 import java.util.Hashtable;
@@ -326,4 +327,28 @@ public class StringUtils {
             return CONTENT_TYPE_FONT;
         }
     }
+
+
+    /**
+     * 获取所有满足正则表达式的字符串
+     * @param str 需要被获取的字符串
+     * @param regex 正则表达式
+     * @return 所有满足正则表达式的字符串
+     */
+    public static ArrayList<String> getAllSatisfyStr(String str, String regex) {
+        if (str == null || str.isEmpty()) {
+            return null;
+        }
+        ArrayList<String> allSatisfyStr = new ArrayList<>();
+        if (regex == null || regex.isEmpty()) {
+            allSatisfyStr.add(str);
+            return allSatisfyStr;
+        }
+        Pattern pattern = Pattern.compile(regex);
+        Matcher matcher = pattern.matcher(str);
+        while (matcher.find()) {
+            allSatisfyStr.add(matcher.group());
+        }
+        return allSatisfyStr;
+    }
 }

+ 10 - 2
app/src/main/java/com/sheep/gamegroup/util/SysAppUtil.java

@@ -461,6 +461,14 @@ public class SysAppUtil {
                 e.printStackTrace();
             }
         }
+        if(TestUtil.isSheep()) {
+            checkUpdateNewVersion(activity, ignoreMd5, action1);
+        } else {
+            TestUtil.checkUpdateFromJenkins(activity, ignoreMd5, action1);
+        }
+    }
+    //通过接口检查更新
+    public static void checkUpdateNewVersion(final Activity activity, final String ignoreMd5, final Action1<Integer> action1) {
         SheepApp.getInstance().getNetComponent().getApiService().getNewVersion(SheepApp.getInstance().getConnectAddress().getVersionType())
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
@@ -474,9 +482,9 @@ public class SysAppUtil {
                     @Override
                     public void onError(BaseMessage baseMessage) {
                         ACache.get(SheepApp.getInstance()).put("version_url", "");
-                        if(ignoreMd5 == null)
+                        if (ignoreMd5 == null)
                             Beta.checkUpgrade();
-                        if(action1 != null)
+                        if (action1 != null)
                             action1.call(0);
                     }
                 });

+ 107 - 1
app/src/main/java/com/sheep/gamegroup/util/TestUtil.java

@@ -6,6 +6,8 @@ import android.app.NotificationChannel;
 import android.app.NotificationManager;
 import android.content.DialogInterface;
 import android.content.Intent;
+import android.content.pm.PackageInfo;
+import android.net.Uri;
 import android.os.Build;
 import android.os.Bundle;
 import android.support.v4.app.NotificationCompat;
@@ -54,14 +56,19 @@ import com.umeng.socialize.ShareAction;
 import com.umeng.socialize.UMShareListener;
 import com.umeng.socialize.bean.SHARE_MEDIA;
 import com.umeng.socialize.media.UMImage;
+import com.zhy.http.okhttp.OkHttpUtils;
+import com.zhy.http.okhttp.callback.FileCallBack;
+import com.zhy.http.okhttp.callback.StringCallback;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
 import cn.jpush.android.api.JPushInterface;
+import okhttp3.Call;
 import rx.android.schedulers.AndroidSchedulers;
 import rx.functions.Action1;
 import rx.schedulers.Schedulers;
@@ -69,6 +76,7 @@ import rx.schedulers.Schedulers;
 import static android.content.Context.NOTIFICATION_SERVICE;
 import static com.sheep.gamegroup.util.CommonUtil.LONG_MAO_APP_PACKAGE_NAME;
 import static com.sheep.gamegroup.util.CommonUtil.LONG_MAO_APP_URI;
+import static com.sheep.jiuyan.samllsheep.utils.ClassFileHelper.DIR;
 
 /**
  * Created by realicing on 2018/5/3.
@@ -558,7 +566,7 @@ public class TestUtil {
     }
 
     private static void testApkSign2() {
-        LogUtil.println("测试签名", ApkUtils.getApkSignMd5StrByPath(new File(ClassFileHelper.DIR, "559E2C4A81182E2674570B4AE3C9C440.apk").getAbsolutePath(), null));
+        LogUtil.println("测试签名", ApkUtils.getApkSignMd5StrByPath(new File(DIR, "559E2C4A81182E2674570B4AE3C9C440.apk").getAbsolutePath(), null));
     }
 
     //测试表情包
@@ -643,6 +651,104 @@ public class TestUtil {
                 })
                 .open();
     }
+
+    private static final String URL_START = "http://10.8.210.20:8080/jenkins/job/android_small_sheep/";
+    //通过jenkins服务器打包结果进行检查更新
+    public static void checkUpdateFromJenkins(final Activity activity, final String ignoreMd5, final Action1<Integer> action1) {
+        //加载jenkins信息来判断是否当前安装的是最新的安装包
+        OkHttpUtils.get().url("http://10.8.210.20:8080/jenkins/job/android_small_sheep").build().execute(new StringCallback() {
+            @Override
+            public void onError(Call call, Exception e, int id) {
+                SysAppUtil.checkUpdateNewVersion(activity, ignoreMd5, action1);
+            }
+
+            @Override
+            public void onResponse(String response, int id) {
+                int start = response.indexOf("最后一次成功的构建结果");
+                int end = response.indexOf("最新修改记录");
+                try {
+                    String result = response.substring(start, end);
+                    ArrayList<String> arrayList = StringUtils.getAllSatisfyStr(result, "<a.+?href=\\\"(.+?)\\\">");
+                    ListUtil.removeItem(arrayList, new ListUtil.CallBack<String, Boolean>() {
+                        @Override
+                        public Boolean call(String msg) {
+                            return !msg.endsWith(".apk\">");
+                        }
+                    });
+                    int index = 0;
+                    switch (BuildConfig.FLAVOR){
+                        case "developSheeptest":
+                            index = BuildConfig.DEBUG ? 0 : 1;
+                            break;
+                        case "sheep":
+                            index = BuildConfig.DEBUG ? 2 : 3;
+                            break;
+                        case "sheeptestjiguang":
+                            index = BuildConfig.DEBUG ? 4 : 5;
+                            break;
+                    }
+                    String url = URL_START + arrayList.get(index).split("\"")[1];
+                    downloadApkAndCheckInstall(url, action1);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    G.showToast(e.getMessage());
+                }
+            }
+        });
+    }
+
+    //下载apk文件后检查是否是最新的
+    private static void downloadApkAndCheckInstall(final String url, final Action1<Integer> action1) {
+        String name = Uri.parse(url).getLastPathSegment();
+        if(name == null){
+            name = url.hashCode() + ".apk";
+        }
+        PackageInfo packageInfo = ApkUtils.getPackageInfo(BuildConfig.APPLICATION_ID);
+        if(packageInfo == null)
+            return;
+        final File myApkFile = new File(packageInfo.applicationInfo.sourceDir);
+        final File file = new File(DIR, name);
+        if (file.exists()) {
+            if (Md5Util.checkMD5(myApkFile, file)) {
+                if(action1 == null)
+                    G.showToast("应用已经是最新版本");
+            } else {
+                if(action1 == null)
+                    G.showToast("文件已经存在,正在安装");
+                ApkUtils.installApk(SheepApp.getInstance(), file.getAbsolutePath());
+            }
+            return;
+        }
+        OkHttpUtils.get().url(url).build().execute(new FileCallBack(DIR, name) {
+            @Override
+            public void inProgress(float progress, long total, int id) {
+                if(action1 == null)
+                    G.showToast(String.format(Locale.CHINA, "%d%%", Math.round(progress * 100)));
+            }
+
+            @Override
+            public void onError(Call call, Exception e, int id) {
+                if(action1 == null)
+                    G.showToast("下载失败");
+            }
+
+            @Override
+            public void onResponse(File response, int id) {
+                if(action1 == null)
+                    G.showToast("下载完成");
+                if (response.exists()) {
+                    if(action1 == null)
+                        G.showToast("文件已经是最新");
+                    if (Md5Util.checkMD5(myApkFile, response)) {
+                        if(action1 == null)
+                            G.showToast("应用已经是最新");
+                    } else {
+                        ApkUtils.installApk(SheepApp.getInstance(), response.getAbsolutePath());
+                    }
+                }
+            }
+        });
+    }
     //开启后可 测试充值与绑定微信和身份信息逻辑 等
     public static boolean isDev() {
         return BuildConfig.DEBUG;