• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui.util;
16 
17 import static android.hardware.devicestate.DeviceState.PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_INNER_PRIMARY;
18 import static android.hardware.devicestate.DeviceState.PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_OUTER_PRIMARY;
19 
20 import android.Manifest;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.content.res.Resources;
25 import android.hardware.devicestate.DeviceState;
26 import android.hardware.devicestate.DeviceStateManager;
27 import android.hardware.devicestate.feature.flags.Flags;
28 import android.provider.Settings;
29 import android.view.DisplayCutout;
30 
31 import com.android.internal.policy.SystemBarUtils;
32 import com.android.systemui.res.R;
33 import com.android.systemui.settings.DisplayTracker;
34 import com.android.systemui.shared.system.QuickStepContract;
35 
36 import java.util.List;
37 import java.util.function.Consumer;
38 
39 public class Utils {
40 
41     private static Boolean sUseQsMediaPlayer = null;
42 
43     /**
44      * Allows lambda iteration over a list. It is done in reverse order so it is safe
45      * to add or remove items during the iteration.  Skips over null items.
46      *
47      * @deprecated According to b/286841705, this is *not* safe: If an item is removed from the
48      *   list, then list.get(i) could throw an IndexOutOfBoundsException. This method should not be
49      *   used; try using `synchronized` or making a copy of the list instead.
50      */
51     @Deprecated
safeForeach(List<T> list, Consumer<T> c)52     public static <T> void safeForeach(List<T> list, Consumer<T> c) {
53         for (int i = list.size() - 1; i >= 0; i--) {
54             T item = list.get(i);
55             if (item != null) {
56                 c.accept(item);
57             }
58         }
59     }
60 
61     /**
62      * Returns {@code true} iff the package {@code packageName} is a headless remote display
63      * provider, i.e, that the package holds the privileged {@code REMOTE_DISPLAY_PROVIDER}
64      * permission and that it doesn't host a launcher icon.
65      */
isHeadlessRemoteDisplayProvider(PackageManager pm, String packageName)66     public static boolean isHeadlessRemoteDisplayProvider(PackageManager pm, String packageName) {
67         if (pm.checkPermission(Manifest.permission.REMOTE_DISPLAY_PROVIDER, packageName)
68                 != PackageManager.PERMISSION_GRANTED) {
69             return false;
70         }
71 
72         Intent homeIntent = new Intent(Intent.ACTION_MAIN);
73         homeIntent.addCategory(Intent.CATEGORY_LAUNCHER);
74         homeIntent.setPackage(packageName);
75 
76         return pm.queryIntentActivities(homeIntent, 0).isEmpty();
77     }
78 
79     /**
80      * Returns {@code true} if the navMode is that of
81      * {@link android.view.WindowManagerPolicyConstants#NAV_BAR_MODE_GESTURAL} AND
82      * the context is that of the default display
83      */
isGesturalModeOnDefaultDisplay(Context context, DisplayTracker displayTracker, int navMode)84     public static boolean isGesturalModeOnDefaultDisplay(Context context,
85             DisplayTracker displayTracker, int navMode) {
86         return context.getDisplayId() == displayTracker.getDefaultDisplayId()
87                 && QuickStepContract.isGesturalMode(navMode);
88     }
89 
90     /**
91      * Returns {@code true} if the device is a foldable device
92      */
isDeviceFoldable(Resources resources, DeviceStateManager deviceStateManager)93     public static boolean isDeviceFoldable(Resources resources,
94             DeviceStateManager deviceStateManager) {
95         if (Flags.deviceStatePropertyMigration()) {
96             List<DeviceState> deviceStates = deviceStateManager.getSupportedDeviceStates();
97             for (int i = 0; i < deviceStates.size(); i++) {
98                 DeviceState state = deviceStates.get(i);
99                 if (state.hasProperty(PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_OUTER_PRIMARY)
100                         || state.hasProperty(
101                         PROPERTY_FOLDABLE_DISPLAY_CONFIGURATION_INNER_PRIMARY)) {
102                     return true;
103                 }
104             }
105             return false;
106         } else {
107             return resources.getIntArray(
108                     com.android.internal.R.array.config_foldedDeviceStates).length != 0;
109         }
110     }
111 
112     /**
113      * Allow the media player to be shown in the QS area, controlled by 2 flags.
114      * On by default, but can be disabled by setting either flag to 0/false.
115      */
useQsMediaPlayer(Context context)116     public static boolean useQsMediaPlayer(Context context) {
117         // Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS can't be toggled at runtime, so simply
118         // cache the first result we fetch and use that going forward. Do this to avoid unnecessary
119         // binder calls which may happen on the critical path.
120         if (sUseQsMediaPlayer == null) {
121             // TODO(b/192412820): Consolidate SHOW_MEDIA_ON_QUICK_SETTINGS into compile-time value.
122             final int settingsFlag = Settings.Global.getInt(context.getContentResolver(),
123                     Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1);
124             final boolean configFlag = context.getResources()
125                     .getBoolean(com.android.internal.R.bool.config_quickSettingsShowMediaPlayer);
126             sUseQsMediaPlayer = settingsFlag > 0 && configFlag;
127         }
128         return sUseQsMediaPlayer;
129     }
130 
131     /**
132      * Allow media resumption controls. Requires {@link #useQsMediaPlayer(Context)} to be enabled.
133      * On by default, but can be disabled by setting to 0
134      */
useMediaResumption(Context context)135     public static boolean useMediaResumption(Context context) {
136         int flag = Settings.Secure.getInt(context.getContentResolver(),
137                 Settings.Secure.MEDIA_CONTROLS_RESUME, 1);
138         return useQsMediaPlayer(context) && flag > 0;
139     }
140 
141     /**
142      * Returns true if the device should use the collapsed layout for the media player when in
143      * landscape (or seascape) orientation
144      */
useCollapsedMediaInLandscape(Resources resources)145     public static boolean useCollapsedMediaInLandscape(Resources resources) {
146         return resources.getBoolean(R.bool.config_quickSettingsMediaLandscapeCollapsed);
147     }
148 
149     /**
150      * Gets the {@link R.dimen#status_bar_header_height_keyguard}.
151      *
152      * @deprecated Prefer SystemBarUtilsState or SystemBarUtilsProxy
153      */
154     @Deprecated
getStatusBarHeaderHeightKeyguard(Context context)155     public static int getStatusBarHeaderHeightKeyguard(Context context) {
156         final int statusBarHeight = SystemBarUtils.getStatusBarHeight(context);
157         final DisplayCutout cutout = context.getDisplay().getCutout();
158         final int waterfallInsetTop = cutout == null ? 0 : cutout.getWaterfallInsets().top;
159         final int statusBarHeaderHeightKeyguard = context.getResources()
160                 .getDimensionPixelSize(R.dimen.status_bar_header_height_keyguard);
161         return Math.max(statusBarHeight, statusBarHeaderHeightKeyguard + waterfallInsetTop);
162     }
163 }
164