| package com.doumee.keyCabinet.utils.update; | 
|   | 
| import android.content.DialogInterface; | 
| import android.view.KeyEvent; | 
|   | 
| import androidx.annotation.NonNull; | 
|   | 
| import com.doumee.keyCabinet.ui.main.MainActivity; | 
| import com.doumee.lib_coremodel.http.rxJava.SimpleObserver; | 
| import com.doumee.lib_coremodel.http.utils.RxUtils; | 
| import com.doumee.keyCabinet.BuildConfig; | 
| import com.doumee.keyCabinet.MApplication; | 
| import com.doumee.keyCabinet.R; | 
| import com.doumee.keyCabinet.bean.AndroidVersionBean; | 
| import com.doumee.keyCabinet.http.Apis; | 
| import com.doumee.keyCabinet.http.param.BaseResponse; | 
| import com.doumee.keyCabinet.http.param.DevAppVersionParam; | 
| import com.doumee.keyCabinet.http.param.RequestBaseObject; | 
|   | 
| public class UpdateUtil { | 
|     public static String newVerName = ""; | 
|     public static String upgradeurl = ""; | 
|     private static String updatelog = ""; | 
|     public MainActivity activity; | 
|     int verCode = 0; | 
|     static String verName = null; | 
|     private static boolean checkupdate; | 
|     // 限制时间,以秒为单位 | 
|     // private final int waitTime; | 
|     // 已经流逝的时间 | 
|     private int passedTime; | 
|     private  DownloadDialog downloadDialog; | 
|     private boolean isNeedShow = true; | 
|     private boolean isNewUserTag = false; | 
|   | 
|     /** | 
|      * @param activity | 
|      * checkupdate true检查更新;false自动更新 | 
|      */ | 
|     public UpdateUtil(MainActivity activity) { | 
|         this.activity = activity; | 
|     } | 
|   | 
|     private void timeListener() { | 
|         passedTime++; | 
|         int modSeed = passedTime % 3; | 
|         if (modSeed == 0) { | 
|             System.out.println("响应中"); | 
|         } else if (modSeed == 1) { | 
|             System.out.println("响应中.."); | 
|         } else if (modSeed == 2) { | 
|             System.out.println("响应中."); | 
|         } | 
|         // // 如果流逝时间大于规定时间则中断线程 | 
|         // if (passedTime > waitTime) { | 
|         // passedTime = waitTime; | 
|         // thread.interrupt(); | 
|         // } | 
|     } | 
|     public static interface VersionCallBack{ | 
|         void isNeedUpdate(boolean isUpdate); | 
|     } | 
|     private VersionCallBack versionCallBack; | 
|   | 
|     // 获取服务器端版本号 | 
|     public void getServerVerCode(VersionCallBack versionCallBack) { | 
|         this.versionCallBack = versionCallBack; | 
|         getUpgradeInfo(); | 
|     } | 
|   | 
|     private void getUpgradeInfo() { | 
|         activity.getVM().getRetrofitService(Apis.class).getVersion() | 
|                 .compose(RxUtils.schedulersTransformer()) | 
|                 .subscribe(new SimpleObserver<BaseResponse<AndroidVersionBean>>(activity.getVM().rxJavaGcManager) { | 
|                     @Override | 
|                     public void onNext(@NonNull BaseResponse<AndroidVersionBean> response) { | 
|                         if (200==response.getCode()) { | 
|                             if (response.getData()!=null) { | 
|                                 AndroidVersionBean data = response.getData(); | 
|                                 if(data!=null&&data.getVersionNum()!=null&&data.getVersionNum()>BuildConfig.VERSION_CODE){ | 
|                                     versionCallBack.isNeedUpdate(true); | 
|                                     upgradeurl=data.getFullFileUrl(); | 
|                                     updatelog=data.getContent(); | 
|                                     newVerName = response.getData().getVersionInfo(); | 
|                                     doNewVersionUpdate(true); | 
|                                 }else { | 
|                                     versionCallBack.isNeedUpdate(false); | 
|                                 } | 
|                             }else { | 
|                                 versionCallBack.isNeedUpdate(false); | 
|                             } | 
|                         }else { | 
|                             versionCallBack.isNeedUpdate(false); | 
|                         } | 
|                     } | 
|   | 
|                     @Override | 
|                     public void onError(@NonNull Throwable e) { | 
|                         activity.runOnUiThread(new Runnable() { | 
|                             @Override | 
|                             public void run() { | 
|                                 versionCallBack.isNeedUpdate(false); | 
|                             } | 
|                         }); | 
|                     } | 
|   | 
|                     @Override | 
|                     public void onComplete() { | 
|   | 
|                     } | 
|                 }); | 
|     } | 
|   | 
|     private void destroy(){ | 
|         if(activity!=null){ | 
|             activity=null; | 
|         } | 
|     } | 
|   | 
|     // 更新新版本 | 
|     public void doNewVersionUpdate(boolean type) { | 
|         StringBuffer sb = new StringBuffer(); | 
|         //sb.append(newVerName); | 
|         sb.append(MApplication.mContext.getString(R.string.download_8)+"\n"); | 
|         String[] arr = updatelog.split(";"); | 
|         for (int i = 0; i < arr.length; i++) { | 
|             if (i == arr.length - 1) { | 
|                 sb.append(arr[i] + "." + "\n\n\r"); | 
|             } else { | 
|                 sb.append(arr[i] + ";" + "\n"); | 
|             } | 
|         } | 
|         // sb.append(updatelog+"\n\n\r"); | 
|         // new UpdateDialog(activity).show(); | 
|         final UpdateDialog updateDialog = new UpdateDialog(activity); | 
|         updateDialog.setText(sb.toString(),newVerName); | 
|         updateDialog.setOnOtemListener(new UpdateDialog.UpdialogListener() { | 
|   | 
|             @Override | 
|             public void setOnclickUpdate() { | 
|                 updateDialog.hide(); | 
|                 downloadDialog = new DownloadDialog(activity, upgradeurl); | 
|                 downloadDialog.show(); | 
|                 downloadDialog.setCanceledOnTouchOutside(false); | 
|                 downloadDialog.setUpdateDialog(updateDialog); | 
|                 downloadDialog | 
|                         .setOnKeyListener(new DialogInterface.OnKeyListener() { | 
|                             @Override | 
|                             public boolean onKey(DialogInterface dialog, | 
|                                                  int keyCode, KeyEvent event) { | 
|                                 if (keyCode == KeyEvent.KEYCODE_BACK | 
|                                         && event.getRepeatCount() == 0) { | 
|                                     if (updateDialog != null) { | 
|                                         updateDialog.show(); | 
|                                     } | 
|                                 } | 
|                                 return false; | 
|                             } | 
|                         }); | 
|             } | 
|   | 
|             @Override | 
|             public void setOnclickNoUpdate() { | 
|                 updateDialog.dismiss(); | 
|             } | 
|         }); | 
|         updateDialog.isneedupdate(type); | 
|         if (type) { | 
|             // 强制升级 | 
|             updateDialog.setCanceledOnTouchOutside(false); | 
|         } else { | 
|             updateDialog.setCanceledOnTouchOutside(true); | 
|         } | 
|         updateDialog.show(); | 
|         updateDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { | 
|             @Override | 
|             public boolean onKey(DialogInterface dialog, int keyCode, | 
|                                  KeyEvent event) { | 
|                 if (keyCode == KeyEvent.KEYCODE_BACK | 
|                         && event.getRepeatCount() == 0) { | 
|                     return true; | 
|                 } else { | 
|                     return false; | 
|   | 
|                 } | 
|             } | 
|         }); | 
|     } | 
|   | 
|     /** | 
|      * 是否需要显示土司 | 
|      * | 
|      * @param isNeedShow | 
|      * @return | 
|      */ | 
|     public UpdateUtil setShowToast(boolean isNeedShow) { | 
|         this.isNeedShow = isNeedShow; | 
|         return this; | 
|     } | 
|   | 
|     /** | 
|      * 是否是新用户 | 
|      * | 
|      * @param isNewUserTag | 
|      * @return | 
|      */ | 
|     public UpdateUtil setNewsUserTag(boolean isNewUserTag) { | 
|         this.isNewUserTag = isNewUserTag; | 
|         return this; | 
|     } | 
|   | 
| } |