From 81e8b203b673f6cdce335d1afaa32e8c20ce596f Mon Sep 17 00:00:00 2001 From: sfsheng0322 Date: Sun, 18 Feb 2018 21:41:20 +0800 Subject: [PATCH] init --- .../java/com/sunfusheng/FirDownloader.java | 12 +--- .../java/com/sunfusheng/FirNotification.java | 58 +++++++++++++++++++ .../main/java/com/sunfusheng/FirUpdater.java | 17 +++++- .../java/com/sunfusheng/FirUpdaterUtils.java | 16 ++--- 4 files changed, 84 insertions(+), 19 deletions(-) diff --git a/FirUpdater/src/main/java/com/sunfusheng/FirDownloader.java b/FirUpdater/src/main/java/com/sunfusheng/FirDownloader.java index afcc98c..aa75bc9 100644 --- a/FirUpdater/src/main/java/com/sunfusheng/FirDownloader.java +++ b/FirUpdater/src/main/java/com/sunfusheng/FirDownloader.java @@ -34,6 +34,7 @@ public class FirDownloader { private volatile int currLength;//当前总共下载的大小 private volatile int threadCountRunning;//正在运行的线程数 private String stateDownload = DOWNLOAD_INIT;//当前线程状态 + private FirNotification firNotification; private DownloadThread[] threadArr; @@ -52,22 +53,13 @@ interface OnDownLoadListener { } public FirDownloader(Context context, String loadUrl, String filePath) { - this(context, loadUrl, filePath, DEFAULT_THREAD_COUNT, null); + this(context, loadUrl, filePath, null); } public FirDownloader(Context context, String loadUrl, String filePath, OnDownLoadListener onDownLoadListener) { - this(context, loadUrl, filePath, DEFAULT_THREAD_COUNT, onDownLoadListener); - } - - public FirDownloader(Context context, String loadUrl, String filePath, int threadCount) { - this(context, loadUrl, filePath, threadCount, null); - } - - public FirDownloader(Context context, String loadUrl, String filePath, int threadCount, OnDownLoadListener onDownLoadListener) { this.context = context; this.loadUrl = loadUrl; this.filePath = filePath; - this.threadCount = threadCount; this.threadCountRunning = 0; this.onDownLoadListener = onDownLoadListener; } diff --git a/FirUpdater/src/main/java/com/sunfusheng/FirNotification.java b/FirUpdater/src/main/java/com/sunfusheng/FirNotification.java index d205ef5..5541d09 100644 --- a/FirUpdater/src/main/java/com/sunfusheng/FirNotification.java +++ b/FirUpdater/src/main/java/com/sunfusheng/FirNotification.java @@ -1,7 +1,65 @@ package com.sunfusheng; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.support.v4.app.NotificationCompat; + /** * @author sunfusheng on 2018/2/17. */ public class FirNotification { + + public static final String CHANNEL_ID = "FirNotification"; + public static final int NOTIFICATION_ID = 1234; + + private Context context; + private NotificationManager manager; + private NotificationCompat.Builder builder; + + public void createBuilder(Context context) { + this.context = context; + manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + builder = new NotificationCompat.Builder(context, CHANNEL_ID); + builder.setSmallIcon(android.R.drawable.stat_sys_download); + builder.setAutoCancel(false); + builder.setOngoing(true); + builder.setShowWhen(false); + builder.setProgress(100, 7, false); + builder.setOngoing(true); + builder.setShowWhen(false); + builder.setDefaults(NotificationCompat.DEFAULT_ALL); + } + + public void setContentTitle(String title) { + builder.setContentTitle(title); + } + + public void setContentText(String text) { + builder.setContentText(text); + } + + public void setIcon(int resId) { + builder.setSmallIcon(resId); + } + + public void cancel() { + manager.notify(NOTIFICATION_ID, builder.build()); + manager.cancel(NOTIFICATION_ID); + } + + public void setContentIntent(Intent intent) { + PendingIntent pIntent = PendingIntent.getActivity(context, 1, intent, 0); + builder.setContentIntent(pIntent); + } + + public void notifyNotification(int progress) { + builder.setProgress(100, progress, false); + manager.notify(NOTIFICATION_ID, builder.build()); + } + + public void notifyNotification() { + manager.notify(NOTIFICATION_ID, builder.build()); + } } diff --git a/FirUpdater/src/main/java/com/sunfusheng/FirUpdater.java b/FirUpdater/src/main/java/com/sunfusheng/FirUpdater.java index 1e3356d..55be876 100644 --- a/FirUpdater/src/main/java/com/sunfusheng/FirUpdater.java +++ b/FirUpdater/src/main/java/com/sunfusheng/FirUpdater.java @@ -19,6 +19,9 @@ public class FirUpdater implements FirDialog.OnClickDownloadDialogListener { private Context context; private FirDialog firDialog; private FirDownloader firDownloader; + private FirNotification firNotification; + + private boolean isBackgroundDownload; public FirUpdater(Context context, String apiToken, String appId) { this.context = context; @@ -46,17 +49,29 @@ public void onClickDownload(DialogInterface dialog) { String fileName = FirAppInfo.appName + "-V" + FirAppInfo.appVersionName + ".apk"; String filePath = Environment.getExternalStorageDirectory() + File.separator + fileName; + firNotification = new FirNotification(); + firNotification.createBuilder(context); + firNotification.setContentTitle("正在下载" + FirAppInfo.appName); + firDownloader = new FirDownloader(context, FirAppInfo.appInstallUrl, filePath); firDownloader.setOnDownLoadListener(new FirDownloader.OnDownLoadListener() { @Override public void onProgress(int totalLength, int currLength, int progress) { Log.d("--->", "progress: " + progress + " totalLength: " + totalLength + " currLength: " + currLength); firDialog.showDownloadDialog(context, progress); + + if (isBackgroundDownload) { + firNotification.setContentText(progress + "%"); + firNotification.notifyNotification(progress); + } } @Override public void onSuccess() { firDialog.dismissDownloadDialog(); + if (isBackgroundDownload) { + firNotification.cancel(); + } FirUpdaterUtils.installApk(context, filePath); } @@ -70,7 +85,7 @@ public void onError() { @Override public void onClickBackgroundDownload(DialogInterface dialog) { - + isBackgroundDownload = true; } @Override diff --git a/FirUpdater/src/main/java/com/sunfusheng/FirUpdaterUtils.java b/FirUpdater/src/main/java/com/sunfusheng/FirUpdaterUtils.java index d3ebe1f..9d95ad7 100644 --- a/FirUpdater/src/main/java/com/sunfusheng/FirUpdaterUtils.java +++ b/FirUpdater/src/main/java/com/sunfusheng/FirUpdaterUtils.java @@ -11,7 +11,6 @@ import android.os.Handler; import android.os.Looper; import android.support.v4.content.FileProvider; -import android.text.TextUtils; import java.io.Closeable; import java.io.File; @@ -108,11 +107,7 @@ public static boolean isAppForeground(Context context) { return false; } - public static void installApk(Context context, String apkPath) { - if (TextUtils.isEmpty(apkPath)) { - return; - } - + public static Intent getInstallApkIntent(Context context, String apkPath) { try { File apkFile = new File(apkPath); if (!apkFile.exists()) { @@ -122,17 +117,22 @@ public static void installApk(Context context, String apkPath) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri apkUri; - if (Build.VERSION.SDK_INT >= 24) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { apkUri = FileProvider.getUriForFile(context, getPackageName(context) + ".file_provider", apkFile); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { apkUri = Uri.fromFile(apkFile); } intent.setDataAndType(apkUri, "application/vnd.android.package-archive"); - context.startActivity(intent); + return intent; } catch (Exception e) { e.printStackTrace(); } + return null; + } + + public static void installApk(Context context, String apkPath) { + context.startActivity(getInstallApkIntent(context, apkPath)); } public static void closeQuietly(final Closeable... closeables) {