2011-11-02 08:35:11 -07:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-02 08:35:11 -07:00
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
2012-04-04 17:31:19 -07:00
|
|
|
import android.app.Activity;
|
2011-11-02 08:36:44 -07:00
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2012-04-04 17:31:19 -07:00
|
|
|
import android.content.IntentFilter;
|
2011-11-02 08:36:44 -07:00
|
|
|
import android.os.BatteryManager;
|
2012-02-21 15:30:51 -08:00
|
|
|
import android.os.Build;
|
2012-01-13 11:08:39 -08:00
|
|
|
import android.os.SystemClock;
|
2012-06-05 10:44:35 -07:00
|
|
|
import android.util.Log;
|
2011-11-02 08:36:44 -07:00
|
|
|
|
2012-07-17 17:54:54 -07:00
|
|
|
public class GeckoBatteryManager
|
|
|
|
extends BroadcastReceiver
|
|
|
|
{
|
2012-06-05 10:44:35 -07:00
|
|
|
private static final String LOGTAG = "GeckoBatteryManager";
|
2011-11-18 10:28:17 -08:00
|
|
|
|
2011-11-09 00:56:37 -08:00
|
|
|
// Those constants should be keep in sync with the ones in:
|
|
|
|
// dom/battery/Constants.h
|
2011-11-09 06:13:37 -08:00
|
|
|
private final static double kDefaultLevel = 1.0;
|
|
|
|
private final static boolean kDefaultCharging = true;
|
2011-12-01 02:51:23 -08:00
|
|
|
private final static double kDefaultRemainingTime = -1.0;
|
2011-11-09 06:13:37 -08:00
|
|
|
private final static double kUnknownRemainingTime = -1.0;
|
2011-11-02 08:36:44 -07:00
|
|
|
|
2012-01-13 11:08:39 -08:00
|
|
|
private static long sLastLevelChange = 0;
|
2011-11-09 06:13:37 -08:00
|
|
|
private static boolean sNotificationsEnabled = false;
|
|
|
|
private static double sLevel = kDefaultLevel;
|
|
|
|
private static boolean sCharging = kDefaultCharging;
|
2011-12-01 02:51:23 -08:00
|
|
|
private static double sRemainingTime = kDefaultRemainingTime;;
|
2011-11-02 08:36:44 -07:00
|
|
|
|
2012-04-04 17:31:19 -07:00
|
|
|
private static boolean isRegistered = false;
|
|
|
|
|
|
|
|
public void registerFor(Activity activity) {
|
|
|
|
if (!isRegistered) {
|
|
|
|
IntentFilter filter = new IntentFilter();
|
|
|
|
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
|
2012-05-29 12:35:25 -07:00
|
|
|
|
2012-06-05 10:44:35 -07:00
|
|
|
// registerReceiver can return null if registering fails
|
2012-05-29 12:35:25 -07:00
|
|
|
isRegistered = activity.registerReceiver(this, filter) != null;
|
|
|
|
if (!isRegistered)
|
|
|
|
Log.e(LOGTAG, "Registering receiver failed");
|
2012-04-04 17:31:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unregisterFor(Activity activity) {
|
|
|
|
if (isRegistered) {
|
2012-06-05 10:44:35 -07:00
|
|
|
try {
|
|
|
|
activity.unregisterReceiver(this);
|
|
|
|
} catch (IllegalArgumentException iae) {
|
|
|
|
Log.e(LOGTAG, "Unregistering receiver failed", iae);
|
|
|
|
}
|
2012-04-04 17:31:19 -07:00
|
|
|
isRegistered = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-02 08:36:44 -07:00
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
|
if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
|
2011-11-18 10:28:17 -08:00
|
|
|
Log.e(LOGTAG, "Got an unexpected intent!");
|
2011-11-02 08:36:44 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean previousCharging = isCharging();
|
2011-11-09 00:56:37 -08:00
|
|
|
double previousLevel = getLevel();
|
2011-11-02 08:36:44 -07:00
|
|
|
|
2012-02-21 15:30:51 -08:00
|
|
|
// NOTE: it might not be common (in 2012) but technically, Android can run
|
|
|
|
// on a device that has no battery so we want to make sure it's not the case
|
|
|
|
// before bothering checking for battery state.
|
|
|
|
// However, the Galaxy Nexus phone advertizes itself as battery-less which
|
|
|
|
// force us to special-case the logic.
|
|
|
|
// See the Google bug: https://code.google.com/p/android/issues/detail?id=22035
|
|
|
|
if (intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false) ||
|
|
|
|
Build.MODEL.equals("Galaxy Nexus")) {
|
2011-11-02 08:36:44 -07:00
|
|
|
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
|
|
|
|
if (plugged == -1) {
|
|
|
|
sCharging = kDefaultCharging;
|
2011-11-18 10:28:17 -08:00
|
|
|
Log.e(LOGTAG, "Failed to get the plugged status!");
|
2011-11-02 08:36:44 -07:00
|
|
|
} else {
|
|
|
|
// Likely, if plugged > 0, it's likely plugged and charging but the doc
|
|
|
|
// isn't clear about that.
|
|
|
|
sCharging = plugged != 0;
|
|
|
|
}
|
|
|
|
|
2011-11-09 06:13:37 -08:00
|
|
|
if (sCharging != previousCharging) {
|
|
|
|
sRemainingTime = kUnknownRemainingTime;
|
|
|
|
// The new remaining time is going to take some time to show up but
|
|
|
|
// it's the best way to show a not too wrong value.
|
2012-01-13 11:08:39 -08:00
|
|
|
sLastLevelChange = 0;
|
2011-11-09 06:13:37 -08:00
|
|
|
}
|
|
|
|
|
2011-11-09 00:56:37 -08:00
|
|
|
// We need two doubles because sLevel is a double.
|
|
|
|
double current = (double)intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
|
|
|
|
double max = (double)intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
|
2011-11-02 08:36:44 -07:00
|
|
|
if (current == -1 || max == -1) {
|
2011-11-18 10:28:17 -08:00
|
|
|
Log.e(LOGTAG, "Failed to get battery level!");
|
2011-11-02 08:36:44 -07:00
|
|
|
sLevel = kDefaultLevel;
|
|
|
|
} else {
|
|
|
|
sLevel = current / max;
|
|
|
|
}
|
2011-11-09 06:13:37 -08:00
|
|
|
|
|
|
|
if (sLevel == 1.0 && sCharging) {
|
|
|
|
sRemainingTime = 0.0;
|
|
|
|
} else if (sLevel != previousLevel) {
|
|
|
|
// Estimate remaining time.
|
2012-01-13 11:08:39 -08:00
|
|
|
if (sLastLevelChange != 0) {
|
|
|
|
// Use elapsedRealtime() because we want to track time across device sleeps.
|
|
|
|
long currentTime = SystemClock.elapsedRealtime();
|
|
|
|
long dt = (currentTime - sLastLevelChange) / 1000;
|
2011-11-09 06:13:37 -08:00
|
|
|
double dLevel = sLevel - previousLevel;
|
|
|
|
|
|
|
|
if (sCharging) {
|
|
|
|
if (dLevel < 0) {
|
2011-11-18 10:28:17 -08:00
|
|
|
Log.w(LOGTAG, "When charging, level should increase!");
|
2011-11-09 06:13:37 -08:00
|
|
|
sRemainingTime = kUnknownRemainingTime;
|
|
|
|
} else {
|
|
|
|
sRemainingTime = Math.round(dt / dLevel * (1.0 - sLevel));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (dLevel > 0) {
|
2011-11-18 10:28:17 -08:00
|
|
|
Log.w(LOGTAG, "When discharging, level should decrease!");
|
2011-11-09 06:13:37 -08:00
|
|
|
sRemainingTime = kUnknownRemainingTime;
|
|
|
|
} else {
|
|
|
|
sRemainingTime = Math.round(dt / -dLevel * sLevel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sLastLevelChange = currentTime;
|
|
|
|
} else {
|
|
|
|
// That's the first time we got an update, we can't do anything.
|
2012-01-13 11:08:39 -08:00
|
|
|
sLastLevelChange = SystemClock.elapsedRealtime();
|
2011-11-09 06:13:37 -08:00
|
|
|
}
|
|
|
|
}
|
2011-11-02 08:36:44 -07:00
|
|
|
} else {
|
|
|
|
sLevel = kDefaultLevel;
|
|
|
|
sCharging = kDefaultCharging;
|
2012-02-21 15:30:51 -08:00
|
|
|
sRemainingTime = 0;
|
2011-11-02 08:36:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We want to inform listeners if the following conditions are fulfilled:
|
|
|
|
* - we have at least one observer;
|
2011-11-09 00:53:01 -08:00
|
|
|
* - the charging state or the level has changed.
|
|
|
|
*
|
2011-11-09 06:13:37 -08:00
|
|
|
* Note: no need to check for a remaining time change given that it's only
|
|
|
|
* updated if there is a level change or a charging change.
|
|
|
|
*
|
2011-11-09 00:53:01 -08:00
|
|
|
* The idea is to prevent doing all the way to the DOM code in the child
|
|
|
|
* process to finally not send an event.
|
2011-11-02 08:36:44 -07:00
|
|
|
*/
|
|
|
|
if (sNotificationsEnabled &&
|
2011-11-09 00:53:01 -08:00
|
|
|
(previousCharging != isCharging() || previousLevel != getLevel())) {
|
2011-11-09 06:13:37 -08:00
|
|
|
GeckoAppShell.notifyBatteryChange(getLevel(), isCharging(), getRemainingTime());
|
2011-11-02 08:36:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isCharging() {
|
|
|
|
return sCharging;
|
|
|
|
}
|
|
|
|
|
2011-11-09 00:56:37 -08:00
|
|
|
public static double getLevel() {
|
2011-11-02 08:36:44 -07:00
|
|
|
return sLevel;
|
|
|
|
}
|
|
|
|
|
2011-11-09 06:13:37 -08:00
|
|
|
public static double getRemainingTime() {
|
|
|
|
return sRemainingTime;
|
|
|
|
}
|
|
|
|
|
2011-11-02 08:35:11 -07:00
|
|
|
public static void enableNotifications() {
|
2011-11-02 08:36:44 -07:00
|
|
|
sNotificationsEnabled = true;
|
2011-11-02 08:35:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void disableNotifications() {
|
2011-11-02 08:36:44 -07:00
|
|
|
sNotificationsEnabled = false;
|
2011-11-02 08:35:11 -07:00
|
|
|
}
|
|
|
|
|
2011-11-09 00:56:37 -08:00
|
|
|
public static double[] getCurrentInformation() {
|
2011-11-09 06:13:37 -08:00
|
|
|
return new double[] { getLevel(), isCharging() ? 1.0 : 0.0, getRemainingTime() };
|
2011-11-02 08:35:11 -07:00
|
|
|
}
|
|
|
|
}
|