fix(android): respect system bar insets
This commit is contained in:
parent
a95ebf71c6
commit
fa7dcb18be
|
|
@ -0,0 +1,42 @@
|
|||
package org.whoneedhelp.mobile;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import android.os.Build;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.test.ext.junit.rules.ActivityScenarioRule;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public final class SystemBarInsetsInstrumentedTest {
|
||||
@Rule
|
||||
public final ActivityScenarioRule<MainActivity> activityRule =
|
||||
new ActivityScenarioRule<>(MainActivity.class);
|
||||
|
||||
@Test
|
||||
public void edgeToEdgeContentUsesSystemBarInsets() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
|
||||
return;
|
||||
}
|
||||
|
||||
activityRule.getScenario().onActivity(activity -> {
|
||||
View contentView = activity.findViewById(R.id.main_content);
|
||||
|
||||
assertNotNull(contentView);
|
||||
assertTrue(
|
||||
"The app content must start below the status bar",
|
||||
contentView.getPaddingTop() > 0
|
||||
);
|
||||
assertTrue(
|
||||
"The app content must end above the navigation bar",
|
||||
contentView.getPaddingBottom() > 0
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -13,6 +13,7 @@ import android.os.Build;
|
|||
import android.os.Bundle;
|
||||
import android.os.CancellationSignal;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.CookieManager;
|
||||
import android.webkit.GeolocationPermissions;
|
||||
|
|
@ -24,6 +25,7 @@ import android.webkit.WebResourceResponse;
|
|||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.activity.ComponentActivity;
|
||||
|
|
@ -39,6 +41,9 @@ import androidx.credentials.GetCredentialResponse;
|
|||
import androidx.credentials.exceptions.ClearCredentialException;
|
||||
import androidx.credentials.exceptions.GetCredentialException;
|
||||
import androidx.credentials.exceptions.NoCredentialException;
|
||||
import androidx.core.graphics.Insets;
|
||||
import androidx.core.view.ViewCompat;
|
||||
import androidx.core.view.WindowInsetsCompat;
|
||||
import androidx.webkit.WebMessageCompat;
|
||||
import androidx.webkit.WebViewCompat;
|
||||
import androidx.webkit.WebViewFeature;
|
||||
|
|
@ -132,7 +137,17 @@ public final class MainActivity extends ComponentActivity {
|
|||
ViewGroup.LayoutParams.MATCH_PARENT
|
||||
)
|
||||
);
|
||||
setContentView(webView);
|
||||
FrameLayout contentView = new FrameLayout(this);
|
||||
contentView.setId(R.id.main_content);
|
||||
contentView.setLayoutParams(
|
||||
new ViewGroup.LayoutParams(
|
||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
||||
ViewGroup.LayoutParams.MATCH_PARENT
|
||||
)
|
||||
);
|
||||
contentView.addView(webView);
|
||||
setContentView(contentView);
|
||||
applySystemBarInsets(contentView);
|
||||
|
||||
WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG);
|
||||
|
||||
|
|
@ -176,6 +191,40 @@ public final class MainActivity extends ComponentActivity {
|
|||
}
|
||||
}
|
||||
|
||||
private void applySystemBarInsets(View contentView) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
|
||||
return;
|
||||
}
|
||||
|
||||
ViewCompat.setOnApplyWindowInsetsListener(contentView, (view, windowInsets) -> {
|
||||
Insets safeInsets = windowInsets.getInsets(
|
||||
WindowInsetsCompat.Type.systemBars()
|
||||
| WindowInsetsCompat.Type.displayCutout()
|
||||
);
|
||||
view.setPadding(
|
||||
safeInsets.left,
|
||||
safeInsets.top,
|
||||
safeInsets.right,
|
||||
safeInsets.bottom
|
||||
);
|
||||
if (PAGE_LOAD_DIAGNOSTICS) {
|
||||
Log.d(
|
||||
LOG_TAG,
|
||||
"Applied system bar insets: left="
|
||||
+ safeInsets.left
|
||||
+ ", top="
|
||||
+ safeInsets.top
|
||||
+ ", right="
|
||||
+ safeInsets.right
|
||||
+ ", bottom="
|
||||
+ safeInsets.bottom
|
||||
);
|
||||
}
|
||||
return windowInsets;
|
||||
});
|
||||
ViewCompat.requestApplyInsets(contentView);
|
||||
}
|
||||
|
||||
private String initialUrl() {
|
||||
return LaunchUrlResolver.initialUrl(
|
||||
trustedOrigin,
|
||||
|
|
|
|||
4
android/app/src/main/res/values/ids.xml
Normal file
4
android/app/src/main/res/values/ids.xml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<item name="main_content" type="id" />
|
||||
</resources>
|
||||
|
|
@ -6,6 +6,6 @@
|
|||
<item name="android:navigationBarColor">@color/brand_green_dark</item>
|
||||
<item name="android:statusBarColor">@color/brand_green</item>
|
||||
<item name="android:windowActionModeOverlay">true</item>
|
||||
<item name="android:windowLightStatusBar">false</item>
|
||||
<item name="android:windowLightStatusBar">true</item>
|
||||
</style>
|
||||
</resources>
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user