From 658fb9b71019dc95857d5943e01f2a2a96d20803 Mon Sep 17 00:00:00 2001 From: manabu-nakamura Date: Tue, 4 Feb 2025 22:42:52 +0900 Subject: [PATCH] [Catalog] add cutout mode preference --- .../catalog/feature/DemoActivity.java | 3 +- .../material/catalog/main/MainActivity.java | 1 + .../preferences/CatalogPreferences.java | 1 + .../preferences/CutoutModePreference.java | 102 ++++++++++++++++++ .../preferences/res/values/strings.xml | 5 + .../WindowPreferencesManager.java | 17 +++ 6 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 catalog/java/io/material/catalog/preferences/CutoutModePreference.java diff --git a/catalog/java/io/material/catalog/feature/DemoActivity.java b/catalog/java/io/material/catalog/feature/DemoActivity.java index 96e900ab6ec..f5fece1b47e 100644 --- a/catalog/java/io/material/catalog/feature/DemoActivity.java +++ b/catalog/java/io/material/catalog/feature/DemoActivity.java @@ -55,10 +55,11 @@ protected void onCreate(@Nullable Bundle bundle) { super.onCreate(bundle); + WindowPreferencesManager windowPreferencesManager = new WindowPreferencesManager(this); if (shouldApplyEdgeToEdgePreference()) { - WindowPreferencesManager windowPreferencesManager = new WindowPreferencesManager(this); windowPreferencesManager.applyEdgeToEdgePreference(getWindow()); } + windowPreferencesManager.applyCutoutModePreference(getWindow()); setContentView(R.layout.cat_demo_activity); diff --git a/catalog/java/io/material/catalog/main/MainActivity.java b/catalog/java/io/material/catalog/main/MainActivity.java index 57e84f1e890..3363040b525 100644 --- a/catalog/java/io/material/catalog/main/MainActivity.java +++ b/catalog/java/io/material/catalog/main/MainActivity.java @@ -51,6 +51,7 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); WindowPreferencesManager windowPreferencesManager = new WindowPreferencesManager(this); windowPreferencesManager.applyEdgeToEdgePreference(getWindow()); + windowPreferencesManager.applyCutoutModePreference(getWindow()); setContentView(R.layout.cat_main_activity); diff --git a/catalog/java/io/material/catalog/preferences/CatalogPreferences.java b/catalog/java/io/material/catalog/preferences/CatalogPreferences.java index 1a8c4ad9205..f0fde7fc930 100644 --- a/catalog/java/io/material/catalog/preferences/CatalogPreferences.java +++ b/catalog/java/io/material/catalog/preferences/CatalogPreferences.java @@ -29,6 +29,7 @@ public class CatalogPreferences extends BaseCatalogPreferences { new ImmutableList.Builder() .addAll(COMMON_PREFERENCES) .add(new DynamicColorPreference()) + .add(new CutoutModePreference()) .build(); @Override diff --git a/catalog/java/io/material/catalog/preferences/CutoutModePreference.java b/catalog/java/io/material/catalog/preferences/CutoutModePreference.java new file mode 100644 index 00000000000..2d1b0f83a36 --- /dev/null +++ b/catalog/java/io/material/catalog/preferences/CutoutModePreference.java @@ -0,0 +1,102 @@ +/* + * Copyright 2025 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.material.catalog.preferences; + +import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS; +import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT; +import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER; +import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; + +import android.content.Context; +import android.os.Build; +import android.util.SparseIntArray; +import androidx.annotation.NonNull; +import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import io.material.catalog.R; +import io.material.catalog.windowpreferences.WindowPreferencesManager; + +/** + * Cutout mode preference to change the cutout mode to default, short edges, never or always. + */ +public class CutoutModePreference extends CatalogPreference { + private static final int OPTION_ID_DEFAULT = 1; + private static final int OPTION_ID_SHORT_EDGES = 2; + private static final int OPTION_ID_NEVER = 3; + private static final int OPTION_ID_ALWAYS = 4; + + private static final SparseIntArray OPTION_ID_TO_CUTOUT_MODE = new SparseIntArray(); + static { + OPTION_ID_TO_CUTOUT_MODE.append(OPTION_ID_DEFAULT, LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT); + OPTION_ID_TO_CUTOUT_MODE.append(OPTION_ID_SHORT_EDGES, + LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES); + OPTION_ID_TO_CUTOUT_MODE.append(OPTION_ID_NEVER, LAYOUT_IN_DISPLAY_CUTOUT_MODE_NEVER); + OPTION_ID_TO_CUTOUT_MODE.append(OPTION_ID_ALWAYS, LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS); + } + + private static final Option DEFAULT_OPTION = + new Option(OPTION_ID_DEFAULT, 0, R.string.cutout_mode_preference_option_default); + + private static final ImmutableList