-
Notifications
You must be signed in to change notification settings - Fork 8
/
BatteryDefenderNotification.java
255 lines (227 loc) · 10.5 KB
/
BatteryDefenderNotification.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright (C) 2022 The PixelExperience Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.systemui.power;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.logging.UiEventLogger;
import com.android.systemui.R;
import com.android.systemui.util.NotificationChannels;
import java.text.NumberFormat;
import java.time.Clock;
import java.util.NoSuchElementException;
import vendor.google.google_battery.IGoogleBattery;
class BatteryDefenderNotification {
private static final String TAG = "BatteryDefenderNotification";
private final Context mContext;
private final NotificationManager mNotificationManager;
private final UiEventLogger mUiEventLogger;
@VisibleForTesting
boolean mDefenderEnabled;
@VisibleForTesting
boolean mPostNotificationVisible;
@VisibleForTesting
boolean mPrvPluggedState;
@VisibleForTesting
boolean mRunBypassActionTask = true;
private int mBatteryLevel;
private SharedPreferences mSharedPreferences;
private boolean mHasSystemFeature = false;
BatteryDefenderNotification(Context context, UiEventLogger uiEventLogger) {
mContext = context;
mUiEventLogger = uiEventLogger;
mNotificationManager = context.getSystemService(NotificationManager.class);
mHasSystemFeature = mContext.getPackageManager().hasSystemFeature("com.google.android.feature.ADAPTIVE_CHARGING");
}
void dispatchIntent(Intent intent) {
String action = intent.getAction();
if ("android.intent.action.BATTERY_CHANGED".equals(action)) {
resolveBatteryChangedIntent(intent);
} else if ("PNW.defenderResumeCharging".equals(action)) {
resumeCharging(BatteryDefenderEvent.BATTERY_DEFENDER_BYPASS_LIMIT);
} else if ("PNW.defenderResumeCharging.settings".equals(action)) {
resumeCharging(BatteryDefenderEvent.BATTERY_DEFENDER_BYPASS_LIMIT_FOR_TIPS);
}
}
private void resolveBatteryChangedIntent(Intent intent) {
mBatteryLevel = PowerUtils.getBatteryLevel(intent);
boolean z = false;
boolean z2 = intent.getIntExtra("plugged", 0) != 0;
if (intent.getIntExtra("health", 1) == 3) {
z = true;
}
boolean isFullyCharged = PowerUtils.isFullyCharged(intent);
Log.d(TAG, "isPlugged: " + z2 + " | isOverheated: " + z + " | defenderEnabled: " + mDefenderEnabled + " | isCharged: " + isFullyCharged);
if (isFullyCharged && mPostNotificationVisible) {
cancelPostNotification();
}
if (z) {
sendNotification(z2);
} else if (mDefenderEnabled) {
cancelNotificationAndSendPostNotification();
}
}
private void sendNotification(boolean z) {
if (!mDefenderEnabled) {
if (mPostNotificationVisible) {
cancelPostNotification();
}
getSharedPreferences().edit().putLong("trigger_time", Clock.systemUTC().millis()).apply();
}
if (!mDefenderEnabled || mPrvPluggedState != z) {
mPrvPluggedState = z;
sendNotificationInternal(z);
}
}
private void sendNotificationInternal(boolean z) {
NotificationCompat.Builder addAction = new NotificationCompat.Builder(mContext, NotificationChannels.BATTERY).setSmallIcon(17303566).setContentTitle(mContext.getString(R.string.defender_notify_title)).setContentText(mContext.getString(R.string.defender_notify_des)).addAction(0, mContext.getString(R.string.battery_health_notify_learn_more), PowerUtils.createHelpArticlePendingIntent(mContext, R.string.defender_notify_help_url));
if (z) {
addAction.addAction(0, mContext.getString(R.string.defender_notify_resume_charge), PowerUtils.createNormalChargingIntent(mContext, "PNW.defenderResumeCharging"));
}
PowerUtils.overrideNotificationAppName(mContext, addAction, 17040783);
mNotificationManager.notifyAsUser("battery_defender", PowerUtils.NOTIFICATION_ID, addAction.build(), UserHandle.ALL);
if (!mDefenderEnabled) {
mDefenderEnabled = true;
if (mUiEventLogger == null) {
return;
}
mUiEventLogger.log(BatteryDefenderEvent.BATTERY_DEFENDER_NOTIFICATION);
}
}
private void cancelNotificationAndSendPostNotification() {
cancelNotification();
if (getSharedPreferences().contains("trigger_time")) {
sendPostNotification();
}
}
private void sendPostNotification() {
long j = getSharedPreferences().getLong("trigger_time", -1L);
String currentTime = j > 0 ? PowerUtils.getCurrentTime(mContext, j) : null;
if (currentTime != null && PowerUtils.postNotificationThreshold(j)) {
NotificationCompat.Builder addAction = new NotificationCompat.Builder(mContext, NotificationChannels.BATTERY).setSmallIcon(17303566).setContentTitle(mContext.getString(R.string.defender_post_notify_title)).setContentText(mContext.getString(R.string.defender_post_notify_des, NumberFormat.getPercentInstance().format(0.800000011920929d), currentTime, PowerUtils.getCurrentTime(mContext, Clock.systemUTC().millis()))).addAction(0, mContext.getString(R.string.battery_health_notify_learn_more), PowerUtils.createHelpArticlePendingIntent(mContext, R.string.defender_notify_help_url));
PowerUtils.overrideNotificationAppName(mContext, addAction, 17040783);
mNotificationManager.notifyAsUser("battery_defender", PowerUtils.POST_NOTIFICATION_ID, addAction.build(), UserHandle.ALL);
mPostNotificationVisible = true;
} else {
Log.w(TAG, "error getting trigger time");
}
clearDefenderStartRecord();
}
private void resumeCharging(BatteryDefenderEvent batteryDefenderEvent) {
if (mUiEventLogger != null) {
mUiEventLogger.logWithPosition(batteryDefenderEvent, 0, (String) null, mBatteryLevel);
}
Log.d(TAG, "resume charging: " + batteryDefenderEvent.mId);
executeBypassActionWithAsync();
mNotificationManager.cancelAsUser("battery_defender", PowerUtils.NOTIFICATION_ID, UserHandle.ALL);
clearDefenderStartRecord();
}
private void executeBypassActionWithAsync() {
if (!mRunBypassActionTask) {
return;
}
AsyncTask.execute(() -> {
IBinder.DeathRecipient cbRecipient = new IBinder.DeathRecipient() {
@Override
public final void binderDied() {
Log.d(TAG, "serviceDied");
}
};
if(!mHasSystemFeature) {
Log.d(TAG, "Device does not support Google Battery");
return;
}
IGoogleBattery initHalInterface = initHalInterface(cbRecipient);
if (initHalInterface == null) {
Log.d(TAG, "Can not init hal interface");
}
try {
try {
initHalInterface.setProperty(2, 17, 1);
initHalInterface.setProperty(3, 17, 1);
initHalInterface.setProperty(1, 17, 1);
} catch (RemoteException e) {
Log.e(TAG, "setProperty error: " + e);
}
} finally {
destroyHalInterface(initHalInterface, cbRecipient);
}
});
}
private static IGoogleBattery initHalInterface(IBinder.DeathRecipient deathReceiver) {
Log.d(TAG, "initHalInterface");
try {
IBinder binder = Binder.allowBlocking(ServiceManager.waitForDeclaredService("vendor.google.google_battery.IGoogleBattery/default"));
IGoogleBattery batteryInterface = null;
if (binder != null) {
batteryInterface = IGoogleBattery.Stub.asInterface(binder);
if (batteryInterface != null && deathReceiver != null) {
binder.linkToDeath(deathReceiver, 0);
}
}
return batteryInterface;
} catch (RemoteException | NoSuchElementException | SecurityException e) {
Log.e(TAG, "failed to get Google Battery HAL: ", e);
return null;
}
}
private static void destroyHalInterface(IGoogleBattery iGoogleBattery, IBinder.DeathRecipient deathRecipient) {
Log.d(TAG, "destroyHalInterface");
if (deathRecipient != null && iGoogleBattery != null) {
iGoogleBattery.asBinder().unlinkToDeath(deathRecipient, 0);
}
}
private SharedPreferences getSharedPreferences() {
if (mSharedPreferences == null) {
mSharedPreferences = mContext.getApplicationContext().getSharedPreferences("defender_shared_prefs", 0);
}
return mSharedPreferences;
}
private void cancelNotification() {
mDefenderEnabled = false;
mNotificationManager.cancelAsUser("battery_defender", PowerUtils.NOTIFICATION_ID, UserHandle.ALL);
}
private void cancelPostNotification() {
mPostNotificationVisible = false;
clearDefenderStartRecord();
mNotificationManager.cancelAsUser("battery_defender", PowerUtils.POST_NOTIFICATION_ID, UserHandle.ALL);
}
private void clearDefenderStartRecord() {
getSharedPreferences().edit().clear().apply();
}
enum BatteryDefenderEvent implements UiEventLogger.UiEventEnum {
BATTERY_DEFENDER_NOTIFICATION(876),
BATTERY_DEFENDER_BYPASS_LIMIT(877),
BATTERY_DEFENDER_BYPASS_LIMIT_FOR_TIPS(878);
private final int mId;
BatteryDefenderEvent(int i) {
mId = i;
}
public int getId() {
return mId;
}
}
}