|
|
@@ -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.
|
|
|
*/
|