Browse Source

Implemented progressBar option for Android

Sebastián Katzer 8 years ago
parent
commit
3b145ca274

+ 8 - 0
src/android/notification/Builder.java

@@ -100,8 +100,16 @@ public class Builder {
                 .setVisibility(options.getVisibility())
                 .setPriority(options.getPriority())
                 .setShowWhen(options.getShowWhen())
+                .setUsesChronometer(options.isWithProgressBar())
                 .setLights(options.getLedColor(), options.getLedOn(), options.getLedOff());
 
+        if (options.isWithProgressBar()) {
+            builder.setProgress(
+                    options.getProgressMaxValue(),
+                    options.getProgressValue(),
+                    options.isIndeterminateProgress());
+        }
+
         if (smallIcon != 0) {
             builder.setSmallIcon(smallIcon);
             builder.setLargeIcon(options.getLargeIcon());

+ 42 - 0
src/android/notification/Options.java

@@ -402,6 +402,48 @@ public class Options {
         return options.optBoolean("showWhen", true);
     }
 
+    /**
+     * If the notification shall display a progress bar.
+     */
+    boolean isWithProgressBar() {
+        return options
+                .optJSONObject("progressBar")
+                .optBoolean("enabled", false);
+    }
+
+    /**
+     * Gets the progress value.
+     *
+     * @return 0 by default.
+     */
+    int getProgressValue() {
+        return options
+                .optJSONObject("progressBar")
+                .optInt("value", 0);
+    }
+
+    /**
+     * Gets the progress value.
+     *
+     * @return 100 by default.
+     */
+    int getProgressMaxValue() {
+        return options
+                .optJSONObject("progressBar")
+                .optInt("maxValue", 100);
+    }
+
+    /**
+     * Gets the progress indeterminate value.
+     *
+     * @return false by default.
+     */
+    boolean isIndeterminateProgress() {
+        return options
+                .optJSONObject("progressBar")
+                .optBoolean("indeterminate", false);
+    }
+
     /**
      * Gets the raw trigger spec as provided by the user.
      */

+ 17 - 3
www/local-notification-util.js

@@ -60,8 +60,8 @@ exports.applyPlatformSpecificOptions = function () {
         defaults.vibrate    = false;
         defaults.lockscreen = true;
         defaults.showWhen   = true;
-        defaults.priority   = 0;
         defaults.defaults   = 0;
+        defaults.priority   = 0;
         break;
     }
 };
@@ -265,12 +265,26 @@ exports.convertTrigger = function (options) {
  * @return [ Map ] Interaction object with trigger spec.
  */
 exports.convertProgressBar = function (options) {
-    var cfg = options.progressBar;
+    var isAndroid = device.platform == 'Android',
+        cfg       = options.progressBar;
 
     if (typeof cfg === 'boolean') {
-        options.progressBar = { enabled: cfg };
+        cfg = options.progressBar = { enabled: cfg };
+    }
+
+    if (typeof cfg.enabled !== 'boolean') {
+        cfg.enabled = !!(cfg.value || cfg.maxValue || cfg.indeterminate !== undefined);
     }
 
+    cfg.value = cfg.value || 0;
+
+    if (isAndroid) {
+        cfg.maxValue      = cfg.maxValue || 100;
+        cfg.indeterminate = cfg.indeterminate !== undefined ? cfg.indeterminate : false;
+    }
+
+    cfg.enabled = !!cfg.enabled;
+
     return options;
 };