15
15
16
16
package com .ceco .gm2 .gravitybox ;
17
17
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
-
23
18
import android .content .BroadcastReceiver ;
24
19
import android .content .Intent ;
25
20
import android .content .Context ;
38
33
import de .robv .android .xposed .XposedHelpers ;
39
34
import de .robv .android .xposed .callbacks .XC_InitPackageResources .InitPackageResourcesParam ;
40
35
import de .robv .android .xposed .callbacks .XC_LayoutInflated ;
41
- import de .robv .android .xposed .callbacks .XCallback ;
42
36
43
37
public class ModBatteryStyle {
44
38
private static final String TAG = "GB:ModBatteryStyle" ;
45
39
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" ;
48
42
private static final boolean DEBUG = false ;
49
43
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" ;
51
46
public static final String EXTRA_MTK_BATTERY_PERCENTAGE_STATE = "state" ;
52
47
public static final String SETTING_MTK_BATTERY_PERCENTAGE = "battery_percentage" ;
53
48
54
49
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 ;
58
55
59
56
private static void log (String message ) {
60
57
XposedBridge .log (TAG + ": " + message );
@@ -70,13 +67,13 @@ public void onReceive(Context context, Intent intent) {
70
67
if (DEBUG ) log ("mBatteryStyle changed to: " + mBatteryStyle );
71
68
}
72
69
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 );
75
72
}
76
73
updateBatteryStyle ();
77
74
} 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 );
80
77
updateBatteryStyle ();
81
78
}
82
79
}
@@ -94,25 +91,25 @@ public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable {
94
91
liparam .res .getIdentifier ("signal_battery_cluster" , "id" , PACKAGE_NAME ));
95
92
96
93
// 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 (
98
95
"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" );
102
99
LinearLayout .LayoutParams lParams = new LinearLayout .LayoutParams (
103
100
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 (
108
105
android .R .color .holo_blue_dark ));
109
- percText .setVisibility (View .GONE );
110
- vg .addView (percText );
106
+ mPercentText .setVisibility (View .GONE );
107
+ vg .addView (mPercentText );
111
108
if (DEBUG ) log ("Battery percent text injected" );
112
109
} else {
113
- percText .setTag ("percentage" );
110
+ mPercentText .setTag ("percentage" );
114
111
}
115
- ModStatusbarColor .setPercentage (percText );
112
+ ModStatusbarColor .setPercentage (mPercentText );
116
113
117
114
// GM2 specific - if there's already view with id "circle_battery", remove it
118
115
if (Build .DISPLAY .toLowerCase ().contains ("gravitymod" )) {
@@ -125,23 +122,23 @@ public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable {
125
122
}
126
123
127
124
// 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" );
130
127
LinearLayout .LayoutParams lParams = new LinearLayout .LayoutParams (
131
128
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 );
137
134
if (DEBUG ) log ("CmCircleBattery injected" );
138
135
139
136
// find battery
140
- ImageView battery = (ImageView ) vg .findViewById (
137
+ mStockBattery = (ImageView ) vg .findViewById (
141
138
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 );
145
142
}
146
143
}
147
144
@@ -152,65 +149,27 @@ public void handleLayoutInflated(LayoutInflatedParam liparam) throws Throwable {
152
149
}
153
150
154
151
public static void init (final XSharedPreferences prefs , ClassLoader classLoader ) {
155
-
156
152
if (DEBUG ) log ("init" );
157
153
158
154
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 );
197
156
198
157
XposedBridge .hookAllConstructors (batteryControllerClass , new XC_MethodHook () {
199
158
200
159
@ Override
201
160
protected void afterHookedMethod (final MethodHookParam param ) throws Throwable {
202
- mBatteryController = param .thisObject ;
203
-
204
161
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 );
207
166
// handle obsolete settings
208
167
if (mBatteryStyle == 4 ) {
209
168
mBatteryStyle = GravityBoxSettings .BATTERY_STYLE_STOCK ;
210
169
}
211
170
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 () ?
214
173
Settings .Secure .getInt (context .getContentResolver (),
215
174
SETTING_MTK_BATTERY_PERCENTAGE , 0 ) == 1 : false ;
216
175
@@ -221,15 +180,8 @@ protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
221
180
}
222
181
context .registerReceiver (mBroadcastReceiver , intentFilter );
223
182
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 {
232
183
updateBatteryStyle ();
184
+ if (DEBUG ) log ("BatteryController constructed" );
233
185
}
234
186
});
235
187
}
@@ -239,37 +191,26 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
239
191
}
240
192
241
193
private static void updateBatteryStyle () {
242
- if (mBatteryController == null ) return ;
243
-
244
194
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 );
263
198
}
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 ));
270
211
}
271
212
} catch (Throwable t ) {
272
213
XposedBridge .log (t );
273
214
}
274
215
}
275
- }
216
+ }
0 commit comments