Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit a92d752

Browse files
committed
Rewrite FoxCompat to support Notch and remove unused stuff.
1 parent afc9164 commit a92d752

File tree

8 files changed

+207
-266
lines changed

8 files changed

+207
-266
lines changed

app/src/main/java/android/os/SystemProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
import com.topjohnwu.superuser.ShellUtils;
66

7+
/**
8+
* I will probably outsource this to a separate library later
9+
*/
710
@Keep
811
public class SystemProperties {
912
@Keep
@@ -13,4 +16,15 @@ public static String get(String key) {
1316
prop = prop.substring(0, prop.length() - 1).trim();
1417
return prop;
1518
}
19+
20+
@Keep
21+
public static int getInt(String key, int def) {
22+
try {
23+
String value = get(key);
24+
if (value.isEmpty()) return def;
25+
return Integer.parseInt(value);
26+
} catch (Exception e) {
27+
return def;
28+
}
29+
}
1630
}

app/src/main/java/com/fox2code/mmm/MainApplication.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fox2code.mmm;
22

33
import android.annotation.SuppressLint;
4+
import android.content.ComponentName;
45
import android.content.Intent;
56
import android.content.SharedPreferences;
67
import android.content.res.Configuration;
@@ -78,6 +79,13 @@ public static Shell build(String... command) {
7879
}
7980

8081
public static void addSecret(Intent intent) {
82+
ComponentName componentName = intent.getComponent();
83+
String packageName = componentName != null ?
84+
componentName.getPackageName() : intent.getPackage();
85+
if (!BuildConfig.APPLICATION_ID.equalsIgnoreCase(packageName)) {
86+
// Code safeguard, we should never reach here.
87+
throw new IllegalArgumentException("Can't add secret to outbound Intent");
88+
}
8189
intent.putExtra("secret", secret);
8290
}
8391

@@ -101,10 +109,6 @@ public static boolean isShowIncompatibleModules() {
101109
return getSharedPreferences().getBoolean("pref_show_incompatible", false);
102110
}
103111

