Skip to content

Commit

Permalink
1.fix rhymelph#81
Browse files Browse the repository at this point in the history
2.add arg notification click
3.cancel over download
  • Loading branch information
linjiaxin committed Dec 27, 2023
1 parent 81b6c1d commit 03db084
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class UpgradeNotification {

private static String CHANNEL_NAME;

public static void createNotification(Context context, int id, String title,boolean indeterminate, Double percent, String contentText, int status) {
public static void createNotification(Context context, int id, String title,boolean indeterminate, Double percent, String contentText, int status, boolean notificationClickable) {
if (CHANNEL_NAME == null) {
try {
CHANNEL_NAME = context.getPackageName() + "_notification";
Expand All @@ -36,9 +36,11 @@ public static void createNotification(Context context, int id, String title,bool
Notification notification;
if (status == DownloadStatus.STATUS_RUNNING.getValue()) {
Intent pauseIntent = new Intent();
pauseIntent.setAction(UpgradeService.RECEIVER_PAUSE);
pauseIntent.putExtra(UpgradeManager.PARAMS_ID, id);
pauseIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName());
if(notificationClickable) {
pauseIntent.setAction(UpgradeService.RECEIVER_PAUSE);
pauseIntent.putExtra(UpgradeManager.PARAMS_ID, id);
pauseIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName());
}


PendingIntent pausePendingIntent =
Expand All @@ -55,9 +57,11 @@ public static void createNotification(Context context, int id, String title,bool
.build();
} else if (status == DownloadStatus.STATUS_SUCCESSFUL.getValue()) {
Intent installIntent = new Intent();
installIntent.setAction(UpgradeManager.DOWNLOAD_INSTALL);
installIntent.putExtra(UpgradeService.DOWNLOAD_ID, id);
installIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName());
if(notificationClickable) {
installIntent.setAction(UpgradeManager.DOWNLOAD_INSTALL);
installIntent.putExtra(UpgradeService.DOWNLOAD_ID, id);
installIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName());
}

PendingIntent installPendingIntent =
PendingIntent.getBroadcast(context, 0, installIntent, getPendingIntentFlag());
Expand All @@ -71,9 +75,11 @@ public static void createNotification(Context context, int id, String title,bool
.build();
} else if (status == DownloadStatus.STATUS_PAUSED.getValue()) {
Intent reStartIntent = new Intent();
reStartIntent.setAction(UpgradeService.RECEIVER_RESTART);
reStartIntent.putExtra(UpgradeManager.PARAMS_ID, id);
reStartIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName());
if(notificationClickable) {
reStartIntent.setAction(UpgradeService.RECEIVER_RESTART);
reStartIntent.putExtra(UpgradeManager.PARAMS_ID, id);
reStartIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName());
}

PendingIntent reStartPendingIntent =
PendingIntent.getBroadcast(context, 0, reStartIntent, getPendingIntentFlag());
Expand All @@ -87,9 +93,11 @@ public static void createNotification(Context context, int id, String title,bool
.build();
} else if (status == DownloadStatus.STATUS_FAILED.getValue()) {
Intent failedIntent = new Intent();
failedIntent.setAction(UpgradeService.RECEIVER_RESTART);
failedIntent.putExtra(UpgradeManager.PARAMS_ID, id);
failedIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName());
if(notificationClickable) {
failedIntent.setAction(UpgradeService.RECEIVER_RESTART);
failedIntent.putExtra(UpgradeManager.PARAMS_ID, id);
failedIntent.putExtra(UpgradeManager.PARAMS_PACKAGE, context.getPackageName());
}

PendingIntent reStartPendingIntent =
PendingIntent.getBroadcast(context, 0, failedIntent, getPendingIntentFlag());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ private void handleNetworkChange(boolean isConnected) {
isFirst = false;
return;
}
runnable.handlerDownloadFinish(false);
runnable.pause(-1);
isFirst = false;
RUpgradeLogger.get().d(TAG, "onReceive: 当前网络已断开");
Expand Down Expand Up @@ -407,6 +408,8 @@ private void handlerDownloadCancel() {
intent.putExtra(PARAMS_PACKAGE, upgradeService.getPackageName());
upgradeService.sendBroadcast(intent);
sqLite.delete(id);
lastCurrentLength = 0;
isFinish = true;
}

private void handlerDownloadPause() {
Expand All @@ -429,18 +432,22 @@ private void handlerDownloadPause() {
}
}

private void handlerDownloadFinish() {
private void handlerDownloadFinish(boolean isSuccess) {
RUpgradeLogger.get().d(TAG, "handlerDownloadFinish: finish");
cancelTimer();
Intent intent = new Intent();
intent.setAction(DOWNLOAD_STATUS);
intent.putExtra(PARAMS_ID, id);
intent.putExtra(PARAMS_APK_NAME, apkName);
intent.putExtra(PARAMS_PATH, downloadFile.getPath());
intent.putExtra(PARAMS_STATUS, DownloadStatus.STATUS_SUCCESSFUL.getValue());
intent.putExtra(PARAMS_PACKAGE, upgradeService.getPackageName());
upgradeService.sendBroadcast(intent);
sqLite.update(id, null, null, DownloadStatus.STATUS_SUCCESSFUL.getValue());
if(isSuccess) {
Intent intent = new Intent();
intent.setAction(DOWNLOAD_STATUS);
intent.putExtra(PARAMS_ID, id);
intent.putExtra(PARAMS_APK_NAME, apkName);
intent.putExtra(PARAMS_PATH, downloadFile.getPath());
intent.putExtra(PARAMS_STATUS, DownloadStatus.STATUS_SUCCESSFUL.getValue());
intent.putExtra(PARAMS_PACKAGE, upgradeService.getPackageName());
upgradeService.sendBroadcast(intent);
sqLite.update(id, null, null, DownloadStatus.STATUS_SUCCESSFUL.getValue());
}else{
sqLite.delete(id);
}
lastCurrentLength = 0;
isFinish = true;
}
Expand Down Expand Up @@ -601,7 +608,7 @@ public void run() {
}
is.close();
if (isRunning) {
handlerDownloadFinish();
handlerDownloadFinish(true);
}
} catch (Exception e) {
timer.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class UpgradeManager extends ContextWrapper {
private BaseInstallFactory installFactory;

private boolean isUseDownloadManager = false;
private boolean notificationClickable = true;

private Integer notificationVisibility = 0;
private UpgradeNotificationStyle notificationStyle = UpgradeNotificationStyle.none;
Expand Down Expand Up @@ -112,9 +113,10 @@ public UpgradeManager(Activity base, MethodChannel channel, DownloadPermissions
}


public void upgrade(final String url, final Map<String, String> header, final String apkName, final Integer notificationVisibility, Integer notificationStyle, Integer installType, Boolean useDownloadManager, final Integer upgradeFlavor, final MethodChannel.Result result) {
public void upgrade(final String url, final Map<String, String> header, final String apkName,final Boolean notificationClickable, final Integer notificationVisibility, Integer notificationStyle, Integer installType, Boolean useDownloadManager, final Integer upgradeFlavor, final MethodChannel.Result result) {
installFactory = installTypeToFactory(installType);
this.isUseDownloadManager = Boolean.TRUE == useDownloadManager;
this.notificationClickable = Boolean.TRUE == notificationClickable;
if (notificationStyle != null) {
this.notificationStyle = UpgradeNotificationStyle.values()[notificationStyle];
} else {
Expand Down Expand Up @@ -413,11 +415,11 @@ public void onReceive(Context context, Intent intent) {
if (!isUseDownloadManager) {
String contentText = notificationStyle == null ? "" : notificationStyle.getNotificationStyleString(context, speed, planTime);
if ((status == DownloadStatus.STATUS_RUNNING.getValue() || status == DownloadStatus.STATUS_SUCCESSFUL.getValue()) && notificationVisibility == 1) {
UpgradeNotification.createNotification(context, (int) id, apkName, max_length == -1, percent, contentText, status);
UpgradeNotification.createNotification(context, (int) id, apkName, max_length == -1, percent, contentText, status,notificationClickable);
} else if (notificationVisibility == 0) {
UpgradeNotification.createNotification(context, (int) id, apkName, max_length == -1, percent, contentText, status);
UpgradeNotification.createNotification(context, (int) id, apkName, max_length == -1, percent, contentText, status,notificationClickable);
} else if (status == DownloadStatus.STATUS_SUCCESSFUL.getValue() && notificationVisibility == 3) {
UpgradeNotification.createNotification(context, (int) id, apkName, max_length == -1, percent, contentText, status);
UpgradeNotification.createNotification(context, (int) id, apkName, max_length == -1, percent, contentText, status,notificationClickable);
}
if (status == DownloadStatus.STATUS_SUCCESSFUL.getValue()) {
installApkById((int) id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void handler(UpgradeManager upgradeManager, MethodCall call, MethodChanne
upgradeManager.upgrade((String) call.argument("url"),
(Map<String, String>) call.argument("header"),
(String) call.argument("fileName"),
(Boolean) call.argument("notificationClickable"),
(Integer) call.argument("notificationVisibility"),
(Integer) call.argument("notificationStyle"),
(Integer) call.argument("installType"),
Expand Down
2 changes: 2 additions & 0 deletions lib/r_upgrade.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class RUpgrade {
String url, {
Map<String, String>? header,
String? fileName,
bool notificationClickable = true,
NotificationVisibility notificationVisibility =
NotificationVisibility.VISIBILITY_VISIBLE,
NotificationStyle notificationStyle = NotificationStyle.planTime,
Expand All @@ -123,6 +124,7 @@ class RUpgrade {
'url': url,
"header": header,
"fileName": fileName,
"notificationClickable": notificationClickable,
"notificationVisibility": notificationVisibility.value,
"notificationStyle": notificationStyle.index,
"installType": installType.index,
Expand Down

0 comments on commit 03db084

Please sign in to comment.