Skip to content

Commit 05c9709

Browse files
committed
Simplify battery styles mod
Avoid adding views into internal label and image lists since we want exclusive control over visibility settings
1 parent 7b4c0db commit 05c9709

File tree

1 file changed

+60
-119
lines changed

1 file changed

+60
-119
lines changed

src/com/ceco/gm2/gravitybox/ModBatteryStyle.java

Lines changed: 60 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515

1616
package com.ceco.gm2.gravitybox;
1717

18-
import static de.robv.android.xposed.XposedHelpers.findClass;
19-
import static de.robv.android.xposed.XposedHelpers.findAndHookMethod;
20-
21-
import java.util.ArrayList;
22-
2318
import android.content.BroadcastReceiver;
2419
import android.content.Intent;
2520
import android.content.Context;
@@ -38,23 +33,25 @@
3833
import de.robv.android.xposed.XposedHelpers;
3934
import de.robv.android.xposed.callbacks.XC_InitPackageResources.InitPackageResourcesParam;
4035
import de.robv.android.xposed.callbacks.XC_LayoutInflated;
41-
import de.robv.android.xposed.callbacks.XCallback;
4236

4337
public class ModBatteryStyle {
4438
private static final String TAG = "GB:ModBatteryStyle";
4539
public static final String PACKAGE_NAME = "com.android.systemui";
46-
public static final String CLASS_PHONE_STATUSBAR = "com.android.systemui.statusbar.phone.PhoneStatusBar";
47-
public static final String CLASS_BATTERY_CONTROLLER = "com.android.systemui.statusbar.policy.BatteryController";
40+
public static final String CLASS_BATTERY_CONTROLLER =
41+
"com.android.systemui.statusbar.policy.BatteryController";
4842
private static final boolean DEBUG = false;
4943

50-
private static final String ACTION_MTK_BATTERY_PERCENTAGE_SWITCH = "mediatek.intent.action.BATTERY_PERCENTAGE_SWITCH";
44+
private static final String ACTION_MTK_BATTERY_PERCENTAGE_SWITCH =
45+
"mediatek.intent.action.BATTERY_PERCENTAGE_SWITCH";
5146
public static final String EXTRA_MTK_BATTERY_PERCENTAGE_STATE = "state";
5247
public static final String SETTING_MTK_BATTERY_PERCENTAGE = "battery_percentage";
5348

5449
private static int mBatteryStyle;
55-
private static boolean mBatteryPercentText;
56-
private static boolean mMtkPercentText;
57-
private static Object mBatteryController;
50+
private static boolean mBatteryPercentTextEnabled;
51+
private static boolean mMtkPercentTextEnabled;
52+
private static TextView mPercentText;
53+
private static CmCircleBattery mCircleBattery;
54+
private static ImageView mStockBattery;
5855

5956
private static void log(String message) {
6057
XposedBridge.log(TAG + ": " + message);
@@ -70,13 +67,13 @@ public void onReceive(Context context, Intent intent) {
7067
if (DEBUG) log("mBatteryStyle changed to: " + mBatteryStyle);
7168
}
7269
if (intent.hasExtra("batteryPercent")) {
73-
mBatteryPercentText = intent.getBooleanExtra("batteryPercent", false);
74-
if (DEBUG) log("mBatteryPercentText changed to: " + mBatteryPercentText);
70+
mBatteryPercentTextEnabled = intent.getBooleanExtra("batteryPercent", false);
71+
if (DEBUG) log("mBatteryPercentText changed to: " + mBatteryPercentTextEnabled);
7572
}
7673
updateBatteryStyle();
7774
} else if (intent.getAction().equals(ACTION_MTK_BATTERY_PERCENTAGE_SWITCH)) {
78-
mMtkPercentText = intent.getIntExtra(EXTRA_MTK_BATTERY_PERCENTAGE_STATE, 0) == 1;
79-
if (DEBUG) log("mMtkPercentText changed to: " + mMtkPercentText);
75+
mMtkPercentTextEnabled = intent.getIntExtra(EXTRA_MTK_BATTERY_PERCENTAGE_STATE, 0) == 1;
76+
if (DEBUG) log("mMtkPercentText changed to: " + mMtkPercentTextEnabled);
8077
updateBatteryStyle();
8178
}
8279
}
@@ -94,25 +91,25 @@ public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable {
9491
liparam.res.getIdentifier("signal_battery_cluster", "id", PACKAGE_NAME));
9592

