Skip to content

Commit

Permalink
Make edge-to-edge display mode configurable in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasBielefeld committed Dec 29, 2024
1 parent d60d151 commit c57e5ea
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ public class SharedData {
public static String PREF_STATUS_BAR;
public static String PREF_LANGUAGE;
public static String PREF_CLOSE_AFTER_SEARCH;
public static String PREF_USE_EDGE_TO_EDGE_DISPLAY_MODE;

public static String DEFAULT_SEARCH_URL;
public static String DEFAULT_ORIENTATION;
public static boolean DEFAULT_STATUS_BAR;
public static boolean DEFAULT_CLOSE_AFTER_SEARCH;
public static boolean DEFAULT_EDGE_TO_EDGE_DISPLAY_MODE;
public static int DEFAULT_THEME;
public static int DEFAULT_SEARCH_SELECTED_INDEX;
public static boolean DEFAULT_HIDE_APP_ICON;
Expand Down Expand Up @@ -88,6 +90,7 @@ public static void reinitializeData(Context context){
PREF_CUSTOM_SEARCH_URL = res.getString(R.string.pref_key_custom_search_url);
PREF_HIDE_APP_ICON = res.getString(R.string.pref_key_hide_app_icon);
PREF_CLOSE_AFTER_SEARCH = res.getString(R.string.pref_key_close_after_search);
PREF_USE_EDGE_TO_EDGE_DISPLAY_MODE = res.getString(R.string.pref_key_edge_to_edge_display_mode);

DEFAULT_SEARCH_SELECTED_INDEX = res.getInteger(R.integer.default_search_engine_v2);
DEFAULT_SEARCH_URL = res.getStringArray(R.array.search_engine_uris)[DEFAULT_SEARCH_SELECTED_INDEX];
Expand All @@ -96,6 +99,7 @@ public static void reinitializeData(Context context){
DEFAULT_THEME = res.getInteger(R.integer.default_theme);
DEFAULT_HIDE_APP_ICON = res.getBoolean(R.bool.default_hide_app_icon);
DEFAULT_CLOSE_AFTER_SEARCH = res.getBoolean(R.bool.default_close_after_search);
DEFAULT_EDGE_TO_EDGE_DISPLAY_MODE = res.getBoolean(R.bool.default_edge_to_edge_display_mode);
}
}

Expand Down Expand Up @@ -136,6 +140,10 @@ public static boolean getSavedCloseAfterSearch() {
return getSavedBoolean(PREF_CLOSE_AFTER_SEARCH, DEFAULT_CLOSE_AFTER_SEARCH);
}

public static boolean getSavedEdgeToEdgeDisplayMode() {
return getSavedBoolean(PREF_USE_EDGE_TO_EDGE_DISPLAY_MODE, DEFAULT_EDGE_TO_EDGE_DISPLAY_MODE);
}

public static void logText(String text){
Log.e("hey",text);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

package de.tobiasbielefeld.searchbar.helper;

import static de.tobiasbielefeld.searchbar.SharedData.getSavedEdgeToEdgeDisplayMode;

import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -30,54 +32,56 @@

public class InsetHelper {

private final static int InsetMinSDKVersion = Build.VERSION_CODES.R;

public static void applyEdgelessMode(ComponentActivity activity) {
public static boolean usesEdgelessMode() {
// only use edgeless mode on Android 11 and up, because
// the windowInsets API doesn't return useful values for older versions
if (Build.VERSION.SDK_INT >= InsetMinSDKVersion) {
return getSavedEdgeToEdgeDisplayMode() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R;
}

public static void applyEdgelessMode(ComponentActivity activity) {
if (usesEdgelessMode()) {
EdgeToEdge.enable(activity);
}
}

public static void applyInsetsForActivity(View leftSpacer, View rightSpacer, View topSpacer) {
applyInset(leftSpacer, InsetLocation.LEFT, InsetMode.SET);
applyInset(rightSpacer, InsetLocation.RIGHT, InsetMode.SET);
applyInset(topSpacer, InsetLocation.TOP, InsetMode.SET);
if (usesEdgelessMode()) {
applyInset(leftSpacer, InsetLocation.LEFT, InsetMode.SET);
applyInset(rightSpacer, InsetLocation.RIGHT, InsetMode.SET);
applyInset(topSpacer, InsetLocation.TOP, InsetMode.SET);
}
}

public static void applyInset(View view, InsetLocation location, InsetMode mode) {
if (Build.VERSION.SDK_INT < InsetMinSDKVersion) {
return;
}

ViewCompat.setOnApplyWindowInsetsListener(view, (v, windowInsets) -> {
Insets insets = windowInsets.getInsets(
WindowInsetsCompat.Type.navigationBars() |
WindowInsetsCompat.Type.systemBars() |
WindowInsetsCompat.Type.displayCutout() |
WindowInsetsCompat.Type.ime()
);
if (usesEdgelessMode()) {
ViewCompat.setOnApplyWindowInsetsListener(view, (v, windowInsets) -> {
Insets insets = windowInsets.getInsets(
WindowInsetsCompat.Type.navigationBars() |
WindowInsetsCompat.Type.systemBars() |
WindowInsetsCompat.Type.displayCutout() |
WindowInsetsCompat.Type.ime()
);

if (mode == InsetMode.PADDING) {
switch (location) {
case LEFT -> v.setPadding(insets.left, 0, 0, 0);
case RIGHT -> v.setPadding(0, 0, insets.right, 0);
case TOP -> v.setPadding(0, insets.top, 0, 0);
case BOTTOM -> v.setPadding(0, 0, 0, insets.bottom);
if (mode == InsetMode.PADDING) {
switch (location) {
case LEFT -> v.setPadding(insets.left, 0, 0, 0);
case RIGHT -> v.setPadding(0, 0, insets.right, 0);
case TOP -> v.setPadding(0, insets.top, 0, 0);
case BOTTOM -> v.setPadding(0, 0, 0, insets.bottom);
}
} else if (mode == InsetMode.SET) {
ViewGroup.LayoutParams params = v.getLayoutParams();
switch (location) {
case LEFT -> params.width = insets.left;
case RIGHT -> params.width = insets.right;
case TOP -> params.height = insets.top;
case BOTTOM -> params.height = insets.bottom;
}
v.setLayoutParams(params);
}
} else if (mode == InsetMode.SET) {
ViewGroup.LayoutParams params = v.getLayoutParams();
switch (location) {
case LEFT -> params.width = insets.left;
case RIGHT -> params.width = insets.right;
case TOP -> params.height = insets.top;
case BOTTOM -> params.height = insets.bottom;
}
v.setLayoutParams(params);
}
return WindowInsetsCompat.CONSUMED;
});
return WindowInsetsCompat.CONSUMED;
});
}
}

public enum InsetLocation {LEFT, RIGHT, TOP, BOTTOM}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,16 @@
import static de.tobiasbielefeld.searchbar.SharedData.*;
import static de.tobiasbielefeld.searchbar.helper.InsetHelper.*;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends CustomAppCompatActivity implements TextWatcher, View.OnClickListener, TextView.OnEditorActionListener {

public EditText searchText;
private ImageView clearButton;
private ActivityResultLauncher<Intent> startForResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -70,6 +74,7 @@ protected void onCreate(Bundle savedInstanceState) {

records = new Records(this, findViewById(R.id.record_list_container));

startForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), this::onActivityResult);
applyInsetsForActivity(findViewById(R.id.system_left_spacer), findViewById(R.id.system_right_spacer), findViewById(R.id.system_top_spacer));
applyInset(findViewById(R.id.system_bottom_spacer), InsetLocation.BOTTOM, InsetMode.SET);
}
Expand All @@ -87,7 +92,7 @@ public boolean onOptionsItemSelected(MenuItem item) {

if (id == R.id.item_settings) { //starts the settings activity
Intent intent = new Intent(getApplicationContext(), Settings.class);
startActivity(intent);
startForResult.launch(intent);
} else if (id == R.id.item_about) { //starts the about activity
Intent intentAbout = new Intent(getApplicationContext(), AboutActivity.class);
startActivity(intentAbout);
Expand Down Expand Up @@ -130,6 +135,15 @@ public void onResume() {
focusSearchBar();
}

private void onActivityResult(ActivityResult result) {
if (result.getResultCode() == RESULT_OK) {
Intent data = result.getData();
if (data != null && data.getBooleanExtra("RELOAD_EDGE_TO_EDGE", false)) {
recreate();
}
}
}

/**
* Starts the search with the text in searchText
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static de.tobiasbielefeld.searchbar.SharedData.showOrHideStatusBar;

import android.content.Intent;
import android.os.Bundle;

import androidx.preference.ListPreference;
Expand All @@ -25,6 +26,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
PreferenceLanguage language = findPreference(getString(R.string.pref_key_language));
PreferenceSearchEngines searchEngines = findPreference(getString(R.string.pref_key_search_engine));
Preference fullscreen = findPreference(getString(R.string.pref_key_hide_status_bar));
Preference useEdgeToEdgeDisplayMode = findPreference(getString(R.string.pref_key_edge_to_edge_display_mode));

bindDialog(searchEngines, DialogSearchEngines.class);
bindDialog(language, DialogLanguage.class);
Expand All @@ -41,19 +43,20 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

assert theme != null;
theme.setOnPreferenceChangeListener((Preference pref, Object newValue) -> {
if (getActivity() != null) {
getActivity().recreate();
}

requireActivity().recreate();
return true;
});

assert fullscreen != null;
fullscreen.setOnPreferenceChangeListener((Preference pref, Object newValue) -> {
if (getActivity() != null) {
showOrHideStatusBar(requireActivity().getWindow(), (boolean) newValue);
}
showOrHideStatusBar(requireActivity().getWindow(), (boolean) newValue);
return true;
});

assert useEdgeToEdgeDisplayMode != null;
useEdgeToEdgeDisplayMode.setOnPreferenceChangeListener((Preference pref, Object newValue) -> {
Intent resultIntent = ((Settings) requireActivity()).resultIntent;
resultIntent.putExtra("RELOAD_EDGE_TO_EDGE", true);
return true;
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static de.tobiasbielefeld.searchbar.helper.InsetHelper.*;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

Expand All @@ -34,6 +35,7 @@
public class Settings extends CustomAppCompatActivity {

public static String FRAGMENT_TAG = "SETTINGS_FRAGMENT_TAG";
public Intent resultIntent = new Intent();

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -59,8 +61,15 @@ protected void onCreate(Bundle savedInstanceState) {
}

@Override
public boolean onOptionsItemSelected(MenuItem item) { //only menu item is the back mButton in the action bar
finish(); //so finish this activity
public boolean onOptionsItemSelected(MenuItem item) {
//only menu item is the back button in the action bar so finish this activity
finish();
return true;
}

@Override
public void finish() {
setResult(RESULT_OK, resultIntent);
super.finish();
}
}
6 changes: 4 additions & 2 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@
<string name="settings_change_theme_dark">"Dunkel"</string>
<string name="settings_change_theme_light">"Hell"</string>
<string name="settings_change_theme_default">"System Einstellungen folgen"</string>
<string name="settings_close_after_search_title">App schließen, nachdem die Suche gestartet wurde</string>
<string name="settings_close_after_search_text">Die App wird nicht erneut geöffnet, wenn im Browser auf „Zurück“ geklickt wird.</string>
<string name="settings_close_after_search_title">App schließen, nachdem die Suche gestartet wurde</string>
<string name="settings_close_after_search_text">Die App wird nicht erneut geöffnet, wenn im Browser auf „Zurück“ geklickt wird.</string>
<string name="settings_use_edge_to_edge_display_mode">"Verwende den \"Edge-to-Edge\"-Anzeige-Modus"</string>

<!-- About -->
<string name="activity_about">"Über"</string>
<string name="about_description">"Zeige Informationen über die Anwendung wie Lizenzen und Änderungen"</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-es-rAR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@

<string name="settings_close_after_search_text">La aplicación no se abrirá nuevamente cuando presiones Atrás desde tu navegador</string>

<string name="settings_use_edge_to_edge_display_mode">"Usar el modo de pantalla \"Edge-to-Edge\""</string>

<!--About-->

<string name="activity_about">Acerca de</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@

<string name="settings_close_after_search_text">La aplicación no se abrirá nuevamente cuando presiones Atrás desde tu navegador</string>

<string name="settings_use_edge_to_edge_display_mode">"Usar el modo de pantalla \"Edge-to-Edge\""</string>

<!--About-->

<string name="activity_about">Acerca de</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<string name="settings_change_theme_default">Suivre les paramètres du système</string>
<string name="settings_close_after_search_title">Fermer l\'application après avoir lancé la recherche</string>
<string name="settings_close_after_search_text">L\'application ne s\'ouvrira pas lorsque vous reviendrez de votre navigateur</string>
<string name="settings_use_edge_to_edge_display_mode">"Utiliser le mode d'affichage \"Edge-to-Edge\""</string>

<!-- languages -->
<string name="settings_language_default">"Langue par défaut"</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@

<string name="settings_close_after_search_text">L\'app non si aprirà più quando premi Indietro dal browser</string>

<string name="settings_use_edge_to_edge_display_mode">"Usa la modalità di visualizzazione \"Edge-to-Edge\""</string>

<!-- languages -->

<string name="settings_language_default">Lingua di default</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@

<string name="settings_close_after_search_text">ブラウザから戻るを押してもアプリは再び開きません</string>

<string name="settings_use_edge_to_edge_display_mode">"「Edge-to-Edge」表示モードを使用する"</string>

<!-- languages -->

<string name="settings_language_default">既定の言語</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-tr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@

<string name="settings_close_after_search_text">Tarayıcınızdan geri tuşuna bastığınızda uygulama tekrar açılmıyor</string>

<string name="settings_use_edge_to_edge_display_mode">"Edge-to-Edge ekran modunu kullan"</string>

<!-- languages -->

<string name="settings_language_default">Varsayılan dil</string>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@

<string name="settings_close_after_search_text">当您从浏览器中按“返回”时,应用程序将不会再次打开</string>

<string name="settings_use_edge_to_edge_display_mode">"使用\"Edge-to-Edge\"显示模式"</string>

<!-- languages -->

<string name="settings_language_default">默认语言</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/bool.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
<bool name="default_status_bar">false</bool>
<bool name="default_hide_app_icon">false</bool>
<bool name="default_close_after_search">false</bool>
<bool name="default_edge_to_edge_display_mode">true</bool>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values/strings-changelog.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="changelog_11_1" translatable="false">Made app compatible with Android 15 and updated support libraries</string>
<string name="changelog_11_2" translatable="false">The app now uses \"edge-to-edge\" display mode on Android 11 and up</string>
<string name="changelog_11_2" translatable="false">The app now uses \"edge-to-edge\" display mode on Android 11 and up (can be disabled in settings)</string>

<string name="changelog_10_1" translatable="false">Add small delay to showing the keyboard to fix it not showing at all</string>
<string name="changelog_10_2" translatable="false">Add option to close app after starting the search</string>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings-non-translatable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<string name="pref_key_custom_search_url" translatable="false">pref_key_custom_search_url</string>
<string name="pref_key_hide_app_icon" translatable="false">pref_key_hide_app_icon</string>
<string name="pref_key_close_after_search" translatable="false">pref_key_close_after_search</string>
<string name="pref_key_edge_to_edge_display_mode" translatable="false">pref_key_edge_to_edge_display_mode</string>

<string name="about_app_github_link" translatable="false"><a href="https://github.com/TobiasBielefeld/Simple-Search">GitHub</a></string>
<string name="about_app_license_link" translatable="false"><a href="https://www.gnu.org/licenses/gpl.html">GPL 3.0+</a></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 @@ -43,6 +43,7 @@
<string name="settings_change_theme_dark">"Dark"</string>
<string name="settings_change_theme_light">"Light"</string>
<string name="settings_change_theme_default">"Follow system settings"</string>
<string name="settings_use_edge_to_edge_display_mode">"Use \"edge-to-edge\" display mode"</string>

<string name="settings_close_after_search_title">"Close app after starting search"</string>
<string name="settings_close_after_search_text">"App will not open again when you press back from your browser"</string>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/pref_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@
app:key="@string/pref_key_hide_status_bar"
app:title="@string/settings_hide_status_bar" />

<SwitchPreferenceCompat
app:iconSpaceReserved="false"
app:singleLineTitle="false"
app:defaultValue="@bool/default_edge_to_edge_display_mode"
app:key="@string/pref_key_edge_to_edge_display_mode"
app:title="@string/settings_use_edge_to_edge_display_mode" />

</PreferenceCategory>

<PreferenceCategory
Expand Down
2 changes: 1 addition & 1 deletion metadata/en/changelogs/15.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
* Made app compatible with Android 15 and updated support libraries
* The app now uses "edge-to-edge" display mode on Android 11 and up
* The app now uses "edge-to-edge" display mode on Android 11 and up (can be disabled in settings)

0 comments on commit c57e5ea

Please sign in to comment.