优化细节

android
Jenly 4 years ago
parent cbb98f0ff5
commit 2263780073
  1. 19
      README.md
  2. 205
      app-updater/src/main/java/com/king/app/updater/service/DownloadService.java

@ -3,7 +3,7 @@
![Image](app/src/main/ic_launcher-web.png)
[![Download](https://img.shields.io/badge/download-App-blue.svg)](https://raw.githubusercontent.com/jenly1314/AppUpdater/master/app/release/app-release.apk)
[![JCenter](https://img.shields.io/badge/JCenter-1.0.9-46C018.svg)](https://bintray.com/beta/#/jenly/maven/app-updater)
[![JCenter](https://img.shields.io/badge/JCenter-1.0.10-46C018.svg)](https://bintray.com/beta/#/jenly/maven/app-updater)
[![JitPack](https://jitpack.io/v/jenly1314/AppUpdater.svg)](https://jitpack.io/#jenly1314/AppUpdater)
[![CI](https://travis-ci.org/jenly1314/AppUpdater.svg?branch=master)](https://travis-ci.org/jenly1314/AppUpdater)
[![CircleCI](https://circleci.com/gh/jenly1314/AppUpdater.svg?style=svg)](https://circleci.com/gh/jenly1314/AppUpdater)
@ -44,7 +44,7 @@ AppUpdater for Android 是一个专注于App更新,一键傻瓜式集成App版
<dependency>
<groupId>com.king.app</groupId>
<artifactId>app-updater</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
<type>pom</type>
</dependency>
@ -52,7 +52,7 @@ AppUpdater for Android 是一个专注于App更新,一键傻瓜式集成App版
<dependency>
<groupId>com.king.app</groupId>
<artifactId>app-dialog</artifactId>
<version>1.0.9</version>
<version>1.0.10</version>
<type>pom</type>
</dependency>
```
@ -61,25 +61,25 @@ AppUpdater for Android 是一个专注于App更新,一键傻瓜式集成App版
//----------AndroidX 版本
//app-updater
implementation 'com.king.app:app-updater:1.0.9-androidx'
implementation 'com.king.app:app-updater:1.0.10-androidx'
//app-dialog
implementation 'com.king.app:app-dialog:1.0.9-androidx'
implementation 'com.king.app:app-dialog:1.0.10-androidx'
//----------Android Support 版本
//app-updater
implementation 'com.king.app:app-updater:1.0.9'
implementation 'com.king.app:app-updater:1.0.10'
//app-dialog
implementation 'com.king.app:app-dialog:1.0.9'
implementation 'com.king.app:app-dialog:1.0.10'
```
### Lvy:
```lvy
//app-updater
<dependency org='com.king.app' name='app-dialog' rev='1.0.9'>
<dependency org='com.king.app' name='app-dialog' rev='1.0.10'>
<artifact name='$AID' ext='pom'></artifact>
</dependency>
//app-dialog
<dependency org='com.king.app' name='app-dialog' rev='1.0.9'>
<dependency org='com.king.app' name='app-dialog' rev='1.0.10'>
<artifact name='$AID' ext='pom'></artifact>
</dependency>
```
@ -152,6 +152,7 @@ AppUpdater for Android 是一个专注于App更新,一键傻瓜式集成App版
#### v1.0.10:2021-3-4
* AppDialogConfig添加构造参数,简化自定义扩展用法
* 优化细节
#### v1.0.9:2020-12-11
* 优化默认Dialog样式的显示细节

@ -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

Loading…
Cancel
Save