Bug 696038 - (3/3) Battery API Android backend. r=cjones
authorMounir Lamouri <mounir.lamouri@gmail.com>
Wed, 02 Nov 2011 16:36:44 +0100
changeset 80320 044bfd4399347c9357c98278c41858c98042789b
parent 80319 5ffe95aa3f6834e7e1dedf985e4ba4a74f2464c3
child 80321 72357a8f0d8979f4d47814e362f586308a5ffccb
push id506
push userclegnitto@mozilla.com
push dateWed, 09 Nov 2011 02:03:18 +0000
treeherdermozilla-aurora@63587fc7bb93 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerscjones
bugs696038
milestone10.0a1
Bug 696038 - (3/3) Battery API Android backend. r=cjones
embedding/android/GeckoApp.java
embedding/android/GeckoBatteryManager.java
--- a/embedding/android/GeckoApp.java
+++ b/embedding/android/GeckoApp.java
@@ -82,16 +82,18 @@ abstract public class GeckoApp
     public static SurfaceView cameraView;
     public static GeckoApp mAppContext;
     public static boolean mFullscreen = false;
     public static File sGREDir = null;
     static Thread mLibLoadThread = null;
     public Handler mMainHandler;
     private IntentFilter mConnectivityFilter;
     private BroadcastReceiver mConnectivityReceiver;
+    private IntentFilter mBatteryFilter;
+    private BroadcastReceiver mBatteryReceiver;
 
     enum LaunchState {PreLaunch, Launching, WaitForDebugger,
                       Launched, GeckoRunning, GeckoExiting};
     private static LaunchState sLaunchState = LaunchState.PreLaunch;
     private static boolean sTryCatchAttached = false;
 
 
     static boolean checkLaunchState(LaunchState checkState) {
@@ -403,16 +405,20 @@ abstract public class GeckoApp
         setContentView(mainLayout,
                        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                                                   ViewGroup.LayoutParams.FILL_PARENT));
 
         mConnectivityFilter = new IntentFilter();
         mConnectivityFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
         mConnectivityReceiver = new GeckoConnectivityReceiver();
 
+        mBatteryFilter = new IntentFilter();
+        mBatteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
+        mBatteryReceiver = new GeckoBatteryManager();
+
         if (!checkAndSetLaunchState(LaunchState.PreLaunch,
                                     LaunchState.Launching))
             return;
 
         checkAndLaunchUpdate();
         mLibLoadThread = new Thread(new Runnable() {
             public void run() {
                 // At some point while loading the gecko libs our default locale gets set
@@ -486,16 +492,17 @@ abstract public class GeckoApp
 
         // Whatever we do here should be fast, because we're blocking
         // the next activity from showing up until we finish.
 
         // onPause will be followed by either onResume or onStop.
         super.onPause();
 
         unregisterReceiver(mConnectivityReceiver);
+        unregisterReceiver(mBatteryReceiver);
     }
 
     @Override
     public void onResume()
     {
         Log.i(LOG_FILE_NAME, "resume");
         if (checkLaunchState(LaunchState.GeckoRunning))
             GeckoAppShell.onResume();
@@ -504,16 +511,17 @@ abstract public class GeckoApp
         super.onResume();
 
         // Just in case. Normally we start in onNewIntent
         if (checkLaunchState(LaunchState.PreLaunch) ||
             checkLaunchState(LaunchState.Launching))
             onNewIntent(getIntent());
 
         registerReceiver(mConnectivityReceiver, mConnectivityFilter);
+        registerReceiver(mBatteryReceiver, mBatteryFilter);
     }
 
     @Override
     public void onStop()
     {
         Log.i(LOG_FILE_NAME, "stop");
         // We're about to be stopped, potentially in preparation for
         // being destroyed.  We're killable after this point -- as I
@@ -521,17 +529,16 @@ abstract public class GeckoApp
         // without going through onDestroy.
         //
         // We might also get an onRestart after this; not sure what
         // that would mean for Gecko if we were to kill it here.
         // Instead, what we should do here is save prefs, session,
         // etc., and generally mark the profile as 'clean', and then
         // dirty it again if we get an onResume.
 
-
         GeckoAppShell.sendEventToGecko(new GeckoEvent(GeckoEvent.ACTIVITY_STOPPING));
         super.onStop();
         GeckoAppShell.putChildInBackground();
     }
 
     @Override
     public void onRestart()
     {
--- a/embedding/android/GeckoBatteryManager.java
+++ b/embedding/android/GeckoBatteryManager.java
@@ -32,20 +32,98 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.gecko;
 
+import java.lang.Math;
+
+import android.util.Log;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+
+import android.os.BatteryManager;
+
 public class GeckoBatteryManager
+  extends BroadcastReceiver
 {
+  private final static float   kDefaultLevel       = 1.0f;
+  private final static boolean kDefaultCharging    = true;
+  private final static float   kMinimumLevelChange = 0.01f;
+
+  private static boolean sNotificationsEnabled     = false;
+  private static float   sLevel                    = kDefaultLevel;
+  private static boolean sCharging                 = kDefaultCharging;
+
+  @Override
+  public void onReceive(Context context, Intent intent) {
+    if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
+      Log.e("GeckoBatteryManager", "Got an unexpected intent!");
+      return;
+    }
+
+    boolean previousCharging = isCharging();
+    float previousLevel = getLevel();
+
+    if (intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false)) {
+      int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
+      if (plugged == -1) {
+        sCharging = kDefaultCharging;
+        Log.e("GeckoBatteryManager", "Failed to get the plugged status!");
+      } else {
+        // Likely, if plugged > 0, it's likely plugged and charging but the doc
+        // isn't clear about that.
+        sCharging = plugged != 0;
+      }
+
+      // We need two floats because sLevel is a float.
+      float current =  (float)intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
+      float max = (float)intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
+      if (current == -1 || max == -1) {
+        Log.e("GeckoBatteryManager", "Failed to get battery level!");
+        sLevel = kDefaultLevel;
+      } else {
+        sLevel = current / max;
+      }
+    } else {
+      sLevel = kDefaultLevel;
+      sCharging = kDefaultCharging;
+    }
+
+    /*
+     * We want to inform listeners if the following conditions are fulfilled:
+     *  - we have at least one observer;
+     *  - the charging state has changed
+     *    OR
+     *    the level has changed of at least kMinimumLevelChange
+     */
+    if (sNotificationsEnabled &&
+        (previousCharging != isCharging() ||
+         Math.abs(previousLevel - getLevel()) >= kMinimumLevelChange)) {
+      GeckoAppShell.notifyBatteryChange(sLevel, sCharging);
+    }
+  }
+
+  public static boolean isCharging() {
+    return sCharging;
+  }
+
+  public static float getLevel() {
+    return sLevel;
+  }
+
   public static void enableNotifications() {
+    sNotificationsEnabled = true;
   }
 
   public static void disableNotifications() {
+    sNotificationsEnabled = false;
   }
 
   public static float[] getCurrentInformation() {
-    return new float[] { 1.0f, 1.0f };
+    return new float[] { getLevel(), isCharging() ? 1.0f : 0.0f };
   }
 }