Skip to content

Commit

Permalink
feat: statusbar horiz padding
Browse files Browse the repository at this point in the history
  • Loading branch information
MonwF committed Dec 7, 2022
1 parent 55e459f commit c249ff2
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public void handleLoadPackage(final LoadPackageParam lpparam) {
System.StatusBarClockAtRightHook(lpparam);
}
if (mPrefs.getBoolean("system_statusbar_batterytempandcurrent")) System.DisplayBatteryDetailHook(lpparam);
if (mPrefs.getBoolean("system_showlux")) System.BrightnessLuxHook(lpparam);
if (mPrefs.getBoolean("system_statusbar_horizmargin")) System.HorizMarginHook(lpparam);
if (mPrefs.getBoolean("system_showpct")) System.BrightnessPctHook(lpparam);
if (mPrefs.getBoolean("system_cleanmirror")) System.ClearBrightnessMirrorHook(lpparam);
if (mPrefs.getBoolean("system_hidelsstatusbar")) System.HideLockScreenStatusBarHook(lpparam);
Expand Down
220 changes: 26 additions & 194 deletions app/src/main/java/name/mikanoshi/customiuizer/mods/System.java
Original file line number Diff line number Diff line change
Expand Up @@ -5407,200 +5407,6 @@ protected void after(MethodHookParam param) throws Throwable {
});
}

