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 android.Manifest;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.content.pm.PackageManager;
21 import android.content.res.Resources;
22 import android.provider.Settings;
23 import android.view.DisplayCutout;
24 
25 import com.android.internal.policy.SystemBarUtils;
26 import com.android.systemui.res.R;
27 import com.android.systemui.settings.DisplayTracker;
28 import com.android.systemui.shared.system.QuickStepContract;
29 
30 import java.util.List;
31 import java.util.function.Consumer;
32 
33 public class Utils {
34 
35     private static Boolean sUseQsMediaPlayer = null;
36 
37     /**
38      * Allows lambda iteration over a list. It is done in reverse order so it is safe
39      * to add or remove items during the iteration.  Skips over null items.
40      *
41      * @deprecated According to b/286841705, this is *not* safe: If an item is removed from the
42      *   list, then list.get(i) could throw an IndexOutOfBoundsException. This method should not be
43      *   used; try using `synchronized` or making a copy of the list instead.
44      */
45     @Deprecated
safeForeach(List<T> list, Consumer<T> c)46     public static <T> void safeForeach(List<T> list, Consumer<T> c) {
47         for (int i = list.size() - 1; i >= 0; i--) {
48             T item = list.get(i);
49             if (item != null) {
50                 c.accept(item);
51             }
52         }
53     }
54 
55     /**
56      * Returns {@code true} iff the package {@code packageName} is a headless remote display
57      * provider, i.e, that the package holds the privileged {@code REMOTE_DISPLAY_PROVIDER}
58      * permission and that it doesn't host a launcher icon.
59      */
isHeadlessRemoteDisplayProvider(PackageManager pm, String packageName)60     public static boolean isHeadlessRemoteDisplayProvider(PackageManager pm, String packageName) {
61         if (pm.checkPermission(Manifest.permission.REMOTE_DISPLAY_PROVIDER, packageName)
62                 != PackageManager.PERMISSION_GRANTED) {
63             return false;
64         }
65 
66         Intent homeIntent = new Intent(Intent.ACTION_MAIN);
67         homeIntent.addCategory(Intent.CATEGORY_LAUNCHER);
68         homeIntent.setPackage(packageName);
69 
70         return pm.queryIntentActivities(homeIntent, 0).isEmpty();
71     }
72 
73     /**
74      * Returns {@code true} if the navMode is that of
75      * {@link android.view.WindowManagerPolicyConstants#NAV_BAR_MODE_GESTURAL} AND
76      * the context is that of the default display
77      */
isGesturalModeOnDefaultDisplay(Context context, DisplayTracker displayTracker, int navMode)78     public static boolean isGesturalModeOnDefaultDisplay(Context context,
79             DisplayTracker displayTracker, int navMode) {
80         return context.getDisplayId() == displayTracker.getDefaultDisplayId()
81                 && QuickStepContract.isGesturalMode(navMode);
82     }
83 
84     /**
85      * Returns {@code true} if the device is a foldable device
86      */
isDeviceFoldable(Context context)87     public static boolean isDeviceFoldable(Context context) {
88         return context.getResources()
89                 .getIntArray(com.android.internal.R.array.config_foldedDeviceStates).length != 0;
90     }
91 
92     /**
93      * Allow the media player to be shown in the QS area, controlled by 2 flags.
94      * On by default, but can be disabled by setting either flag to 0/false.
95      */
useQsMediaPlayer(Context context)96     public static boolean useQsMediaPlayer(Context context) {
97         // Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS can't be toggled at runtime, so simply
98         // cache the first result we fetch and use that going forward. Do this to avoid unnecessary
99         // binder calls which may happen on the critical path.
100         if (sUseQsMediaPlayer == null) {
101             // TODO(b/192412820): Consolidate SHOW_MEDIA_ON_QUICK_SETTINGS into compile-time value.
102             final int settingsFlag = Settings.Global.getInt(context.getContentResolver(),
103                     Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1);
104             final boolean configFlag = context.getResources()
105                     .getBoolean(com.android.internal.R.bool.config_quickSettingsShowMediaPlayer);
106             sUseQsMediaPlayer = settingsFlag > 0 && configFlag;
107         }
108         return sUseQsMediaPlayer;
109     }
110 
111     /**
112      * Allow media resumption controls. Requires {@link #useQsMediaPlayer(Context)} to be enabled.
113      * On by default, but can be disabled by setting to 0
114      */
useMediaResumption(Context context)115     public static boolean useMediaResumption(Context context) {
116         int flag = Settings.Secure.getInt(context.getContentResolver(),
117                 Settings.Secure.MEDIA_CONTROLS_RESUME, 1);
118         return useQsMediaPlayer(context) && flag > 0;
119     }
120 
121     /**
122      * Returns true if the device should use the collapsed layout for the media player when in
123      * landscape (or seascape) orientation
124      */
useCollapsedMediaInLandscape(Resources resources)125     public static boolean useCollapsedMediaInLandscape(Resources resources) {
126         return resources.getBoolean(R.bool.config_quickSettingsMediaLandscapeCollapsed);
127     }
128 
129     /**
130      * Gets the {@link R.dimen#status_bar_header_height_keyguard}.
131      *
132      * @deprecated Prefer SystemBarUtilsState or SystemBarUtilsProxy
133      */
134     @Deprecated
getStatusBarHeaderHeightKeyguard(Context context)135     public static int getStatusBarHeaderHeightKeyguard(Context context) {
136         final int statusBarHeight = SystemBarUtils.getStatusBarHeight(context);
137         final DisplayCutout cutout = context.getDisplay().getCutout();
138         final int waterfallInsetTop = cutout == null ? 0 : cutout.getWaterfallInsets().top;
139         final int statusBarHeaderHeightKeyguard = context.getResources()
140                 .getDimensionPixelSize(R.dimen.status_bar_header_height_keyguard);
141         return Math.max(statusBarHeight, statusBarHeaderHeightKeyguard + waterfallInsetTop);
142     }
143 }
144