Skip to content

Commit

Permalink
Refactor update shown logic with null top activity
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Jan 31, 2018
1 parent acafdc7 commit fd96ba7
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.lzh.framework.updatepluginlib.model.Update;
import org.lzh.framework.updatepluginlib.util.ActivityManager;
import org.lzh.framework.updatepluginlib.util.SafeDialogHandle;
import org.lzh.framework.updatepluginlib.util.Utils;

/**
* <p><b>核心操作类</b>
Expand Down Expand Up @@ -63,15 +64,15 @@ public void hasUpdate(Update update) {
CheckNotifier notifier = builder.getCheckNotifier();
notifier.setBuilder(builder);
notifier.setUpdate(update);
Activity current = ActivityManager.get().topActivity();

if (!builder.getUpdateStrategy().isShowUpdateDialog(update)) {
if (Utils.isValid(current)
&& builder.getUpdateStrategy().isShowUpdateDialog(update)) {
Dialog dialog = notifier.create(current);
SafeDialogHandle.safeShowDialog(dialog);
} else {
notifier.sendDownloadRequest();
return;
}

Activity current = ActivityManager.get().topActivity();
Dialog dialog = notifier.create(current);
SafeDialogHandle.safeShowDialog(dialog);
} catch (Throwable t) {
onCheckError(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ private DownloadCallback getInnerCB() {
}

Activity current = ActivityManager.get().topActivity();
innerCB = builder.getDownloadNotifier().create(update,current);
if (Utils.isValid(current)) {
innerCB = builder.getDownloadNotifier().create(update,current);
}
return innerCB;
}

Expand Down Expand Up @@ -101,16 +103,16 @@ public void run() {
notifier.setBuilder(updateBuilder);
notifier.setUpdate(update);
notifier.setFile(file);
if (updateBuilder.getUpdateStrategy().isAutoInstall()) {
notifier.sendToInstall();
} else {
final Activity current = ActivityManager.get().topActivity();
Activity current = ActivityManager.get().topActivity();
if (Utils.isValid(current)
&& !builder.getUpdateStrategy().isAutoInstall()) {
Dialog dialog = notifier.create(current);
SafeDialogHandle.safeShowDialog(dialog);
} else {
notifier.sendToInstall();
}
}
});

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@
public class DefaultDownloadNotifier implements DownloadNotifier {
@Override
public DownloadCallback create(Update update, Activity activity) {
if (activity == null || activity.isFinishing()) {
Log.e("DownDialogCreator--->","show download dialog failed:activity was recycled or finished");
return null;
}
final ProgressDialog dialog = new ProgressDialog(activity);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ public class DefaultInstallNotifier extends InstallNotifier {

@Override
public Dialog create(Activity activity) {
if (activity == null || activity.isFinishing()) {
Log.e("DownDialogCreator--->","show install dialog failed:activity was recycled or finished");
return null;
}
String updateContent = activity.getText(R.string.update_version_name)
+ ": " + update.getVersionName() + "\n\n\n"
+ update.getUpdateContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@
public class DefaultUpdateNotifier extends CheckNotifier {
@Override
public Dialog create(Activity activity) {

if (activity == null || activity.isFinishing()) {
Log.e("CheckNotifier--->","Activity was recycled or finished,dialog shown failed!");
return null;
}

String updateContent = activity.getText(R.string.update_version_name)
+ ": " + update.getVersionName() + "\n\n\n"
+ update.getUpdateContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void safeShowDialog(Dialog dialog) {
}
Activity bindAct = getActivity(dialog);

if (bindAct == null || bindAct.isFinishing()) {
if (!Utils.isValid(bindAct)) {
Log.d("Dialog shown failed:","The Dialog bind's Activity was recycled or finished!");
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.lzh.framework.updatepluginlib.util;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;

Expand All @@ -32,5 +33,7 @@ public static Handler getMainHandler() {
return handler;
}


public static boolean isValid(Activity activity) {
return activity != null && !activity.isFinishing();
}
}

0 comments on commit fd96ba7

Please sign in to comment.