| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- package com.qingdaofushan.homecaresas.view;
- import static android.os.Build.VERSION_CODES.Q;
- import android.annotation.SuppressLint;
- import android.app.Notification;
- import android.app.NotificationChannel;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Build;
- import android.os.Environment;
- import android.util.Log;
- import android.widget.RemoteViews;
- import androidx.core.app.NotificationCompat;
- import androidx.core.content.FileProvider;
- import com.lzy.okgo.OkGo;
- import com.lzy.okgo.model.Progress;
- import com.lzy.okgo.request.GetRequest;
- import com.lzy.okserver.OkDownload;
- import com.lzy.okserver.download.DownloadListener;
- import com.lzy.okserver.download.DownloadTask;
- import com.qingdaofushan.homecaresas.R;
- import com.qingdaofushan.homecaresas.activity.MainActivity;
- import com.qingdaofushan.homecaresas.component.App;
- import com.qingdaofushan.homecaresas.data.AppUpDataItem;
- import com.qingdaofushan.homecaresas.downloadmanager.DefaultRetryPolicy;
- import com.qingdaofushan.homecaresas.downloadmanager.DownloadRequest;
- import com.qingdaofushan.homecaresas.downloadmanager.DownloadStatusListener;
- import com.qingdaofushan.homecaresas.downloadmanager.ThinDownloadManager;
- import com.qingdaofushan.homecaresas.utils.DeviceUtil;
- import com.qingdaofushan.homecaresas.utils.ToastUtil;
- import java.io.File;
- /**
- * apk版本升级检测类
- */
- public class VersionCheckingUtil {
- private Context mContext;
- private AppUpDataItem mAppUpDataItem;
- private DownloadDialog downloadDialog;
- private long mkeyTime;
- private int mType;
- private ThinDownloadManager thinDownloadManager;
- private static final int DOWNLOAD_THREAD_POOL_SIZE = 4;
- private File apkFile;
- private NotificationManager notificationManager;
- private Notification notification;
- /**
- * @param context
- * @param appUpDataItem
- * @param type 判断哪个类唤起的
- */
- public void isChecking(Context context, AppUpDataItem appUpDataItem, int type) {
- mContext = context;
- mAppUpDataItem = appUpDataItem;
- mType = type;
- //getUpdatelevel 是否强制更新
- if (!DeviceUtil.getVersionName(mContext).equals(appUpDataItem.getVersion())) {
- if (1 == appUpDataItem.getUpdateLevel()) {
- myShowDialog(false);
- } else {
- myShowDialog(true);
- }
- } else {
- if (mType == 2) {
- ToastUtil.showToast("已是最新版本!");
- }
- }
- notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
- initNotification();
- }
- //弹框升级款
- private void myShowDialog(final boolean b) {
- if (downloadDialog == null) {
- downloadDialog = new DownloadDialog(mContext);
- downloadDialog.setTitle("测到新版本,是否安装?");
- downloadDialog.setIsDismiss(b);
- downloadDialog.setMessage(mAppUpDataItem.getVersionDesc());
- downloadDialog.setOnClickBottomListener(new DownloadDialog.OnClickBottomListener() {
- @Override
- public void onPositiveClick() {
- downloadDialog.dismiss();
- // downloadDialog.setProgressBarVisibility(View.VISIBLE);
- showNotification();
- if (Build.VERSION.SDK_INT >= Q) {
- downLoadApkNew();
- } else {
- downloadApk();
- }
- }
- @Override
- public void onNegtiveClick() {
- if (b) {
- downloadDialog.dismiss();
- } else {
- // exit();
- }
- }
- });
- downloadDialog.show();
- }
- }
- @SuppressLint("NewApi")
- private void downLoadApkNew() {
- String filepath = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/updateApk/";
- String filename = "homelifemini.apk";
- //这一步,系统会自动为你在沙盒中创建文件夹
- apkFile = new File(filepath, filename);
- thinDownloadManager = new ThinDownloadManager(DOWNLOAD_THREAD_POOL_SIZE);
- Uri downloadUri = Uri.parse(mAppUpDataItem.getAppUrl());
- Uri destinationUri = Uri.fromFile(apkFile);
- DownloadRequest downloadRequest = new DownloadRequest(downloadUri)
- .setRetryPolicy(new DefaultRetryPolicy())
- .setDestinationURI(destinationUri).setPriority(DownloadRequest.Priority.HIGH)
- .setDownloadContext(mContext)
- .setDeleteDestinationFileOnFailure(true)
- .setDownloadListener(new DownloadStatusListener() {
- @Override
- public void onDownloadComplete(int id) {
- cancelNotification();
- install(App.getInstance(), apkFile);
- }
- @Override
- public void onDownloadFailed(int id, int errorCode, String errorMessage) {
- }
- @Override
- public void onProgress(int id, long totalBytes, long downloadedBytes, int progress) {
- Log.e("pwx", "id = " + id + " ; total = " + totalBytes + " ; downloaded = " + downloadedBytes + " ; progress = " + progress);
- downloadDialog.setProgress(progress);
- RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification);
- //使用notification.xml文件作VIEW
- remoteViews.setProgressBar(R.id.pb, 100, 0, false);
- notification.contentView = remoteViews;
- // initNotification();
- notification.contentView.setProgressBar(R.id.pb, 100, progress, false);
- notification.contentView.setTextViewText(R.id.tv_progress, progress + "%");
- showNotification();
- }
- });
- thinDownloadManager.add(downloadRequest);
- }
- //下载方法
- private void downloadApk() {
- GetRequest<File> request = OkGo.get(mAppUpDataItem.getAppUrl());
- DownloadTask task = OkDownload.request(mAppUpDataItem.getVersion(), request).save().register(new DownloadListener(mAppUpDataItem.getVersion()) {
- @Override
- public void onStart(Progress progress) {
- }
- @Override
- public void onProgress(Progress progress) {
- int progressInt = (int) (progress.fraction * 100.0);
- downloadDialog.setProgress(progressInt);
- RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification);
- //使用notification.xml文件作VIEW
- remoteViews.setProgressBar(R.id.pb, 100, 0, false);
- notification.contentView = remoteViews;
- notification.contentView.setProgressBar(R.id.pb, 100, progressInt, false);
- notification.contentView.setTextViewText(R.id.tv_progress, progressInt + "%");
- showNotification();
- }
- @Override
- public void onError(Progress progress) {
- ToastUtil.showToast("网络错误,下载失败!");
- }
- @Override
- public void onFinish(final File file, Progress progress) {
- cancelNotification();
- install(App.getInstance(), file);
- }
- @Override
- public void onRemove(Progress progress) {
- }
- });
- task.start();
- }
- /**
- * 通过隐式意图调用系统安装程序安装APK
- */
- public void install(Context context, File file) {
- Intent intent = new Intent(Intent.ACTION_VIEW);
- // 由于没有在Activity环境下启动Activity,设置下面的标签
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- //判读版本是否在7.0以上
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
- //参数1 上下文, 参数2 Provider主机地址 和配置文件中保持一致 参数3 共享的文件
- Uri apkUri = FileProvider.getUriForFile(context, "com.rdgk.cemetery.fileprovider", file);
- //添加这一句表示对目标应用临时授权该Uri所代表的文件
- intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
- intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
- intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
- } else {
- intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
- }
- context.startActivity(intent);
- }
- //退出方法
- private void exit() {
- // if ((System.currentTimeMillis() - mkeyTime) > 2000) {
- // mkeyTime = System.currentTimeMillis();
- // ToastUtils.show("再按一次退出程序");
- // } else {
- // Intent intent = new Intent("com.sharedtalent.openhr.mvp.base.BaseMvpActivity");
- // intent.putExtra("closeAll", 1);
- // mContext.sendBroadcast(intent);//发送广播
- // Activity activity = null;
- // if (mType == 1) {
- // activity = (ShrHomeActivity) mContext;
- // } else if (mType == 2) {
- // activity = (SetAboutActivity) mContext;
- // }
- // activity.finish();
- // }
- }
- private void initNotification() {
- RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification);
- //使用notification.xml文件作VIEW
- remoteViews.setProgressBar(R.id.pb, 100, 0, false);
- //设置进度条,最大值 为100,当前值为0,最后一个参数为true时显示条纹
- //(就是在Android Market下载软件,点击下载但还没获取到目标大小时的状态)
- Intent notificationIntent = new Intent(mContext, MainActivity.class);
- PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
- notification = new NotificationCompat.Builder(mContext)
- .setChannelId(mContext.getPackageName())//channelId非常重要,不设置通知栏不展示
- .setContentIntent(contentIntent)
- .setContentTitle("应用更新")
- .setWhen(System.currentTimeMillis())
- .setContent(remoteViews)// 设置自定义通知栏布局,support兼容包25以下,找不到此方法,需要使用setContent(remoteViews)
- .setPriority(Notification.PRIORITY_HIGH)// 设置优先级,低优先级可能被隐藏
- .setSmallIcon(R.mipmap.ic_launcher)
- .setOnlyAlertOnce(true)
- .build();
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- NotificationChannel mChannel = new NotificationChannel(mContext.getPackageName(), "robot", NotificationManager.IMPORTANCE_HIGH);
- notificationManager.createNotificationChannel(mChannel);
- }
- // notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
- // notification = new Notification(R.mipmap.app_icon, "应用更新", System.currentTimeMillis());
- // notification.contentView = new RemoteViews(mContext.getPackageName(), R.layout.notification);
- // //使用notification.xml文件作VIEW
- // notification.contentView.setProgressBar(R.id.pb, 100, 0, false);
- // //设置进度条,最大值 为100,当前值为0,最后一个参数为true时显示条纹
- // //(就是在Android Market下载软件,点击下载但还没获取到目标大小时的状态)
- // Intent notificationIntent = new Intent(mContext, ShrHomeActivity.class);
- // PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
- // notification.contentIntent = contentIntent;
- }
- /**
- * Called when the activity is first created.
- */
- private int notification_id = 101;
- public void showNotification() {
- notificationManager.notify(notification_id, notification);
- }
- public void cancelNotification() {
- notificationManager.cancel(notification_id);
- }
- }
|