Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Catalog] add cutout mode preference #4577

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion catalog/java/io/material/catalog/feature/DemoActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions catalog/java/io/material/catalog/main/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class CatalogPreferences extends BaseCatalogPreferences {
new ImmutableList.Builder<CatalogPreference>()
.addAll(COMMON_PREFERENCES)
.add(new DynamicColorPreference())
.add(new CutoutModePreference())
.build();

@Override
Expand Down
102 changes: 102 additions & 0 deletions catalog/java/io/material/catalog/preferences/CutoutModePreference.java
Original file line number Diff line number Diff line change
@@ -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<Option> OPTIONS;
static {
List<Option> list = new ArrayList<>();
Collections.addAll(list,
DEFAULT_OPTION,
new Option(OPTION_ID_SHORT_EDGES, 0, R.string.cutout_mode_preference_option_short_edges),
new Option(OPTION_ID_NEVER, 0, R.string.cutout_mode_preference_option_never));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
list.add(new Option(OPTION_ID_ALWAYS, 0, R.string.cutout_mode_preference_option_always));
}
OPTIONS = new ImmutableList.Builder<Option>()
.addAll(list)
.build();
}

public CutoutModePreference() {
super(R.string.cutout_mode_preference_description);
}

@Override
protected boolean isEnabled() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.P;
}

@Override
@NonNull
protected ImmutableList<Option> getOptions() {
return OPTIONS;
}

@Override
@NonNull
protected Option getDefaultOption() {
return DEFAULT_OPTION;
}

@Override
protected void apply(@NonNull Context context, @NonNull Option selectedOption) {
new WindowPreferencesManager(context).setCutoutMode(
OPTION_ID_TO_CUTOUT_MODE.get(selectedOption.id));
}

@Override
protected boolean shouldRecreateActivityOnOptionChanged() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@
<string name="edge_to_edge_preference_description" description="Title of edge to edge mode toggle [CHAR LIMIT=NONE]">Edge To Edge</string>
<string name="edge_to_edge_preference_option_on" description="Option to enable edge to edge mode [CHAR LIMIT=NONE]">On</string>
<string name="edge_to_edge_preference_option_off" description="Option to disable edge to edge mode [CHAR LIMIT=NONE]">Off</string>
<string name="cutout_mode_preference_description" translatable="false">Cutout Mode</string>
<string name="cutout_mode_preference_option_default" translatable="false">Default</string>
<string name="cutout_mode_preference_option_short_edges" translatable="false">Short edges</string>
<string name="cutout_mode_preference_option_never" translatable="false">Never</string>
<string name="cutout_mode_preference_option_always" translatable="false">Always</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.material.catalog.windowpreferences;

import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build.VERSION;
Expand All @@ -31,6 +33,7 @@ public class WindowPreferencesManager {

private static final String PREFERENCES_NAME = "window_preferences";
private static final String KEY_EDGE_TO_EDGE_ENABLED = "edge_to_edge_enabled";
private static final String KEY_CUTOUT_MODE = "cutout_mode";

private final Context context;
private final OnApplyWindowInsetsListener listener;
Expand Down Expand Up @@ -71,6 +74,20 @@ public void applyEdgeToEdgePreference(Window window) {
window.getDecorView(), isEdgeToEdgeEnabled() ? listener : null);
}

public void setCutoutMode(int cutoutMode) {
getSharedPreferences()
.edit()
.putInt(KEY_CUTOUT_MODE, cutoutMode)
.commit();
}

public void applyCutoutModePreference(Window window) {
if (VERSION.SDK_INT >= VERSION_CODES.P) {
window.getAttributes().layoutInDisplayCutoutMode =
getSharedPreferences().getInt(KEY_CUTOUT_MODE, LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT);
}
}

private SharedPreferences getSharedPreferences() {
return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}
Expand Down