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.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      */
safeForeach(List<T> list, Consumer<T> c)45     public static <T> void safeForeach(List<T> list, Consumer<T> c) {
46         for (int i = list.size() - 1; i >= 0; i--) {
47             T item = list.get(i);
48             if (item != null) {
49                 c.accept(item);
50             }
51         }
52     }
53 
54     /**
55      * Returns {@code true} iff the package {@code packageName} is a headless remote display
56      * provider, i.e, that the package holds the privileged {@code REMOTE_DISPLAY_PROVIDER}
57      * permission and that it doesn't host a launcher icon.
58      */
isHeadlessRemoteDisplayProvider(PackageManager pm, String packageName)59     public static boolean isHeadlessRemoteDisplayProvider(PackageManager pm, String packageName) {
60         if (pm.checkPermission(Manifest.permission.REMOTE_DISPLAY_PROVIDER, packageName)
61                 != PackageManager.PERMISSION_GRANTED) {
62             return false;
63         }
64 
65         Intent homeIntent = new Intent(Intent.ACTION_MAIN);
66         homeIntent.addCategory(Intent.CATEGORY_LAUNCHER);
67         homeIntent.setPackage(packageName);
68 
69         return pm.queryIntentActivities(homeIntent, 0).isEmpty();
70     }
71 
72     /**
73      * Returns {@code true} if the navMode is that of
74      * {@link android.view.WindowManagerPolicyConstants#NAV_BAR_MODE_GESTURAL} AND
75      * the context is that of the default display
76      */
isGesturalModeOnDefaultDisplay(Context context, DisplayTracker displayTracker, int navMode)77     public static boolean isGesturalModeOnDefaultDisplay(Context context,
78             DisplayTracker displayTracker, int navMode) {
79         return context.getDisplayId() == displayTracker.getDefaultDisplayId()
80                 && QuickStepContract.isGesturalMode(navMode);
81     }
82 
83     /**
84      * Allow the media player to be shown in the QS area, controlled by 2 flags.
85      * Off by default, but can be disabled by setting to 0
86      */
useQsMediaPlayer(Context context)87     public static boolean useQsMediaPlayer(Context context) {
88         // TODO(b/192412820): Replace SHOW_MEDIA_ON_QUICK_SETTINGS with compile-time value
89         // Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS can't be toggled at runtime, so simply
90         // cache the first result we fetch and use that going forward. Do this to avoid unnecessary
91         // binder calls which may happen on the critical path.
92         if (sUseQsMediaPlayer == null) {
93             int flag = Settings.Global.getInt(context.getContentResolver(),
94                     Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1);
95             sUseQsMediaPlayer = flag > 0;
96         }
97         return sUseQsMediaPlayer;
98     }
99 
100     /**
101      * Allow media resumption controls. Requires {@link #useQsMediaPlayer(Context)} to be enabled.
102      * On by default, but can be disabled by setting to 0
103      */
useMediaResumption(Context context)104     public static boolean useMediaResumption(Context context) {
105         int flag = Settings.Secure.getInt(context.getContentResolver(),
106                 Settings.Secure.MEDIA_CONTROLS_RESUME, 1);
107         return useQsMediaPlayer(context) && flag > 0;
108     }
109 
110     /**
111      * Returns true if the device should use the collapsed layout for the media player when in
112      * landscape (or seascape) orientation
113      */
useCollapsedMediaInLandscape(Resources resources)114     public static boolean useCollapsedMediaInLandscape(Resources resources) {
115         return resources.getBoolean(R.bool.config_quickSettingsMediaLandscapeCollapsed);
116     }
117 
118     /**
119      * Gets the {@link R.dimen#status_bar_header_height_keyguard}.
120      */
getStatusBarHeaderHeightKeyguard(Context context)121     public static int getStatusBarHeaderHeightKeyguard(Context context) {
122         final int statusBarHeight = SystemBarUtils.getStatusBarHeight(context);
123         final DisplayCutout cutout = context.getDisplay().getCutout();
124         final int waterfallInsetTop = cutout == null ? 0 : cutout.getWaterfallInsets().top;
125         final int statusBarHeaderHeightKeyguard = context.getResources()
126                 .getDimensionPixelSize(R.dimen.status_bar_header_height_keyguard);
127         return Math.max(statusBarHeight, statusBarHeaderHeightKeyguard + waterfallInsetTop);
128     }
129 }
130