package com.gh.download; 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.os.Build; import android.support.v4.app.NotificationCompat; import com.gh.common.util.AppDebugConfig; import com.gh.common.util.EntranceUtils; import com.gh.common.util.PlatformUtils; import com.gh.gamecenter.R; import com.lightgame.download.DownloadEntity; import com.lightgame.download.DownloadStatus; import java.util.List; /** * @author CsHeng * @Date 26/06/2017 * @Time 6:01 PM */ public class DownloadNotification { public static final String ACTION_INSTALL = "com.gh.gamecenter.INSTALL"; public static final String ACTION_DOWNLOAD = "com.gh.gamecenter.DOWNLOAD"; public static final int ICON = R.drawable.logo; public static final int NOTIFY_ID = 0x123; public static final int WAIT_NOTIFY_ID = 0x124; private static final String CHANNEL_ID = "Halo_Download"; private static NotificationManager getNotificationManager(Context context) { return (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); } //TODO 下载重构 public static void showDownloadDoneNotification(Context context, DownloadEntity downloadEntity) { final NotificationManager manager = getNotificationManager(context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW); manager.createNotificationChannel(channel); } Intent intent = new Intent(); intent.putExtra(EntranceUtils.KEY_PATH, downloadEntity.getPath()); intent.setAction(ACTION_INSTALL); final int requestCode = downloadEntity.getPackageName().hashCode(); final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); String text; String title; if (downloadEntity.isPluggable()) { title = "下载完成,点击继续插件化"; text = downloadEntity.getName() + " - " + PlatformUtils.getInstance(context).getPlatformName(downloadEntity.getPlatform()); } else { if (downloadEntity.isPlugin()) { text = downloadEntity.getName() + " - " + PlatformUtils.getInstance(context).getPlatformName(downloadEntity.getPlatform()); } else { text = downloadEntity.getName(); } title = "下载完成,点击立即安装"; } final Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.logo) .setTicker(title) .setContentTitle(text) .setContentText(title) .setContentIntent(pendingIntent).build(); // notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音 notification.flags |= Notification.FLAG_AUTO_CANCEL; // // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。 manager.notify(requestCode, notification); } public static void showDownloadingNotification(Context context) { if (AppDebugConfig.IS_DEBUG) { AppDebugConfig.logMethodWithParams(DownloadNotification.class, context); } int downloadingSize = 0; final List list = DownloadManager.getInstance(context).getAll(); if (list == null) return; for (DownloadEntity entity : list) { if (entity.getStatus().equals(DownloadStatus.downloading) || entity.getStatus().equals(DownloadStatus.waiting) || entity.getStatus().equals(DownloadStatus.pause) || entity.getStatus().equals(DownloadStatus.timeout) || entity.getStatus().equals(DownloadStatus.neterror)) { downloadingSize++; } } NotificationManager nManager = getNotificationManager(context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW); nManager.createNotificationChannel(channel); } if (downloadingSize == 0) { nManager.cancel(NOTIFY_ID); } else { Intent intent = new Intent(); intent.setAction(ACTION_DOWNLOAD); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(ICON) .setTicker("点击查看") .setContentTitle("正在下载" + downloadingSize + "个游戏") .setContentText("点击查看") .setContentIntent(pendingIntent).build(); // notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音 notification.flags |= Notification.FLAG_NO_CLEAR; // 通知无法手动清除 nManager.notify(NOTIFY_ID, notification); } } public static void showWaitingNotification(Context context) { if (AppDebugConfig.IS_DEBUG) { AppDebugConfig.logMethodWithParams(DownloadNotification.class, context); } NotificationManager nManager = getNotificationManager(context); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW); nManager.createNotificationChannel(channel); } Intent intent = new Intent(); intent.setAction(ACTION_DOWNLOAD); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(ICON) .setTicker("连接WiFi后自动恢复下载") .setContentTitle("下载已暂停") .setContentText("连接WiFi后自动恢复下载") .setContentIntent(pendingIntent).build(); notification.flags = Notification.FLAG_AUTO_CANCEL; if (nManager != null) nManager.notify(WAIT_NOTIFY_ID, notification); } }