diff --git a/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java b/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java index 7714af9..7cac7a9 100644 --- a/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java +++ b/app-updater/src/main/java/com/king/app/updater/http/HttpManager.java @@ -73,6 +73,7 @@ public class HttpManager implements IHttpManager { * 异步下载任务 */ private class DownloadTask extends AsyncTask { + private String url; private String path; @@ -102,7 +103,7 @@ public class HttpManager implements IHttpManager { connect.setReadTimeout(mTimeout); connect.setConnectTimeout(mTimeout); - if(requestProperty!=null){ + if(requestProperty != null){ for(Map.Entry entry : requestProperty.entrySet()){ connect.setRequestProperty(entry.getKey(),entry.getValue()); } @@ -150,6 +151,10 @@ public class HttpManager implements IHttpManager { connect.disconnect(); + if(progress <= 0 && length <= 0){ + throw new IllegalStateException(String.format("contentLength = %d",length)); + } + return file; } case HttpURLConnection.HTTP_MULT_CHOICE: @@ -187,7 +192,7 @@ public class HttpManager implements IHttpManager { @Override protected void onPreExecute() { super.onPreExecute(); - if(callback!=null){ + if(callback != null){ callback.onStart(url); } } @@ -195,8 +200,8 @@ public class HttpManager implements IHttpManager { @Override protected void onPostExecute(File file) { super.onPostExecute(file); - if(callback!=null){ - if(file!=null){ + if(callback != null){ + if(file != null){ callback.onFinish(file); }else{ callback.onError(exception); @@ -208,7 +213,7 @@ public class HttpManager implements IHttpManager { @Override protected void onProgressUpdate(Long... values) { super.onProgressUpdate(values); - if(callback!=null){ + if(callback != null){ if(!isCancelled()){ callback.onProgress(values[0],values[1]); } @@ -219,7 +224,7 @@ public class HttpManager implements IHttpManager { @Override protected void onCancelled() { super.onCancelled(); - if(callback!=null){ + if(callback != null){ callback.onCancel(); } } diff --git a/app-updater/src/main/java/com/king/app/updater/http/OkHttpManager.java b/app-updater/src/main/java/com/king/app/updater/http/OkHttpManager.java index 5347059..66cc494 100644 --- a/app-updater/src/main/java/com/king/app/updater/http/OkHttpManager.java +++ b/app-updater/src/main/java/com/king/app/updater/http/OkHttpManager.java @@ -158,7 +158,7 @@ public class OkHttpManager implements IHttpManager { fos.write(buffer,0,len); progress += len; //更新进度 - if(length>0){ + if(length > 0){ publishProgress(progress,length); } } @@ -169,6 +169,10 @@ public class OkHttpManager implements IHttpManager { response.close(); + if(progress <= 0 && length <= 0){ + throw new IllegalStateException(String.format("contentLength = %d",length)); + } + return file; }else {//连接失败 @@ -186,7 +190,7 @@ public class OkHttpManager implements IHttpManager { @Override protected void onPreExecute() { super.onPreExecute(); - if(callback!=null){ + if(callback != null){ callback.onStart(url); } } @@ -194,8 +198,8 @@ public class OkHttpManager implements IHttpManager { @Override protected void onPostExecute(File file) { super.onPostExecute(file); - if(callback!=null){ - if(file!=null){ + if(callback != null){ + if(file != null){ callback.onFinish(file); }else{ callback.onError(exception); @@ -207,7 +211,7 @@ public class OkHttpManager implements IHttpManager { @Override protected void onProgressUpdate(Long... values) { super.onProgressUpdate(values); - if(callback!=null){ + if(callback != null){ if(!isCancelled()){ callback.onProgress(values[0],values[1]); } @@ -218,7 +222,7 @@ public class OkHttpManager implements IHttpManager { @Override protected void onCancelled() { super.onCancelled(); - if(callback!=null){ + if(callback != null){ callback.onCancel(); } } diff --git a/app-updater/src/main/java/com/king/app/updater/util/NotificationUtils.java b/app-updater/src/main/java/com/king/app/updater/util/NotificationUtils.java new file mode 100644 index 0000000..3e203c8 --- /dev/null +++ b/app-updater/src/main/java/com/king/app/updater/util/NotificationUtils.java @@ -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 Jenly + */ +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); + } + +} diff --git a/app-updater/src/main/res/values/strings.xml b/app-updater/src/main/res/values/strings.xml index 36d23be..928517b 100644 --- a/app-updater/src/main/res/values/strings.xml +++ b/app-updater/src/main/res/values/strings.xml @@ -5,7 +5,7 @@ 正在获取下载数据… 版本更新 - 下载更新中… + 正在下载… 下载完成 点击安装 diff --git a/app/release/app-release.apk b/app/release/app-release.apk new file mode 100644 index 0000000..c09d2d8 Binary files /dev/null and b/app/release/app-release.apk differ diff --git a/app/release/output.json b/app/release/output.json new file mode 100644 index 0000000..325c206 --- /dev/null +++ b/app/release/output.json @@ -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":{}}] \ No newline at end of file