private static final TextView[] lux = { null, null, null };
private static class LuxListener implements SensorEventListener {
@Override
public void onSensorChanged(SensorEvent event) {
long cTime = currentTimeMillis();
String caption = null;

if (lux[0] != null && lux[0].isAttachedToWindow()) try {
Long last = (Long)lux[0].getTag();
if (last != null && cTime - last < 750) return;
caption = Helpers.getModuleRes(lux[0].getContext()).getString(R.string.lux, String.valueOf(Math.round(event.values[0])));
lux[0].setText(caption);
lux[0].setTag(cTime);
} catch (Throwable t) {
XposedBridge.log(t);
}

if (lux[1] != null && lux[1].isAttachedToWindow()) try {
Long last = (Long)lux[1].getTag();
if (last != null && cTime - last < 750) return;
if (caption == null)
caption = Helpers.getModuleRes(lux[1].getContext()).getString(R.string.lux, String.valueOf(Math.round(event.values[0])));
lux[1].setText(caption);
lux[1].setTag(cTime);
} catch (Throwable t) {
XposedBridge.log(t);
}

if (lux[2] != null && lux[2].isAttachedToWindow()) try {
Long last = (Long)lux[2].getTag();
if (last != null && cTime - last < 750) return;
if (caption == null)
caption = Helpers.getModuleRes(lux[2].getContext()).getString(R.string.lux, String.valueOf(Math.round(event.values[0])));
lux[2].setText(caption);
lux[2].setTag(cTime);
} catch (Throwable t) {
XposedBridge.log(t);
}
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
}

private static void registerLuxListener(Object statusBar) {
try {
Context mContext = (Context)XposedHelpers.getObjectField(statusBar, "mContext");
PowerManager powerMgr = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
if (!powerMgr.isInteractive()) return;
boolean sBootCompleted = XposedHelpers.getBooleanField(statusBar, "sBootCompleted");
if (!sBootCompleted) return;
SensorManager sensorMgr = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
LuxListener mLuxListener = (LuxListener)XposedHelpers.getAdditionalInstanceField(statusBar, "mLuxListener");
sensorMgr.registerListener(mLuxListener, sensorMgr.getDefaultSensor(Sensor.TYPE_LIGHT), SensorManager.SENSOR_DELAY_NORMAL);
XposedHelpers.setAdditionalInstanceField(statusBar, "mListenerEnabled", true);
} catch (Throwable t) {
XposedBridge.log(t);
}
}

private static void unregisterLuxListener(Object statusBar) {
try {
Context mContext = (Context)XposedHelpers.getObjectField(statusBar, "mContext");
SensorManager sensorMgr = (SensorManager)mContext.getSystemService(Context.SENSOR_SERVICE);
LuxListener mLuxListener = (LuxListener)XposedHelpers.getAdditionalInstanceField(statusBar, "mLuxListener");
sensorMgr.unregisterListener(mLuxListener);
XposedHelpers.setAdditionalInstanceField(statusBar, "mListenerEnabled", false);
} catch (Throwable t) {
XposedBridge.log(t);
}
}

public static void setTextColors(Context context, TextView textView) {
textView.setTextColor(Helpers.isNightMode(context) ? 0xFFDDDDDD : 0xFF606060);
if (Helpers.isNightMode(context))
textView.setShadowLayer(context.getResources().getDisplayMetrics().density, 1f, 1f, 0xEE222224);
else
textView.setShadowLayer(0, 0, 0, Color.WHITE);
}

public static void BrightnessLuxHook(LoadPackageParam lpparam) {
MethodHook hook = new MethodHook() {
@Override
protected void after(MethodHookParam param) throws Throwable {
Context context = (Context)param.args[0];
ViewGroup container = (ViewGroup)param.thisObject;
int port = context.getResources().getIdentifier("qs_brightness", "id", lpparam.packageName);
int land = context.getResources().getIdentifier("qs_brightness_land", "id", lpparam.packageName);
if (container.getId() != port && container.getId() != land) return;

TextView clux = new TextView(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.alignWithParent = true;
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
clux.setLayoutParams(lp);
clux.setGravity(Gravity.CENTER);

boolean isAlt = MainModule.mPrefs.getBoolean("system_showlux_style");
setTextColors(context, clux);
if (isAlt) {
clux.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
clux.setAlpha(0.33f);
}

container.addView(clux);

if ("QCToggleSliderView".equals(param.thisObject.getClass().getSimpleName())) {
if (container.getId() == land)
lux[2] = clux;
else
lux[1] = clux;
} else {
lux[0] = clux;
}
}
};

Helpers.findAndHookConstructor("com.android.systemui.settings.ToggleSliderView", lpparam.classLoader, Context.class, AttributeSet.class, int.class, hook);
Helpers.findAndHookConstructor("com.android.systemui.miui.controlcenter.QCToggleSliderView", lpparam.classLoader, Context.class, AttributeSet.class, int.class, hook);

Helpers.hookAllMethods("com.android.systemui.statusbar.phone.StatusBar", lpparam.classLoader, "makeStatusBarView", new MethodHook() {
@Override
protected void after(final MethodHookParam param) throws Throwable {
LuxListener mLuxListener = new LuxListener();
XposedHelpers.setAdditionalInstanceField(param.thisObject, "mLuxListener", mLuxListener);
XposedHelpers.setAdditionalInstanceField(param.thisObject, "mListenerEnabled", false);
}
});

Helpers.findAndHookMethod("com.android.systemui.statusbar.phone.StatusBar", lpparam.classLoader, "setPanelExpanded", boolean.class, new MethodHook() {
@Override
protected void after(final MethodHookParam param) throws Throwable {
boolean isExpanded = (boolean)param.args[0];
boolean mListenerEnabled = (boolean)XposedHelpers.getAdditionalInstanceField(param.thisObject, "mListenerEnabled");

if (isExpanded && !mListenerEnabled)
registerLuxListener(param.thisObject);
else if (!isExpanded && mListenerEnabled)
unregisterLuxListener(param.thisObject);
}
});

if (Helpers.is12()) {
Helpers.findAndHookMethod("com.android.systemui.miui.controlcenter.QSControlCenterPanel", lpparam.classLoader, "setListening", boolean.class, new MethodHook() {
@Override
protected void after(final MethodHookParam param) throws Throwable {
boolean isExpanded = (boolean)param.args[0];
Context mContext = (Context)XposedHelpers.getObjectField(param.thisObject, "mContext");
Object mApplication = XposedHelpers.callMethod(mContext.getApplicationContext(), "getSystemUIApplication");
Object mStatusBar = XposedHelpers.callMethod(mApplication, "getComponent", findClassIfExists("com.android.systemui.statusbar.phone.StatusBar", lpparam.classLoader));
boolean mListenerEnabled = (boolean)XposedHelpers.getAdditionalInstanceField(mStatusBar, "mListenerEnabled");
if (isExpanded && !mListenerEnabled)
registerLuxListener(mStatusBar);
else if (!isExpanded && mListenerEnabled)
unregisterLuxListener(mStatusBar);
}
});

Helpers.findAndHookMethod("com.android.systemui.miui.controlcenter.QSControlCenterPanel", lpparam.classLoader, "updateResources", new MethodHook() {
@Override
protected void after(final MethodHookParam param) throws Throwable {
Context mContext = (Context)XposedHelpers.getObjectField(param.thisObject, "mContext");
if (lux[1] != null) setTextColors(mContext, lux[1]);
if (lux[2] != null) setTextColors(mContext, lux[2]);
}
});
}

Helpers.findAndHookMethod("com.android.systemui.statusbar.phone.StatusBar", lpparam.classLoader, "onScreenTurnedOff", new MethodHook() {
@Override
protected void after(final MethodHookParam param) throws Throwable {
unregisterLuxListener(param.thisObject);
}
});

Helpers.findAndHookMethod("com.android.systemui.statusbar.phone.StatusBar", lpparam.classLoader, "onScreenTurnedOn", new MethodHook() {
@Override
protected void after(final MethodHookParam param) throws Throwable {
boolean mPanelExpanded = XposedHelpers.getBooleanField(param.thisObject, "mPanelExpanded");
boolean mListenerEnabled = (boolean)XposedHelpers.getAdditionalInstanceField(param.thisObject, "mListenerEnabled");
if (mPanelExpanded && !mListenerEnabled) registerLuxListener(param.thisObject);
}
});

Helpers.findAndHookMethod("com.android.systemui.statusbar.phone.StatusBar", lpparam.classLoader, "onBootCompleted", new MethodHook() {
@Override
protected void after(final MethodHookParam param) throws Throwable {
boolean mPanelExpanded = XposedHelpers.getBooleanField(param.thisObject, "mPanelExpanded");
boolean mListenerEnabled = (boolean)XposedHelpers.getAdditionalInstanceField(param.thisObject, "mListenerEnabled");
if (mPanelExpanded && !mListenerEnabled) registerLuxListener(param.thisObject);
}
});
}

@SuppressLint("StaticFieldLeak")
private static TextView mPct = null;
private static void initPct(ViewGroup container, int source, Context context) {
Expand Down Expand Up @@ -8191,6 +7997,32 @@ protected void after(final MethodHookParam param) throws Throwable {
});
}

public static void HorizMarginHook(LoadPackageParam lpparam) {
MainModule.resHooks.setDensityReplacement(lpparam.packageName, "dimen", "status_bar_padding_start", 0);
MainModule.resHooks.setDensityReplacement(lpparam.packageName, "dimen", "status_bar_padding_end", 0);
Helpers.hookAllMethods("com.android.systemui.statusbar.phone.StatusBarWindowView", lpparam.classLoader, "paddingNeededForCutoutAndRoundedCorner", new MethodHook() {
@Override
protected void before(final MethodHookParam param) throws Throwable {
Context context = Helpers.findContext();
int leftMargin = MainModule.mPrefs.getInt("system_statusbar_horizmargin_left", 16);
float marginLeft = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
leftMargin,
context.getResources().getDisplayMetrics()
);
leftMargin = (int) marginLeft;
int rightMargin = MainModule.mPrefs.getInt("system_statusbar_horizmargin_right", 16);
float marginRight = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
rightMargin,
context.getResources().getDisplayMetrics()
);
rightMargin = (int) marginRight;
param.setResult(new Pair<Integer, Integer>(Integer.valueOf(leftMargin), Integer.valueOf(rightMargin)));
}
});
}