104-
public static boolean isForceEnglish() {
105-
return getSharedPreferences().getBoolean("pref_force_english", false);
106-
}
107-
108112
public static boolean isForceDarkTerminal() {
109113
return getSharedPreferences().getBoolean("pref_force_dark_terminal", false);
110114
}
@@ -190,7 +194,6 @@ public Markwon getMarkwon() {
190194
if (contextThemeWrapper == null) {
191195
contextThemeWrapper = this.markwonThemeContext =
192196
new CompatThemeWrapper(this, this.managerThemeResId);
193-
contextThemeWrapper.setForceEnglish(isForceEnglish());
194197
}
195198
Markwon markwon = Markwon.builder(contextThemeWrapper).usePlugin(HtmlPlugin.create())
196199
.usePlugin(SyntaxHighlightPlugin.create(
@@ -327,14 +330,12 @@ public void onCreate() {
327330

328331
@Override
329332
public void onCreateCompatActivity(CompatActivity compatActivity) {
330-
this.setForceEnglish(isForceEnglish());
331333
super.onCreateCompatActivity(compatActivity);
332334
compatActivity.setTheme(this.managerThemeResId);
333335
}
334336

335337
@Override
336338
public void onRefreshUI(CompatActivity compatActivity) {
337-
this.setForceEnglish(isForceEnglish());
338339
super.onRefreshUI(compatActivity);
339340
compatActivity.setThemeRecreate(this.managerThemeResId);
340341
}

app/src/main/java/com/fox2code/mmm/compat/CompatActivity.java

Lines changed: 83 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@
1212
import android.os.Build;
1313
import android.os.Bundle;
1414
import android.os.SystemProperties;
15-
import android.util.AttributeSet;
1615
import android.util.Log;
1716
import android.util.TypedValue;
17+
import android.view.Display;
1818
import android.view.KeyCharacterMap;
1919
import android.view.KeyEvent;
2020
import android.view.Menu;
2121
import android.view.MenuItem;
22+
import android.view.Surface;
2223
import android.view.View;
2324
import android.view.ViewConfiguration;
24-
import android.widget.ScrollView;
2525

2626
import androidx.annotation.AttrRes;
2727
import androidx.annotation.CallSuper;
2828
import androidx.annotation.ColorInt;
2929
import androidx.annotation.ColorRes;
3030
import androidx.annotation.Dimension;
3131
import androidx.annotation.DrawableRes;
32+
import androidx.annotation.IntDef;
3233
import androidx.annotation.NonNull;
3334
import androidx.annotation.Nullable;
3435
import androidx.annotation.Px;
@@ -37,19 +38,20 @@
3738
import androidx.appcompat.app.AppCompatActivity;
3839
import androidx.core.content.ContextCompat;
3940
import androidx.core.graphics.ColorUtils;
41+
import androidx.core.graphics.Insets;
4042
import androidx.core.view.WindowInsetsCompat;
4143
import androidx.core.widget.NestedScrollView;
4244
import androidx.fragment.app.Fragment;
4345
import androidx.recyclerview.widget.RecyclerView;
4446

4547
import com.fox2code.mmm.Constants;
4648
import com.fox2code.mmm.R;
47-
import com.kieronquinn.monetcompat.app.MonetCompatActivity;
4849
import com.kieronquinn.monetcompat.extensions.views.ViewExtensions_RecyclerViewKt;
4950
import com.kieronquinn.monetcompat.extensions.views.ViewExtensions_ScrollViewKt;
5051

52+
import java.lang.annotation.Retention;
53+
import java.lang.annotation.RetentionPolicy;
5154
import java.lang.ref.WeakReference;
52-
import java.util.Locale;
5355
import java.util.Objects;
5456

5557
import rikka.insets.WindowInsetsHelper;
@@ -71,13 +73,13 @@ public boolean onBackPressed(CompatActivity compatActivity) {
7173
};
7274

7375
final WeakReference<CompatActivity> selfReference;
74-
private final CompatConfigHelper compatConfigHelper = new CompatConfigHelper(this);
7576
private CompatActivity.OnActivityResultCallback onActivityResultCallback;
7677
private CompatActivity.OnBackPressedCallback onBackPressedCallback;
7778
private MenuItem.OnMenuItemClickListener menuClickListener;
7879
private CharSequence menuContentDescription;
79-
@StyleRes
80-
private int setThemeDynamic = 0;
80+
private int displayCutoutHeight = 0;
81+
@Rotation private int cachedRotation = 0;
82+
@StyleRes private int setThemeDynamic = 0;
8183
private boolean onCreateCalledOnce = false;
8284
private boolean onCreateCalled = false;
8385
private boolean isRefreshUi = false;
@@ -107,6 +109,8 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
107109
}
108110
}));
109111
this.hasHardwareNavBar = this.hasHardwareNavBar0();
112+
this.displayCutoutHeight = CompatNotch.getNotchHeight(this);
113+
this.cachedRotation = this.getRotation();
110114
this.onCreateCalledOnce = true;
111115
}
112116
Application application = this.getApplication();
@@ -115,17 +119,28 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
115119
}
116120
super.onCreate(savedInstanceState);
117121
this.onCreateCalled = true;
118-
this.checkResourcesOverrides(
119-
this.forceEnglish, this.nightModeOverride);
120122
}
121123

124+
122125
@Override
123126
protected void onResume() {
124127
this.hasHardwareNavBar = this.hasHardwareNavBar0();
128+
this.displayCutoutHeight = CompatNotch.getNotchHeight(this);
129+
this.cachedRotation = this.getRotation();
125130
super.onResume();
126131
this.refreshUI();
127132
}
128133

