Selaa lähdekoodia

download notification

billyyoyo 6 vuotta sitten
vanhempi
commit
b7e06c2ee6

+ 5 - 0
app/src/main/java/com/sheep/gamegroup/util/js/KFZSJs.java

@@ -489,6 +489,11 @@ public class KFZSJs {
     }
 
     @JavascriptInterface
+    public void buyVip(){
+        Jump2View.getInstance().gotoVip(activity);
+    }
+
+    @JavascriptInterface
     public void checkError(Object error, Object data, String errorMsg) {
         String title = "提示";
         String btnRightText = "确认";

+ 139 - 28
app/src/main/java/com/sheep/jiuyan/samllsheep/service/DownloadService.java

@@ -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);
+        }
+
+    }
 }

+ 48 - 0
app/src/main/res/layout/download_notification.xml

@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="utf-8"?>
+<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="60dp"
+    android:id="@+id/body_view"
+    android:paddingLeft="16dp"
+    android:paddingRight="16dp">
+
+    <ImageView
+        android:id="@+id/icon_view"
+        android:src="@mipmap/icon_unread"
+        android:layout_centerVertical="true"
+        android:layout_width="@dimen/dp_40"
+        android:layout_height="@dimen/dp_40" />
+
+    <TextView
+        android:id="@+id/title_view"
+        android:layout_toRightOf="@+id/icon_view"
+        android:layout_marginLeft="12dp"
+        android:textSize="@dimen/sp_14"
+        android:layout_marginTop="12dp"
+        android:textColor="@color/white"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content" />
+
+    <ProgressBar
+        android:id="@+id/progress_view"
+        style="?android:attr/progressBarStyleHorizontal"
+        android:max="100"
+        android:progress="0"
+        android:layout_margin="10dp"
+        android:layout_alignParentBottom="true"
+        android:layout_toRightOf="@+id/icon_view"
+        android:layout_toLeftOf="@+id/delete_btn"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content" />
+
+    <ImageView
+        android:id="@+id/delete_btn"
+        android:src="@mipmap/cross_delete"
+        android:padding="10dp"
+        android:layout_alignParentRight="true"
+        android:layout_centerVertical="true"
+        android:layout_width="@dimen/view_size_30"
+        android:layout_height="@dimen/view_size_30"
+        android:tint="@color/white"/>
+
+</RelativeLayout>