1212import android .os .Build ;
1313import android .os .Bundle ;
1414import android .os .SystemProperties ;
15- import android .util .AttributeSet ;
1615import android .util .Log ;
1716import android .util .TypedValue ;
17+ import android .view .Display ;
1818import android .view .KeyCharacterMap ;
1919import android .view .KeyEvent ;
2020import android .view .Menu ;
2121import android .view .MenuItem ;
22+ import android .view .Surface ;
2223import android .view .View ;
2324import android .view .ViewConfiguration ;
24- import android .widget .ScrollView ;
2525
2626import androidx .annotation .AttrRes ;
2727import androidx .annotation .CallSuper ;
2828import androidx .annotation .ColorInt ;
2929import androidx .annotation .ColorRes ;
3030import androidx .annotation .Dimension ;
3131import androidx .annotation .DrawableRes ;
32+ import androidx .annotation .IntDef ;
3233import androidx .annotation .NonNull ;
3334import androidx .annotation .Nullable ;
3435import androidx .annotation .Px ;
3738import androidx .appcompat .app .AppCompatActivity ;
3839import androidx .core .content .ContextCompat ;
3940import androidx .core .graphics .ColorUtils ;
41+ import androidx .core .graphics .Insets ;
4042import androidx .core .view .WindowInsetsCompat ;
4143import androidx .core .widget .NestedScrollView ;
4244import androidx .fragment .app .Fragment ;
4345import androidx .recyclerview .widget .RecyclerView ;
4446
4547import com .fox2code .mmm .Constants ;
4648import com .fox2code .mmm .R ;
47- import com .kieronquinn .monetcompat .app .MonetCompatActivity ;
4849import com .kieronquinn .monetcompat .extensions .views .ViewExtensions_RecyclerViewKt ;
4950import com .kieronquinn .monetcompat .extensions .views .ViewExtensions_ScrollViewKt ;
5051
52+ import java .lang .annotation .Retention ;
53+ import java .lang .annotation .RetentionPolicy ;
5154import java .lang .ref .WeakReference ;
52- import java .util .Locale ;
5355import java .util .Objects ;
5456
5557import 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