优化细节

pull/27/head
jenly1314 5 years ago
parent a10687ad07
commit 82a9b36a7d
  1. 6
      app-dialog/src/main/java/com/king/app/dialog/AppDialog.java
  2. 6
      app-dialog/src/main/java/com/king/app/dialog/fragment/AppDialogFragment.java
  3. 5
      app-updater/src/main/java/com/king/app/updater/AppUpdater.java
  4. 13
      app-updater/src/main/java/com/king/app/updater/service/DownloadService.java
  5. 3
      app/src/main/java/com/king/appupdater/MainActivity.java

@ -233,8 +233,10 @@ public enum AppDialog {
mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { mDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override @Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && isCancel){ if(keyCode == KeyEvent.KEYCODE_BACK){
dismissDialog(); if(isCancel){
dismissDialog();
}
return true; return true;
} }
return false; return false;

@ -26,10 +26,10 @@ public class AppDialogFragment extends BaseDialogFragment {
@Override @Override
public int getRootLayoutId() { public int getRootLayoutId() {
if(config == null){ if(config != null){
config = new AppDialogConfig(); return config.getLayoutId();
} }
return config.getLayoutId(); return R.layout.app_dialog;
} }
public void init(View rootView){ public void init(View rootView){

@ -85,8 +85,8 @@ public class AppUpdater {
*/ */
public void start(){ public void start(){
if(mConfig!=null && !TextUtils.isEmpty(mConfig.getUrl())){ if(mConfig!=null && !TextUtils.isEmpty(mConfig.getUrl())){
//如果mContext是Activity,则默认会校验一次动态权限。 //如果mContext是Activity,并且配置了下载路径,则默认会校验一次动态权限。
if(mContext instanceof Activity){ if(mContext instanceof Activity && !TextUtils.isEmpty(mConfig.getPath())){
PermissionUtils.verifyReadAndWritePermissions((Activity) mContext,Constants.RE_CODE_STORAGE_PERMISSION); PermissionUtils.verifyReadAndWritePermissions((Activity) mContext,Constants.RE_CODE_STORAGE_PERMISSION);
} }
@ -337,6 +337,7 @@ public class AppUpdater {
return this; return this;
} }
public AppUpdater build(@NonNull Context context){ public AppUpdater build(@NonNull Context context){
AppUpdater appUpdater = new AppUpdater(context,mConfig); AppUpdater appUpdater = new AppUpdater(context,mConfig);
return appUpdater; return appUpdater;

@ -12,6 +12,7 @@ import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.widget.RemoteViews;
import com.king.app.updater.R; import com.king.app.updater.R;
import com.king.app.updater.UpdateConfig; import com.king.app.updater.UpdateConfig;
@ -224,6 +225,8 @@ public class DownloadService extends Service {
private boolean isReDownload; private boolean isReDownload;
private boolean isDeleteCancelFile;
private UpdateCallback callback; private UpdateCallback callback;
@ -252,6 +255,7 @@ public class DownloadService extends Service {
this.isShowPercentage = config.isShowPercentage(); this.isShowPercentage = config.isShowPercentage();
this.isReDownload = config.isReDownload(); this.isReDownload = config.isReDownload();
this.isDeleteCancelFile = config.isDeleteCancelFile();
} }
@ -315,7 +319,7 @@ public class DownloadService extends Service {
@Override @Override
public void onError(Exception e) { public void onError(Exception e) {
Log.w(Constants.TAG,e); Log.w(Constants.TAG,"onError:"+ e.getMessage());
isDownloading = false; isDownloading = false;
//支持下载失败重新并最多支持失败下载3次 //支持下载失败重新并最多支持失败下载3次
boolean isReDownload = this.isReDownload && mCount < 3; boolean isReDownload = this.isReDownload && mCount < 3;
@ -339,7 +343,7 @@ public class DownloadService extends Service {
if(callback!=null){ if(callback!=null){
callback.onCancel(); callback.onCancel();
} }
if(mFile!=null){ if(isDeleteCancelFile && mFile!=null){
mFile.delete(); mFile.delete();
} }
stopService(); stopService();
@ -369,6 +373,7 @@ public class DownloadService extends Service {
createNotificationChannel(channelId,channelName,isVibrate,isSound); createNotificationChannel(channelId,channelName,isVibrate,isSound);
} }
NotificationCompat.Builder builder = buildNotification(channelId,icon,title,content); NotificationCompat.Builder builder = buildNotification(channelId,icon,title,content);
builder.setPriority(NotificationManager.IMPORTANCE_HIGH);
if(isVibrate && isSound){ if(isVibrate && isSound){
builder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND); builder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND);
}else if(isVibrate){ }else if(isVibrate){
@ -376,6 +381,7 @@ public class DownloadService extends Service {
}else if(isSound){ }else if(isSound){
builder.setDefaults(Notification.DEFAULT_SOUND); builder.setDefaults(Notification.DEFAULT_SOUND);
} }
Notification notification = builder.build(); Notification notification = builder.build();
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONLY_ALERT_ONCE; notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONLY_ALERT_ONCE;
notifyNotification(notifyId,notification); notifyNotification(notifyId,notification);
@ -393,6 +399,7 @@ public class DownloadService extends Service {
*/ */
private void showProgressNotification(int notifyId,String channelId,@DrawableRes int icon,CharSequence title,CharSequence content,int progress,int 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); NotificationCompat.Builder builder = buildNotification(channelId,icon,title,content,progress,size);
Notification notification = builder.build(); Notification notification = builder.build();
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONLY_ALERT_ONCE; notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONLY_ALERT_ONCE;
notifyNotification(notifyId,notification); notifyNotification(notifyId,notification);
@ -479,7 +486,7 @@ public class DownloadService extends Service {
*/ */
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel(String channelId, String channelName,boolean isVibrate,boolean isSound){ 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); channel.enableVibration(isVibrate);
if(!isSound){ if(!isSound){
channel.setSound(null,null); channel.setSound(null,null);

@ -193,7 +193,8 @@ public class MainActivity extends AppCompatActivity {
AppDialog.INSTANCE.dismissDialog(); AppDialog.INSTANCE.dismissDialog();
} }
}); });
AppDialog.INSTANCE.showDialog(getContext(),AppDialog.INSTANCE.createAppDialogView(getContext(),config),true); //强制升级,拦截返回
AppDialog.INSTANCE.showDialog(getContext(),AppDialog.INSTANCE.createAppDialogView(getContext(),config),false);
} }
/** /**

Loading…
Cancel
Save