Files
assistant-android/app/src/main/java/com/gh/download/DownloadNotification.java
2017-10-16 16:49:22 +08:00

107 lines
4.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.gh.download;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
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;
private static final int NOTIFY_ID = 0x123;
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);
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)
.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<DownloadEntity> list = DownloadManager.getInstance(context).getAll();
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 = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
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)
.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);
}
}
}