Эх сурвалжийг харах

Refactored internal builder cache

Sebastián Katzer 8 жил өмнө
parent
commit
fb993c3181

+ 2 - 24
src/android/notification/Builder.java

@@ -31,7 +31,6 @@ import android.support.v4.app.NotificationCompat;
 import android.support.v4.app.NotificationCompat.MessagingStyle.Message;
 import android.support.v4.media.app.NotificationCompat.MediaStyle;
 import android.support.v4.media.session.MediaSessionCompat;
-import android.util.SparseArray;
 
 import java.util.List;
 import java.util.Random;
@@ -49,9 +48,6 @@ import static de.appplant.cordova.plugin.notification.Notification.EXTRA_UPDATE;
 @SuppressWarnings("Convert2Diamond")
 public final class Builder {
 
-    // Cache for the builder instances
-    private static SparseArray<NotificationCompat.Builder> cache = null;
-
     // Application context passed by constructor
     private final Context context;
 
@@ -153,7 +149,6 @@ public final class Builder {
         }
 
         if (options.isWithProgressBar()) {
-            cacheBuilder(builder);
             builder.setProgress(
                     options.getProgressMaxValue(),
                     options.getProgressValue(),
@@ -416,31 +411,14 @@ public final class Builder {
      * Returns a cached builder instance or creates a new one.
      */
     private NotificationCompat.Builder findOrCreateBuilder() {
-        NotificationCompat.Builder builder = null;
         int key = options.getId();
-
-        if (cache != null) {
-            builder = cache.get(key);
-        }
+        NotificationCompat.Builder builder = Notification.getCachedBuilder(key);
 
         if (builder == null) {
-            builder = new NotificationCompat.Builder(context, Manager.CHANNEL_ID);
+            builder = new NotificationCompat.Builder(context, options.getChannel());
         }
 
         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);
-    }
-
 }

+ 1 - 11
src/android/notification/Manager.java

@@ -50,6 +50,7 @@ import static de.appplant.cordova.plugin.notification.Notification.Type.TRIGGERE
  * state like triggered or scheduled. Offers shortcut ways to schedule,
  * cancel or clear local notifications.
  */
+@SuppressWarnings("Convert2Diamond")
 public final class Manager {
 
     // TODO: temporary
@@ -284,17 +285,6 @@ public final class Manager {
         return getByIds(ids);
     }
 
-    /**
-     * 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.
      */

+ 47 - 10
src/android/notification/Notification.java

@@ -33,6 +33,7 @@ import android.service.notification.StatusBarNotification;
 import android.support.v4.app.NotificationCompat;
 import android.support.v4.util.ArraySet;
 import android.support.v4.util.Pair;
+import android.util.SparseArray;
 
 import org.json.JSONException;
 import org.json.JSONObject;
@@ -55,6 +56,7 @@ import static android.support.v4.app.NotificationManagerCompat.IMPORTANCE_MIN;
  * Wrapper class around OS notification class. Handles basic operations
  * like show, delete, cancel for a single local notification instance.
  */
+@SuppressWarnings("Convert2Diamond")
 public final class Notification {
 
     // Used to differ notifications by their life cycle state
@@ -74,6 +76,9 @@ public final class Notification {
     // Key for private preferences
     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
     private final Context context;
 
@@ -233,15 +238,15 @@ public final class Notification {
      *
      * @param intent The intent to broadcast.
      * @param cls    The broadcast class.
+     *
+     * @return false if the receiver could not be invoked.
      */
     private boolean trigger (Intent intent, Class<?> cls) {
         BroadcastReceiver receiver;
 
         try {
             receiver = (BroadcastReceiver) cls.newInstance();
-        } catch (InstantiationException e) {
-            return false;
-        } catch (IllegalAccessException e) {
+        } catch (InstantiationException | IllegalAccessException e) {
             return false;
         }
 
@@ -254,10 +259,7 @@ public final class Notification {
      */
     public void clear() {
         getNotMgr().cancel(getId());
-
-        if (isRepeating())
-            return;
-
+        if (isRepeating()) return;
         unpersist();
     }
 
@@ -267,7 +269,8 @@ public final class Notification {
     public void cancel() {
         cancelScheduledAlarms();
         unpersist();
-        getNotMgr().cancel(options.getId());
+        getNotMgr().cancel(getId());
+        clearCache();
     }
 
     /**
@@ -302,9 +305,11 @@ public final class Notification {
      * Present the local notification to user.
      */
     public void show() {
+        if (builder == null) return;
 
-        if (builder == null)
-            return;
+        if (options.isWithProgressBar()) {
+            cacheBuilder();
+        }
 
         grantPermissionToPlaySoundFromExternal();
         getNotMgr().notify(getId(), builder.build());
@@ -418,6 +423,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.
      */

+ 5 - 8
src/android/notification/Options.java

@@ -54,6 +54,7 @@ import static android.support.v4.app.NotificationCompat.VISIBILITY_SECRET;
  * possible option values. Class provides simple readers and more advanced
  * methods to convert independent values into platform specific values.
  */
+@SuppressWarnings("Convert2Diamond")
 public final class Options {
 
     // Key name for bundled sound extra
@@ -91,7 +92,7 @@ public final class Options {
      * @param context The application context.
      * @param options The options dict map.
      */
-    public Options(Context context, JSONObject options) {
+    Options(Context context, JSONObject options) {
         this.context = context;
         this.options = options;
         this.assets  = AssetUtil.getInstance(context);
@@ -132,7 +133,7 @@ public final class Options {
      *
      * @return The notification ID as the string
      */
-    public String getIdentifier() {
+    String getIdentifier() {
         return getId().toString();
     }
 
@@ -321,11 +322,7 @@ public final class Options {
 
             int aRGB = Integer.parseInt(hex, 16);
             return aRGB + 0xFF000000;
-        } catch (NumberFormatException e) {
-            e.printStackTrace();
-        } catch (NoSuchFieldException e) {
-            e.printStackTrace();
-        } catch (IllegalAccessException e) {
+        } catch (NumberFormatException | NoSuchFieldException | IllegalAccessException e) {
             e.printStackTrace();
         }
 
@@ -335,7 +332,7 @@ public final class Options {
     /**
      * Sound file path for the local notification.
      */
-    public Uri getSound() {
+    Uri getSound() {
         return assets.parse(options.optString("sound", null));
     }