VersionCheckingUtil.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package com.qingdaofushan.homecaresas.view;
  2. import static android.os.Build.VERSION_CODES.Q;
  3. import android.annotation.SuppressLint;
  4. import android.app.Notification;
  5. import android.app.NotificationChannel;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.net.Uri;
  11. import android.os.Build;
  12. import android.os.Environment;
  13. import android.util.Log;
  14. import android.widget.RemoteViews;
  15. import androidx.core.app.NotificationCompat;
  16. import androidx.core.content.FileProvider;
  17. import com.lzy.okgo.OkGo;
  18. import com.lzy.okgo.model.Progress;
  19. import com.lzy.okgo.request.GetRequest;
  20. import com.lzy.okserver.OkDownload;
  21. import com.lzy.okserver.download.DownloadListener;
  22. import com.lzy.okserver.download.DownloadTask;
  23. import com.qingdaofushan.homecaresas.R;
  24. import com.qingdaofushan.homecaresas.activity.MainActivity;
  25. import com.qingdaofushan.homecaresas.component.App;
  26. import com.qingdaofushan.homecaresas.data.AppUpDataItem;
  27. import com.qingdaofushan.homecaresas.downloadmanager.DefaultRetryPolicy;
  28. import com.qingdaofushan.homecaresas.downloadmanager.DownloadRequest;
  29. import com.qingdaofushan.homecaresas.downloadmanager.DownloadStatusListener;
  30. import com.qingdaofushan.homecaresas.downloadmanager.ThinDownloadManager;
  31. import com.qingdaofushan.homecaresas.utils.DeviceUtil;
  32. import com.qingdaofushan.homecaresas.utils.ToastUtil;
  33. import java.io.File;
  34. /**
  35. * apk版本升级检测类
  36. */
  37. public class VersionCheckingUtil {
  38. private Context mContext;
  39. private AppUpDataItem mAppUpDataItem;
  40. private DownloadDialog downloadDialog;
  41. private long mkeyTime;
  42. private int mType;
  43. private ThinDownloadManager thinDownloadManager;
  44. private static final int DOWNLOAD_THREAD_POOL_SIZE = 4;
  45. private File apkFile;
  46. private NotificationManager notificationManager;
  47. private Notification notification;
  48. /**
  49. * @param context
  50. * @param appUpDataItem
  51. * @param type 判断哪个类唤起的
  52. */
  53. public void isChecking(Context context, AppUpDataItem appUpDataItem, int type) {
  54. mContext = context;
  55. mAppUpDataItem = appUpDataItem;
  56. mType = type;
  57. //getUpdatelevel 是否强制更新
  58. if (!DeviceUtil.getVersionName(mContext).equals(appUpDataItem.getVersion())) {
  59. if (1 == appUpDataItem.getUpdateLevel()) {
  60. myShowDialog(false);
  61. } else {
  62. myShowDialog(true);
  63. }
  64. } else {
  65. if (mType == 2) {
  66. ToastUtil.showToast("已是最新版本!");
  67. }
  68. }
  69. notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
  70. initNotification();
  71. }
  72. //弹框升级款
  73. private void myShowDialog(final boolean b) {
  74. if (downloadDialog == null) {
  75. downloadDialog = new DownloadDialog(mContext);
  76. downloadDialog.setTitle("测到新版本,是否安装?");
  77. downloadDialog.setIsDismiss(b);
  78. downloadDialog.setMessage(mAppUpDataItem.getVersionDesc());
  79. downloadDialog.setOnClickBottomListener(new DownloadDialog.OnClickBottomListener() {
  80. @Override
  81. public void onPositiveClick() {
  82. downloadDialog.dismiss();
  83. // downloadDialog.setProgressBarVisibility(View.VISIBLE);
  84. showNotification();
  85. if (Build.VERSION.SDK_INT >= Q) {
  86. downLoadApkNew();
  87. } else {
  88. downloadApk();
  89. }
  90. }
  91. @Override
  92. public void onNegtiveClick() {
  93. if (b) {
  94. downloadDialog.dismiss();
  95. } else {
  96. // exit();
  97. }
  98. }
  99. });
  100. downloadDialog.show();
  101. }
  102. }
  103. @SuppressLint("NewApi")
  104. private void downLoadApkNew() {
  105. String filepath = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/updateApk/";
  106. String filename = "homelifemini.apk";
  107. //这一步,系统会自动为你在沙盒中创建文件夹
  108. apkFile = new File(filepath, filename);
  109. thinDownloadManager = new ThinDownloadManager(DOWNLOAD_THREAD_POOL_SIZE);
  110. Uri downloadUri = Uri.parse(mAppUpDataItem.getAppUrl());
  111. Uri destinationUri = Uri.fromFile(apkFile);
  112. DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
  113. .setRetryPolicy(new DefaultRetryPolicy())
  114. .setDestinationURI(destinationUri).setPriority(DownloadRequest.Priority.HIGH)
  115. .setDownloadContext(mContext)
  116. .setDeleteDestinationFileOnFailure(true)
  117. .setDownloadListener(new DownloadStatusListener() {
  118. @Override
  119. public void onDownloadComplete(int id) {
  120. cancelNotification();
  121. install(App.getInstance(), apkFile);
  122. }
  123. @Override
  124. public void onDownloadFailed(int id, int errorCode, String errorMessage) {
  125. }
  126. @Override
  127. public void onProgress(int id, long totalBytes, long downloadedBytes, int progress) {
  128. Log.e("pwx", "id = " + id + " ; total = " + totalBytes + " ; downloaded = " + downloadedBytes + " ; progress = " + progress);
  129. downloadDialog.setProgress(progress);
  130. RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification);
  131. //使用notification.xml文件作VIEW
  132. remoteViews.setProgressBar(R.id.pb, 100, 0, false);
  133. notification.contentView = remoteViews;
  134. // initNotification();
  135. notification.contentView.setProgressBar(R.id.pb, 100, progress, false);
  136. notification.contentView.setTextViewText(R.id.tv_progress, progress + "%");
  137. showNotification();
  138. }
  139. });
  140. thinDownloadManager.add(downloadRequest);
  141. }
  142. //下载方法
  143. private void downloadApk() {
  144. GetRequest<File> request = OkGo.get(mAppUpDataItem.getAppUrl());
  145. DownloadTask task = OkDownload.request(mAppUpDataItem.getVersion(), request).save().register(new DownloadListener(mAppUpDataItem.getVersion()) {
  146. @Override
  147. public void onStart(Progress progress) {
  148. }
  149. @Override
  150. public void onProgress(Progress progress) {
  151. int progressInt = (int) (progress.fraction * 100.0);
  152. downloadDialog.setProgress(progressInt);
  153. RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification);
  154. //使用notification.xml文件作VIEW
  155. remoteViews.setProgressBar(R.id.pb, 100, 0, false);
  156. notification.contentView = remoteViews;
  157. notification.contentView.setProgressBar(R.id.pb, 100, progressInt, false);
  158. notification.contentView.setTextViewText(R.id.tv_progress, progressInt + "%");
  159. showNotification();
  160. }
  161. @Override
  162. public void onError(Progress progress) {
  163. ToastUtil.showToast("网络错误,下载失败!");
  164. }
  165. @Override
  166. public void onFinish(final File file, Progress progress) {
  167. cancelNotification();
  168. install(App.getInstance(), file);
  169. }
  170. @Override
  171. public void onRemove(Progress progress) {
  172. }
  173. });
  174. task.start();
  175. }
  176. /**
  177. * 通过隐式意图调用系统安装程序安装APK
  178. */
  179. public void install(Context context, File file) {
  180. Intent intent = new Intent(Intent.ACTION_VIEW);
  181. // 由于没有在Activity环境下启动Activity,设置下面的标签
  182. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  183. //判读版本是否在7.0以上
  184. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  185. //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件
  186. Uri apkUri = FileProvider.getUriForFile(context, "com.rdgk.cemetery.fileprovider", file);
  187. //添加这一句表示对目标应用临时授权该Uri所代表的文件
  188. intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  189. intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  190. intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
  191. } else {
  192. intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
  193. }
  194. context.startActivity(intent);
  195. }
  196. //退出方法
  197. private void exit() {
  198. // if ((System.currentTimeMillis() - mkeyTime) > 2000) {
  199. // mkeyTime = System.currentTimeMillis();
  200. // ToastUtils.show("再按一次退出程序");
  201. // } else {
  202. // Intent intent = new Intent("com.sharedtalent.openhr.mvp.base.BaseMvpActivity");
  203. // intent.putExtra("closeAll", 1);
  204. // mContext.sendBroadcast(intent);//发送广播
  205. // Activity activity = null;
  206. // if (mType == 1) {
  207. // activity = (ShrHomeActivity) mContext;
  208. // } else if (mType == 2) {
  209. // activity = (SetAboutActivity) mContext;
  210. // }
  211. // activity.finish();
  212. // }
  213. }
  214. private void initNotification() {
  215. RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification);
  216. //使用notification.xml文件作VIEW
  217. remoteViews.setProgressBar(R.id.pb, 100, 0, false);
  218. //设置进度条,最大值 为100,当前值为0,最后一个参数为true时显示条纹
  219. //(就是在Android Market下载软件,点击下载但还没获取到目标大小时的状态)
  220. Intent notificationIntent = new Intent(mContext, MainActivity.class);
  221. PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
  222. notification = new NotificationCompat.Builder(mContext)
  223. .setChannelId(mContext.getPackageName())//channelId非常重要,不设置通知栏不展示
  224. .setContentIntent(contentIntent)
  225. .setContentTitle("应用更新")
  226. .setWhen(System.currentTimeMillis())
  227. .setContent(remoteViews)// 设置自定义通知栏布局,support兼容包25以下,找不到此方法,需要使用setContent(remoteViews)
  228. .setPriority(Notification.PRIORITY_HIGH)// 设置优先级,低优先级可能被隐藏
  229. .setSmallIcon(R.mipmap.ic_launcher)
  230. .setOnlyAlertOnce(true)
  231. .build();
  232. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  233. NotificationChannel mChannel = new NotificationChannel(mContext.getPackageName(), "robot", NotificationManager.IMPORTANCE_HIGH);
  234. notificationManager.createNotificationChannel(mChannel);
  235. }
  236. // notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
  237. // notification = new Notification(R.mipmap.app_icon, "应用更新", System.currentTimeMillis());
  238. // notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.notification);
  239. // //使用notification.xml文件作VIEW
  240. // notification.contentView.setProgressBar(R.id.pb, 100, 0, false);
  241. // //设置进度条,最大值 为100,当前值为0,最后一个参数为true时显示条纹
  242. // //(就是在Android Market下载软件,点击下载但还没获取到目标大小时的状态)
  243. // Intent notificationIntent = new Intent(mContext, ShrHomeActivity.class);
  244. // PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
  245. // notification.contentIntent = contentIntent;
  246. }
  247. /**
  248. * Called when the activity is first created.
  249. */
  250. private int notification_id = 101;
  251. public void showNotification() {
  252. notificationManager.notify(notification_id, notification);
  253. }
  254. public void cancelNotification() {
  255. notificationManager.cancel(notification_id);
  256. }
  257. }