Replace setApkInternal with more specific functions.

The `setApkInternal` method had to infer the intent of the caller
based on the arguments which were passed on, and then do specific
things depending on the input. Instead, this change has three
distinct actions which can happen (add/remove/update). Each of
these methods does only one thing, and doesn't have to guess
the intent of the caller. The only exception may be "add", which
will (for convenience) delegate to "update" if it already knows
about the apk in question.
This commit is contained in:
Peter Serwylo 2017-02-24 11:35:20 +11:00 committed by mvp76
parent d00de69974
commit 2ad61a4eb7

View File

@ -109,16 +109,8 @@ public class AppUpdateStatusManager {
return returnValues; return returnValues;
} }
private void setApkInternal(Apk apk, @NonNull Status status, PendingIntent intent) { private void updateApkInternal(@NonNull AppUpdateStatus entry, @NonNull Status status, PendingIntent intent) {
if (apk == null) { Utils.debugLog(LOGTAG, "Update APK " + entry.apk.apkName + " state to " + status.name());
return;
}
synchronized (appMapping) {
AppUpdateStatus entry = appMapping.get(apk.getUrl());
if (entry != null) {
// Update
Utils.debugLog(LOGTAG, "Update APK " + apk.apkName + " state to " + status.name());
boolean isStatusUpdate = (entry.status != status); boolean isStatusUpdate = (entry.status != status);
entry.status = status; entry.status = status;
entry.intent = intent; entry.intent = intent;
@ -127,10 +119,11 @@ public class AppUpdateStatusManager {
entry.intent = getContentIntent(entry); entry.intent = getContentIntent(entry);
} }
notifyChange(entry, isStatusUpdate); notifyChange(entry, isStatusUpdate);
} else { }
// Add
private void addApkInternal(@NonNull Apk apk, @NonNull Status status, PendingIntent intent) {
Utils.debugLog(LOGTAG, "Add APK " + apk.apkName + " with state " + status.name()); Utils.debugLog(LOGTAG, "Add APK " + apk.apkName + " with state " + status.name());
entry = createAppEntry(apk, status, intent); AppUpdateStatus entry = createAppEntry(apk, status, intent);
// If intent not set, see if we need to create a default intent // If intent not set, see if we need to create a default intent
if (entry.intent == null) { if (entry.intent == null) {
entry.intent = getContentIntent(entry); entry.intent = getContentIntent(entry);
@ -138,8 +131,6 @@ public class AppUpdateStatusManager {
appMapping.put(entry.getUniqueKey(), entry); appMapping.put(entry.getUniqueKey(), entry);
notifyAdd(entry); notifyAdd(entry);
} }
}
}
private void notifyChange() { private void notifyChange() {
if (!isBatchUpdating) { if (!isBatchUpdating) {
@ -192,20 +183,31 @@ public class AppUpdateStatusManager {
} }
/** /**
* Add an Apk to the AppUpdateStatusManager manager. * Add an Apk to the AppUpdateStatusManager manager (or update it if we already know about it).
* @param apk The apk to add. * @param apk The apk to add.
* @param status The current status of the app * @param status The current status of the app
* @param pendingIntent Action when notification is clicked. Can be null for default action(s) * @param pendingIntent Action when notification is clicked. Can be null for default action(s)
*/ */
public void addApk(Apk apk, @NonNull Status status, PendingIntent pendingIntent) { public void addApk(Apk apk, @NonNull Status status, PendingIntent pendingIntent) {
setApkInternal(apk, status, pendingIntent); if (apk == null) {
return;
}
synchronized (appMapping) {
AppUpdateStatus entry = appMapping.get(apk.getUrl());
if (entry != null) {
updateApkInternal(entry, status, pendingIntent);
} else {
addApkInternal(apk, status, pendingIntent);
}
}
} }
public void updateApk(String key, @NonNull Status status, PendingIntent pendingIntent) { public void updateApk(String key, @NonNull Status status, PendingIntent pendingIntent) {
synchronized (appMapping) { synchronized (appMapping) {
AppUpdateStatus entry = appMapping.get(key); AppUpdateStatus entry = appMapping.get(key);
if (entry != null) { if (entry != null) {
setApkInternal(entry.apk, status, pendingIntent); updateApkInternal(entry, status, pendingIntent);
} }
} }
} }