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