9693
// inject percent text if it doesn't exist
97-
TextView percText = (TextView) vg.findViewById(liparam.res.getIdentifier(
94+
mPercentText = (TextView) vg.findViewById(liparam.res.getIdentifier(
9895
"percentage", "id", PACKAGE_NAME));
99-
if (percText == null) {
100-
percText = new TextView(vg.getContext());
101-
percText.setTag("percentage");
96+
if (mPercentText == null) {
97+
mPercentText = new TextView(vg.getContext());
98+
mPercentText.setTag("percentage");
10299
LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(
103100
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
104-
percText.setLayoutParams(lParams);
105-
percText.setPadding(4, 0, 0, 0);
106-
percText.setTextSize(1, 16);
107-
percText.setTextColor(vg.getContext().getResources().getColor(
101+
mPercentText.setLayoutParams(lParams);
102+
mPercentText.setPadding(4, 0, 0, 0);
103+
mPercentText.setTextSize(1, 16);
104+
mPercentText.setTextColor(vg.getContext().getResources().getColor(
108105
android.R.color.holo_blue_dark));
109-
percText.setVisibility(View.GONE);
110-
vg.addView(percText);
106+
mPercentText.setVisibility(View.GONE);
107+
vg.addView(mPercentText);
111108
if (DEBUG) log("Battery percent text injected");
112109
} else {
113-
percText.setTag("percentage");
110+
mPercentText.setTag("percentage");
114111
}
115-
ModStatusbarColor.setPercentage(percText);
112+
ModStatusbarColor.setPercentage(mPercentText);
116113

117114
// GM2 specific - if there's already view with id "circle_battery", remove it
118115
if (Build.DISPLAY.toLowerCase().contains("gravitymod")) {
@@ -125,23 +122,23 @@ public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable {
125122
}
126123

127124
// inject circle battery view
128-
CmCircleBattery circleBattery = new CmCircleBattery(vg.getContext());
129-
circleBattery.setTag("circle_battery");
125+
mCircleBattery = new CmCircleBattery(vg.getContext());
126+
mCircleBattery.setTag("circle_battery");
130127
LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(
131128
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
132-
circleBattery.setLayoutParams(lParams);
133-
circleBattery.setPadding(4, 0, 0, 0);
134-
circleBattery.setVisibility(View.GONE);
135-
ModStatusbarColor.setCircleBattery(circleBattery);
136-
vg.addView(circleBattery);
129+
mCircleBattery.setLayoutParams(lParams);
130+
mCircleBattery.setPadding(4, 0, 0, 0);
131+
mCircleBattery.setVisibility(View.GONE);
132+
ModStatusbarColor.setCircleBattery(mCircleBattery);
133+
vg.addView(mCircleBattery);
137134
if (DEBUG) log("CmCircleBattery injected");
138135

139136
// find battery
140-
ImageView battery = (ImageView) vg.findViewById(
137+
mStockBattery = (ImageView) vg.findViewById(
141138
liparam.res.getIdentifier("battery", "id", PACKAGE_NAME));
142-
if (battery != null) {
143-
battery.setTag("stock_battery");
144-
ModStatusbarColor.setBattery(battery);
139+
if (mStockBattery != null) {
140+
mStockBattery.setTag("stock_battery");
141+
ModStatusbarColor.setBattery(mStockBattery);
145142
}
146143
}
147144

@@ -152,65 +149,27 @@ public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable {
152149
}
153150

154151
public static void init(final XSharedPreferences prefs, ClassLoader classLoader) {
155-
156152
if (DEBUG) log("init");
157153

158154
try {
159-
160-
Class<?> phoneStatusBarClass = findClass(CLASS_PHONE_STATUSBAR, classLoader);
161-
Class<?> batteryControllerClass = findClass(CLASS_BATTERY_CONTROLLER, classLoader);
162-
163-
findAndHookMethod(phoneStatusBarClass, "makeStatusBarView", new XC_MethodHook(XCallback.PRIORITY_HIGHEST) {
164-
165-
@Override
166-
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
167-
168-
Object mBatteryController = XposedHelpers.getObjectField(param.thisObject, "mBatteryController");
169-
View mStatusBarView = (View) XposedHelpers.getObjectField(param.thisObject, "mStatusBarView");
170-
171-
ImageView circleBattery = (ImageView) mStatusBarView.findViewWithTag("circle_battery");
172-
if (circleBattery != null) {
173-
XposedHelpers.callMethod(mBatteryController, "addIconView", circleBattery);
174-
if (DEBUG) log("BatteryController.addIconView(circleBattery)");
175-
}
176-
177-
TextView percText = (TextView) mStatusBarView.findViewWithTag("percentage");
178-
if (percText != null) {
179-
// add percent text only in case there is no label with "percentage" tag present
180-
@SuppressWarnings("unchecked")
181-
ArrayList<TextView> mLabelViews =
182-
(ArrayList<TextView>) XposedHelpers.getObjectField(mBatteryController, "mLabelViews");
183-
boolean percentTextExists = false;
184-
for (TextView tv : mLabelViews) {
185-
if ("percentage".equals(tv.getTag())) {
186-
percentTextExists = true;
187-
break;
188-
}
189-
}
190-
if (!percentTextExists) {
191-
XposedHelpers.callMethod(mBatteryController, "addLabelView", percText);
192-
if (DEBUG) log("BatteryController.addLabelView(percText)");
193-
}
194-
}
195-
}
196-
});
155+
Class<?> batteryControllerClass = XposedHelpers.findClass(CLASS_BATTERY_CONTROLLER, classLoader);
197156

198157
XposedBridge.hookAllConstructors(batteryControllerClass, new XC_MethodHook() {
199158

200159
@Override
201160
protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
202-
mBatteryController = param.thisObject;
203-
204161
prefs.reload();
205-
mBatteryStyle = Integer.valueOf(prefs.getString(GravityBoxSettings.PREF_KEY_BATTERY_STYLE, "1"));
206-
mBatteryPercentText = prefs.getBoolean(GravityBoxSettings.PREF_KEY_BATTERY_PERCENT_TEXT, false);
162+
mBatteryStyle = Integer.valueOf(prefs.getString(
163+
GravityBoxSettings.PREF_KEY_BATTERY_STYLE, "1"));
164+
mBatteryPercentTextEnabled = prefs.getBoolean(
165+
GravityBoxSettings.PREF_KEY_BATTERY_PERCENT_TEXT, false);
207166
// handle obsolete settings
208167
if (mBatteryStyle == 4) {
209168
mBatteryStyle = GravityBoxSettings.BATTERY_STYLE_STOCK;
210169
}
211170

212-
Context context = (Context) XposedHelpers.getObjectField(mBatteryController, "mContext");
213-
mMtkPercentText = Utils.isMtkDevice() ?
171+
Context context = (Context) XposedHelpers.getObjectField(param.thisObject, "mContext");
172+
mMtkPercentTextEnabled = Utils.isMtkDevice() ?
214173
Settings.Secure.getInt(context.getContentResolver(),
215174
SETTING_MTK_BATTERY_PERCENTAGE, 0) == 1 : false;
216175

@@ -221,15 +180,8 @@ protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
221180
}
222181
context.registerReceiver(mBroadcastReceiver, intentFilter);
223182

224-
if (DEBUG) log("BatteryController constructed");
225-
}
226-
});
227-
228-
findAndHookMethod(batteryControllerClass, "onReceive", Context.class, Intent.class, new XC_MethodHook() {
229-
230-
@Override
231-
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
232183
updateBatteryStyle();
184+
if (DEBUG) log("BatteryController constructed");
233185
}
234186
});
235187
}
@@ -239,37 +191,26 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
239191
}
240192

241193
private static void updateBatteryStyle() {
242-
if (mBatteryController == null) return;
243-
244194
try {
245-
@SuppressWarnings("unchecked")
246-
ArrayList<ImageView> mIconViews =
247-
(ArrayList<ImageView>) XposedHelpers.getObjectField(mBatteryController, "mIconViews");
248-
@SuppressWarnings("unchecked")
249-
ArrayList<TextView> mLabelViews =
250-
(ArrayList<TextView>) XposedHelpers.getObjectField(mBatteryController, "mLabelViews");
251-
252-
for (ImageView iv : mIconViews) {
253-
if ("stock_battery".equals(iv.getTag())) {
254-
iv.setVisibility((mBatteryStyle == GravityBoxSettings.BATTERY_STYLE_STOCK) ?
255-
View.VISIBLE : View.GONE);
256-
} else if ("circle_battery".equals(iv.getTag())) {
257-
iv.setVisibility((mBatteryStyle == GravityBoxSettings.BATTERY_STYLE_CIRCLE ||
258-
mBatteryStyle == GravityBoxSettings.BATTERY_STYLE_CIRCLE_PERCENT) ?
259-
View.VISIBLE : View.GONE);
260-
((CmCircleBattery)iv).setPercentage(
261-
mBatteryStyle == GravityBoxSettings.BATTERY_STYLE_CIRCLE_PERCENT);
262-
}
195+
if (mStockBattery != null) {
196+
mStockBattery.setVisibility((mBatteryStyle == GravityBoxSettings.BATTERY_STYLE_STOCK) ?
197+
View.VISIBLE : View.GONE);
263198
}
264-
265-
for (TextView tv : mLabelViews) {
266-
if ("percentage".equals(tv.getTag())) {
267-
tv.setVisibility(
268-
((mBatteryPercentText || mMtkPercentText) ? View.VISIBLE : View.GONE));
269-
}
199+
200+
if (mCircleBattery != null) {
201+
mCircleBattery.setVisibility((mBatteryStyle == GravityBoxSettings.BATTERY_STYLE_CIRCLE ||
202+
mBatteryStyle == GravityBoxSettings.BATTERY_STYLE_CIRCLE_PERCENT) ?
203+
View.VISIBLE : View.GONE);
204+
((CmCircleBattery)mCircleBattery).setPercentage(
205+
mBatteryStyle == GravityBoxSettings.BATTERY_STYLE_CIRCLE_PERCENT);
206+
}
207+
208+
if (mPercentText != null) {
209+
mPercentText.setVisibility(((mBatteryPercentTextEnabled || mMtkPercentTextEnabled) ?
210+
View.VISIBLE : View.GONE));
270211
}
271212
} catch (Throwable t) {
272213
XposedBridge.log(t);
273214
}
274215
}
275-
}
216+
}

0 commit comments

Comments
 (0)