blob: b1792d003290dacc56f21bfa638b2e1c177a6a0a [file] [log] [blame]
Jason Monkf66d3772017-05-30 14:31:51 -04001/*
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
15package com.android.systemui.util;
16
Vinit Nayak3f35db52019-08-08 17:31:48 -070017import static android.view.Display.DEFAULT_DISPLAY;
18
Narayan Kamath9f72a1e2019-05-17 15:58:24 +010019import android.Manifest;
Vinit Nayak3f35db52019-08-08 17:31:48 -070020import android.content.Context;
Narayan Kamath9f72a1e2019-05-17 15:58:24 +010021import android.content.Intent;
22import android.content.pm.PackageManager;
Matt Pietal5a19cb62019-10-30 12:31:07 -040023import android.provider.Settings;
Charles He6a79b0d2017-09-18 09:50:58 +010024import android.view.View;
25
Vinit Nayak3f35db52019-08-08 17:31:48 -070026import com.android.systemui.shared.system.QuickStepContract;
Charles He6a79b0d2017-09-18 09:50:58 +010027import com.android.systemui.statusbar.CommandQueue;
28
Jason Monkf66d3772017-05-30 14:31:51 -040029import java.util.List;
30import java.util.function.Consumer;
31
32public class Utils {
33
34 /**
35 * Allows lambda iteration over a list. It is done in reverse order so it is safe
Beverlydb2cef22018-06-15 11:04:02 -040036 * to add or remove items during the iteration. Skips over null items.
Jason Monkf66d3772017-05-30 14:31:51 -040037 */
38 public static <T> void safeForeach(List<T> list, Consumer<T> c) {
39 for (int i = list.size() - 1; i >= 0; i--) {
Beverlydb2cef22018-06-15 11:04:02 -040040 T item = list.get(i);
41 if (item != null) {
42 c.accept(item);
43 }
Jason Monkf66d3772017-05-30 14:31:51 -040044 }
45 }
Charles He6a79b0d2017-09-18 09:50:58 +010046
47 /**
48 * Sets the visibility of an UI element according to the DISABLE_* flags in
49 * {@link android.app.StatusBarManager}.
50 */
51 public static class DisableStateTracker implements CommandQueue.Callbacks,
52 View.OnAttachStateChangeListener {
53 private final int mMask1;
54 private final int mMask2;
Dave Mankoffbcaca8a2019-10-31 18:04:08 -040055 private final CommandQueue mCommandQueue;
Charles He6a79b0d2017-09-18 09:50:58 +010056 private View mView;
57 private boolean mDisabled;
58
Dave Mankoffbcaca8a2019-10-31 18:04:08 -040059 public DisableStateTracker(int disableMask, int disable2Mask, CommandQueue commandQueue) {
Charles He6a79b0d2017-09-18 09:50:58 +010060 mMask1 = disableMask;
61 mMask2 = disable2Mask;
Dave Mankoffbcaca8a2019-10-31 18:04:08 -040062 mCommandQueue = commandQueue;
Charles He6a79b0d2017-09-18 09:50:58 +010063 }
64
65 @Override
66 public void onViewAttachedToWindow(View v) {
67 mView = v;
Dave Mankoffbcaca8a2019-10-31 18:04:08 -040068 mCommandQueue.addCallback(this);
Charles He6a79b0d2017-09-18 09:50:58 +010069 }
70
71 @Override
72 public void onViewDetachedFromWindow(View v) {
Dave Mankoffbcaca8a2019-10-31 18:04:08 -040073 mCommandQueue.removeCallback(this);
Charles He6a79b0d2017-09-18 09:50:58 +010074 mView = null;
75 }
76
77 /**
78 * Sets visibility of this {@link View} given the states passed from
Charles Chenf3d295c2018-11-30 18:15:21 +080079 * {@link com.android.systemui.statusbar.CommandQueue.Callbacks#disable(int, int, int)}.
Charles He6a79b0d2017-09-18 09:50:58 +010080 */
81 @Override
Charles Chenf3d295c2018-11-30 18:15:21 +080082 public void disable(int displayId, int state1, int state2, boolean animate) {
83 if (displayId != mView.getDisplay().getDisplayId()) {
84 return;
85 }
Charles He6a79b0d2017-09-18 09:50:58 +010086 final boolean disabled = ((state1 & mMask1) != 0) || ((state2 & mMask2) != 0);
87 if (disabled == mDisabled) return;
88 mDisabled = disabled;
89 mView.setVisibility(disabled ? View.GONE : View.VISIBLE);
90 }
91
92 /** @return {@code true} if and only if this {@link View} is currently disabled */
93 public boolean isDisabled() {
94 return mDisabled;
95 }
96 }
Narayan Kamath9f72a1e2019-05-17 15:58:24 +010097
98
99 /**
100 * Returns {@code true} iff the package {@code packageName} is a headless remote display
101 * provider, i.e, that the package holds the privileged {@code REMOTE_DISPLAY_PROVIDER}
102 * permission and that it doesn't host a launcher icon.
103 */
104 public static boolean isHeadlessRemoteDisplayProvider(PackageManager pm, String packageName) {
105 if (pm.checkPermission(Manifest.permission.REMOTE_DISPLAY_PROVIDER, packageName)
106 != PackageManager.PERMISSION_GRANTED) {
107 return false;
108 }
109
110 Intent homeIntent = new Intent(Intent.ACTION_MAIN);
111 homeIntent.addCategory(Intent.CATEGORY_LAUNCHER);
112 homeIntent.setPackage(packageName);
113
114 return pm.queryIntentActivities(homeIntent, 0).isEmpty();
115 }
116
Vinit Nayak3f35db52019-08-08 17:31:48 -0700117 /**
118 * Returns {@code true} if the navMode is that of
119 * {@link android.view.WindowManagerPolicyConstants#NAV_BAR_MODE_GESTURAL} AND
120 * the context is that of the default display
121 */
122 public static boolean isGesturalModeOnDefaultDisplay(Context context, int navMode) {
123 return context.getDisplayId() == DEFAULT_DISPLAY
124 && QuickStepContract.isGesturalMode(navMode);
125 }
126
Matt Pietal5a19cb62019-10-30 12:31:07 -0400127 /**
128 * Allow the media player to be shown in the QS area, controlled by 2 flags.
Lucas Dupinfbe9117ed2020-04-29 16:26:21 -0700129 * Off by default, but can be disabled by setting to 0
Matt Pietal5a19cb62019-10-30 12:31:07 -0400130 */
131 public static boolean useQsMediaPlayer(Context context) {
Lucas Dupinfbe9117ed2020-04-29 16:26:21 -0700132 int flag = Settings.Global.getInt(context.getContentResolver(),
Lucas Dupin03859d72020-05-12 12:27:47 -0700133 Settings.Global.SHOW_MEDIA_ON_QUICK_SETTINGS, 1);
Matt Pietal5a19cb62019-10-30 12:31:07 -0400134 return flag > 0;
135 }
Jason Monkf66d3772017-05-30 14:31:51 -0400136}