上层接收电量变化广播,添加未充电判断,做出弹框或关机动作
SystemUI\src\com\android\systemui\power\PowerUI.java
void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(action)) {
ThreadUtils.postOnBackgroundThread(() -> {
if (mPowerManager.isPowerSaveMode()) {
mWarnings.dismissLowBatteryWarning();
}
});
} else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {//电量变化广播
mHasReceivedBattery = true;
final int oldBatteryLevel = mBatteryLevel;
mBatteryLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 100);
final int oldBatteryStatus = mBatteryStatus;
mBatteryStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
final int oldPlugType = mPlugType;
mPlugType = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 1);
final int oldInvalidCharger = mInvalidCharger;
mInvalidCharger = intent.getIntExtra(BatteryManager.EXTRA_INVALID_CHARGER, 0);
mLastBatteryStateSnapshot = mCurrentBatteryStateSnapshot;
final boolean plugged = mPlugType != 0;
final boolean oldPlugged = oldPlugType != 0;
int oldBucket = findBatteryLevelBucket(oldBatteryLevel);
int bucket = findBatteryLevelBucket(mBatteryLevel);
/**
* Unisoc: 5G resident reminder Feature decoupling(3/3)
* AR: AR.599.001744.006995.022501
* method: hook interface
* Unisoc Code @{
*/
if(powerUIEx != null)
powerUIEx.remind5GReside(mBatteryLevel,mHandler);
/* @} */
if (DEBUG) {
Slog.d(TAG, "buckets ....." + mLowBatteryAlertCloseLevel
+ " .. " + mLowBatteryReminderLevels[0]
+ " .. " + mLowBatteryReminderLevels[1]);
Slog.d(TAG, "level " + oldBatteryLevel + " --> " + mBatteryLevel);
Slog.d(TAG, "status " + oldBatteryStatus + " --> " + mBatteryStatus);
Slog.d(TAG, "plugType " + oldPlugType + " --> " + mPlugType);
Slog.d(TAG, "invalidCharger " + oldInvalidCharger + " --> " + mInvalidCharger);
Slog.d(TAG, "bucket " + oldBucket + " --> " + bucket);
Slog.d(TAG, "plugged " + oldPlugged + " --> " + plugged);
}
if(mBatteryLevel==15 && !plugged){ //判断未充电,电量15时弹框提示
//Toast.makeText(mContext, "Battery low. The device will power off when the battery drops to 10%.", Toast.LENGTH_LONG).show(); //也可以通过toast提示
mWarnings.showMyLowBatteryWarning();
}
if(mBatteryLevel<11 && !plugged){ //未充电,电量低于10关机
try{
Intent shutdownBatteryLevel = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);
shutdownBatteryLevel.putExtra(Intent.EXTRA_KEY_CONFIRM,false);
shutdownBatteryLevel.putExtra("shutdown_mode","battery");
shutdownBatteryLevel.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivityAsUser(shutdownBatteryLevel,UserHandle.CURRENT);
}catch(Exception e){
Slog.d(TAG, "batteryLevel e="+e.toString());
}
}
mWarnings.update(mBatteryLevel, bucket, mScreenOffTime);
........
public interface WarningsUI {
/**
* Updates battery and screen info for determining whether to trigger battery warnings or
* not.
* @param batteryLevel The current battery level
* @param bucket The current battery bucket
* @param screenOffTime How long the screen has been off in millis
*/
void update(int batteryLevel, int bucket, long screenOffTime);
void dismissLowBatteryWarning();
void showMyLowBatteryWarning();// add 这个方法在PowerNotificationWarnings.java实现
public
SystemUI\src\com\android\systemui\power\PowerNotificationWarnings.java
void showInvalidChargerWarning() {
mInvalidCharger = true;
updateNotification();
}
// add
public void showJzhkLowBatteryWarning() {
final SystemUIDialog d = new SystemUIDialog(mContext);
//d.setIconAttribute(android.R.attr.alertDialogIcon);
d.setTitle("Warning");
d.setCancelable(false);
d.setMessage("Battery low. The device will power off when the battery drops to 10%.");
d.setPositiveButton(com.android.internal.R.string.ok, null);
d.setShowForAllUsers(true);
d.show();
}
// add end
public