|
|
|
@ -1,19 +1,12 @@ |
|
|
|
|
package com.king.app.updater.service; |
|
|
|
|
|
|
|
|
|
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.Intent; |
|
|
|
|
import android.os.Binder; |
|
|
|
|
import android.os.Build; |
|
|
|
|
import android.os.IBinder; |
|
|
|
|
import android.support.annotation.DrawableRes; |
|
|
|
|
import android.support.annotation.Nullable; |
|
|
|
|
import android.support.annotation.RequiresApi; |
|
|
|
|
import android.support.v4.app.NotificationCompat; |
|
|
|
|
import android.support.v4.content.ContextCompat; |
|
|
|
|
import android.text.TextUtils; |
|
|
|
|
import android.util.Log; |
|
|
|
@ -25,6 +18,7 @@ import com.king.app.updater.constant.Constants; |
|
|
|
|
import com.king.app.updater.http.HttpManager; |
|
|
|
|
import com.king.app.updater.http.IHttpManager; |
|
|
|
|
import com.king.app.updater.util.AppUtils; |
|
|
|
|
import com.king.app.updater.util.NotificationUtils; |
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
|
|
|
|
|
@ -250,7 +244,7 @@ public class DownloadService extends Service { |
|
|
|
|
this.channelId = TextUtils.isEmpty(config.getChannelId()) ? Constants.DEFAULT_NOTIFICATION_CHANNEL_ID : config.getChannelId(); |
|
|
|
|
this.channelName = TextUtils.isEmpty(config.getChannelName()) ? Constants.DEFAULT_NOTIFICATION_CHANNEL_NAME : config.getChannelName(); |
|
|
|
|
} |
|
|
|
|
if(config.getNotificationIcon() <=0 ){ |
|
|
|
|
if(config.getNotificationIcon() <= 0){ |
|
|
|
|
this.notificationIcon = AppUtils.getAppIcon(getContext()); |
|
|
|
|
}else{ |
|
|
|
|
this.notificationIcon = config.getNotificationIcon(); |
|
|
|
@ -275,7 +269,7 @@ public class DownloadService extends Service { |
|
|
|
|
isDownloading = true; |
|
|
|
|
mLastProgress = 0; |
|
|
|
|
if(isShowNotification){ |
|
|
|
|
showStartNotification(notifyId,channelId,channelName,notificationIcon,getString(R.string.app_updater_start_notification_title),getString(R.string.app_updater_start_notification_content),config.isVibrate(),config.isSound()); |
|
|
|
|
NotificationUtils.showStartNotification(getContext(),notifyId,channelId,channelName,notificationIcon,getString(R.string.app_updater_start_notification_title),getString(R.string.app_updater_start_notification_content),config.isVibrate(),config.isSound()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if(callback!=null){ |
|
|
|
@ -302,7 +296,7 @@ public class DownloadService extends Service { |
|
|
|
|
content += percentage; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
showProgressNotification(notifyId, channelId, notificationIcon, getString(R.string.app_updater_progress_notification_title), content, currProgress, 100); |
|
|
|
|
NotificationUtils.showProgressNotification(getContext(),notifyId, channelId, notificationIcon, getString(R.string.app_updater_progress_notification_title), content, currProgress, 100); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -317,7 +311,7 @@ public class DownloadService extends Service { |
|
|
|
|
public void onFinish(File file) { |
|
|
|
|
Log.d(Constants.TAG,"onFinish:" + file); |
|
|
|
|
isDownloading = false; |
|
|
|
|
showFinishNotification(notifyId,channelId,notificationIcon,getString(R.string.app_updater_finish_notification_title),getString(R.string.app_updater_finish_notification_content),file,authority); |
|
|
|
|
NotificationUtils.showFinishNotification(getContext(),notifyId,channelId,notificationIcon,getString(R.string.app_updater_finish_notification_title),getString(R.string.app_updater_finish_notification_content),file,authority); |
|
|
|
|
if(isInstallApk){ |
|
|
|
|
AppUtils.installApk(getContext(),file,authority); |
|
|
|
|
} |
|
|
|
@ -334,7 +328,7 @@ public class DownloadService extends Service { |
|
|
|
|
//支持下载失败时重新下载,当重新下载次数不超过限制时才被允许
|
|
|
|
|
boolean isReDownload = this.isReDownload && mCount < reDownloads; |
|
|
|
|
String content = isReDownload ? getString(R.string.app_updater_error_notification_content_re_download) : getString(R.string.app_updater_error_notification_content); |
|
|
|
|
showErrorNotification(notifyId,channelId,notificationIcon,getString(R.string.app_updater_error_notification_title),content,isReDownload,config); |
|
|
|
|
NotificationUtils.showErrorNotification(getContext(),notifyId,channelId,notificationIcon,getString(R.string.app_updater_error_notification_title),content,isReDownload,config); |
|
|
|
|
|
|
|
|
|
if(callback!=null){ |
|
|
|
|
callback.onError(e); |
|
|
|
@ -349,7 +343,7 @@ public class DownloadService extends Service { |
|
|
|
|
public void onCancel() { |
|
|
|
|
Log.d(Constants.TAG,"onCancel"); |
|
|
|
|
isDownloading = false; |
|
|
|
|
cancelNotification(notifyId); |
|
|
|
|
NotificationUtils.cancelNotification(getContext(),notifyId); |
|
|
|
|
if(callback!=null){ |
|
|
|
|
callback.onCancel(); |
|
|
|
|
} |
|
|
|
@ -367,191 +361,6 @@ public class DownloadService extends Service { |
|
|
|
|
super.onDestroy(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
//---------------------------------------- Notification
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 显示开始下载是的通知 |
|
|
|
|
* @param notifyId |
|
|
|
|
* @param channelId |
|
|
|
|
* @param channelName |
|
|
|
|
* @param icon |
|
|
|
|
* @param title |
|
|
|
|
* @param content |
|
|
|
|
*/ |
|
|
|
|
private void showStartNotification(int notifyId,String channelId, String channelName,@DrawableRes int icon,CharSequence title,CharSequence content,boolean isVibrate,boolean isSound){ |
|
|
|
|
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){ |
|
|
|
|
createNotificationChannel(channelId,channelName,isVibrate,isSound); |
|
|
|
|
} |
|
|
|
|
NotificationCompat.Builder builder = buildNotification(channelId,icon,title,content); |
|
|
|
|
builder.setPriority(NotificationManager.IMPORTANCE_HIGH); |
|
|
|
|
if(isVibrate && isSound){ |
|
|
|
|
builder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND); |
|
|
|
|
}else if(isVibrate){ |
|
|
|
|
builder.setDefaults(Notification.DEFAULT_VIBRATE); |
|
|
|
|
}else if(isSound){ |
|
|
|
|
builder.setDefaults(Notification.DEFAULT_SOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Notification notification = builder.build(); |
|
|
|
|
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONLY_ALERT_ONCE; |
|
|
|
|
notifyNotification(notifyId,notification); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 显示下载中的通知(更新进度) |
|
|
|
|
* @param notifyId |
|
|
|
|
* @param channelId |
|
|
|
|
* @param icon |
|
|
|
|
* @param title |
|
|
|
|
* @param content |
|
|
|
|
* @param progress |
|
|
|
|
* @param size |
|
|
|
|
*/ |
|
|
|
|
private void showProgressNotification(int notifyId,String channelId,@DrawableRes int icon,CharSequence title,CharSequence content,int progress,int size){ |
|
|
|
|
NotificationCompat.Builder builder = buildNotification(channelId,icon,title,content,progress,size); |
|
|
|
|
|
|
|
|
|
Notification notification = builder.build(); |
|
|
|
|
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONLY_ALERT_ONCE; |
|
|
|
|
notifyNotification(notifyId,notification); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 显示下载完成时的通知(点击安装) |
|
|
|
|
* @param notifyId |
|
|
|
|
* @param channelId |
|
|
|
|
* @param icon |
|
|
|
|
* @param title |
|
|
|
|
* @param content |
|
|
|
|
* @param file |
|
|
|
|
*/ |
|
|
|
|
private void showFinishNotification(int notifyId,String channelId,@DrawableRes int icon,CharSequence title,CharSequence content,File file,String authority){ |
|
|
|
|
cancelNotification(notifyId); |
|
|
|
|
NotificationCompat.Builder builder = buildNotification(channelId,icon,title,content); |
|
|
|
|
builder.setAutoCancel(true); |
|
|
|
|
Intent intent = AppUtils.getInstallIntent(getContext(),file,authority); |
|
|
|
|
PendingIntent clickIntent = PendingIntent.getActivity(getContext(), notifyId,intent, PendingIntent.FLAG_UPDATE_CURRENT); |
|
|
|
|
builder.setContentIntent(clickIntent); |
|
|
|
|
Notification notification = builder.build(); |
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL; |
|
|
|
|
notifyNotification(notifyId,notification); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void showErrorNotification(int notifyId,String channelId,@DrawableRes int icon,CharSequence title,CharSequence content,boolean isReDownload,UpdateConfig config){ |
|
|
|
|
NotificationCompat.Builder builder = buildNotification(channelId,icon,title,content); |
|
|
|
|
builder.setAutoCancel(true); |
|
|
|
|
if(isReDownload){//重新下载
|
|
|
|
|
Intent intent = new Intent(getContext(),DownloadService.class); |
|
|
|
|
intent.putExtra(Constants.KEY_RE_DOWNLOAD,true); |
|
|
|
|
intent.putExtra(Constants.KEY_UPDATE_CONFIG,config); |
|
|
|
|
PendingIntent clickIntent = PendingIntent.getService(getContext(), notifyId,intent, PendingIntent.FLAG_UPDATE_CURRENT); |
|
|
|
|
builder.setContentIntent(clickIntent); |
|
|
|
|
}else{ |
|
|
|
|
PendingIntent clickIntent = PendingIntent.getService(getContext(), notifyId,new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); |
|
|
|
|
builder.setContentIntent(clickIntent); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Notification notification = builder.build(); |
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL; |
|
|
|
|
notifyNotification(notifyId,notification); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 显示通知信息(非第一次) |
|
|
|
|
* @param notifyId |
|
|
|
|
* @param channelId |
|
|
|
|
* @param icon |
|
|
|
|
* @param title |
|
|
|
|
* @param content |
|
|
|
|
*/ |
|
|
|
|
private void showNotification(int notifyId,String channelId,@DrawableRes int icon,CharSequence title,CharSequence content,boolean isAutoCancel){ |
|
|
|
|
NotificationCompat.Builder builder = buildNotification(channelId,icon,title,content); |
|
|
|
|
builder.setAutoCancel(isAutoCancel); |
|
|
|
|
Notification notification = builder.build(); |
|
|
|
|
notification.flags = Notification.FLAG_AUTO_CANCEL; |
|
|
|
|
notifyNotification(notifyId,notification); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 取消通知 |
|
|
|
|
* @param notifyId |
|
|
|
|
*/ |
|
|
|
|
private void cancelNotification(int notifyId){ |
|
|
|
|
getNotificationManager().cancel(notifyId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取通知管理器 |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
private NotificationManager getNotificationManager(){ |
|
|
|
|
return (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建一个通知渠道(兼容0以上版本) |
|
|
|
|
* @param channelId |
|
|
|
|
* @param channelName |
|
|
|
|
*/ |
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.O) |
|
|
|
|
private void createNotificationChannel(String channelId, String channelName,boolean isVibrate,boolean isSound){ |
|
|
|
|
NotificationChannel channel = new NotificationChannel(channelId,channelName, NotificationManager.IMPORTANCE_HIGH); |
|
|
|
|
channel.enableVibration(isVibrate); |
|
|
|
|
if(!isSound){ |
|
|
|
|
channel.setSound(null,null); |
|
|
|
|
} |
|
|
|
|
getNotificationManager().createNotificationChannel(channel); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 构建一个通知构建器 |
|
|
|
|
* @param channelId |
|
|
|
|
* @param icon |
|
|
|
|
* @param title |
|
|
|
|
* @param content |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
private NotificationCompat.Builder buildNotification(String channelId, @DrawableRes int icon,CharSequence title,CharSequence content){ |
|
|
|
|
return buildNotification(channelId,icon,title,content,Constants.NONE,Constants.NONE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 构建一个通知构建器 |
|
|
|
|
* @param channelId |
|
|
|
|
* @param icon |
|
|
|
|
* @param title |
|
|
|
|
* @param content |
|
|
|
|
* @param progress |
|
|
|
|
* @param size |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
private NotificationCompat.Builder buildNotification(String channelId,@DrawableRes int icon,CharSequence title,CharSequence content,int progress,int size){ |
|
|
|
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(),channelId); |
|
|
|
|
builder.setSmallIcon(icon); |
|
|
|
|
|
|
|
|
|
builder.setContentTitle(title); |
|
|
|
|
builder.setContentText(content); |
|
|
|
|
builder.setOngoing(true); |
|
|
|
|
|
|
|
|
|
if(progress!= Constants.NONE && size!=Constants.NONE){ |
|
|
|
|
builder.setProgress(size,progress,false); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return builder; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 更新通知栏 |
|
|
|
|
* @param id |
|
|
|
|
* @param notification |
|
|
|
|
*/ |
|
|
|
|
private void notifyNotification(int id, Notification notification){ |
|
|
|
|
getNotificationManager().notify(id,notification); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------- Binder
|
|
|
|
|
|
|
|
|
|
@Nullable |
|
|
|
|