From d7a9bd8dd05883c822f7e724f455d7c5fe10737b Mon Sep 17 00:00:00 2001 From: jenly1314 Date: Wed, 4 Dec 2019 15:39:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BB=86=E8=8A=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/king/app/dialog/AppDialog.java | 8 +++++--- .../app/dialog/fragment/AppDialogFragment.java | 6 +++--- .../main/java/com/king/app/updater/AppUpdater.java | 5 +++-- .../king/app/updater/service/DownloadService.java | 14 ++++++++++---- .../java/com/king/appupdater/MainActivity.java | 8 ++++++-- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/app-dialog/src/main/java/com/king/app/dialog/AppDialog.java b/app-dialog/src/main/java/com/king/app/dialog/AppDialog.java index 21e4cf0..f829f31 100644 --- a/app-dialog/src/main/java/com/king/app/dialog/AppDialog.java +++ b/app-dialog/src/main/java/com/king/app/dialog/AppDialog.java @@ -232,8 +232,10 @@ public enum AppDialog { mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { - if(keyCode == KeyEvent.KEYCODE_BACK && isCancel){ - dismissDialog(); + if(keyCode == KeyEvent.KEYCODE_BACK){ + if(isCancel){ + dismissDialog(); + } return true; } return false; @@ -263,4 +265,4 @@ public enum AppDialog { //------------------------------------------- -} +} \ No newline at end of file diff --git a/app-dialog/src/main/java/com/king/app/dialog/fragment/AppDialogFragment.java b/app-dialog/src/main/java/com/king/app/dialog/fragment/AppDialogFragment.java index f3e3481..9f089dc 100644 --- a/app-dialog/src/main/java/com/king/app/dialog/fragment/AppDialogFragment.java +++ b/app-dialog/src/main/java/com/king/app/dialog/fragment/AppDialogFragment.java @@ -26,10 +26,10 @@ public class AppDialogFragment extends BaseDialogFragment { @Override public int getRootLayoutId() { - if(config == null){ - config = new AppDialogConfig(); + if(config != null){ + return config.getLayoutId(); } - return config.getLayoutId(); + return R.layout.app_dialog; } public void init(View rootView){ diff --git a/app-updater/src/main/java/com/king/app/updater/AppUpdater.java b/app-updater/src/main/java/com/king/app/updater/AppUpdater.java index 323e503..fbfa2b2 100644 --- a/app-updater/src/main/java/com/king/app/updater/AppUpdater.java +++ b/app-updater/src/main/java/com/king/app/updater/AppUpdater.java @@ -84,11 +84,12 @@ public class AppUpdater { */ public void start(){ if(mConfig!=null && !TextUtils.isEmpty(mConfig.getUrl())){ - //如果mContext是Activity,则默认会校验一次动态权限。 - if(mContext instanceof Activity){ + //如果mContext是Activity,并且配置了下载路径,则默认会校验一次动态权限。 + if(mContext instanceof Activity && !TextUtils.isEmpty(mConfig.getPath())){ PermissionUtils.verifyReadAndWritePermissions((Activity) mContext,Constants.RE_CODE_STORAGE_PERMISSION); } + if(mConfig.isShowNotification() && !PermissionUtils.isNotificationEnabled(mContext)){ Log.w(Constants.TAG,"Notification permission not enabled."); } diff --git a/app-updater/src/main/java/com/king/app/updater/service/DownloadService.java b/app-updater/src/main/java/com/king/app/updater/service/DownloadService.java index 027c7f2..39dc89a 100644 --- a/app-updater/src/main/java/com/king/app/updater/service/DownloadService.java +++ b/app-updater/src/main/java/com/king/app/updater/service/DownloadService.java @@ -223,6 +223,8 @@ public class DownloadService extends Service { private boolean isReDownload; + private boolean isDeleteCancelFile; + private UpdateCallback callback; @@ -251,6 +253,7 @@ public class DownloadService extends Service { this.isShowPercentage = config.isShowPercentage(); this.isReDownload = config.isReDownload(); + this.isDeleteCancelFile = config.isDeleteCancelFile(); } @@ -314,7 +317,7 @@ public class DownloadService extends Service { @Override public void onError(Exception e) { - Log.w(Constants.TAG,e); + Log.w(Constants.TAG,"onError:"+ e.getMessage()); isDownloading = false; //支持下载失败重新并最多支持失败下载3次 boolean isReDownload = this.isReDownload && mCount < 3; @@ -338,7 +341,7 @@ public class DownloadService extends Service { if(callback!=null){ callback.onCancel(); } - if(mFile!=null){ + if(isDeleteCancelFile && mFile!=null){ mFile.delete(); } stopService(); @@ -368,6 +371,7 @@ public class DownloadService extends Service { 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){ @@ -375,6 +379,7 @@ public class DownloadService extends Service { }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); @@ -392,6 +397,7 @@ public class DownloadService extends Service { */ 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); @@ -478,7 +484,7 @@ public class DownloadService extends Service { */ @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_DEFAULT); + NotificationChannel channel = new NotificationChannel(channelId,channelName, NotificationManager.IMPORTANCE_HIGH); channel.enableVibration(isVibrate); if(!isSound){ channel.setSound(null,null); @@ -562,4 +568,4 @@ public class DownloadService extends Service { } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/king/appupdater/MainActivity.java b/app/src/main/java/com/king/appupdater/MainActivity.java index e15336d..285112a 100644 --- a/app/src/main/java/com/king/appupdater/MainActivity.java +++ b/app/src/main/java/com/king/appupdater/MainActivity.java @@ -24,6 +24,9 @@ import com.king.app.updater.util.PermissionUtils; import java.io.File; +/** + * @author Jenly + */ public class MainActivity extends AppCompatActivity { private final Object mLock = new Object(); @@ -189,7 +192,8 @@ public class MainActivity extends AppCompatActivity { AppDialog.INSTANCE.dismissDialog(); } }); - AppDialog.INSTANCE.showDialog(getContext(),AppDialog.INSTANCE.createAppDialogView(getContext(),config),true); + //强制升级,拦截返回 + AppDialog.INSTANCE.showDialog(getContext(),AppDialog.INSTANCE.createAppDialogView(getContext(),config),false); } /** @@ -289,4 +293,4 @@ public class MainActivity extends AppCompatActivity { break; } } -} +} \ No newline at end of file