public static void MobileTypeSingleHook(LoadPackageParam lpparam) {
MethodHook showSingleMobileType = new MethodHook(MethodHook.PRIORITY_HIGHEST) {
@Override
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@
<string name="system_statusbar_mobiletype_single_title">移动网络类型单独显示</string>
<string name="system_statusbar_mobiletype_single_leftmargin_title">左侧间距</string>
<string name="system_statusbar_mobiletype_single_verticaloffset">上下偏移量</string>
<string name="system_statusbar_horizmargin_title">状态栏水平边距</string>
<string name="system_notifrowmenu_appinfo">应用信息</string>
<string name="system_notifrowmenu_forceclose">强制关闭</string>
<string name="restart_systemui">重启系统界面</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,7 @@
<string name="system_statusbar_mobiletype_single_title">Put mobile network type at the right of signal icon</string>
<string name="system_statusbar_mobiletype_single_leftmargin_title">Left margin</string>
<string name="system_statusbar_mobiletype_single_verticaloffset">Vertical offset</string>
<string name="system_statusbar_horizmargin_title">Statusbar horizontal padding</string>
<string name="system_statusbaricons_atright_title">Move some icons to the right</string>
<string name="system_statusbaricons_atright_summ">Mainly for devices with cutout at the center of the top edge</string>
<string name="system_statusbaricons_netspeed_title">Network speed indicator</string>
Expand Down
41 changes: 27 additions & 14 deletions app/src/main/res/xml/prefs_system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -483,12 +483,39 @@
android:key="pref_key_system_statusbarheight"
android:title="@string/system_statusbarheight_title"
android:defaultValue="19"
app:isPreferenceVisible="false"
miuizer:offtext="@string/array_default"
miuizer:minValue="19"
miuizer:maxValue="100"
miuizer:stepValue="1"
miuizer:format="%d dip" />

<name.mikanoshi.customiuizer.prefs.CheckBoxPreferenceEx
android:key="pref_key_system_statusbar_horizmargin"
android:title="@string/system_statusbar_horizmargin_title" />

<name.mikanoshi.customiuizer.prefs.SeekBarPreference
android:key="pref_key_system_statusbar_horizmargin_left"
android:title="@string/system_statusbar_mobiletype_single_leftmargin_title"
android:dependency="pref_key_system_statusbar_horizmargin"
android:defaultValue="16"
miuizer:child="true"
miuizer:minValue="0"
miuizer:maxValue="60"
miuizer:stepValue="1"
miuizer:format="%s dip" />

<name.mikanoshi.customiuizer.prefs.SeekBarPreference
android:key="pref_key_system_statusbar_horizmargin_right"
android:title="@string/system_statusbar_dualsimin2rows_rightmargin_title"
android:dependency="pref_key_system_statusbar_horizmargin"
android:defaultValue="16"
miuizer:child="true"
miuizer:minValue="0"
miuizer:maxValue="30"
miuizer:stepValue="1"
miuizer:format="%s dip" />

<name.mikanoshi.customiuizer.prefs.CheckBoxPreferenceEx
android:key="pref_key_system_statusbarcolor"
android:title="@string/system_statusbarcolor_title"
Expand Down Expand Up @@ -587,20 +614,6 @@
android:summary="@string/system_cleanmirror_summ"
android:defaultValue="false" />

<name.mikanoshi.customiuizer.prefs.CheckBoxPreferenceEx
android:key="pref_key_system_showlux"
android:title="@string/system_showlux_title"
android:summary="@string/system_showlux_summ"
android:defaultValue="false" />

<name.mikanoshi.customiuizer.prefs.CheckBoxPreferenceEx
android:key="pref_key_system_showlux_style"
android:title="@string/system_showlux_style_title"
android:summary="@string/system_showlux_style_summ"
android:dependency="pref_key_system_showlux"
android:defaultValue="false"
miuizer:child="true" />

<name.mikanoshi.customiuizer.prefs.CheckBoxPreferenceEx
android:key="pref_key_system_showpct"
android:title="@string/system_showpct_title"
Expand Down

0 comments on commit c249ff2

Please sign in to comment.