134+
@Override
135+
public void onConfigurationChanged(@NonNull Configuration newConfig) {
136+
super.onConfigurationChanged(newConfig);
137+
if (this.cachedRotation != this.getRotation() &&
138+
this.onCreateCalledOnce) {
139+
this.cachedRotation = this.getRotation();
140+
this.displayCutoutHeight = CompatNotch.getNotchHeight(this);
141+
}
142+
}
143+
129144
@Override
130145
public void finish() {
131146
this.onActivityResultCallback = null;
@@ -151,8 +166,6 @@ public void refreshUI() {
151166
} finally {
152167
this.isRefreshUi = false;
153168
}
154-
this.checkResourcesOverrides(
155-
this.forceEnglish, this.nightModeOverride);
156169
}
157170
}
158171

@@ -239,8 +252,7 @@ public View getActionBarView() {
239252
}
240253
}
241254

242-
@Dimension
243-
@Px
255+
@Dimension @Px
244256
public int getActionBarHeight() {
245257
androidx.appcompat.app.ActionBar compatActionBar;
246258
try {
@@ -266,15 +278,6 @@ public int getActionBarHeight() {
266278
}
267279
}
268280

269-
public int getActionBarHeight(Activity activity) {
270-
TypedValue tv = new TypedValue();
271-
int actionBarHeight = 0;
272-
if (activity.getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
273-
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
274-
}
275-
return actionBarHeight;
276-
}
277-
278281
public void setActionBarBackground(Drawable drawable) {
279282
androidx.appcompat.app.ActionBar compatActionBar;
280283
try {
@@ -295,8 +298,17 @@ public void setActionBarBackground(Drawable drawable) {
295298
@Dimension @Px
296299
@SuppressLint("InternalInsetResource")
297300
public int getStatusBarHeight() {
298-
int height = WindowInsetsCompat.CONSUMED.getInsets(
299-
WindowInsetsCompat.Type.statusBars()).top;
301+
// Check display cutout height
302+
int height = this.getRotation() == 0 ?
303+
this.displayCutoutHeight : 0;
304+
// Check consumed insets
305+
if (WindowInsetsCompat.CONSUMED.isConsumed()) {
306+
Insets insets = WindowInsetsCompat.CONSUMED.getInsets(
307+
WindowInsetsCompat.Type.statusBars());
308+
if (insets.top == 0) {
309+
height = Math.max(height, insets.bottom);
310+
}
311+
}
300312
// Check system resources
301313
int id = Resources.getSystem().getIdentifier(
302314
"status_bar_height", "dimen", "android");
@@ -307,8 +319,16 @@ public int getStatusBarHeight() {
307319
@Dimension @Px
308320
@SuppressLint("InternalInsetResource")
309321
public int getNavigationBarHeight() {
310-
int height = WindowInsetsCompat.CONSUMED.getInsets(
311-
WindowInsetsCompat.Type.navigationBars()).bottom;
322+
int height = 0;
323+
// Check consumed insets
324+
if (WindowInsetsCompat.CONSUMED.isConsumed()) {
325+
Insets insets = WindowInsetsCompat.CONSUMED.getInsets(
326+
WindowInsetsCompat.Type.statusBars());
327+
if (insets.top != 0) {
328+
height = Math.max(height,
329+
insets.bottom - insets.top);
330+
}
331+
}
312332
// Check system resources
313333
int id = Resources.getSystem().getIdentifier(
314334
"config_showNavigationBar", "bool", "android");
@@ -408,19 +428,10 @@ protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean fi
408428
super.overridePendingTransition(
409429
android.R.anim.fade_in, android.R.anim.fade_out);
410430
} else {
411-
this.compatConfigHelper.checkResourcesOverrides(theme,
412-
this.forceEnglish, this.nightModeOverride);
413431
super.onApplyThemeResource(theme, resid, first);
414432
}
415433
}
416434

417-
@Override
418-
public void onConfigurationChanged(@NonNull Configuration newConfig) {
419-
this.compatConfigHelper.checkResourcesOverrides(newConfig,
420-
this.forceEnglish, this.nightModeOverride);
421-
super.onConfigurationChanged(newConfig);
422-
}
423-
424435
public void setOnBackPressedCallback(OnBackPressedCallback onBackPressedCallback) {
425436
this.onBackPressedCallback = onBackPressedCallback;
426437
}
@@ -487,31 +498,6 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
487498
}
488499
}
489500

490-
public void setForceEnglish(boolean forceEnglish) {
491-
if (this.forceEnglish == forceEnglish) return;
492-
this.forceEnglish = forceEnglish;
493-
this.checkResourcesOverrides(forceEnglish, this.nightModeOverride);
494-
}
495-
496-
public void setNightModeOverride(Boolean nightModeOverride) {
497-
if (this.nightModeOverride == nightModeOverride) return;
498-
this.nightModeOverride = nightModeOverride;
499-
this.checkResourcesOverrides(this.forceEnglish, nightModeOverride);
500-
}
501-
502-
void propagateResourcesOverride(boolean forceEnglish, Boolean nightModeOverride) {
503-
if (this.forceEnglish == forceEnglish &&
504-
this.nightModeOverride == nightModeOverride) return;
505-
this.forceEnglish = forceEnglish;
506-
this.nightModeOverride = nightModeOverride;
507-
this.checkResourcesOverrides(forceEnglish, nightModeOverride);
508-
}
509-
510-
private void checkResourcesOverrides(boolean forceEnglish, Boolean nightModeOverride) {
511-
if (this.isRefreshUi || !this.onCreateCalled) return; // Wait before reload
512-
this.compatConfigHelper.checkResourcesOverrides(forceEnglish, nightModeOverride);
513-
}
514-
515501
public boolean isLightTheme() {
516502
Resources.Theme theme = this.getTheme();
517503
TypedValue typedValue = new TypedValue();
@@ -544,8 +530,34 @@ public final int getColorCompat(@ColorRes @AttrRes int color) {
544530
return ContextCompat.getColor(this, color);
545531
}
546532

547-
public Locale getUserLocale() {
548-
return this.compatConfigHelper.getUserLocale();
533+
/**
534+
* Note: This value can change at runtime on some devices,
535+
* and return true if DisplayCutout is simulated.
536+
* */
537+
public boolean hasNotch() {
538+
if (!this.onCreateCalledOnce) {
539+
Log.w(TAG, "hasNotch() called before onCreate()");
540+
return CompatNotch.getNotchHeight(this) != 0;
541+
}
542+
return this.displayCutoutHeight != 0;
543+
}
544+
545+
@SuppressWarnings("deprecation")
546+
@Nullable @Override
547+
public Display getDisplay() {
548+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
549+
return super.getDisplay();
550+
}
551+
return this.getWindowManager().getDefaultDisplay();
552+
}
553+
554+
@Rotation
555+
public int getRotation() {
556+
Display display = this.getDisplay();
557+
return display != null ? display.getRotation() :
558+
this.getResources().getConfiguration().orientation ==
559+
Configuration.ORIENTATION_LANDSCAPE ?
560+
Surface.ROTATION_90 : Surface.ROTATION_0;
549561
}
550562

551563
public static CompatActivity getCompatActivity(View view) {
@@ -584,4 +596,13 @@ public interface ApplicationCallbacks {
584596

585597
void onRefreshUI(CompatActivity compatActivity);
586598
}
599+
600+
@IntDef(open = true, value = {
601+
Surface.ROTATION_0,
602+
Surface.ROTATION_90,
603+
Surface.ROTATION_180,
604+
Surface.ROTATION_270
605+
})
606+
@Retention(RetentionPolicy.SOURCE)
607+
private @interface Rotation {}
587608
}

0 commit comments

Comments
 (0)