/
WindowPreferencesManager.java
135 lines (116 loc) · 5.03 KB
/
WindowPreferencesManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 2019 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.windowpreferences;
import static android.graphics.Color.TRANSPARENT;
import static android.view.View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
import static android.view.View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
import static android.view.View.SYSTEM_UI_FLAG_VISIBLE;
import static com.google.android.material.color.MaterialColors.isColorLight;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.view.View;
import android.view.Window;
import androidx.annotation.RequiresApi;
import androidx.core.graphics.ColorUtils;
import com.google.android.material.color.MaterialColors;
/** Helper that saves the current window preferences for the Catalog. */
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 int EDGE_TO_EDGE_BAR_ALPHA = 128;
@RequiresApi(VERSION_CODES.LOLLIPOP)
private static final int EDGE_TO_EDGE_FLAGS =
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
private final Context context;
public WindowPreferencesManager(Context context) {
this.context = context;
}
@SuppressWarnings("ApplySharedPref")
public void toggleEdgeToEdgeEnabled() {
getSharedPreferences()
.edit()
.putBoolean(KEY_EDGE_TO_EDGE_ENABLED, !isEdgeToEdgeEnabled())
.commit();
}
public boolean isEdgeToEdgeEnabled() {
return getSharedPreferences()
.getBoolean(KEY_EDGE_TO_EDGE_ENABLED, VERSION.SDK_INT >= VERSION_CODES.Q);
}
@SuppressWarnings("RestrictTo")
public void applyEdgeToEdgePreference(Window window) {
if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) {
return;
}
boolean edgeToEdgeEnabled = isEdgeToEdgeEnabled();
int statusBarColor = getStatusBarColor(isEdgeToEdgeEnabled());
int navbarColor = getNavBarColor(isEdgeToEdgeEnabled());
boolean lightBackground =
isColorLight(MaterialColors.getColor(context, android.R.attr.colorBackground, Color.BLACK));
boolean lightStatusBar = isColorLight(statusBarColor);
boolean showDarkStatusBarIcons =
lightStatusBar || (statusBarColor == TRANSPARENT && lightBackground);
boolean lightNavbar = isColorLight(navbarColor);
boolean showDarkNavbarIcons = lightNavbar || (navbarColor == TRANSPARENT && lightBackground);
View decorView = window.getDecorView();
int currentStatusBar =
showDarkStatusBarIcons && VERSION.SDK_INT >= VERSION_CODES.M
? SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
: 0;
int currentNavBar =
showDarkNavbarIcons && VERSION.SDK_INT >= VERSION_CODES.O
? SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
: 0;
window.setNavigationBarColor(navbarColor);
window.setStatusBarColor(statusBarColor);
int systemUiVisibility = (edgeToEdgeEnabled ? EDGE_TO_EDGE_FLAGS : SYSTEM_UI_FLAG_VISIBLE)
| currentStatusBar
| currentNavBar;
decorView.setSystemUiVisibility(systemUiVisibility);
}
@SuppressWarnings("RestrictTo")
@TargetApi(VERSION_CODES.LOLLIPOP)
private int getStatusBarColor(boolean isEdgeToEdgeEnabled) {
if (isEdgeToEdgeEnabled && VERSION.SDK_INT < VERSION_CODES.M) {
int opaqueStatusBarColor =
MaterialColors.getColor(context, android.R.attr.statusBarColor, Color.BLACK);
return ColorUtils.setAlphaComponent(opaqueStatusBarColor, EDGE_TO_EDGE_BAR_ALPHA);
}
if (isEdgeToEdgeEnabled) {
return TRANSPARENT;
}
return MaterialColors.getColor(context, android.R.attr.statusBarColor, Color.BLACK);
}
@SuppressWarnings("RestrictTo")
@TargetApi(VERSION_CODES.LOLLIPOP)
private int getNavBarColor(boolean isEdgeToEdgeEnabled) {
if (isEdgeToEdgeEnabled && VERSION.SDK_INT < VERSION_CODES.O_MR1) {
int opaqueNavBarColor =
MaterialColors.getColor(context, android.R.attr.navigationBarColor, Color.BLACK);
return ColorUtils.setAlphaComponent(opaqueNavBarColor, EDGE_TO_EDGE_BAR_ALPHA);
}
if (isEdgeToEdgeEnabled) {
return TRANSPARENT;
}
return MaterialColors.getColor(context, android.R.attr.navigationBarColor, Color.BLACK);
}
private SharedPreferences getSharedPreferences() {
return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE);
}
}