优化细节

android
Jenly 4 years ago
parent 165709991f
commit cbb98f0ff5
  1. 17
      app-updater/src/main/java/com/king/app/updater/http/HttpManager.java
  2. 16
      app-updater/src/main/java/com/king/app/updater/http/OkHttpManager.java
  3. 222
      app-updater/src/main/java/com/king/app/updater/util/NotificationUtils.java
  4. 2
      app-updater/src/main/res/values/strings.xml
  5. BIN
      app/release/app-release.apk
  6. 1
      app/release/output.json

@ -73,6 +73,7 @@ public class HttpManager implements IHttpManager {
* 异步下载任务 * 异步下载任务
*/ */
private class DownloadTask extends AsyncTask<Void,Long,File> { private class DownloadTask extends AsyncTask<Void,Long,File> {
private String url; private String url;
private String path; private String path;
@ -102,7 +103,7 @@ public class HttpManager implements IHttpManager {
connect.setReadTimeout(mTimeout); connect.setReadTimeout(mTimeout);
connect.setConnectTimeout(mTimeout); connect.setConnectTimeout(mTimeout);
if(requestProperty!=null){ if(requestProperty != null){
for(Map.Entry<String,String> entry : requestProperty.entrySet()){ for(Map.Entry<String,String> entry : requestProperty.entrySet()){
connect.setRequestProperty(entry.getKey(),entry.getValue()); connect.setRequestProperty(entry.getKey(),entry.getValue());
} }
@ -150,6 +151,10 @@ public class HttpManager implements IHttpManager {
connect.disconnect(); connect.disconnect();
if(progress <= 0 && length <= 0){
throw new IllegalStateException(String.format("contentLength = %d",length));
}
return file; return file;
} }
case HttpURLConnection.HTTP_MULT_CHOICE: case HttpURLConnection.HTTP_MULT_CHOICE:
@ -187,7 +192,7 @@ public class HttpManager implements IHttpManager {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
super.onPreExecute(); super.onPreExecute();
if(callback!=null){ if(callback != null){
callback.onStart(url); callback.onStart(url);
} }
} }
@ -195,8 +200,8 @@ public class HttpManager implements IHttpManager {
@Override @Override
protected void onPostExecute(File file) { protected void onPostExecute(File file) {
super.onPostExecute(file); super.onPostExecute(file);
if(callback!=null){ if(callback != null){
if(file!=null){ if(file != null){
callback.onFinish(file); callback.onFinish(file);
}else{ }else{
callback.onError(exception); callback.onError(exception);
@ -208,7 +213,7 @@ public class HttpManager implements IHttpManager {
@Override @Override
protected void onProgressUpdate(Long... values) { protected void onProgressUpdate(Long... values) {
super.onProgressUpdate(values); super.onProgressUpdate(values);
if(callback!=null){ if(callback != null){
if(!isCancelled()){ if(!isCancelled()){
callback.onProgress(values[0],values[1]); callback.onProgress(values[0],values[1]);
} }
@ -219,7 +224,7 @@ public class HttpManager implements IHttpManager {
@Override @Override
protected void onCancelled() { protected void onCancelled() {
super.onCancelled(); super.onCancelled();
if(callback!=null){ if(callback != null){
callback.onCancel(); callback.onCancel();
} }
} }

@ -158,7 +158,7 @@ public class OkHttpManager implements IHttpManager {
fos.write(buffer,0,len); fos.write(buffer,0,len);
progress += len; progress += len;
//更新进度 //更新进度
if(length>0){ if(length > 0){
publishProgress(progress,length); publishProgress(progress,length);
} }
} }
@ -169,6 +169,10 @@ public class OkHttpManager implements IHttpManager {
response.close(); response.close();
if(progress <= 0 && length <= 0){
throw new IllegalStateException(String.format("contentLength = %d",length));
}
return file; return file;
}else {//连接失败 }else {//连接失败
@ -186,7 +190,7 @@ public class OkHttpManager implements IHttpManager {
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
super.onPreExecute(); super.onPreExecute();
if(callback!=null){ if(callback != null){
callback.onStart(url); callback.onStart(url);
} }
} }
@ -194,8 +198,8 @@ public class OkHttpManager implements IHttpManager {
@Override @Override
protected void onPostExecute(File file) { protected void onPostExecute(File file) {
super.onPostExecute(file); super.onPostExecute(file);
if(callback!=null){ if(callback != null){
if(file!=null){ if(file != null){
callback.onFinish(file); callback.onFinish(file);
}else{ }else{
callback.onError(exception); callback.onError(exception);
@ -207,7 +211,7 @@ public class OkHttpManager implements IHttpManager {
@Override @Override
protected void onProgressUpdate(Long... values) { protected void onProgressUpdate(Long... values) {
super.onProgressUpdate(values); super.onProgressUpdate(values);
if(callback!=null){ if(callback != null){
if(!isCancelled()){ if(!isCancelled()){
callback.onProgress(values[0],values[1]); callback.onProgress(values[0],values[1]);
} }
@ -218,7 +222,7 @@ public class OkHttpManager implements IHttpManager {
@Override @Override
protected void onCancelled() { protected void onCancelled() {
super.onCancelled(); super.onCancelled();
if(callback!=null){ if(callback != null){
callback.onCancel(); callback.onCancel();
} }
} }

@ -0,0 +1,222 @@
package com.king.app.updater.util;
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.annotation.DrawableRes;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import com.king.app.updater.UpdateConfig;
import com.king.app.updater.constant.Constants;
import com.king.app.updater.service.DownloadService;
import java.io.File;
/**
* @author <a href="mailto:jenly1314@gmail.com">Jenly</a>
*/
public class NotificationUtils {
private NotificationUtils(){
throw new AssertionError();
}
/**
* 显示开始下载是的通知
* @param notifyId
* @param channelId
* @param channelName
* @param icon
* @param title
* @param content
*/
public static void showStartNotification(Context context, 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(context,channelId,channelName,isVibrate,isSound);
}
NotificationCompat.Builder builder = buildNotification(context,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(context,notifyId,notification);
}
/**
* 显示下载中的通知更新进度
* @param notifyId
* @param channelId
* @param icon
* @param title
* @param content
* @param progress
* @param size
*/
public static void showProgressNotification(Context context, int notifyId,String channelId,@DrawableRes int icon,CharSequence title,CharSequence content,int progress,int size){
NotificationCompat.Builder builder = buildNotification(context,channelId,icon,title,content,progress,size);
Notification notification = builder.build();
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONLY_ALERT_ONCE;
notifyNotification(context,notifyId,notification);
}
/**
* 显示下载完成时的通知点击安装
* @param notifyId
* @param channelId
* @param icon
* @param title
* @param content
* @param file
*/
public static void showFinishNotification(Context context, int notifyId, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content, File file, String authority){
cancelNotification(context,notifyId);
NotificationCompat.Builder builder = buildNotification(context,channelId,icon,title,content);
builder.setAutoCancel(true);
Intent intent = AppUtils.getInstallIntent(context,file,authority);
PendingIntent clickIntent = PendingIntent.getActivity(context, notifyId,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(clickIntent);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notifyNotification(context,notifyId,notification);
}
/**
* 现在下载失败通知
* @param context
* @param notifyId
* @param channelId
* @param icon
* @param title
* @param content
* @param isReDownload
* @param config
*/
public static void showErrorNotification(Context context, int notifyId, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content, boolean isReDownload, UpdateConfig config){
NotificationCompat.Builder builder = buildNotification(context,channelId,icon,title,content);
builder.setAutoCancel(true);
if(isReDownload){//重新下载
Intent intent = new Intent(context, DownloadService.class);
intent.putExtra(Constants.KEY_RE_DOWNLOAD,true);
intent.putExtra(Constants.KEY_UPDATE_CONFIG,config);
PendingIntent clickIntent = PendingIntent.getService(context, notifyId,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(clickIntent);
}else{
PendingIntent clickIntent = PendingIntent.getService(context, notifyId,new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(clickIntent);
}
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notifyNotification(context,notifyId,notification);
}
/**
* 显示通知信息非第一次
* @param notifyId
* @param channelId
* @param icon
* @param title
* @param content
*/
public static void showNotification(Context context, int notifyId,String channelId,@DrawableRes int icon,CharSequence title,CharSequence content,boolean isAutoCancel){
NotificationCompat.Builder builder = buildNotification(context,channelId,icon,title,content);
builder.setAutoCancel(isAutoCancel);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notifyNotification(context,notifyId,notification);
}
/**
* 取消通知
* @param notifyId
*/
public static void cancelNotification(Context context, int notifyId){
getNotificationManager(context).cancel(notifyId);
}
/**
* 获取通知管理器
* @return
*/
public static NotificationManager getNotificationManager(Context context){
return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
}
/**
* 创建一个通知渠道兼容0以上版本
* @param channelId
* @param channelName
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public static void createNotificationChannel(Context context, 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(context).createNotificationChannel(channel);
}
/**
* 构建一个通知构建器
* @param channelId
* @param icon
* @param title
* @param content
* @return
*/
private static NotificationCompat.Builder buildNotification(Context context, String channelId, @DrawableRes int icon,CharSequence title,CharSequence content){
return buildNotification(context,channelId,icon,title,content,Constants.NONE,Constants.NONE);
}
/**
* 构建一个通知构建器
* @param channelId
* @param icon
* @param title
* @param content
* @param progress
* @param size
* @return
*/
private static NotificationCompat.Builder buildNotification(Context context, String channelId, @DrawableRes int icon, CharSequence title, CharSequence content, int progress, int size){
NotificationCompat.Builder builder = new NotificationCompat.Builder(context,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 static void notifyNotification(Context context, int id, Notification notification){
getNotificationManager(context).notify(id,notification);
}
}

@ -5,7 +5,7 @@
<string name="app_updater_start_notification_content">正在获取下载数据…</string> <string name="app_updater_start_notification_content">正在获取下载数据…</string>
<string name="app_updater_progress_notification_title">版本更新</string> <string name="app_updater_progress_notification_title">版本更新</string>
<string name="app_updater_progress_notification_content">下载更新中</string> <string name="app_updater_progress_notification_content">正在下载…</string>
<string name="app_updater_finish_notification_title">下载完成</string> <string name="app_updater_finish_notification_title">下载完成</string>
<string name="app_updater_finish_notification_content">点击安装</string> <string name="app_updater_finish_notification_content">点击安装</string>

Binary file not shown.

@ -0,0 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":17,"versionName":"1.0.10","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
Loading…
Cancel
Save