Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(796)

Unified Diff: components/precache/android/java/src/org/chromium/components/precache/DeviceState.java

Issue 2333063002: Record UMA for battery level when precache starts and ends (Closed)
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/precache/android/java/src/org/chromium/components/precache/DeviceState.java
diff --git a/components/precache/android/java/src/org/chromium/components/precache/DeviceState.java b/components/precache/android/java/src/org/chromium/components/precache/DeviceState.java
index 1a63a26a87b00b84882735e5daf7f8ba319c9236..8027a3f2aa605477a88a638189c040f04f41a3d1 100644
--- a/components/precache/android/java/src/org/chromium/components/precache/DeviceState.java
+++ b/components/precache/android/java/src/org/chromium/components/precache/DeviceState.java
@@ -17,6 +17,9 @@ import org.chromium.base.VisibleForTesting;
public class DeviceState {
private static DeviceState sDeviceState = null;
+ // Saved battery level percentage.
+ private int mSavedBatteryPercentage = 0;
+
/** Disallow Construction of DeviceState objects. Use {@link #getInstance()} instead to create
* a singleton instance.
*/
@@ -58,6 +61,36 @@ public class DeviceState {
|| status == BatteryManager.BATTERY_STATUS_FULL;
}
+ /**
+ * @return the previously saved battery level percentage.
+ * @param context the application context
+ */
+ public int getSavedBatteryPercentage() {
+ return mSavedBatteryPercentage;
+ }
+
+ /**
+ * Saves the current battery level percentage to be retrieved later.
+ */
+ public void saveCurrentBatteryPercentage(Context context) {
+ mSavedBatteryPercentage = getCurrentBatteryPercentage(context);
+ }
+
+ /**
+ * @return the current battery level as percentage.
+ * @param context the application context
+ */
+ public int getCurrentBatteryPercentage(Context context) {
+ IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
+ Intent batteryStatus = context.registerReceiver(null, iFilter);
+ if (batteryStatus == null) return 0;
+ int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
+ int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
+ if (scale == 0) return 0;
+
+ return Math.round(100 * level / (float) scale);
+ }
+
/** @return whether the currently active network is unmetered. */
public boolean isUnmeteredNetworkAvailable(Context context) {
NetworkInfoDelegate networkInfo =

Powered by Google App Engine
This is Rietveld 408576698