Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1 | /** |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.server.vr; |
| 17 | |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 18 | import static android.view.Display.INVALID_DISPLAY; |
| 19 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 20 | import android.Manifest; |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 21 | import android.app.ActivityManagerInternal; |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 22 | import android.app.ActivityManagerInternal.ScreenObserver; |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 24 | import android.app.AppOpsManager; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 25 | import android.app.INotificationManager; |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 26 | import android.app.Vr2dDisplayProperties; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 27 | import android.app.NotificationManager; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 28 | import android.annotation.NonNull; |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 30 | import android.content.ComponentName; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 31 | import android.content.ContentResolver; |
| 32 | import android.content.Context; |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 33 | import android.content.Intent; |
| 34 | import android.content.IntentFilter; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 35 | import android.content.pm.ApplicationInfo; |
| 36 | import android.content.pm.PackageManager; |
| 37 | import android.content.pm.PackageManager.NameNotFoundException; |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 38 | import android.hardware.display.DisplayManager; |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 39 | import android.os.Binder; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 40 | import android.os.Handler; |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 41 | import android.os.IBinder; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 42 | import android.os.IInterface; |
| 43 | import android.os.Looper; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 44 | import android.os.Message; |
| 45 | import android.os.RemoteCallbackList; |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 46 | import android.os.RemoteException; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 47 | import android.os.ServiceManager; |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 48 | import android.os.SystemProperties; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 49 | import android.os.UserHandle; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 50 | import android.provider.Settings; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 51 | import android.service.notification.NotificationListenerService; |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 52 | import android.service.vr.IPersistentVrStateCallbacks; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 53 | import android.service.vr.IVrListener; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 54 | import android.service.vr.IVrManager; |
| 55 | import android.service.vr.IVrStateCallbacks; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 56 | import android.service.vr.VrListenerService; |
Ruben Brunk | 1f4f26b | 2016-08-02 14:21:36 -0700 | [diff] [blame] | 57 | import android.text.TextUtils; |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 58 | import android.util.ArrayMap; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 59 | import android.util.ArraySet; |
| 60 | import android.util.Slog; |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 61 | import android.util.SparseArray; |
Fyodor Kupolov | ab781c2 | 2017-12-19 16:44:26 -0800 | [diff] [blame] | 62 | |
| 63 | import com.android.server.FgThread; |
Adrian Roos | e99bc05 | 2017-11-20 17:55:31 +0100 | [diff] [blame] | 64 | import com.android.server.wm.WindowManagerInternal; |
Tarandeep Singh | 89a6c48 | 2017-11-21 14:26:11 -0800 | [diff] [blame] | 65 | import android.view.inputmethod.InputMethodManagerInternal; |
Santos Cordon | c14513d | 2016-12-14 14:52:59 -0800 | [diff] [blame] | 66 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 67 | import com.android.internal.R; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 68 | import com.android.internal.util.DumpUtils; |
Karthik Ravi Shankar | 99493db | 2017-03-08 18:30:19 -0800 | [diff] [blame] | 69 | import com.android.server.LocalServices; |
Craig Donner | 9906259 | 2016-04-14 21:45:19 -0700 | [diff] [blame] | 70 | import com.android.server.SystemConfig; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 71 | import com.android.server.SystemService; |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 72 | import com.android.server.utils.ManagedApplicationService.PendingEvent; |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 73 | import com.android.server.utils.ManagedApplicationService.LogEvent; |
| 74 | import com.android.server.utils.ManagedApplicationService.LogFormattable; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 75 | import com.android.server.vr.EnabledComponentsObserver.EnabledComponentChangeListener; |
| 76 | import com.android.server.utils.ManagedApplicationService; |
| 77 | import com.android.server.utils.ManagedApplicationService.BinderChecker; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 78 | |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 79 | import java.io.FileDescriptor; |
| 80 | import java.io.PrintWriter; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 81 | import java.lang.StringBuilder; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 82 | import java.text.SimpleDateFormat; |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 83 | import java.util.Arrays; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 84 | import java.util.ArrayDeque; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 85 | import java.util.ArrayList; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 86 | import java.util.Collection; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 87 | import java.util.Date; |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 88 | import java.util.List; |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 89 | import java.util.Objects; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 90 | |
| 91 | /** |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 92 | * Service tracking whether VR mode is active, and notifying listening services of state changes. |
| 93 | * <p/> |
| 94 | * Services running in system server may modify the state of VrManagerService via the interface in |
| 95 | * VrManagerInternal, and may register to receive callbacks when the system VR mode changes via the |
| 96 | * interface given in VrStateListener. |
| 97 | * <p/> |
| 98 | * Device vendors may choose to receive VR state changes by implementing the VR mode HAL, e.g.: |
| 99 | * hardware/libhardware/modules/vr |
| 100 | * <p/> |
| 101 | * In general applications may enable or disable VR mode by calling |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 102 | * {@link android.app.Activity#setVrModeEnabled)}. An application may also implement a service to |
| 103 | * be run while in VR mode by implementing {@link android.service.vr.VrListenerService}. |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 104 | * |
Elliot Waite | 54de774 | 2017-01-11 15:30:35 -0800 | [diff] [blame] | 105 | * @see android.service.vr.VrListenerService |
| 106 | * @see com.android.server.vr.VrManagerInternal |
| 107 | * @see com.android.server.vr.VrStateListener |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 108 | * |
| 109 | * @hide |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 110 | */ |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 111 | public class VrManagerService extends SystemService |
| 112 | implements EnabledComponentChangeListener, ScreenObserver { |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 113 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 114 | public static final String TAG = "VrManagerService"; |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 115 | static final boolean DBG = false; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 116 | |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 117 | private static final int PENDING_STATE_DELAY_MS = 300; |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 118 | private static final int EVENT_LOG_SIZE = 64; |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 119 | private static final int INVALID_APPOPS_MODE = -1; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 120 | /** Null set of sleep sleep flags. */ |
| 121 | private static final int FLAG_NONE = 0; |
| 122 | /** Flag set when the device is not sleeping. */ |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 123 | private static final int FLAG_AWAKE = 1 << 0; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 124 | /** Flag set when the screen has been turned on. */ |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 125 | private static final int FLAG_SCREEN_ON = 1 << 1; |
| 126 | /** Flag set when the keyguard is not active. */ |
| 127 | private static final int FLAG_KEYGUARD_UNLOCKED = 1 << 2; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 128 | /** Flag indicating that all system sleep flags have been set.*/ |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 129 | private static final int FLAG_ALL = FLAG_AWAKE | FLAG_SCREEN_ON | FLAG_KEYGUARD_UNLOCKED; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 130 | |
Ruben Brunk | baa4b55 | 2016-02-02 16:27:37 -0800 | [diff] [blame] | 131 | private static native void initializeNative(); |
| 132 | private static native void setVrModeNative(boolean enabled); |
| 133 | |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 134 | private final Object mLock = new Object(); |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 135 | |
| 136 | private final IBinder mOverlayToken = new Binder(); |
| 137 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 138 | // State protected by mLock |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 139 | private boolean mVrModeAllowed; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 140 | private boolean mVrModeEnabled; |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 141 | private boolean mPersistentVrModeEnabled; |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 142 | private boolean mRunning2dInVr; |
| 143 | private int mVrAppProcessId; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 144 | private EnabledComponentsObserver mComponentObserver; |
| 145 | private ManagedApplicationService mCurrentVrService; |
Ruben Brunk | 52ea662 | 2017-10-02 23:51:25 -0700 | [diff] [blame] | 146 | private ManagedApplicationService mCurrentVrCompositorService; |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 147 | private ComponentName mDefaultVrService; |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 148 | private Context mContext; |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 149 | private ComponentName mCurrentVrModeComponent; |
| 150 | private int mCurrentVrModeUser; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 151 | private boolean mWasDefaultGranted; |
| 152 | private boolean mGuard; |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 153 | private final RemoteCallbackList<IVrStateCallbacks> mVrStateRemoteCallbacks = |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 154 | new RemoteCallbackList<>(); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 155 | private final RemoteCallbackList<IPersistentVrStateCallbacks> |
| 156 | mPersistentVrStateRemoteCallbacks = new RemoteCallbackList<>(); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 157 | private int mPreviousCoarseLocationMode = INVALID_APPOPS_MODE; |
| 158 | private int mPreviousManageOverlayMode = INVALID_APPOPS_MODE; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 159 | private VrState mPendingState; |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 160 | private boolean mLogLimitHit; |
| 161 | private final ArrayDeque<LogFormattable> mLoggingDeque = new ArrayDeque<>(EVENT_LOG_SIZE); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 162 | private final NotificationAccessManager mNotifAccessManager = new NotificationAccessManager(); |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 163 | private INotificationManager mNotificationManager; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 164 | /** Tracks the state of the screen and keyguard UI.*/ |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 165 | private int mSystemSleepFlags = FLAG_AWAKE | FLAG_KEYGUARD_UNLOCKED; |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Set when ACTION_USER_UNLOCKED is fired. We shouldn't try to bind to the |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 168 | * vr service before then. This gets set only once the first time the user unlocks the device |
| 169 | * and stays true thereafter. |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 170 | */ |
| 171 | private boolean mUserUnlocked; |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 172 | private Vr2dDisplay mVr2dDisplay; |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 173 | private boolean mBootsToVr; |
Steven Thomas | 1356ec9 | 2017-09-07 11:26:38 -0700 | [diff] [blame] | 174 | private boolean mStandby; |
| 175 | private boolean mUseStandbyToExitVrMode; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 176 | |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 177 | // Handles events from the managed services (e.g. VrListenerService and any bound VR compositor |
| 178 | // service). |
| 179 | private final ManagedApplicationService.EventCallback mEventCallback |
| 180 | = new ManagedApplicationService.EventCallback() { |
| 181 | @Override |
| 182 | public void onServiceEvent(LogEvent event) { |
| 183 | logEvent(event); |
| 184 | |
| 185 | ComponentName component = null; |
| 186 | synchronized (mLock) { |
| 187 | component = ((mCurrentVrService == null) ? null : mCurrentVrService.getComponent()); |
Steven Thomas | 9aedfb1 | 2017-12-06 19:41:07 -0800 | [diff] [blame] | 188 | |
| 189 | // If the VrCore main service was disconnected or the binding died we'll rebind |
| 190 | // automatically. Call focusedActivityChanged() once we rebind. |
| 191 | if (component != null && component.equals(event.component) && |
| 192 | (event.event == LogEvent.EVENT_DISCONNECTED || |
| 193 | event.event == LogEvent.EVENT_BINDING_DIED)) { |
| 194 | callFocusedActivityChangedLocked(); |
| 195 | } |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // If not on an AIO device and we permanently stopped trying to connect to the |
| 199 | // VrListenerService (or don't have one bound), leave persistent VR mode and VR mode. |
| 200 | if (!mBootsToVr && event.event == LogEvent.EVENT_STOPPED_PERMANENTLY && |
| 201 | (component == null || component.equals(event.component))) { |
| 202 | Slog.e(TAG, "VrListenerSevice has died permanently, leaving system VR mode."); |
| 203 | // We're not a native VR device. Leave VR + persistent mode. |
| 204 | setPersistentVrModeEnabled(false); |
| 205 | } |
| 206 | } |
| 207 | }; |
| 208 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 209 | private static final int MSG_VR_STATE_CHANGE = 0; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 210 | private static final int MSG_PENDING_VR_STATE_CHANGE = 1; |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 211 | private static final int MSG_PERSISTENT_VR_MODE_STATE_CHANGE = 2; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 212 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 213 | /** |
| 214 | * Set whether VR mode may be enabled. |
| 215 | * <p/> |
| 216 | * If VR mode is not allowed to be enabled, calls to set VR mode will be cached. When VR mode |
| 217 | * is again allowed to be enabled, the most recent cached state will be applied. |
| 218 | * |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 219 | */ |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 220 | private void updateVrModeAllowedLocked() { |
Steven Thomas | 1356ec9 | 2017-09-07 11:26:38 -0700 | [diff] [blame] | 221 | boolean ignoreSleepFlags = mBootsToVr && mUseStandbyToExitVrMode; |
| 222 | boolean disallowedByStandby = mStandby && mUseStandbyToExitVrMode; |
| 223 | boolean allowed = (mSystemSleepFlags == FLAG_ALL || ignoreSleepFlags) && mUserUnlocked |
| 224 | && !disallowedByStandby; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 225 | if (mVrModeAllowed != allowed) { |
| 226 | mVrModeAllowed = allowed; |
Joe Onorato | 82ba91d | 2017-04-27 16:18:05 -0700 | [diff] [blame] | 227 | if (DBG) Slog.d(TAG, "VR mode is " + ((allowed) ? "allowed" : "disallowed")); |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 228 | if (mVrModeAllowed) { |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 229 | if (mBootsToVr) { |
| 230 | setPersistentVrModeEnabled(true); |
| 231 | } |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 232 | if (mBootsToVr && !mVrModeEnabled) { |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 233 | setVrMode(true, mDefaultVrService, 0, -1, null); |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 234 | } |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 235 | } else { |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 236 | // Disable persistent mode when VR mode isn't allowed, allows an escape hatch to |
| 237 | // exit persistent VR mode when screen is turned off. |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 238 | setPersistentModeAndNotifyListenersLocked(false); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 239 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 240 | // Set pending state to current state. |
| 241 | mPendingState = (mVrModeEnabled && mCurrentVrService != null) |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 242 | ? new VrState(mVrModeEnabled, mRunning2dInVr, mCurrentVrService.getComponent(), |
| 243 | mCurrentVrService.getUserId(), mVrAppProcessId, mCurrentVrModeComponent) |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 244 | : null; |
| 245 | |
| 246 | // Unbind current VR service and do necessary callbacks. |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 247 | updateCurrentVrServiceLocked(false, false, null, 0, -1, null); |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 252 | private void setScreenOn(boolean isScreenOn) { |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 253 | setSystemState(FLAG_SCREEN_ON, isScreenOn); |
| 254 | } |
| 255 | |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 256 | @Override |
| 257 | public void onAwakeStateChanged(boolean isAwake) { |
| 258 | setSystemState(FLAG_AWAKE, isAwake); |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void onKeyguardStateChanged(boolean isShowing) { |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 263 | setSystemState(FLAG_KEYGUARD_UNLOCKED, !isShowing); |
| 264 | } |
| 265 | |
| 266 | private void setSystemState(int flags, boolean isOn) { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 267 | synchronized(mLock) { |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 268 | int oldState = mSystemSleepFlags; |
| 269 | if (isOn) { |
| 270 | mSystemSleepFlags |= flags; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 271 | } else { |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 272 | mSystemSleepFlags &= ~flags; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 273 | } |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 274 | if (oldState != mSystemSleepFlags) { |
| 275 | if (DBG) Slog.d(TAG, "System state: " + getStateAsString()); |
| 276 | updateVrModeAllowedLocked(); |
| 277 | } |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Santos Cordon | b5cbecb | 2017-10-05 11:31:15 -0700 | [diff] [blame] | 281 | private String getStateAsString() { |
| 282 | return new StringBuilder() |
| 283 | .append((mSystemSleepFlags & FLAG_AWAKE) != 0 ? "awake, " : "") |
| 284 | .append((mSystemSleepFlags & FLAG_SCREEN_ON) != 0 ? "screen_on, " : "") |
| 285 | .append((mSystemSleepFlags & FLAG_KEYGUARD_UNLOCKED) != 0 ? "keyguard_off" : "") |
| 286 | .toString(); |
| 287 | } |
| 288 | |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 289 | private void setUserUnlocked() { |
| 290 | synchronized(mLock) { |
| 291 | mUserUnlocked = true; |
| 292 | updateVrModeAllowedLocked(); |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Steven Thomas | 1356ec9 | 2017-09-07 11:26:38 -0700 | [diff] [blame] | 296 | private void setStandbyEnabled(boolean standby) { |
| 297 | synchronized(mLock) { |
| 298 | if (!mBootsToVr) { |
| 299 | Slog.e(TAG, "Attempting to set standby mode on a non-standalone device"); |
| 300 | return; |
| 301 | } |
| 302 | mStandby = standby; |
| 303 | updateVrModeAllowedLocked(); |
| 304 | } |
| 305 | } |
| 306 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 307 | private final Handler mHandler = new Handler() { |
| 308 | @Override |
| 309 | public void handleMessage(Message msg) { |
| 310 | switch(msg.what) { |
| 311 | case MSG_VR_STATE_CHANGE : { |
| 312 | boolean state = (msg.arg1 == 1); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 313 | int i = mVrStateRemoteCallbacks.beginBroadcast(); |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 314 | while (i > 0) { |
| 315 | i--; |
| 316 | try { |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 317 | mVrStateRemoteCallbacks.getBroadcastItem(i).onVrStateChanged(state); |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 318 | } catch (RemoteException e) { |
| 319 | // Noop |
| 320 | } |
| 321 | } |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 322 | mVrStateRemoteCallbacks.finishBroadcast(); |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 323 | } break; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 324 | case MSG_PENDING_VR_STATE_CHANGE : { |
| 325 | synchronized(mLock) { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 326 | if (mVrModeAllowed) { |
| 327 | VrManagerService.this.consumeAndApplyPendingStateLocked(); |
| 328 | } |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 329 | } |
| 330 | } break; |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 331 | case MSG_PERSISTENT_VR_MODE_STATE_CHANGE : { |
| 332 | boolean state = (msg.arg1 == 1); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 333 | int i = mPersistentVrStateRemoteCallbacks.beginBroadcast(); |
| 334 | while (i > 0) { |
| 335 | i--; |
| 336 | try { |
| 337 | mPersistentVrStateRemoteCallbacks.getBroadcastItem(i) |
| 338 | .onPersistentVrStateChanged(state); |
| 339 | } catch (RemoteException e) { |
| 340 | // Noop |
| 341 | } |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 342 | } |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 343 | mPersistentVrStateRemoteCallbacks.finishBroadcast(); |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 344 | } break; |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 345 | default : |
| 346 | throw new IllegalStateException("Unknown message type: " + msg.what); |
| 347 | } |
| 348 | } |
| 349 | }; |
| 350 | |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 351 | // Event used to log when settings are changed for dumpsys logs. |
| 352 | private static class SettingEvent implements LogFormattable { |
| 353 | public final long timestamp; |
| 354 | public final String what; |
| 355 | |
| 356 | SettingEvent(String what) { |
| 357 | this.timestamp = System.currentTimeMillis(); |
| 358 | this.what = what; |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public String toLogString(SimpleDateFormat dateFormat) { |
| 363 | return dateFormat.format(new Date(timestamp)) + " " + what; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | // Event used to track changes of the primary on-screen VR activity. |
| 368 | private static class VrState implements LogFormattable { |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 369 | final boolean enabled; |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 370 | final boolean running2dInVr; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 371 | final int userId; |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 372 | final int processId; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 373 | final ComponentName targetPackageName; |
| 374 | final ComponentName callingPackage; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 375 | final long timestamp; |
| 376 | final boolean defaultPermissionsGranted; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 377 | |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 378 | VrState(boolean enabled, boolean running2dInVr, ComponentName targetPackageName, int userId, |
| 379 | int processId, ComponentName callingPackage) { |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 380 | this.enabled = enabled; |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 381 | this.running2dInVr = running2dInVr; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 382 | this.userId = userId; |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 383 | this.processId = processId; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 384 | this.targetPackageName = targetPackageName; |
| 385 | this.callingPackage = callingPackage; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 386 | this.defaultPermissionsGranted = false; |
| 387 | this.timestamp = System.currentTimeMillis(); |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 388 | } |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 389 | |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 390 | VrState(boolean enabled, boolean running2dInVr, ComponentName targetPackageName, int userId, |
| 391 | int processId, ComponentName callingPackage, boolean defaultPermissionsGranted) { |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 392 | this.enabled = enabled; |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 393 | this.running2dInVr = running2dInVr; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 394 | this.userId = userId; |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 395 | this.processId = processId; |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 396 | this.targetPackageName = targetPackageName; |
| 397 | this.callingPackage = callingPackage; |
| 398 | this.defaultPermissionsGranted = defaultPermissionsGranted; |
| 399 | this.timestamp = System.currentTimeMillis(); |
| 400 | } |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 401 | |
| 402 | @Override |
| 403 | public String toLogString(SimpleDateFormat dateFormat) { |
| 404 | String tab = " "; |
| 405 | String newLine = "\n"; |
| 406 | StringBuilder sb = new StringBuilder(dateFormat.format(new Date(timestamp))); |
| 407 | sb.append(tab); |
| 408 | sb.append("State changed to:"); |
| 409 | sb.append(tab); |
| 410 | sb.append((enabled) ? "ENABLED" : "DISABLED"); |
| 411 | sb.append(newLine); |
| 412 | if (enabled) { |
| 413 | sb.append(tab); |
| 414 | sb.append("User="); |
| 415 | sb.append(userId); |
| 416 | sb.append(newLine); |
| 417 | sb.append(tab); |
| 418 | sb.append("Current VR Activity="); |
| 419 | sb.append((callingPackage == null) ? "None" : callingPackage.flattenToString()); |
| 420 | sb.append(newLine); |
| 421 | sb.append(tab); |
| 422 | sb.append("Bound VrListenerService="); |
| 423 | sb.append((targetPackageName == null) ? "None" |
| 424 | : targetPackageName.flattenToString()); |
| 425 | sb.append(newLine); |
| 426 | if (defaultPermissionsGranted) { |
| 427 | sb.append(tab); |
| 428 | sb.append("Default permissions granted to the bound VrListenerService."); |
| 429 | sb.append(newLine); |
| 430 | } |
| 431 | } |
| 432 | return sb.toString(); |
| 433 | } |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 434 | } |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 435 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 436 | private static final BinderChecker sBinderChecker = new BinderChecker() { |
| 437 | @Override |
| 438 | public IInterface asInterface(IBinder binder) { |
| 439 | return IVrListener.Stub.asInterface(binder); |
| 440 | } |
| 441 | |
| 442 | @Override |
| 443 | public boolean checkType(IInterface service) { |
| 444 | return service instanceof IVrListener; |
| 445 | } |
| 446 | }; |
| 447 | |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 448 | private final class NotificationAccessManager { |
| 449 | private final SparseArray<ArraySet<String>> mAllowedPackages = new SparseArray<>(); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 450 | private final ArrayMap<String, Integer> mNotificationAccessPackageToUserId = |
| 451 | new ArrayMap<>(); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 452 | |
| 453 | public void update(Collection<String> packageNames) { |
| 454 | int currentUserId = ActivityManager.getCurrentUser(); |
| 455 | |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 456 | ArraySet<String> allowed = mAllowedPackages.get(currentUserId); |
| 457 | if (allowed == null) { |
| 458 | allowed = new ArraySet<>(); |
| 459 | } |
| 460 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 461 | // Make sure we revoke notification access for listeners in other users |
| 462 | final int listenerCount = mNotificationAccessPackageToUserId.size(); |
| 463 | for (int i = listenerCount - 1; i >= 0; i--) { |
| 464 | final int grantUserId = mNotificationAccessPackageToUserId.valueAt(i); |
| 465 | if (grantUserId != currentUserId) { |
| 466 | String packageName = mNotificationAccessPackageToUserId.keyAt(i); |
| 467 | revokeNotificationListenerAccess(packageName, grantUserId); |
| 468 | revokeNotificationPolicyAccess(packageName); |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 469 | revokeCoarseLocationPermissionIfNeeded(packageName, grantUserId); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 470 | mNotificationAccessPackageToUserId.removeAt(i); |
| 471 | } |
| 472 | } |
| 473 | |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 474 | for (String pkg : allowed) { |
| 475 | if (!packageNames.contains(pkg)) { |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 476 | revokeNotificationListenerAccess(pkg, currentUserId); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 477 | revokeNotificationPolicyAccess(pkg); |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 478 | revokeCoarseLocationPermissionIfNeeded(pkg, currentUserId); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 479 | mNotificationAccessPackageToUserId.remove(pkg); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | for (String pkg : packageNames) { |
| 483 | if (!allowed.contains(pkg)) { |
| 484 | grantNotificationPolicyAccess(pkg); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 485 | grantNotificationListenerAccess(pkg, currentUserId); |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 486 | grantCoarseLocationPermissionIfNeeded(pkg, currentUserId); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 487 | mNotificationAccessPackageToUserId.put(pkg, currentUserId); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | allowed.clear(); |
| 492 | allowed.addAll(packageNames); |
| 493 | mAllowedPackages.put(currentUserId, allowed); |
| 494 | } |
| 495 | } |
| 496 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 497 | /** |
| 498 | * Called when a user, package, or setting changes that could affect whether or not the |
| 499 | * currently bound VrListenerService is changed. |
| 500 | */ |
| 501 | @Override |
| 502 | public void onEnabledComponentChanged() { |
| 503 | synchronized (mLock) { |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 504 | int currentUser = ActivityManager.getCurrentUser(); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 505 | // Update listeners |
| 506 | ArraySet<ComponentName> enabledListeners = mComponentObserver.getEnabled(currentUser); |
| 507 | |
| 508 | ArraySet<String> enabledPackages = new ArraySet<>(); |
| 509 | for (ComponentName n : enabledListeners) { |
| 510 | String pkg = n.getPackageName(); |
| 511 | if (isDefaultAllowed(pkg)) { |
| 512 | enabledPackages.add(n.getPackageName()); |
| 513 | } |
| 514 | } |
| 515 | mNotifAccessManager.update(enabledPackages); |
| 516 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 517 | if (!mVrModeAllowed) { |
| 518 | return; // Don't do anything, we shouldn't be in VR mode. |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 519 | } |
| 520 | |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 521 | // If there is a pending state change, we'd better deal with that first |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 522 | consumeAndApplyPendingStateLocked(false); |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 523 | |
| 524 | if (mCurrentVrService == null) { |
| 525 | return; // No active services |
| 526 | } |
| 527 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 528 | // There is an active service, update it if needed |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 529 | updateCurrentVrServiceLocked(mVrModeEnabled, mRunning2dInVr, |
| 530 | mCurrentVrService.getComponent(), mCurrentVrService.getUserId(), |
| 531 | mVrAppProcessId, mCurrentVrModeComponent); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 535 | private final IVrManager mVrManager = new IVrManager.Stub() { |
| 536 | |
| 537 | @Override |
| 538 | public void registerListener(IVrStateCallbacks cb) { |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 539 | enforceCallerPermissionAnyOf(Manifest.permission.ACCESS_VR_MANAGER, |
| 540 | Manifest.permission.ACCESS_VR_STATE); |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 541 | if (cb == null) { |
| 542 | throw new IllegalArgumentException("Callback binder object is null."); |
| 543 | } |
| 544 | |
| 545 | VrManagerService.this.addStateCallback(cb); |
| 546 | } |
| 547 | |
| 548 | @Override |
| 549 | public void unregisterListener(IVrStateCallbacks cb) { |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 550 | enforceCallerPermissionAnyOf(Manifest.permission.ACCESS_VR_MANAGER, |
| 551 | Manifest.permission.ACCESS_VR_STATE); |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 552 | if (cb == null) { |
| 553 | throw new IllegalArgumentException("Callback binder object is null."); |
| 554 | } |
| 555 | |
| 556 | VrManagerService.this.removeStateCallback(cb); |
| 557 | } |
| 558 | |
| 559 | @Override |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 560 | public void registerPersistentVrStateListener(IPersistentVrStateCallbacks cb) { |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 561 | enforceCallerPermissionAnyOf(Manifest.permission.ACCESS_VR_MANAGER, |
| 562 | Manifest.permission.ACCESS_VR_STATE); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 563 | if (cb == null) { |
| 564 | throw new IllegalArgumentException("Callback binder object is null."); |
| 565 | } |
| 566 | |
| 567 | VrManagerService.this.addPersistentStateCallback(cb); |
| 568 | } |
| 569 | |
| 570 | @Override |
| 571 | public void unregisterPersistentVrStateListener(IPersistentVrStateCallbacks cb) { |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 572 | enforceCallerPermissionAnyOf(Manifest.permission.ACCESS_VR_MANAGER, |
| 573 | Manifest.permission.ACCESS_VR_STATE); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 574 | if (cb == null) { |
| 575 | throw new IllegalArgumentException("Callback binder object is null."); |
| 576 | } |
| 577 | |
| 578 | VrManagerService.this.removePersistentStateCallback(cb); |
| 579 | } |
| 580 | |
| 581 | @Override |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 582 | public boolean getVrModeState() { |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 583 | enforceCallerPermissionAnyOf(Manifest.permission.ACCESS_VR_MANAGER, |
| 584 | Manifest.permission.ACCESS_VR_STATE); |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 585 | return VrManagerService.this.getVrMode(); |
| 586 | } |
| 587 | |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 588 | @Override |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 589 | public boolean getPersistentVrModeEnabled() { |
| 590 | enforceCallerPermissionAnyOf(Manifest.permission.ACCESS_VR_MANAGER, |
| 591 | Manifest.permission.ACCESS_VR_STATE); |
| 592 | return VrManagerService.this.getPersistentVrMode(); |
| 593 | } |
| 594 | |
| 595 | @Override |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 596 | public void setPersistentVrModeEnabled(boolean enabled) { |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 597 | enforceCallerPermissionAnyOf(Manifest.permission.RESTRICTED_VR_ACCESS); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 598 | VrManagerService.this.setPersistentVrModeEnabled(enabled); |
| 599 | } |
| 600 | |
| 601 | @Override |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 602 | public void setVr2dDisplayProperties( |
| 603 | Vr2dDisplayProperties vr2dDisplayProp) { |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 604 | enforceCallerPermissionAnyOf(Manifest.permission.RESTRICTED_VR_ACCESS); |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 605 | VrManagerService.this.setVr2dDisplayProperties(vr2dDisplayProp); |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | @Override |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 609 | public int getVr2dDisplayId() { |
| 610 | return VrManagerService.this.getVr2dDisplayId(); |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | @Override |
Ruben Brunk | 52ea662 | 2017-10-02 23:51:25 -0700 | [diff] [blame] | 614 | public void setAndBindCompositor(String componentName) { |
| 615 | enforceCallerPermissionAnyOf(Manifest.permission.RESTRICTED_VR_ACCESS); |
| 616 | VrManagerService.this.setAndBindCompositor( |
| 617 | (componentName == null) ? null : ComponentName.unflattenFromString(componentName)); |
| 618 | } |
| 619 | |
| 620 | @Override |
Steven Thomas | 1356ec9 | 2017-09-07 11:26:38 -0700 | [diff] [blame] | 621 | public void setStandbyEnabled(boolean standby) { |
| 622 | enforceCallerPermissionAnyOf(Manifest.permission.ACCESS_VR_MANAGER); |
| 623 | VrManagerService.this.setStandbyEnabled(standby); |
| 624 | } |
| 625 | |
| 626 | @Override |
Tarandeep Singh | 89a6c48 | 2017-11-21 14:26:11 -0800 | [diff] [blame] | 627 | public void setVrInputMethod(ComponentName componentName) { |
| 628 | enforceCallerPermissionAnyOf(Manifest.permission.RESTRICTED_VR_ACCESS); |
| 629 | InputMethodManagerInternal imm = |
| 630 | LocalServices.getService(InputMethodManagerInternal.class); |
| 631 | imm.startVrInputMethodNoCheck(componentName); |
| 632 | } |
| 633 | |
| 634 | @Override |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 635 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 636 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
| 637 | |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 638 | pw.println("********* Dump of VrManagerService *********"); |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 639 | pw.println("VR mode is currently: " + ((mVrModeAllowed) ? "allowed" : "disallowed")); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 640 | pw.println("Persistent VR mode is currently: " + |
| 641 | ((mPersistentVrModeEnabled) ? "enabled" : "disabled")); |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 642 | pw.println("Currently bound VR listener service: " |
Ruben Brunk | a25b93c | 2017-10-05 19:41:42 -0700 | [diff] [blame] | 643 | + ((mCurrentVrService == null) |
| 644 | ? "None" : mCurrentVrService.getComponent().flattenToString())); |
Ruben Brunk | 52ea662 | 2017-10-02 23:51:25 -0700 | [diff] [blame] | 645 | pw.println("Currently bound VR compositor service: " |
| 646 | + ((mCurrentVrCompositorService == null) |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 647 | ? "None" : mCurrentVrCompositorService.getComponent().flattenToString())); |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 648 | pw.println("Previous state transitions:\n"); |
| 649 | String tab = " "; |
| 650 | dumpStateTransitions(pw); |
| 651 | pw.println("\n\nRemote Callbacks:"); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 652 | int i=mVrStateRemoteCallbacks.beginBroadcast(); // create the broadcast item array |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 653 | while(i-->0) { |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 654 | pw.print(tab); |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 655 | pw.print(mVrStateRemoteCallbacks.getBroadcastItem(i)); |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 656 | if (i>0) pw.println(","); |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 657 | } |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 658 | mVrStateRemoteCallbacks.finishBroadcast(); |
| 659 | pw.println("\n\nPersistent Vr State Remote Callbacks:"); |
| 660 | i=mPersistentVrStateRemoteCallbacks.beginBroadcast(); |
| 661 | while(i-->0) { |
| 662 | pw.print(tab); |
| 663 | pw.print(mPersistentVrStateRemoteCallbacks.getBroadcastItem(i)); |
| 664 | if (i>0) pw.println(","); |
| 665 | } |
| 666 | mPersistentVrStateRemoteCallbacks.finishBroadcast(); |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 667 | pw.println("\n"); |
| 668 | pw.println("Installed VrListenerService components:"); |
| 669 | int userId = mCurrentVrModeUser; |
| 670 | ArraySet<ComponentName> installed = mComponentObserver.getInstalled(userId); |
| 671 | if (installed == null || installed.size() == 0) { |
| 672 | pw.println("None"); |
| 673 | } else { |
| 674 | for (ComponentName n : installed) { |
| 675 | pw.print(tab); |
| 676 | pw.println(n.flattenToString()); |
| 677 | } |
| 678 | } |
| 679 | pw.println("Enabled VrListenerService components:"); |
| 680 | ArraySet<ComponentName> enabled = mComponentObserver.getEnabled(userId); |
| 681 | if (enabled == null || enabled.size() == 0) { |
| 682 | pw.println("None"); |
| 683 | } else { |
| 684 | for (ComponentName n : enabled) { |
| 685 | pw.print(tab); |
| 686 | pw.println(n.flattenToString()); |
| 687 | } |
| 688 | } |
| 689 | pw.println("\n"); |
| 690 | pw.println("********* End of VrManagerService Dump *********"); |
Dan Sandler | dc34df5 | 2016-04-07 21:04:46 -0400 | [diff] [blame] | 691 | } |
| 692 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 693 | }; |
| 694 | |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 695 | /** |
| 696 | * Enforces that at lease one of the specified permissions is held by the caller. |
| 697 | * Throws SecurityException if none of the specified permissions are held. |
| 698 | * |
| 699 | * @param permissions One or more permissions to check against. |
| 700 | */ |
| 701 | private void enforceCallerPermissionAnyOf(String... permissions) { |
| 702 | for (String permission : permissions) { |
| 703 | if (mContext.checkCallingOrSelfPermission(permission) |
| 704 | == PackageManager.PERMISSION_GRANTED) { |
| 705 | return; |
| 706 | } |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 707 | } |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 708 | throw new SecurityException("Caller does not hold at least one of the permissions: " |
| 709 | + Arrays.toString(permissions)); |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 710 | } |
| 711 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 712 | /** |
| 713 | * Implementation of VrManagerInternal. Callable only from system services. |
| 714 | */ |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 715 | private final class LocalService extends VrManagerInternal { |
| 716 | @Override |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 717 | public void setVrMode(boolean enabled, ComponentName packageName, int userId, int processId, |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 718 | ComponentName callingPackage) { |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 719 | VrManagerService.this.setVrMode(enabled, packageName, userId, processId, callingPackage); |
Ruben Brunk | af9f004 | 2016-06-15 17:08:35 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | @Override |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 723 | public void onScreenStateChanged(boolean isScreenOn) { |
| 724 | VrManagerService.this.setScreenOn(isScreenOn); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | @Override |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 728 | public boolean isCurrentVrListener(String packageName, int userId) { |
| 729 | return VrManagerService.this.isCurrentVrListener(packageName, userId); |
| 730 | } |
| 731 | |
| 732 | @Override |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 733 | public int hasVrPackage(ComponentName packageName, int userId) { |
| 734 | return VrManagerService.this.hasVrPackage(packageName, userId); |
| 735 | } |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 736 | |
| 737 | @Override |
| 738 | public void setPersistentVrModeEnabled(boolean enabled) { |
| 739 | VrManagerService.this.setPersistentVrModeEnabled(enabled); |
| 740 | } |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 741 | |
| 742 | @Override |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 743 | public void setVr2dDisplayProperties( |
| 744 | Vr2dDisplayProperties compatDisplayProp) { |
| 745 | VrManagerService.this.setVr2dDisplayProperties(compatDisplayProp); |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | @Override |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 749 | public int getVr2dDisplayId() { |
| 750 | return VrManagerService.this.getVr2dDisplayId(); |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | @Override |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 754 | public void addPersistentVrModeStateListener(IPersistentVrStateCallbacks listener) { |
| 755 | VrManagerService.this.addPersistentStateCallback(listener); |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 756 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | public VrManagerService(Context context) { |
| 760 | super(context); |
| 761 | } |
| 762 | |
| 763 | @Override |
| 764 | public void onStart() { |
Ruben Brunk | baa4b55 | 2016-02-02 16:27:37 -0800 | [diff] [blame] | 765 | synchronized(mLock) { |
| 766 | initializeNative(); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 767 | mContext = getContext(); |
Ruben Brunk | baa4b55 | 2016-02-02 16:27:37 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 770 | mBootsToVr = SystemProperties.getBoolean("ro.boot.vr", false); |
Steven Thomas | 1356ec9 | 2017-09-07 11:26:38 -0700 | [diff] [blame] | 771 | mUseStandbyToExitVrMode = mBootsToVr |
Steven Thomas | d007c46 | 2017-11-13 16:31:37 -0800 | [diff] [blame] | 772 | && SystemProperties.getBoolean("persist.vr.use_standby_to_exit_vr_mode", true); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 773 | publishLocalService(VrManagerInternal.class, new LocalService()); |
Craig Donner | 8deb67c | 2017-02-07 18:10:32 -0800 | [diff] [blame] | 774 | publishBinderService(Context.VR_SERVICE, mVrManager.asBinder()); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 775 | } |
| 776 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 777 | @Override |
| 778 | public void onBootPhase(int phase) { |
Ruben Brunk | 8507004 | 2016-03-21 12:10:38 -0700 | [diff] [blame] | 779 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
Jeff Sharkey | 9765e44 | 2017-12-14 22:15:14 -0700 | [diff] [blame] | 780 | LocalServices.getService(ActivityManagerInternal.class) |
| 781 | .registerScreenObserver(this); |
| 782 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 783 | mNotificationManager = INotificationManager.Stub.asInterface( |
| 784 | ServiceManager.getService(Context.NOTIFICATION_SERVICE)); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 785 | synchronized (mLock) { |
| 786 | Looper looper = Looper.getMainLooper(); |
| 787 | Handler handler = new Handler(looper); |
| 788 | ArrayList<EnabledComponentChangeListener> listeners = new ArrayList<>(); |
| 789 | listeners.add(this); |
| 790 | mComponentObserver = EnabledComponentsObserver.build(mContext, handler, |
| 791 | Settings.Secure.ENABLED_VR_LISTENERS, looper, |
| 792 | android.Manifest.permission.BIND_VR_LISTENER_SERVICE, |
| 793 | VrListenerService.SERVICE_INTERFACE, mLock, listeners); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 794 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 795 | mComponentObserver.rebuildAll(); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 796 | } |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 797 | |
| 798 | //TODO: something more robust than picking the first one |
| 799 | ArraySet<ComponentName> defaultVrComponents = |
| 800 | SystemConfig.getInstance().getDefaultVrComponents(); |
| 801 | if (defaultVrComponents.size() > 0) { |
| 802 | mDefaultVrService = defaultVrComponents.valueAt(0); |
| 803 | } else { |
| 804 | Slog.i(TAG, "No default vr listener service found."); |
| 805 | } |
Santos Cordon | 2f22da7 | 2017-02-27 17:24:44 -0800 | [diff] [blame] | 806 | |
| 807 | DisplayManager dm = |
| 808 | (DisplayManager) getContext().getSystemService(Context.DISPLAY_SERVICE); |
Tarandeep Singh | e1cfcf4 | 2017-07-10 18:50:00 -0700 | [diff] [blame] | 809 | mVr2dDisplay = new Vr2dDisplay( |
| 810 | dm, |
| 811 | LocalServices.getService(ActivityManagerInternal.class), |
| 812 | LocalServices.getService(WindowManagerInternal.class), |
| 813 | mVrManager); |
Santos Cordon | c22c563 | 2017-06-21 16:03:49 -0700 | [diff] [blame] | 814 | mVr2dDisplay.init(getContext(), mBootsToVr); |
Steven Thomas | 68cc84f | 2017-06-08 16:55:04 -0700 | [diff] [blame] | 815 | |
| 816 | IntentFilter intentFilter = new IntentFilter(); |
| 817 | intentFilter.addAction(Intent.ACTION_USER_UNLOCKED); |
| 818 | getContext().registerReceiver(new BroadcastReceiver() { |
| 819 | @Override |
| 820 | public void onReceive(Context context, Intent intent) { |
| 821 | if (Intent.ACTION_USER_UNLOCKED.equals(intent.getAction())) { |
| 822 | VrManagerService.this.setUserUnlocked(); |
| 823 | } |
| 824 | } |
| 825 | }, intentFilter); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 829 | @Override |
| 830 | public void onStartUser(int userHandle) { |
| 831 | synchronized (mLock) { |
| 832 | mComponentObserver.onUsersChanged(); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | @Override |
| 837 | public void onSwitchUser(int userHandle) { |
Fyodor Kupolov | ab781c2 | 2017-12-19 16:44:26 -0800 | [diff] [blame] | 838 | FgThread.getHandler().post(() -> { |
| 839 | synchronized (mLock) { |
| 840 | mComponentObserver.onUsersChanged(); |
| 841 | } |
| 842 | }); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 843 | |
| 844 | } |
| 845 | |
| 846 | @Override |
| 847 | public void onStopUser(int userHandle) { |
| 848 | synchronized (mLock) { |
| 849 | mComponentObserver.onUsersChanged(); |
| 850 | } |
| 851 | |
| 852 | } |
| 853 | |
| 854 | @Override |
| 855 | public void onCleanupUser(int userHandle) { |
| 856 | synchronized (mLock) { |
| 857 | mComponentObserver.onUsersChanged(); |
| 858 | } |
| 859 | } |
| 860 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 861 | private void updateOverlayStateLocked(String exemptedPackage, int newUserId, int oldUserId) { |
| 862 | AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class); |
| 863 | |
| 864 | // If user changed drop restrictions for the old user. |
| 865 | if (oldUserId != newUserId) { |
| 866 | appOpsManager.setUserRestrictionForUser(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, |
| 867 | false, mOverlayToken, null, oldUserId); |
| 868 | } |
| 869 | |
| 870 | // Apply the restrictions for the current user based on vr state |
| 871 | String[] exemptions = (exemptedPackage == null) ? new String[0] : |
| 872 | new String[] { exemptedPackage }; |
| 873 | |
| 874 | appOpsManager.setUserRestrictionForUser(AppOpsManager.OP_SYSTEM_ALERT_WINDOW, |
Ruben Brunk | 2781b41 | 2017-04-24 12:07:17 -0700 | [diff] [blame] | 875 | mVrModeEnabled, mOverlayToken, exemptions, newUserId); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | private void updateDependentAppOpsLocked(String newVrServicePackage, int newUserId, |
| 879 | String oldVrServicePackage, int oldUserId) { |
| 880 | // If VR state changed and we also have a VR service change. |
| 881 | if (Objects.equals(newVrServicePackage, oldVrServicePackage)) { |
| 882 | return; |
| 883 | } |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 884 | final long identity = Binder.clearCallingIdentity(); |
| 885 | try { |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 886 | // Set overlay exception state based on VR enabled and current service |
| 887 | updateOverlayStateLocked(newVrServicePackage, newUserId, oldUserId); |
Svetoslav Ganov | 47d98de | 2016-03-01 15:01:14 -0800 | [diff] [blame] | 888 | } finally { |
| 889 | Binder.restoreCallingIdentity(identity); |
| 890 | } |
| 891 | } |
| 892 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 893 | /** |
| 894 | * Send VR mode changes (if the mode state has changed), and update the bound/unbound state of |
| 895 | * the currently selected VR listener service. If the component selected for the VR listener |
| 896 | * service has changed, unbind the previous listener and bind the new listener (if enabled). |
| 897 | * <p/> |
| 898 | * Note: Must be called while holding {@code mLock}. |
| 899 | * |
| 900 | * @param enabled new state for VR mode. |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 901 | * @param running2dInVr true if we have a top-level 2D intent. |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 902 | * @param component new component to be bound as a VR listener. |
| 903 | * @param userId user owning the component to be bound. |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 904 | * @param processId the process hosting the activity specified by calling. |
| 905 | * @param calling the component currently using VR mode or a 2D intent. |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 906 | * |
| 907 | * @return {@code true} if the component/user combination specified is valid. |
| 908 | */ |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 909 | private boolean updateCurrentVrServiceLocked(boolean enabled, boolean running2dInVr, |
| 910 | @NonNull ComponentName component, int userId, int processId, ComponentName calling) { |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 911 | |
| 912 | boolean sendUpdatedCaller = false; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 913 | final long identity = Binder.clearCallingIdentity(); |
| 914 | try { |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 915 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 916 | boolean validUserComponent = (mComponentObserver.isValid(component, userId) == |
| 917 | EnabledComponentsObserver.NO_ERROR); |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 918 | boolean goingIntoVrMode = validUserComponent && enabled; |
| 919 | if (!mVrModeEnabled && !goingIntoVrMode) { |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 920 | return validUserComponent; // Disabled -> Disabled transition does nothing. |
| 921 | } |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 922 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 923 | String oldVrServicePackage = mCurrentVrService != null |
| 924 | ? mCurrentVrService.getComponent().getPackageName() : null; |
| 925 | final int oldUserId = mCurrentVrModeUser; |
| 926 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 927 | // Notify system services and VR HAL of mode change. |
| 928 | changeVrModeLocked(goingIntoVrMode); |
Ruben Brunk | 29931bc | 2016-03-11 00:24:26 -0800 | [diff] [blame] | 929 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 930 | boolean nothingChanged = false; |
| 931 | if (!goingIntoVrMode) { |
| 932 | // Not going into VR mode, unbind whatever is running |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 933 | if (mCurrentVrService != null) { |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 934 | Slog.i(TAG, "Leaving VR mode, disconnecting " |
| 935 | + mCurrentVrService.getComponent() + " for user " |
| 936 | + mCurrentVrService.getUserId()); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 937 | mCurrentVrService.disconnect(); |
Ruben Brunk | 52ea662 | 2017-10-02 23:51:25 -0700 | [diff] [blame] | 938 | updateCompositorServiceLocked(UserHandle.USER_NULL, null); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 939 | mCurrentVrService = null; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 940 | } else { |
| 941 | nothingChanged = true; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 942 | } |
| 943 | } else { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 944 | // Going into VR mode |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 945 | if (mCurrentVrService != null) { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 946 | // Unbind any running service that doesn't match the latest component/user |
| 947 | // selection. |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 948 | if (mCurrentVrService.disconnectIfNotMatching(component, userId)) { |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 949 | Slog.i(TAG, "VR mode component changed to " + component |
| 950 | + ", disconnecting " + mCurrentVrService.getComponent() |
| 951 | + " for user " + mCurrentVrService.getUserId()); |
Ruben Brunk | 52ea662 | 2017-10-02 23:51:25 -0700 | [diff] [blame] | 952 | updateCompositorServiceLocked(UserHandle.USER_NULL, null); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 953 | createAndConnectService(component, userId); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 954 | sendUpdatedCaller = true; |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 955 | } else { |
| 956 | nothingChanged = true; |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 957 | } |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 958 | // The service with the correct component/user is already bound, do nothing. |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 959 | } else { |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 960 | // Nothing was previously running, bind a new service for the latest |
| 961 | // component/user selection. |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 962 | createAndConnectService(component, userId); |
| 963 | sendUpdatedCaller = true; |
| 964 | } |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 965 | } |
| 966 | |
Ruben Brunk | 666505e | 2017-05-04 16:54:46 -0700 | [diff] [blame] | 967 | if ((calling != null || mPersistentVrModeEnabled) |
Alex Sakhartchouk | ec4d3e4 | 2017-07-24 22:12:39 -0400 | [diff] [blame] | 968 | && !Objects.equals(calling, mCurrentVrModeComponent) |
| 969 | || mRunning2dInVr != running2dInVr) { |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 970 | sendUpdatedCaller = true; |
| 971 | } |
Ruben Brunk | 7196a6b | 2017-03-22 15:43:24 -0700 | [diff] [blame] | 972 | mCurrentVrModeComponent = calling; |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 973 | mRunning2dInVr = running2dInVr; |
| 974 | mVrAppProcessId = processId; |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 975 | |
| 976 | if (mCurrentVrModeUser != userId) { |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 977 | mCurrentVrModeUser = userId; |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 978 | sendUpdatedCaller = true; |
| 979 | } |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 980 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 981 | String newVrServicePackage = mCurrentVrService != null |
| 982 | ? mCurrentVrService.getComponent().getPackageName() : null; |
| 983 | final int newUserId = mCurrentVrModeUser; |
| 984 | |
| 985 | // Update AppOps settings that change state when entering/exiting VR mode, or changing |
| 986 | // the current VrListenerService. |
| 987 | updateDependentAppOpsLocked(newVrServicePackage, newUserId, |
| 988 | oldVrServicePackage, oldUserId); |
| 989 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 990 | if (mCurrentVrService != null && sendUpdatedCaller) { |
Steven Thomas | 9aedfb1 | 2017-12-06 19:41:07 -0800 | [diff] [blame] | 991 | callFocusedActivityChangedLocked(); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 992 | } |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 993 | |
| 994 | if (!nothingChanged) { |
| 995 | logStateLocked(); |
| 996 | } |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 997 | |
| 998 | return validUserComponent; |
| 999 | } finally { |
| 1000 | Binder.restoreCallingIdentity(identity); |
| 1001 | } |
| 1002 | } |
| 1003 | |
Steven Thomas | 9aedfb1 | 2017-12-06 19:41:07 -0800 | [diff] [blame] | 1004 | private void callFocusedActivityChangedLocked() { |
| 1005 | final ComponentName c = mCurrentVrModeComponent; |
| 1006 | final boolean b = mRunning2dInVr; |
| 1007 | final int pid = mVrAppProcessId; |
| 1008 | mCurrentVrService.sendEvent(new PendingEvent() { |
| 1009 | @Override |
| 1010 | public void runEvent(IInterface service) throws RemoteException { |
| 1011 | // Under specific (and unlikely) timing scenarios, when VrCore |
| 1012 | // crashes and is rebound, focusedActivityChanged() may be |
| 1013 | // called a 2nd time with the same arguments. IVrListeners |
| 1014 | // should make sure to handle that scenario gracefully. |
| 1015 | IVrListener l = (IVrListener) service; |
| 1016 | l.focusedActivityChanged(c, b, pid); |
| 1017 | } |
| 1018 | }); |
| 1019 | } |
| 1020 | |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 1021 | private boolean isDefaultAllowed(String packageName) { |
Craig Donner | 0568f28 | 2016-04-11 13:02:44 -0700 | [diff] [blame] | 1022 | PackageManager pm = mContext.getPackageManager(); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 1023 | |
| 1024 | ApplicationInfo info = null; |
| 1025 | try { |
| 1026 | info = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA); |
| 1027 | } catch (NameNotFoundException e) { |
| 1028 | } |
| 1029 | |
| 1030 | if (info == null || !(info.isSystemApp() || info.isUpdatedSystemApp())) { |
| 1031 | return false; |
| 1032 | } |
| 1033 | return true; |
| 1034 | } |
| 1035 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1036 | private void grantNotificationPolicyAccess(String pkg) { |
| 1037 | NotificationManager nm = mContext.getSystemService(NotificationManager.class); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 1038 | nm.setNotificationPolicyAccessGranted(pkg, true); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | private void revokeNotificationPolicyAccess(String pkg) { |
| 1042 | NotificationManager nm = mContext.getSystemService(NotificationManager.class); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 1043 | // Remove any DND zen rules possibly created by the package. |
| 1044 | nm.removeAutomaticZenRules(pkg); |
| 1045 | // Remove Notification Policy Access. |
| 1046 | nm.setNotificationPolicyAccessGranted(pkg, false); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1047 | } |
| 1048 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 1049 | private void grantNotificationListenerAccess(String pkg, int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1050 | NotificationManager nm = mContext.getSystemService(NotificationManager.class); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1051 | PackageManager pm = mContext.getPackageManager(); |
| 1052 | ArraySet<ComponentName> possibleServices = EnabledComponentsObserver.loadComponentNames(pm, |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 1053 | userId, NotificationListenerService.SERVICE_INTERFACE, |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1054 | android.Manifest.permission.BIND_NOTIFICATION_LISTENER_SERVICE); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1055 | |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1056 | for (ComponentName c : possibleServices) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1057 | if (Objects.equals(c.getPackageName(), pkg)) { |
| 1058 | nm.setNotificationListenerAccessGrantedForUser(c, userId, true); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1059 | } |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1060 | } |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1061 | } |
| 1062 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 1063 | private void revokeNotificationListenerAccess(String pkg, int userId) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1064 | NotificationManager nm = mContext.getSystemService(NotificationManager.class); |
| 1065 | List<ComponentName> current = nm.getEnabledNotificationListeners(userId); |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 1066 | |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1067 | for (ComponentName component : current) { |
Ruben Brunk | 1f4f26b | 2016-08-02 14:21:36 -0700 | [diff] [blame] | 1068 | if (component != null && component.getPackageName().equals(pkg)) { |
Julia Reynolds | b852e56 | 2017-06-06 16:14:18 -0400 | [diff] [blame] | 1069 | nm.setNotificationListenerAccessGrantedForUser(component, userId, false); |
Ruben Brunk | 8820f94 | 2016-05-09 12:46:28 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1072 | } |
| 1073 | |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 1074 | private void grantCoarseLocationPermissionIfNeeded(String pkg, int userId) { |
| 1075 | // Don't clobber the user if permission set in current state explicitly |
| 1076 | if (!isPermissionUserUpdated(Manifest.permission.ACCESS_COARSE_LOCATION, pkg, userId)) { |
Ruben Brunk | 12ab5e1 | 2016-11-10 15:27:30 -0800 | [diff] [blame] | 1077 | try { |
| 1078 | mContext.getPackageManager().grantRuntimePermission(pkg, |
| 1079 | Manifest.permission.ACCESS_COARSE_LOCATION, new UserHandle(userId)); |
| 1080 | } catch (IllegalArgumentException e) { |
| 1081 | // Package was removed during update. |
| 1082 | Slog.w(TAG, "Could not grant coarse location permission, package " + pkg |
| 1083 | + " was removed."); |
| 1084 | } |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | private void revokeCoarseLocationPermissionIfNeeded(String pkg, int userId) { |
| 1089 | // Don't clobber the user if permission set in current state explicitly |
| 1090 | if (!isPermissionUserUpdated(Manifest.permission.ACCESS_COARSE_LOCATION, pkg, userId)) { |
Ruben Brunk | 12ab5e1 | 2016-11-10 15:27:30 -0800 | [diff] [blame] | 1091 | try { |
| 1092 | mContext.getPackageManager().revokeRuntimePermission(pkg, |
| 1093 | Manifest.permission.ACCESS_COARSE_LOCATION, new UserHandle(userId)); |
| 1094 | } catch (IllegalArgumentException e) { |
| 1095 | // Package was removed during update. |
| 1096 | Slog.w(TAG, "Could not revoke coarse location permission, package " + pkg |
| 1097 | + " was removed."); |
| 1098 | } |
Craig Donner | cae8cdf | 2016-07-28 22:39:05 +0000 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 1102 | private boolean isPermissionUserUpdated(String permission, String pkg, int userId) { |
| 1103 | final int flags = mContext.getPackageManager().getPermissionFlags( |
| 1104 | permission, pkg, new UserHandle(userId)); |
| 1105 | return (flags & (PackageManager.FLAG_PERMISSION_USER_SET |
| 1106 | | PackageManager.FLAG_PERMISSION_USER_FIXED)) != 0; |
| 1107 | } |
| 1108 | |
| 1109 | private ArraySet<String> getNotificationListeners(ContentResolver resolver, int userId) { |
| 1110 | String flat = Settings.Secure.getStringForUser(resolver, |
| 1111 | Settings.Secure.ENABLED_NOTIFICATION_LISTENERS, userId); |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1112 | |
| 1113 | ArraySet<String> current = new ArraySet<>(); |
| 1114 | if (flat != null) { |
| 1115 | String[] allowed = flat.split(":"); |
| 1116 | for (String s : allowed) { |
Ruben Brunk | 1f4f26b | 2016-08-02 14:21:36 -0700 | [diff] [blame] | 1117 | if (!TextUtils.isEmpty(s)) { |
| 1118 | current.add(s); |
| 1119 | } |
Ruben Brunk | 98576cf | 2016-03-07 18:54:28 -0800 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | return current; |
| 1123 | } |
| 1124 | |
| 1125 | private static String formatSettings(Collection<String> c) { |
| 1126 | if (c == null || c.isEmpty()) { |
| 1127 | return ""; |
| 1128 | } |
| 1129 | |
| 1130 | StringBuilder b = new StringBuilder(); |
| 1131 | boolean start = true; |
| 1132 | for (String s : c) { |
| 1133 | if ("".equals(s)) { |
| 1134 | continue; |
| 1135 | } |
| 1136 | if (!start) { |
| 1137 | b.append(':'); |
| 1138 | } |
| 1139 | b.append(s); |
| 1140 | start = false; |
| 1141 | } |
| 1142 | return b.toString(); |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 1147 | private void createAndConnectService(@NonNull ComponentName component, int userId) { |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 1148 | mCurrentVrService = createVrListenerService(component, userId); |
Ruben Brunk | c7354fe | 2016-03-07 23:37:12 -0800 | [diff] [blame] | 1149 | mCurrentVrService.connect(); |
| 1150 | Slog.i(TAG, "Connecting " + component + " for user " + userId); |
| 1151 | } |
| 1152 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1153 | /** |
| 1154 | * Send VR mode change callbacks to HAL and system services if mode has actually changed. |
| 1155 | * <p/> |
| 1156 | * Note: Must be called while holding {@code mLock}. |
| 1157 | * |
| 1158 | * @param enabled new state of the VR mode. |
| 1159 | */ |
Svetoslav Ganov | e33f613 | 2016-06-01 16:25:31 -0700 | [diff] [blame] | 1160 | private void changeVrModeLocked(boolean enabled) { |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1161 | if (mVrModeEnabled != enabled) { |
| 1162 | mVrModeEnabled = enabled; |
| 1163 | |
| 1164 | // Log mode change event. |
| 1165 | Slog.i(TAG, "VR mode " + ((mVrModeEnabled) ? "enabled" : "disabled")); |
| 1166 | setVrModeNative(mVrModeEnabled); |
| 1167 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1168 | onVrModeChangedLocked(); |
| 1169 | } |
| 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * Notify system services of VR mode change. |
| 1174 | * <p/> |
| 1175 | * Note: Must be called while holding {@code mLock}. |
| 1176 | */ |
| 1177 | private void onVrModeChangedLocked() { |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1178 | mHandler.sendMessage(mHandler.obtainMessage(MSG_VR_STATE_CHANGE, |
| 1179 | (mVrModeEnabled) ? 1 : 0, 0)); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /** |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 1183 | * Helper function for making ManagedApplicationService for VrListenerService instances. |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1184 | */ |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 1185 | private ManagedApplicationService createVrListenerService(@NonNull ComponentName component, |
| 1186 | int userId) { |
| 1187 | int retryType = (mBootsToVr) ? ManagedApplicationService.RETRY_FOREVER |
| 1188 | : ManagedApplicationService.RETRY_NEVER; |
| 1189 | return ManagedApplicationService.build(mContext, component, userId, |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1190 | R.string.vr_listener_binding_label, Settings.ACTION_VR_LISTENER_SETTINGS, |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 1191 | sBinderChecker, /*isImportant*/true, retryType, mHandler, mEventCallback); |
| 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * Helper function for making ManagedApplicationService for VR Compositor instances. |
| 1196 | */ |
| 1197 | private ManagedApplicationService createVrCompositorService(@NonNull ComponentName component, |
| 1198 | int userId) { |
| 1199 | int retryType = (mBootsToVr) ? ManagedApplicationService.RETRY_FOREVER |
| 1200 | : ManagedApplicationService.RETRY_BEST_EFFORT; |
| 1201 | return ManagedApplicationService.build(mContext, component, userId, /*clientLabel*/0, |
| 1202 | /*settingsAction*/null, /*binderChecker*/null, /*isImportant*/true, retryType, |
| 1203 | mHandler, /*disconnectCallback*/mEventCallback); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1204 | } |
| 1205 | |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 1206 | /** |
| 1207 | * Apply the pending VR state. If no state is pending, disconnect any currently bound |
| 1208 | * VR listener service. |
| 1209 | */ |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1210 | private void consumeAndApplyPendingStateLocked() { |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 1211 | consumeAndApplyPendingStateLocked(true); |
| 1212 | } |
| 1213 | |
| 1214 | /** |
| 1215 | * Apply the pending VR state. |
| 1216 | * |
| 1217 | * @param disconnectIfNoPendingState if {@code true}, then any currently bound VR listener |
| 1218 | * service will be disconnected if no state is pending. If this is {@code false} then the |
| 1219 | * nothing will be changed when there is no pending state. |
| 1220 | */ |
| 1221 | private void consumeAndApplyPendingStateLocked(boolean disconnectIfNoPendingState) { |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1222 | if (mPendingState != null) { |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 1223 | updateCurrentVrServiceLocked(mPendingState.enabled, mPendingState.running2dInVr, |
| 1224 | mPendingState.targetPackageName, mPendingState.userId, mPendingState.processId, |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1225 | mPendingState.callingPackage); |
| 1226 | mPendingState = null; |
Ruben Brunk | 1ad4a2d | 2017-01-06 11:23:49 -0800 | [diff] [blame] | 1227 | } else if (disconnectIfNoPendingState) { |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 1228 | updateCurrentVrServiceLocked(false, false, null, 0, -1, null); |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 1232 | private void logStateLocked() { |
| 1233 | ComponentName currentBoundService = (mCurrentVrService == null) ? null : |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 1234 | mCurrentVrService.getComponent(); |
| 1235 | logEvent(new VrState(mVrModeEnabled, mRunning2dInVr, currentBoundService, |
| 1236 | mCurrentVrModeUser, mVrAppProcessId, mCurrentVrModeComponent, mWasDefaultGranted)); |
| 1237 | } |
| 1238 | |
| 1239 | private void logEvent(LogFormattable event) { |
| 1240 | synchronized (mLoggingDeque) { |
| 1241 | if (mLoggingDeque.size() == EVENT_LOG_SIZE) { |
| 1242 | mLoggingDeque.removeFirst(); |
| 1243 | mLogLimitHit = true; |
| 1244 | } |
| 1245 | mLoggingDeque.add(event); |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 1246 | } |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | private void dumpStateTransitions(PrintWriter pw) { |
| 1250 | SimpleDateFormat d = new SimpleDateFormat("MM-dd HH:mm:ss.SSS"); |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 1251 | synchronized (mLoggingDeque) { |
| 1252 | if (mLoggingDeque.size() == 0) { |
| 1253 | pw.print(" "); |
| 1254 | pw.println("None"); |
| 1255 | } |
| 1256 | |
| 1257 | if (mLogLimitHit) { |
| 1258 | pw.println("..."); // Indicates log overflow |
| 1259 | } |
| 1260 | |
| 1261 | for (LogFormattable event : mLoggingDeque) { |
| 1262 | pw.println(event.toLogString(d)); |
Ruben Brunk | 2ff9c01 | 2016-04-28 10:58:29 -0700 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1267 | /* |
| 1268 | * Implementation of VrManagerInternal calls. These are callable from system services. |
| 1269 | */ |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1270 | private void setVrMode(boolean enabled, @NonNull ComponentName targetPackageName, |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 1271 | int userId, int processId, @NonNull ComponentName callingPackage) { |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1272 | |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1273 | synchronized (mLock) { |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1274 | VrState pending; |
| 1275 | ComponentName targetListener; |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1276 | |
| 1277 | // If the device is in persistent VR mode, then calls to disable VR mode are ignored, |
| 1278 | // and the system default VR listener is used. |
| 1279 | boolean targetEnabledState = enabled || mPersistentVrModeEnabled; |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 1280 | boolean running2dInVr = !enabled && mPersistentVrModeEnabled; |
| 1281 | if (running2dInVr) { |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1282 | targetListener = mDefaultVrService; |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1283 | } else { |
| 1284 | targetListener = targetPackageName; |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1285 | } |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 1286 | |
| 1287 | pending = new VrState(targetEnabledState, running2dInVr, targetListener, |
| 1288 | userId, processId, callingPackage); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1289 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 1290 | if (!mVrModeAllowed) { |
| 1291 | // We're not allowed to be in VR mode. Make this state pending. This will be |
| 1292 | // applied the next time we are allowed to enter VR mode unless it is superseded by |
| 1293 | // another call. |
| 1294 | mPendingState = pending; |
| 1295 | return; |
| 1296 | } |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1297 | |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1298 | if (!targetEnabledState && mCurrentVrService != null) { |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1299 | // If we're transitioning out of VR mode, delay briefly to avoid expensive HAL calls |
| 1300 | // and service bind/unbind in case we are immediately switching to another VR app. |
| 1301 | if (mPendingState == null) { |
| 1302 | mHandler.sendEmptyMessageDelayed(MSG_PENDING_VR_STATE_CHANGE, |
| 1303 | PENDING_STATE_DELAY_MS); |
| 1304 | } |
| 1305 | |
Ruben Brunk | 040484c | 2016-10-28 20:18:57 -0700 | [diff] [blame] | 1306 | mPendingState = pending; |
Ruben Brunk | b56b72a | 2016-04-15 11:53:09 -0700 | [diff] [blame] | 1307 | return; |
| 1308 | } else { |
| 1309 | mHandler.removeMessages(MSG_PENDING_VR_STATE_CHANGE); |
| 1310 | mPendingState = null; |
| 1311 | } |
| 1312 | |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 1313 | updateCurrentVrServiceLocked(targetEnabledState, running2dInVr, targetListener, |
| 1314 | userId, processId, callingPackage); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | private void setPersistentVrModeEnabled(boolean enabled) { |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 1319 | synchronized(mLock) { |
| 1320 | setPersistentModeAndNotifyListenersLocked(enabled); |
Alex Sakhartchouk | a26b435 | 2017-07-25 17:08:43 -0400 | [diff] [blame] | 1321 | // Disabling persistent mode should disable the overall vr mode. |
| 1322 | if (!enabled) { |
Albert Chaulk | 16d2397 | 2017-07-14 12:32:57 -0400 | [diff] [blame] | 1323 | setVrMode(false, null, 0, -1, null); |
Zak Cohen | 56345f4 | 2017-01-26 13:54:28 -0800 | [diff] [blame] | 1324 | } |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1325 | } |
| 1326 | } |
| 1327 | |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 1328 | public void setVr2dDisplayProperties( |
| 1329 | Vr2dDisplayProperties compatDisplayProp) { |
| 1330 | if (mVr2dDisplay != null) { |
| 1331 | mVr2dDisplay.setVirtualDisplayProperties(compatDisplayProp); |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 1332 | return; |
| 1333 | } |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 1334 | Slog.w(TAG, "Vr2dDisplay is null!"); |
Karthik Ravi Shankar | cdf9ce7 | 2017-04-12 15:31:20 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 1337 | private int getVr2dDisplayId() { |
| 1338 | if (mVr2dDisplay != null) { |
| 1339 | return mVr2dDisplay.getVirtualDisplayId(); |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 1340 | } |
Karthik Ravi Shankar | 2b9aaed | 2017-05-01 01:34:19 -0700 | [diff] [blame] | 1341 | Slog.w(TAG, "Vr2dDisplay is null!"); |
Karthik Ravi Shankar | 3a47ec2 | 2017-03-08 18:09:35 -0800 | [diff] [blame] | 1342 | return INVALID_DISPLAY; |
| 1343 | } |
| 1344 | |
Ruben Brunk | 52ea662 | 2017-10-02 23:51:25 -0700 | [diff] [blame] | 1345 | private void setAndBindCompositor(ComponentName componentName) { |
| 1346 | final int userId = UserHandle.getCallingUserId(); |
| 1347 | final long token = Binder.clearCallingIdentity(); |
| 1348 | synchronized (mLock) { |
| 1349 | updateCompositorServiceLocked(userId, componentName); |
| 1350 | } |
| 1351 | Binder.restoreCallingIdentity(token); |
| 1352 | } |
| 1353 | |
| 1354 | private void updateCompositorServiceLocked(int userId, ComponentName componentName) { |
| 1355 | if (mCurrentVrCompositorService != null |
| 1356 | && mCurrentVrCompositorService.disconnectIfNotMatching(componentName, userId)) { |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 1357 | Slog.i(TAG, "Disconnecting compositor service: " |
| 1358 | + mCurrentVrCompositorService.getComponent()); |
Ruben Brunk | 52ea662 | 2017-10-02 23:51:25 -0700 | [diff] [blame] | 1359 | // Check if existing service matches the requested one, if not (or if the requested |
| 1360 | // component is null) disconnect it. |
| 1361 | mCurrentVrCompositorService = null; |
| 1362 | } |
| 1363 | |
| 1364 | if (componentName != null && mCurrentVrCompositorService == null) { |
| 1365 | // We don't have an existing service matching the requested component, so attempt to |
| 1366 | // connect one. |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 1367 | Slog.i(TAG, "Connecting compositor service: " + componentName); |
| 1368 | mCurrentVrCompositorService = createVrCompositorService(componentName, userId); |
Ruben Brunk | 52ea662 | 2017-10-02 23:51:25 -0700 | [diff] [blame] | 1369 | mCurrentVrCompositorService.connect(); |
| 1370 | } |
| 1371 | } |
| 1372 | |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 1373 | private void setPersistentModeAndNotifyListenersLocked(boolean enabled) { |
| 1374 | if (mPersistentVrModeEnabled == enabled) { |
| 1375 | return; |
| 1376 | } |
Ruben Brunk | d675f51 | 2017-10-03 15:39:41 -0700 | [diff] [blame] | 1377 | String eventName = "Persistent VR mode " + ((enabled) ? "enabled" : "disabled"); |
| 1378 | Slog.i(TAG, eventName); |
| 1379 | logEvent(new SettingEvent(eventName)); |
Zak Cohen | 732aeb0 | 2017-01-29 14:19:52 -0800 | [diff] [blame] | 1380 | mPersistentVrModeEnabled = enabled; |
| 1381 | |
| 1382 | mHandler.sendMessage(mHandler.obtainMessage(MSG_PERSISTENT_VR_MODE_STATE_CHANGE, |
| 1383 | (mPersistentVrModeEnabled) ? 1 : 0, 0)); |
| 1384 | } |
| 1385 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1386 | private int hasVrPackage(@NonNull ComponentName targetPackageName, int userId) { |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1387 | synchronized (mLock) { |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1388 | return mComponentObserver.isValid(targetPackageName, userId); |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | |
Ruben Brunk | 8f1d5cb | 2016-03-22 18:08:41 -0700 | [diff] [blame] | 1392 | private boolean isCurrentVrListener(String packageName, int userId) { |
| 1393 | synchronized (mLock) { |
| 1394 | if (mCurrentVrService == null) { |
| 1395 | return false; |
| 1396 | } |
| 1397 | return mCurrentVrService.getComponent().getPackageName().equals(packageName) && |
| 1398 | userId == mCurrentVrService.getUserId(); |
| 1399 | } |
| 1400 | } |
| 1401 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1402 | /* |
| 1403 | * Implementation of IVrManager calls. |
| 1404 | */ |
| 1405 | |
| 1406 | private void addStateCallback(IVrStateCallbacks cb) { |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 1407 | mVrStateRemoteCallbacks.register(cb); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1408 | } |
| 1409 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1410 | private void removeStateCallback(IVrStateCallbacks cb) { |
Steven Thomas | b70845c | 2017-03-16 14:32:37 -0700 | [diff] [blame] | 1411 | mVrStateRemoteCallbacks.unregister(cb); |
| 1412 | } |
| 1413 | |
| 1414 | private void addPersistentStateCallback(IPersistentVrStateCallbacks cb) { |
| 1415 | mPersistentVrStateRemoteCallbacks.register(cb); |
| 1416 | } |
| 1417 | |
| 1418 | private void removePersistentStateCallback(IPersistentVrStateCallbacks cb) { |
| 1419 | mPersistentVrStateRemoteCallbacks.unregister(cb); |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1420 | } |
| 1421 | |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1422 | private boolean getVrMode() { |
Ruben Brunk | e24b9a6 | 2016-02-16 21:38:24 -0800 | [diff] [blame] | 1423 | synchronized (mLock) { |
Ruben Brunk | c7be3be | 2016-04-01 17:07:51 -0700 | [diff] [blame] | 1424 | return mVrModeEnabled; |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1425 | } |
| 1426 | } |
Santos Cordon | e6d7723 | 2017-08-08 17:11:41 -0700 | [diff] [blame] | 1427 | |
| 1428 | private boolean getPersistentVrMode() { |
| 1429 | synchronized (mLock) { |
| 1430 | return mPersistentVrModeEnabled; |
| 1431 | } |
| 1432 | } |
Ruben Brunk | dd18a0b | 2015-12-04 16:16:31 -0800 | [diff] [blame] | 1433 | } |