Fields Summary |
---|
private static final String | TAG |
private static final boolean | DEBUG |
private static final String | TAG_NOTIFICATION |
private static final int | ID_NOTIFICATION |
private static final int | SHOWING_NOTHING |
private static final int | SHOWING_WARNING |
private static final int | SHOWING_SAVER |
private static final int | SHOWING_INVALID_CHARGER |
private static final String[] | SHOWING_STRINGS |
private static final String | ACTION_SHOW_BATTERY_SETTINGS |
private static final String | ACTION_START_SAVER |
private static final String | ACTION_STOP_SAVER |
private static final String | ACTION_DISMISSED_WARNING |
private static final android.media.AudioAttributes | AUDIO_ATTRIBUTES |
private final android.content.Context | mContext |
private final android.app.NotificationManager | mNoMan |
private final android.os.PowerManager | mPowerMan |
private final android.os.Handler | mHandler |
private final Receiver | mReceiver |
private final android.content.Intent | mOpenBatterySettings |
private final android.content.Intent | mOpenSaverSettings |
private int | mBatteryLevel |
private int | mBucket |
private long | mScreenOffTime |
private int | mShowing |
private long | mBucketDroppedNegativeTimeMs |
private boolean | mSaver |
private boolean | mWarning |
private boolean | mPlaySound |
private boolean | mInvalidCharger |
private com.android.systemui.statusbar.phone.SystemUIDialog | mSaverConfirmation |
private final android.content.DialogInterface.OnClickListener | mStartSaverMode |
Methods Summary |
---|
private void | addStopSaverAction(Notification.Builder nb)
nb.addAction(0,
mContext.getString(R.string.battery_saver_notification_action_text),
pendingBroadcast(ACTION_STOP_SAVER));
|
private void | attachLowBatterySound(Notification.Builder b)
final ContentResolver cr = mContext.getContentResolver();
final int silenceAfter = Settings.Global.getInt(cr,
Settings.Global.LOW_BATTERY_SOUND_TIMEOUT, 0);
final long offTime = SystemClock.elapsedRealtime() - mScreenOffTime;
if (silenceAfter > 0
&& mScreenOffTime > 0
&& offTime > silenceAfter) {
Slog.i(TAG, "screen off too long (" + offTime + "ms, limit " + silenceAfter
+ "ms): not waking up the user with low battery sound");
return;
}
if (DEBUG) {
Slog.d(TAG, "playing low battery sound. pick-a-doop!"); // WOMP-WOMP is deprecated
}
if (Settings.Global.getInt(cr, Settings.Global.POWER_SOUNDS_ENABLED, 1) == 1) {
final String soundPath = Settings.Global.getString(cr,
Settings.Global.LOW_BATTERY_SOUND);
if (soundPath != null) {
final Uri soundUri = Uri.parse("file://" + soundPath);
if (soundUri != null) {
b.setSound(soundUri, AUDIO_ATTRIBUTES);
if (DEBUG) Slog.d(TAG, "playing sound " + soundUri);
}
}
}
|
private void | dismissInvalidChargerNotification()
if (mInvalidCharger) Slog.i(TAG, "dismissing invalid charger notification");
mInvalidCharger = false;
updateNotification();
|
public void | dismissInvalidChargerWarning()
dismissInvalidChargerNotification();
|
private void | dismissLowBatteryNotification()
if (mWarning) Slog.i(TAG, "dismissing low battery notification");
mWarning = false;
updateNotification();
|
public void | dismissLowBatteryWarning()
if (DEBUG) Slog.d(TAG, "dismissing low battery warning: level=" + mBatteryLevel);
dismissLowBatteryNotification();
|
private void | dismissSaverNotification()
if (mSaver) Slog.i(TAG, "dismissing saver notification");
mSaver = false;
updateNotification();
|
public void | dump(java.io.PrintWriter pw)
pw.print("mSaver="); pw.println(mSaver);
pw.print("mWarning="); pw.println(mWarning);
pw.print("mPlaySound="); pw.println(mPlaySound);
pw.print("mInvalidCharger="); pw.println(mInvalidCharger);
pw.print("mShowing="); pw.println(SHOWING_STRINGS[mShowing]);
pw.print("mSaverConfirmation="); pw.println(mSaverConfirmation != null ? "not null" : null);
|
private boolean | hasBatterySettings()
return mOpenBatterySettings.resolveActivity(mContext.getPackageManager()) != null;
|
private boolean | hasSaverSettings()
return mOpenSaverSettings.resolveActivity(mContext.getPackageManager()) != null;
|
public boolean | isInvalidChargerWarningShowing()
return mInvalidCharger;
|
private android.app.PendingIntent | pendingActivity(android.content.Intent intent)
return PendingIntent.getActivityAsUser(mContext,
0, intent, 0, null, UserHandle.CURRENT);
|
private android.app.PendingIntent | pendingBroadcast(java.lang.String action)
return PendingIntent.getBroadcastAsUser(mContext,
0, new Intent(action), 0, UserHandle.CURRENT);
|
private void | setSaverMode(boolean mode)
mPowerMan.setPowerSaveMode(mode);
|
private static android.content.Intent | settings(java.lang.String action)
return new Intent(action).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_MULTIPLE_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
| Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
private void | showInvalidChargerNotification()
final Notification.Builder nb = new Notification.Builder(mContext)
.setSmallIcon(R.drawable.ic_power_low)
.setWhen(0)
.setShowWhen(false)
.setOngoing(true)
.setContentTitle(mContext.getString(R.string.invalid_charger_title))
.setContentText(mContext.getString(R.string.invalid_charger_text))
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setColor(mContext.getResources().getColor(
com.android.internal.R.color.system_notification_accent_color));
final Notification n = nb.build();
if (n.headsUpContentView != null) {
n.headsUpContentView.setViewVisibility(com.android.internal.R.id.right_icon, View.GONE);
}
mNoMan.notifyAsUser(TAG_NOTIFICATION, ID_NOTIFICATION, n, UserHandle.ALL);
|
public void | showInvalidChargerWarning()
mInvalidCharger = true;
updateNotification();
|
public void | showLowBatteryWarning(boolean playSound)
Slog.i(TAG,
"show low battery warning: level=" + mBatteryLevel
+ " [" + mBucket + "] playSound=" + playSound);
mPlaySound = playSound;
mWarning = true;
updateNotification();
|
public void | showSaverMode(boolean mode)
mSaver = mode;
if (mSaver && mSaverConfirmation != null) {
mSaverConfirmation.dismiss();
}
updateNotification();
|
private void | showSaverNotification()
final Notification.Builder nb = new Notification.Builder(mContext)
.setSmallIcon(R.drawable.ic_power_saver)
.setContentTitle(mContext.getString(R.string.battery_saver_notification_title))
.setContentText(mContext.getString(R.string.battery_saver_notification_text))
.setOngoing(true)
.setShowWhen(false)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setColor(mContext.getResources().getColor(
com.android.internal.R.color.battery_saver_mode_color));
addStopSaverAction(nb);
if (hasSaverSettings()) {
nb.setContentIntent(pendingActivity(mOpenSaverSettings));
}
mNoMan.notifyAsUser(TAG_NOTIFICATION, ID_NOTIFICATION, nb.build(), UserHandle.ALL);
|
private void | showStartSaverConfirmation()
if (mSaverConfirmation != null) return;
final SystemUIDialog d = new SystemUIDialog(mContext);
d.setTitle(R.string.battery_saver_confirmation_title);
d.setMessage(com.android.internal.R.string.battery_saver_description);
d.setNegativeButton(android.R.string.cancel, null);
d.setPositiveButton(R.string.battery_saver_confirmation_ok, mStartSaverMode);
d.setShowForAllUsers(true);
d.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
mSaverConfirmation = null;
}
});
d.show();
mSaverConfirmation = d;
|
private void | showWarningNotification()
final int textRes = mSaver ? R.string.battery_low_percent_format_saver_started
: R.string.battery_low_percent_format;
final String percentage = NumberFormat.getPercentInstance().format((double) mBatteryLevel / 100.0);
final Notification.Builder nb = new Notification.Builder(mContext)
.setSmallIcon(R.drawable.ic_power_low)
// Bump the notification when the bucket dropped.
.setWhen(mBucketDroppedNegativeTimeMs)
.setShowWhen(false)
.setContentTitle(mContext.getString(R.string.battery_low_title))
.setContentText(mContext.getString(textRes, percentage))
.setOnlyAlertOnce(true)
.setDeleteIntent(pendingBroadcast(ACTION_DISMISSED_WARNING))
.setPriority(Notification.PRIORITY_MAX)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setColor(mContext.getResources().getColor(
com.android.internal.R.color.battery_saver_mode_color));
if (hasBatterySettings()) {
nb.setContentIntent(pendingBroadcast(ACTION_SHOW_BATTERY_SETTINGS));
}
if (!mSaver) {
nb.addAction(0,
mContext.getString(R.string.battery_saver_start_action),
pendingBroadcast(ACTION_START_SAVER));
} else {
addStopSaverAction(nb);
}
if (mPlaySound) {
attachLowBatterySound(nb);
mPlaySound = false;
}
final Notification n = nb.build();
if (n.headsUpContentView != null) {
n.headsUpContentView.setViewVisibility(com.android.internal.R.id.right_icon, View.GONE);
}
mNoMan.notifyAsUser(TAG_NOTIFICATION, ID_NOTIFICATION, n, UserHandle.ALL);
|
public void | update(int batteryLevel, int bucket, long screenOffTime)
mBatteryLevel = batteryLevel;
if (bucket >= 0) {
mBucketDroppedNegativeTimeMs = 0;
} else if (bucket < mBucket) {
mBucketDroppedNegativeTimeMs = System.currentTimeMillis();
}
mBucket = bucket;
mScreenOffTime = screenOffTime;
|
public void | updateLowBatteryWarning()
updateNotification();
|
private void | updateNotification()
if (DEBUG) Slog.d(TAG, "updateNotification mWarning=" + mWarning + " mPlaySound="
+ mPlaySound + " mSaver=" + mSaver + " mInvalidCharger=" + mInvalidCharger);
if (mInvalidCharger) {
showInvalidChargerNotification();
mShowing = SHOWING_INVALID_CHARGER;
} else if (mWarning) {
showWarningNotification();
mShowing = SHOWING_WARNING;
} else if (mSaver) {
showSaverNotification();
mShowing = SHOWING_SAVER;
} else {
mNoMan.cancelAsUser(TAG_NOTIFICATION, ID_NOTIFICATION, UserHandle.ALL);
mShowing = SHOWING_NOTHING;
}
|
public void | userSwitched()
updateNotification();
|