|
|
@@ -2,20 +2,27 @@ package com.sheep.jiuyan.samllsheep.service;
|
|
|
|
|
|
import android.app.Activity;
|
|
|
import android.app.DownloadManager;
|
|
|
+import android.app.Notification;
|
|
|
+import android.app.NotificationChannel;
|
|
|
+import android.app.NotificationManager;
|
|
|
+import android.app.PendingIntent;
|
|
|
import android.app.Service;
|
|
|
import android.content.Context;
|
|
|
import android.content.DialogInterface;
|
|
|
import android.content.Intent;
|
|
|
import android.net.Uri;
|
|
|
+import android.os.Build;
|
|
|
import android.os.Handler;
|
|
|
import android.os.IBinder;
|
|
|
import android.os.Message;
|
|
|
import android.support.annotation.NonNull;
|
|
|
import android.support.annotation.Nullable;
|
|
|
+import android.support.v4.app.NotificationCompat;
|
|
|
import android.support.v7.app.AlertDialog;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
import android.view.View;
|
|
|
+import android.widget.RemoteViews;
|
|
|
|
|
|
import com.kfzs.duanduan.utils.ApkUtils;
|
|
|
import com.kfzs.duanduan.view.DialogStorageLow;
|
|
|
@@ -38,15 +45,18 @@ import com.sheep.gamegroup.util.FileUtil;
|
|
|
import com.sheep.gamegroup.util.LogUtil;
|
|
|
import com.sheep.gamegroup.util.StringUtils;
|
|
|
import com.sheep.gamegroup.util.TestUtil;
|
|
|
+import com.sheep.jiuyan.samllsheep.R;
|
|
|
import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
import com.sheep.jiuyan.samllsheep.utils.G;
|
|
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
|
|
|
|
import java.io.File;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
import java.util.Map;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
|
|
|
import static com.sheep.jiuyan.samllsheep.utils.ClassFileHelper.DIR;
|
|
|
|
|
|
@@ -61,34 +71,13 @@ public class DownloadService extends Service {
|
|
|
private final static int TAG_TYPE = 1002;
|
|
|
|
|
|
private DownloadUtil downloadUtil;
|
|
|
+ private static Handler mHandler;
|
|
|
|
|
|
@Override
|
|
|
public void onCreate() {
|
|
|
super.onCreate();
|
|
|
- this.
|
|
|
- downloadUtil = new DownloadUtil();
|
|
|
-// Util.setLogger(new Util.Logger() {
|
|
|
-// @Override
|
|
|
-// public void e(String tag, String msg, Exception e) {
|
|
|
-// Log.e("sheep-" + tag, msg + "\n" + e.getMessage());
|
|
|
-// e.printStackTrace();
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public void w(String tag, String msg) {
|
|
|
-// Log.w("sheep-" + tag, msg);
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public void d(String tag, String msg) {
|
|
|
-// Log.d("sheep-" + tag, msg);
|
|
|
-// }
|
|
|
-//
|
|
|
-// @Override
|
|
|
-// public void i(String tag, String msg) {
|
|
|
-// Log.i("sheep-" + tag, msg);
|
|
|
-// }
|
|
|
-// });
|
|
|
+ this.downloadUtil = new DownloadUtil();
|
|
|
+ this.mHandler = new Handler();
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -96,11 +85,17 @@ public class DownloadService extends Service {
|
|
|
if (intent != null) {
|
|
|
String download_url = intent.getStringExtra("download_url");
|
|
|
String file_path = intent.getStringExtra("file_path");
|
|
|
- String type = intent.getStringExtra("type");
|
|
|
- if (type == null) {
|
|
|
- type = FileUtil.getExtensionName(file_path);
|
|
|
+ if (intent.hasExtra("download_id")) {
|
|
|
+ DownloadNotification.delete(download_url);
|
|
|
+ } else if (intent.hasExtra("install_path")) {
|
|
|
+ DownloadNotification.install(download_url, intent.getStringExtra("install_path"));
|
|
|
+ } else {
|
|
|
+ String type = intent.getStringExtra("type");
|
|
|
+ if (type == null) {
|
|
|
+ type = FileUtil.getExtensionName(file_path);
|
|
|
+ }
|
|
|
+ startDownload(download_url, file_path, type, 0);
|
|
|
}
|
|
|
- startDownload(download_url, file_path, type, 0);
|
|
|
}
|
|
|
return super.onStartCommand(intent, flags, startId);
|
|
|
}
|
|
|
@@ -152,6 +147,8 @@ public class DownloadService extends Service {
|
|
|
DownLoadInfo downloadInfo = downloadUtil.setDownloadTaskTotal(task.getUrl(), totalLength);
|
|
|
if (downloadInfo != null) {
|
|
|
EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_START).setData(downloadInfo));
|
|
|
+ } else {
|
|
|
+ new DownloadNotification(task, totalLength).show();
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
@@ -174,6 +171,8 @@ public class DownloadService extends Service {
|
|
|
DownLoadInfo downloadInfo = downloadUtil.setDownloadTaskPercent(currentOffset, taskSpeed.averageSpeed(), task.getId(), task.getUrl());
|
|
|
if (downloadInfo != null) {
|
|
|
EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_RUNNING).setData(downloadInfo));
|
|
|
+ } else {
|
|
|
+ DownloadNotification.progress(task.getUrl(), currentOffset);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
@@ -205,6 +204,7 @@ public class DownloadService extends Service {
|
|
|
EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DOWNLOAD_COMPLETE).setData(downLoadInfo));
|
|
|
}
|
|
|
ApkUtils.installApk(getApplicationContext(), task.getFile().getAbsolutePath());
|
|
|
+ DownloadNotification.progress(task.getUrl(), -1l);
|
|
|
}
|
|
|
break;
|
|
|
case PRE_ALLOCATE_FAILED://预先分配失败
|
|
|
@@ -386,4 +386,115 @@ public class DownloadService extends Service {
|
|
|
G.showToast("浏览器开始下载apk");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private static class DownloadNotification {
|
|
|
+
|
|
|
+ private static AtomicInteger INCREMENT = new AtomicInteger(1);
|
|
|
+
|
|
|
+ private static Map<String, DownloadNotification> notificationMap = new HashMap<>();
|
|
|
+
|
|
|
+ private NotificationManager notificationManager;
|
|
|
+
|
|
|
+ private Integer id;
|
|
|
+ private Long total;
|
|
|
+ private DownloadTask task;
|
|
|
+ private Notification notification;
|
|
|
+ private RemoteViews view;
|
|
|
+ private boolean completed = false;
|
|
|
+
|
|
|
+ public DownloadNotification(DownloadTask t, Long totalSize) {
|
|
|
+ notificationManager = (NotificationManager) SheepApp.getInstance().getSystemService(NOTIFICATION_SERVICE);
|
|
|
+ id = INCREMENT.incrementAndGet();
|
|
|
+ task = t;
|
|
|
+ total = totalSize;
|
|
|
+ if (id == 1000) {
|
|
|
+ INCREMENT.set(1);
|
|
|
+ id = 1;
|
|
|
+ }
|
|
|
+ view = new RemoteViews(SheepApp.getInstance().getPackageName(), R.layout.download_notification);
|
|
|
+ view.setTextViewText(R.id.title_view, SheepApp.getInstance().getString(R.string.app_name) + "下载中......");
|
|
|
+ Intent din = new Intent(SheepApp.getInstance(), DownloadService.class).putExtra("download_id", id).putExtra("download_url", task.getUrl());
|
|
|
+ PendingIntent delIntent = PendingIntent.getService(SheepApp.getInstance(), 0, din, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
+ view.setOnClickPendingIntent(R.id.delete_btn, delIntent);
|
|
|
+
|
|
|
+ Intent iin = new Intent(SheepApp.getInstance(), DownloadService.class).putExtra("install_path", task.getFile().getAbsolutePath()).putExtra("download_url", task.getUrl());
|
|
|
+ PendingIntent installIntent = PendingIntent.getService(SheepApp.getInstance(), 0, iin, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
+ view.setOnClickPendingIntent(R.id.body_view, installIntent);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void refreshProgress(long offset) {
|
|
|
+ int progress = (offset == 01l) ? 100 : (int) (offset * 100 / total);
|
|
|
+ view.setProgressBar(R.id.progress_view, 100, progress, false);
|
|
|
+ if (progress == 100) {
|
|
|
+ view.setTextViewText(R.id.title_view, SheepApp.getInstance().getString(R.string.app_name) + "下载完成");
|
|
|
+ completed = true;
|
|
|
+ }
|
|
|
+ notificationManager.notify(id, notification);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void progress(String url, long offset) {
|
|
|
+ DownloadNotification notification = notificationMap.get(url);
|
|
|
+ if (notification != null) {
|
|
|
+ mHandler.post(() -> notification.refreshProgress(offset));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void install(String url, String path) {
|
|
|
+ DownloadNotification notification = notificationMap.get(url);
|
|
|
+ if (notification != null && notification.completed) {
|
|
|
+ notificationMap.remove(url);
|
|
|
+ notification.task.cancel();
|
|
|
+ NotificationManager nMgr = (NotificationManager) SheepApp.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+ if (nMgr != null) {
|
|
|
+ nMgr.cancel(notification.id);
|
|
|
+ }
|
|
|
+ LogUtil.logE("Download install path: " + path);
|
|
|
+ ApkUtils.installApk(SheepApp.getInstance(), path);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void delete(String url) {
|
|
|
+ DownloadNotification notification = notificationMap.get(url);
|
|
|
+ if (notification != null) {
|
|
|
+ notificationMap.remove(url);
|
|
|
+ notification.task.cancel();
|
|
|
+ if (notification.task.getFile().exists()) {
|
|
|
+ notification.task.getFile().delete();
|
|
|
+ }
|
|
|
+ NotificationManager nMgr = (NotificationManager) SheepApp.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+ if (nMgr != null) {
|
|
|
+ nMgr.cancel(notification.id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void show() {
|
|
|
+ String channelId = "sheep_download_notification";
|
|
|
+ String name = SheepApp.getInstance().getString(R.string.app_name);
|
|
|
+ Notification notification;
|
|
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
+ NotificationChannel mChannel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_HIGH);
|
|
|
+ notificationManager.createNotificationChannel(mChannel);
|
|
|
+ notification = new NotificationCompat.Builder(SheepApp.getInstance(), channelId)
|
|
|
+ .setCustomContentView(view)
|
|
|
+ .setContentTitle(SheepApp.getInstance().getString(R.string.app_name))
|
|
|
+ .setContentText("下载中")
|
|
|
+ .setSmallIcon(R.mipmap.ic_launcher)
|
|
|
+ .setOngoing(true)
|
|
|
+ .build();
|
|
|
+ } else {
|
|
|
+ NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(SheepApp.getInstance(), channelId)
|
|
|
+ .setCustomContentView(view)
|
|
|
+ .setContentTitle(SheepApp.getInstance().getString(R.string.app_name))
|
|
|
+ .setContentText("下载中")
|
|
|
+ .setSmallIcon(R.mipmap.ic_launcher)
|
|
|
+ .setOngoing(true);
|
|
|
+ notification = notificationBuilder.build();
|
|
|
+ }
|
|
|
+ notificationManager.notify(id, notification);
|
|
|
+ this.notification = notification;
|
|
|
+ notificationMap.put(task.getUrl(), this);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|