Commit fb993c31 by Sebastián Katzer

Refactored internal builder cache

parent 6137bc0b
...@@ -31,7 +31,6 @@ import android.support.v4.app.NotificationCompat; ...@@ -31,7 +31,6 @@ import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.MessagingStyle.Message; import android.support.v4.app.NotificationCompat.MessagingStyle.Message;
import android.support.v4.media.app.NotificationCompat.MediaStyle; import android.support.v4.media.app.NotificationCompat.MediaStyle;
import android.support.v4.media.session.MediaSessionCompat; import android.support.v4.media.session.MediaSessionCompat;
import android.util.SparseArray;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
...@@ -49,9 +48,6 @@ import static de.appplant.cordova.plugin.notification.Notification.EXTRA_UPDATE; ...@@ -49,9 +48,6 @@ import static de.appplant.cordova.plugin.notification.Notification.EXTRA_UPDATE;
@SuppressWarnings("Convert2Diamond") @SuppressWarnings("Convert2Diamond")
public final class Builder { public final class Builder {
// Cache for the builder instances
private static SparseArray<NotificationCompat.Builder> cache = null;
// Application context passed by constructor // Application context passed by constructor
private final Context context; private final Context context;
...@@ -153,7 +149,6 @@ public final class Builder { ...@@ -153,7 +149,6 @@ public final class Builder {
} }
if (options.isWithProgressBar()) { if (options.isWithProgressBar()) {
cacheBuilder(builder);
builder.setProgress( builder.setProgress(
options.getProgressMaxValue(), options.getProgressMaxValue(),
options.getProgressValue(), options.getProgressValue(),
...@@ -416,31 +411,14 @@ public final class Builder { ...@@ -416,31 +411,14 @@ public final class Builder {
* Returns a cached builder instance or creates a new one. * Returns a cached builder instance or creates a new one.
*/ */
private NotificationCompat.Builder findOrCreateBuilder() { private NotificationCompat.Builder findOrCreateBuilder() {
NotificationCompat.Builder builder = null;
int key = options.getId(); int key = options.getId();
NotificationCompat.Builder builder = Notification.getCachedBuilder(key);
if (cache != null) {
builder = cache.get(key);
}
if (builder == null) { if (builder == null) {
builder = new NotificationCompat.Builder(context, Manager.CHANNEL_ID); builder = new NotificationCompat.Builder(context, options.getChannel());
} }
return builder; return builder;
} }
/**
* Caches the builder instance so it can be used later.
*
* @param builder The instance to cache.
*/
private void cacheBuilder (NotificationCompat.Builder builder) {
if (cache == null) {
cache = new SparseArray<NotificationCompat.Builder>();
}
cache.put(options.getId(), builder);
}
} }
...@@ -50,6 +50,7 @@ import static de.appplant.cordova.plugin.notification.Notification.Type.TRIGGERE ...@@ -50,6 +50,7 @@ import static de.appplant.cordova.plugin.notification.Notification.Type.TRIGGERE
* state like triggered or scheduled. Offers shortcut ways to schedule, * state like triggered or scheduled. Offers shortcut ways to schedule,
* cancel or clear local notifications. * cancel or clear local notifications.
*/ */
@SuppressWarnings("Convert2Diamond")
public final class Manager { public final class Manager {
// TODO: temporary // TODO: temporary
...@@ -285,17 +286,6 @@ public final class Manager { ...@@ -285,17 +286,6 @@ public final class Manager {
} }
/** /**
* If a notification with an ID exists.
*
* @param id Notification ID
*
* @return true if found.
*/
public boolean exist (int id) {
return get(id) != null;
}
/**
* List of properties from all local notifications. * List of properties from all local notifications.
*/ */
public List<JSONObject> getOptions() { public List<JSONObject> getOptions() {
......
...@@ -33,6 +33,7 @@ import android.service.notification.StatusBarNotification; ...@@ -33,6 +33,7 @@ import android.service.notification.StatusBarNotification;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.support.v4.util.ArraySet; import android.support.v4.util.ArraySet;
import android.support.v4.util.Pair; import android.support.v4.util.Pair;
import android.util.SparseArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
...@@ -55,6 +56,7 @@ import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_MIN; ...@@ -55,6 +56,7 @@ import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_MIN;
* Wrapper class around OS notification class. Handles basic operations * Wrapper class around OS notification class. Handles basic operations
* like show, delete, cancel for a single local notification instance. * like show, delete, cancel for a single local notification instance.
*/ */
@SuppressWarnings("Convert2Diamond")
public final class Notification { public final class Notification {
// Used to differ notifications by their life cycle state // Used to differ notifications by their life cycle state
...@@ -74,6 +76,9 @@ public final class Notification { ...@@ -74,6 +76,9 @@ public final class Notification {
// Key for private preferences // Key for private preferences
private static final String PREF_KEY_PID = "NOTIFICATION_PID"; private static final String PREF_KEY_PID = "NOTIFICATION_PID";
// Cache for the builder instances
static SparseArray<NotificationCompat.Builder> cache = null;
// Application context passed by constructor // Application context passed by constructor
private final Context context; private final Context context;
...@@ -233,15 +238,15 @@ public final class Notification { ...@@ -233,15 +238,15 @@ public final class Notification {
* *
* @param intent The intent to broadcast. * @param intent The intent to broadcast.
* @param cls The broadcast class. * @param cls The broadcast class.
*
* @return false if the receiver could not be invoked.
*/ */
private boolean trigger (Intent intent, Class<?> cls) { private boolean trigger (Intent intent, Class<?> cls) {
BroadcastReceiver receiver; BroadcastReceiver receiver;
try { try {
receiver = (BroadcastReceiver) cls.newInstance(); receiver = (BroadcastReceiver) cls.newInstance();
} catch (InstantiationException e) { } catch (InstantiationException | IllegalAccessException e) {
return false;
} catch (IllegalAccessException e) {
return false; return false;
} }
...@@ -254,10 +259,7 @@ public final class Notification { ...@@ -254,10 +259,7 @@ public final class Notification {
*/ */
public void clear() { public void clear() {
getNotMgr().cancel(getId()); getNotMgr().cancel(getId());
if (isRepeating()) return;
if (isRepeating())
return;
unpersist(); unpersist();
} }
...@@ -267,7 +269,8 @@ public final class Notification { ...@@ -267,7 +269,8 @@ public final class Notification {
public void cancel() { public void cancel() {
cancelScheduledAlarms(); cancelScheduledAlarms();
unpersist(); unpersist();
getNotMgr().cancel(options.getId()); getNotMgr().cancel(getId());
clearCache();
} }
/** /**
...@@ -302,9 +305,11 @@ public final class Notification { ...@@ -302,9 +305,11 @@ public final class Notification {
* Present the local notification to user. * Present the local notification to user.
*/ */
public void show() { public void show() {
if (builder == null) return;
if (builder == null) if (options.isWithProgressBar()) {
return; cacheBuilder();
}
grantPermissionToPlaySoundFromExternal(); grantPermissionToPlaySoundFromExternal();
getNotMgr().notify(getId(), builder.build()); getNotMgr().notify(getId(), builder.build());
...@@ -419,6 +424,38 @@ public final class Notification { ...@@ -419,6 +424,38 @@ public final class Notification {
} }
/** /**
* Caches the builder instance so it can be used later.
*/
private void cacheBuilder () {
if (cache == null) {
cache = new SparseArray<NotificationCompat.Builder>();
}
cache.put(getId(), builder);
}
/**
* Find the cached builder instance.
*
* @param key The key under where to look for the builder.
*
* @return null if no builder instance could be found.
*/
static NotificationCompat.Builder getCachedBuilder (int key) {
return (cache != null) ? cache.get(key) : null;
}
/**
* Caches the builder instance so it can be used later.
*/
private void clearCache () {
if (cache != null) {
cache.delete(getId());
}
}
/**
* Shared private preferences for the application. * Shared private preferences for the application.
*/ */
private SharedPreferences getPrefs (String key) { private SharedPreferences getPrefs (String key) {
......
...@@ -54,6 +54,7 @@ import static android.support.v4.app.NotificationCompat.VISIBILITY_SECRET; ...@@ -54,6 +54,7 @@ import static android.support.v4.app.NotificationCompat.VISIBILITY_SECRET;
* possible option values. Class provides simple readers and more advanced * possible option values. Class provides simple readers and more advanced
* methods to convert independent values into platform specific values. * methods to convert independent values into platform specific values.
*/ */
@SuppressWarnings("Convert2Diamond")
public final class Options { public final class Options {
// Key name for bundled sound extra // Key name for bundled sound extra
...@@ -91,7 +92,7 @@ public final class Options { ...@@ -91,7 +92,7 @@ public final class Options {
* @param context The application context. * @param context The application context.
* @param options The options dict map. * @param options The options dict map.
*/ */
public Options(Context context, JSONObject options) { Options(Context context, JSONObject options) {
this.context = context; this.context = context;
this.options = options; this.options = options;
this.assets = AssetUtil.getInstance(context); this.assets = AssetUtil.getInstance(context);
...@@ -132,7 +133,7 @@ public final class Options { ...@@ -132,7 +133,7 @@ public final class Options {
* *
* @return The notification ID as the string * @return The notification ID as the string
*/ */
public String getIdentifier() { String getIdentifier() {
return getId().toString(); return getId().toString();
} }
...@@ -321,11 +322,7 @@ public final class Options { ...@@ -321,11 +322,7 @@ public final class Options {
int aRGB = Integer.parseInt(hex, 16); int aRGB = Integer.parseInt(hex, 16);
return aRGB + 0xFF000000; return aRGB + 0xFF000000;
} catch (NumberFormatException e) { } catch (NumberFormatException | NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
...@@ -335,7 +332,7 @@ public final class Options { ...@@ -335,7 +332,7 @@ public final class Options {
/** /**
* Sound file path for the local notification. * Sound file path for the local notification.
*/ */
public Uri getSound() { Uri getSound() {
return assets.parse(options.optString("sound", null)); return assets.parse(options.optString("sound", null));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment