Browse Source

App升级优化

weizhengliang 3 years ago
parent
commit
5c1ff9d244

+ 2 - 0
middleware/src/main/code/com/wdkl/ncs/android/middleware/common/Constants.kt

@@ -22,6 +22,8 @@ class Constants {
         var tcp_connected: Boolean = false
         var back_id: Int? = -1 //后备设备id
 
+        var app_updating = false
+
         //0--白天,1--夜晚
         var day_state = -1
 

+ 14 - 3
nursehome/src/main/java/com/wdkl/ncs/android/component/nursehome/activity/AppUpdateActivity.kt

@@ -37,6 +37,7 @@ class AppUpdateActivity :BaseActivity<AppUpdatePresenter, UpdateLayBinding>(), A
     }
 
     override fun init() {
+        Constants.app_updating = true
         if (!TextUtils.isEmpty(Constants.app_path)) {
             downLoadAPK(urlManager.buyer + "/" + Constants.app_path)
         } else {
@@ -51,11 +52,21 @@ class AppUpdateActivity :BaseActivity<AppUpdatePresenter, UpdateLayBinding>(), A
     fun downLoadAPK(url: String) {
         Log.d(TAG, "downLoadAPK  url==$url")
         activity_appupdate_dialog_progressview.setCurProgress(0)
-        HttpHelper.download(url, object : HttpHelper.DownloadListener {
+        activity_update_version.setText(Constants.app_path.substringAfterLast("/"))
+        activity_update_version.setOnLongClickListener {
+            Log.d(TAG, "downLoadAPK cancel...")
+            showMessage("取消升级")
+            HttpHelper.cancelRequestByTag(TAG)
+            activity.finish()
+
+            return@setOnLongClickListener true
+        }
+
+        HttpHelper.download(url, TAG, object : HttpHelper.DownloadListener {
             override fun onDownloadSuccess() {
                 Log.d("download", "onDownloadSuccess==" + "成功")
                 runOnUiThread {
-                    activity_calling_bed_text_download.text = "正在安装,请稍后..."
+                    activity_update_text_download.text = "正在安装,请稍后..."
                 }
                 startInstallApk()
             }
@@ -103,7 +114,7 @@ class AppUpdateActivity :BaseActivity<AppUpdatePresenter, UpdateLayBinding>(), A
     }
 
     override fun destory() {
-        //
+        Constants.app_updating = false
     }
 
     //数据加载错误

+ 11 - 8
nursehome/src/main/java/com/wdkl/ncs/android/component/nursehome/activity/NurseHomeActivity.kt

@@ -113,8 +113,6 @@ class NurseHomeActivity  : BaseActivity<NurseHomeActivityPresenter, ActivityNurs
     //事件提醒window
     private var incidentWindow: IncidentWindow? = null
 
-    private var updating :Boolean = false
-
     private val localThread = Thread()
 
     private var ledList = ArrayList<LedItem>()
@@ -375,13 +373,18 @@ class NurseHomeActivity  : BaseActivity<NurseHomeActivityPresenter, ActivityNurs
         Log.d("AppUpdate", "loadAppVersion111 =====>>  versionNo: " + appInfo.versionNo + ", path: " + appInfo.appPath)
         showMessage("获取版本成功,当前版本号: " + BuildConfig.VERSION_CODE + ", 服务器版本号: " + appInfo.versionNo)
 
-        //服务器版本和当前版本不一致才做升级操作
-        if (BuildConfig.VERSION_CODE != appInfo.versionNo && !updating) {
-            Constants.app_path = appInfo.appPath
-            updating = true
-            AppTool.Time.delay(200) {
-                push("/nursehome/update")
+        if (BuildConfig.VERSION_CODE < appInfo.versionNo) {
+            if (!Constants.app_updating) {
+                Constants.app_path = appInfo.appPath
+                Constants.app_updating = true
+                AppTool.Time.delay(200) {
+                    push("/nursehome/update")
+                }
+            } else {
+                showMessage("正在升级中...")
             }
+        } else {
+            showMessage("当前已是最新版本")
         }
     }
 

+ 3 - 1
nursehome/src/main/java/com/wdkl/ncs/android/component/nursehome/fragment/SystemSettingsFragment.kt

@@ -205,8 +205,10 @@ class SystemSettingsFragment:BaseFragment<SystemSettingsPresenter,FragmentSystem
 
 
         software_and_information_tv.setText("软件版本: V" + BuildConfig.VERSION_NAME + "_" + BuildConfig.VERSION_CODE)
-        software_and_information_tv.setOnClickListener {
+        software_and_information_tv.setOnLongClickListener {
             (activity as NurseHomeActivity).checkApp()
+
+            return@setOnLongClickListener true
         }
 
         save_settings_tv.setOnClickListener(this)

+ 34 - 6
nursehome/src/main/java/com/wdkl/ncs/android/component/nursehome/util/HttpHelper.java

@@ -21,23 +21,28 @@ import static com.wdkl.ncs.android.component.nursehome.util.AppUpdateHelper.FILE
 
 public class HttpHelper {
     private final static String TAG = "HttpHelper";
+    private static OkHttpClient okHttpClient = null;
 
     /**
      * @param url   服务器地址
      * @param file  所要上传的文件
      */
-    public static void upload(String url, File file, final UploadCallback callback) {
-        OkHttpClient client = new OkHttpClient();
+    public static void upload(String url, File file, Object tag, final UploadCallback callback) {
+        if (okHttpClient == null) {
+            okHttpClient = new OkHttpClient();
+        }
+
         RequestBody requestBody = new MultipartBody.Builder()
                 .setType(MultipartBody.FORM)
                 .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file))
                 .build();
         Request request = new Request.Builder()
                 .url(url)
+                .tag(tag)
                 .post(requestBody)
                 .build();
 
-        client.newCall(request).enqueue(new Callback() {
+        okHttpClient.newCall(request).enqueue(new Callback() {
             @Override
             public void onFailure(Call call, IOException e) {
                 if (callback != null) {
@@ -63,9 +68,16 @@ public class HttpHelper {
         });
     }
 
-    public static void download(String url, final DownloadListener listener) {
-        Request request = new Request.Builder().url(url).build();
-        OkHttpClient okHttpClient = new OkHttpClient();
+    public static void download(String url, Object tag, final DownloadListener listener) {
+        if (okHttpClient == null) {
+            okHttpClient = new OkHttpClient();
+        }
+
+        Request request = new Request.Builder()
+                .url(url)
+                .tag(tag)
+                .build();
+
         okHttpClient.newCall(request).enqueue(new Callback() {
             @Override
             public void onFailure(Call call, IOException e) {
@@ -122,6 +134,22 @@ public class HttpHelper {
         });
     }
 
+    public static void cancelRequestByTag(Object tag) {
+        if (okHttpClient != null && tag != null) {
+            for (Call call : okHttpClient.dispatcher().queuedCalls()) {
+                if (tag.equals(call.request().tag())) {
+                    call.cancel();
+                }
+            }
+
+            for (Call call : okHttpClient.dispatcher().runningCalls()) {
+                if (tag.equals(call.request().tag())) {
+                    call.cancel();
+                }
+            }
+        }
+    }
+
     private static String isHaveExistDir(File downloadFile, File sonFile) throws IOException {
         Log.d("download", "downloadFile.mkdirs()==" + downloadFile.mkdirs());
         Log.d("download", "sonFile.mkdir()==" + sonFile.mkdir());

+ 1 - 1
nursehome/src/main/java/com/wdkl/ncs/android/component/nursehome/util/XCrashUtils.java

@@ -138,7 +138,7 @@ public class XCrashUtils {
         if (logFile.exists()) {
             final UrlManager urlManager = UrlManager.Companion.build();
             String url = urlManager.getBuyer() + "ncs/upload/file";
-            HttpHelper.upload(url, logFile, new HttpHelper.UploadCallback() {
+            HttpHelper.upload(url, logFile, TAG, new HttpHelper.UploadCallback() {
                 @Override
                 public void onFail() {
                     Log.e(TAG,"错误日志文件上传失败!");

+ 11 - 1
nursehome/src/main/res/layout/update_lay.xml

@@ -8,7 +8,7 @@
         android:orientation="vertical">
 
         <TextView
-            android:id="@+id/activity_calling_bed_text_download"
+            android:id="@+id/activity_update_text_download"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerInParent="true"
@@ -16,6 +16,16 @@
             android:textColor="#3D3D63"
             android:textSize="30sp" />
 
+        <TextView
+            android:id="@+id/activity_update_version"
+            android:layout_width="wrap_content"
+            android:layout_height="wrap_content"
+            android:layout_below="@id/activity_update_text_download"
+            android:layout_centerHorizontal="true"
+            android:layout_marginTop="40dp"
+            android:text="V--"
+            android:textSize="20sp"/>
+
         <com.wdkl.ncs.android.lib.widget.ProgressView
             android:id="@+id/activity_appupdate_dialog_progressview"
             android:layout_width="match_parent"