Merge "Add ability to speak to provider for enhanced estimate"

This commit is contained in:
TreeHugger Robot
2017-05-05 04:50:11 +00:00
committed by Android (Google) Code Review
4 changed files with 125 additions and 6 deletions

View File

@@ -16,7 +16,10 @@
package com.android.settings.fuelgauge;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import com.android.internal.os.BatterySipper;
/**
@@ -62,4 +65,24 @@ public interface PowerUsageFeatureProvider {
* Check whether the anomaly detection is enabled
*/
boolean isAnomalyDetectionEnabled();
/**
* Returns an improved prediction for battery time remaining.
*/
long getEnhancedBatteryPrediction(Context context);
/**
* Checks whether the toggle for enhanced battery predictions is enabled.
*/
boolean isEnhancedBatteryPredictionEnabled(Context context);
/**
* Returns the Uri used to query for an enhanced battery prediction from a cursor loader.
*/
Uri getEnhancedBatteryPredictionUri();
/**
* Returns the the estimate in the cursor as a long or -1 if the cursor is null
*/
long getTimeRemainingEstimate(Cursor cursor);
}

View File

@@ -19,6 +19,8 @@ package com.android.settings.fuelgauge;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Process;
import com.android.internal.os.BatterySipper;
import com.android.internal.util.ArrayUtils;
@@ -89,4 +91,24 @@ public class PowerUsageFeatureProviderImpl implements PowerUsageFeatureProvider
public boolean isAnomalyDetectionEnabled() {
return false;
}
@Override
public long getEnhancedBatteryPrediction(Context context) {
return -1;
}
@Override
public boolean isEnhancedBatteryPredictionEnabled(Context context) {
return false;
}
@Override
public Uri getEnhancedBatteryPredictionUri() {
return null;
}
@Override
public long getTimeRemainingEstimate(Cursor cursor) {
return 0;
}
}

View File

@@ -19,11 +19,15 @@ package com.android.settings.fuelgauge;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.Loader;
import android.content.res.TypedArray;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.BatteryStats;
import android.os.Build;
import android.os.Bundle;
@@ -38,9 +42,9 @@ import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceGroup;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.text.format.Formatter;
import android.util.Log;
import android.util.SparseArray;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@@ -102,6 +106,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
@VisibleForTesting
static final int ANOMALY_LOADER = 1;
private static final int BATTERY_ESTIMATE_LOADER = 2;
private static final int MENU_STATS_TYPE = Menu.FIRST;
@VisibleForTesting
static final int MENU_HIGH_POWER_APPS = Menu.FIRST + 3;
@@ -124,6 +129,9 @@ public class PowerUsageSummary extends PowerUsageBase implements
PowerUsageFeatureProvider mPowerFeatureProvider;
@VisibleForTesting
BatteryUtils mBatteryUtils;
@VisibleForTesting
long mEnhancedEstimate = -1;
/**
* SparseArray that maps uid to {@link Anomaly}, so we could find {@link Anomaly} by uid
*/
@@ -158,6 +166,40 @@ public class PowerUsageSummary extends PowerUsageBase implements
}
};
private LoaderManager.LoaderCallbacks<Cursor> mBatteryPredictionLoaderCallbacks =
new LoaderManager.LoaderCallbacks<Cursor>() {
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
final Uri queryUri = mPowerFeatureProvider.getEnhancedBatteryPredictionUri();
return new CursorLoader(getContext(), queryUri, null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
try {
if (cursor != null && cursor.moveToFirst()) {
mEnhancedEstimate =
mPowerFeatureProvider.getTimeRemainingEstimate(cursor);
}
} finally {
cursor.close();
}
final long elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
Intent batteryBroadcast = getContext().registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(getContext(),
batteryBroadcast, mStatsHelper.getStats(), elapsedRealtimeUs, false);
useEnhancedEstimateIfAvailable(getContext(), batteryInfo);
updateHeaderPreference(batteryInfo);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// do nothing
}
};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
@@ -175,6 +217,10 @@ public class PowerUsageSummary extends PowerUsageBase implements
mAnomalySparseArray = new SparseArray<>();
initFeatureProvider();
if (mPowerFeatureProvider != null) {
getLoaderManager().initLoader(BATTERY_ESTIMATE_LOADER, Bundle.EMPTY,
mBatteryPredictionLoaderCallbacks);
}
}
@Override
@@ -454,6 +500,7 @@ public class PowerUsageSummary extends PowerUsageBase implements
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
BatteryInfo batteryInfo = BatteryInfo.getBatteryInfo(context, batteryBroadcast,
mStatsHelper.getStats(), elapsedRealtimeUs, false);
useEnhancedEstimateIfAvailable(context, batteryInfo);
updateHeaderPreference(batteryInfo);
final long runningTime = calculateRunningTimeBasedOnStatsType();
@@ -697,6 +744,18 @@ public class PowerUsageSummary extends PowerUsageBase implements
}
}
@VisibleForTesting
void useEnhancedEstimateIfAvailable(Context context, BatteryInfo batteryInfo) {
if (mEnhancedEstimate > 0) {
final Resources resources = context.getResources();
batteryInfo.remainingTimeUs = mEnhancedEstimate;
String timeString = Formatter.formatShortElapsedTime(context, mEnhancedEstimate);
batteryInfo.remainingLabel = resources.getString(
com.android.settingslib.R.string.power_remaining_duration_only,
timeString);
}
}
private static List<BatterySipper> getFakeStats() {
ArrayList<BatterySipper> stats = new ArrayList<>();
float use = 5;