Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | package com.android.systemui; |
| 18 | |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 23 | import android.content.IntentFilter; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 24 | import android.content.ServiceConnection; |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 25 | import android.graphics.Rect; |
| 26 | import android.os.Binder; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
| 28 | import android.os.IBinder; |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 29 | import android.os.Looper; |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 30 | import android.os.PatternMatcher; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
| 32 | import android.os.UserHandle; |
Winson Chung | 387e38b | 2018-07-09 14:30:54 -0700 | [diff] [blame] | 33 | import android.provider.Settings; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 34 | import android.util.Log; |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 35 | import android.view.SurfaceControl; |
| 36 | |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 37 | import com.android.systemui.OverviewProxyService.OverviewProxyListener; |
Winson Chung | c167427 | 2018-02-21 10:15:17 -0800 | [diff] [blame] | 38 | import com.android.systemui.recents.events.EventBus; |
| 39 | import com.android.systemui.recents.events.activity.DockedFirstAnimationFrameEvent; |
Matthew Ng | 1b1d346 | 2018-03-02 11:43:38 -0800 | [diff] [blame] | 40 | import com.android.systemui.recents.misc.SystemServicesProxy; |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 41 | import com.android.systemui.shared.recents.IOverviewProxy; |
| 42 | import com.android.systemui.shared.recents.ISystemUiProxy; |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 43 | import com.android.systemui.shared.system.ActivityManagerWrapper; |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 44 | import com.android.systemui.shared.system.GraphicBufferCompat; |
Matthew Ng | c603a50 | 2018-04-18 17:14:22 -0700 | [diff] [blame] | 45 | import com.android.systemui.stackdivider.Divider; |
Winson Chung | caf2b81 | 2018-01-26 10:29:46 -0800 | [diff] [blame] | 46 | import com.android.systemui.statusbar.phone.StatusBar; |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 47 | import com.android.systemui.statusbar.policy.CallbackController; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 48 | import com.android.systemui.statusbar.policy.DeviceProvisionedController; |
| 49 | import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener; |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 50 | |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 51 | import java.io.FileDescriptor; |
| 52 | import java.io.PrintWriter; |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 53 | import java.util.ArrayList; |
| 54 | import java.util.List; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 55 | |
Winson Chung | 6e4b0b56 | 2018-04-04 09:13:37 -0700 | [diff] [blame] | 56 | import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE; |
Matthew Ng | c1a97b1 | 2018-03-28 14:02:00 -0700 | [diff] [blame] | 57 | import static com.android.systemui.shared.system.NavigationBarCompat.FLAG_DISABLE_SWIPE_UP; |
Matthew Ng | 8f25fb96 | 2018-01-16 17:17:24 -0800 | [diff] [blame] | 58 | import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType; |
| 59 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Class to send information from overview to launcher with a binder. |
| 62 | */ |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 63 | public class OverviewProxyService implements CallbackController<OverviewProxyListener>, Dumpable { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 64 | |
Sunny Goyal | 09c12d7 | 2018-03-27 10:14:49 -0700 | [diff] [blame] | 65 | private static final String ACTION_QUICKSTEP = "android.intent.action.QUICKSTEP_SERVICE"; |
| 66 | |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 67 | public static final String TAG_OPS = "OverviewProxyService"; |
| 68 | public static final boolean DEBUG_OVERVIEW_PROXY = false; |
Matthew Ng | 9608748 | 2018-06-27 15:32:09 -0700 | [diff] [blame] | 69 | private static final long BACKOFF_MILLIS = 1000; |
Matthew Ng | e3c5af4 | 2018-05-02 16:58:44 -0700 | [diff] [blame] | 70 | private static final long DEFERRED_CALLBACK_MILLIS = 5000; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 71 | |
| 72 | private final Context mContext; |
| 73 | private final Handler mHandler; |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 74 | private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser; |
Sunny Goyal | 09c12d7 | 2018-03-27 10:14:49 -0700 | [diff] [blame] | 75 | private final ComponentName mRecentsComponentName; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 76 | private final DeviceProvisionedController mDeviceProvisionedController |
| 77 | = Dependency.get(DeviceProvisionedController.class); |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 78 | private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>(); |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 79 | private final Intent mQuickStepIntent; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 80 | |
| 81 | private IOverviewProxy mOverviewProxy; |
| 82 | private int mConnectionBackoffAttempts; |
Matthew Ng | 8f25fb96 | 2018-01-16 17:17:24 -0800 | [diff] [blame] | 83 | private @InteractionType int mInteractionFlags; |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 84 | private boolean mIsEnabled; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 85 | |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 86 | private ISystemUiProxy mSysUiProxy = new ISystemUiProxy.Stub() { |
Winson Chung | caf2b81 | 2018-01-26 10:29:46 -0800 | [diff] [blame] | 87 | |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 88 | public GraphicBufferCompat screenshot(Rect sourceCrop, int width, int height, int minLayer, |
| 89 | int maxLayer, boolean useIdentityTransform, int rotation) { |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 90 | long token = Binder.clearCallingIdentity(); |
| 91 | try { |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 92 | return new GraphicBufferCompat(SurfaceControl.screenshotToBuffer(sourceCrop, width, |
| 93 | height, minLayer, maxLayer, useIdentityTransform, rotation)); |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 94 | } finally { |
| 95 | Binder.restoreCallingIdentity(token); |
| 96 | } |
| 97 | } |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 98 | |
Winson Chung | caf2b81 | 2018-01-26 10:29:46 -0800 | [diff] [blame] | 99 | public void startScreenPinning(int taskId) { |
| 100 | long token = Binder.clearCallingIdentity(); |
| 101 | try { |
| 102 | mHandler.post(() -> { |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 103 | StatusBar statusBar = SysUiServiceProvider.getComponent(mContext, |
Winson Chung | caf2b81 | 2018-01-26 10:29:46 -0800 | [diff] [blame] | 104 | StatusBar.class); |
| 105 | if (statusBar != null) { |
| 106 | statusBar.showScreenPinningRequest(taskId, false /* allowCancel */); |
| 107 | } |
| 108 | }); |
| 109 | } finally { |
| 110 | Binder.restoreCallingIdentity(token); |
| 111 | } |
| 112 | } |
| 113 | |
Winson Chung | c167427 | 2018-02-21 10:15:17 -0800 | [diff] [blame] | 114 | public void onSplitScreenInvoked() { |
| 115 | long token = Binder.clearCallingIdentity(); |
| 116 | try { |
| 117 | EventBus.getDefault().post(new DockedFirstAnimationFrameEvent()); |
| 118 | } finally { |
| 119 | Binder.restoreCallingIdentity(token); |
| 120 | } |
| 121 | } |
| 122 | |
Tracy Zhou | 2759905 | 2018-04-16 15:47:29 -0700 | [diff] [blame] | 123 | public void onOverviewShown(boolean fromHome) { |
| 124 | long token = Binder.clearCallingIdentity(); |
| 125 | try { |
| 126 | mHandler.post(() -> { |
| 127 | for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) { |
| 128 | mConnectionCallbacks.get(i).onOverviewShown(fromHome); |
| 129 | } |
| 130 | }); |
| 131 | } finally { |
| 132 | Binder.restoreCallingIdentity(token); |
| 133 | } |
| 134 | } |
| 135 | |
Matthew Ng | 8f25fb96 | 2018-01-16 17:17:24 -0800 | [diff] [blame] | 136 | public void setInteractionState(@InteractionType int flags) { |
| 137 | long token = Binder.clearCallingIdentity(); |
| 138 | try { |
| 139 | if (mInteractionFlags != flags) { |
| 140 | mInteractionFlags = flags; |
| 141 | mHandler.post(() -> { |
| 142 | for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) { |
| 143 | mConnectionCallbacks.get(i).onInteractionFlagsChanged(flags); |
| 144 | } |
| 145 | }); |
| 146 | } |
| 147 | } finally { |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 148 | Prefs.putInt(mContext, Prefs.Key.QUICK_STEP_INTERACTION_FLAGS, mInteractionFlags); |
Matthew Ng | 8f25fb96 | 2018-01-16 17:17:24 -0800 | [diff] [blame] | 149 | Binder.restoreCallingIdentity(token); |
| 150 | } |
| 151 | } |
Matthew Ng | c603a50 | 2018-04-18 17:14:22 -0700 | [diff] [blame] | 152 | |
| 153 | public Rect getNonMinimizedSplitScreenSecondaryBounds() { |
| 154 | long token = Binder.clearCallingIdentity(); |
| 155 | try { |
Winson Chung | 06ca274 | 2018-06-29 12:26:49 -0700 | [diff] [blame] | 156 | Divider divider = SysUiServiceProvider.getComponent(mContext, Divider.class); |
Matthew Ng | c603a50 | 2018-04-18 17:14:22 -0700 | [diff] [blame] | 157 | if (divider != null) { |
| 158 | return divider.getView().getNonMinimizedSplitScreenSecondaryBounds(); |
| 159 | } |
| 160 | return null; |
| 161 | } finally { |
| 162 | Binder.restoreCallingIdentity(token); |
| 163 | } |
| 164 | } |
Matthew Ng | 96985e7 | 2018-05-08 15:46:13 -0700 | [diff] [blame] | 165 | |
| 166 | public void setBackButtonAlpha(float alpha, boolean animate) { |
| 167 | long token = Binder.clearCallingIdentity(); |
| 168 | try { |
| 169 | mHandler.post(() -> { |
Matthew Ng | 53896454 | 2018-05-30 11:27:39 -0700 | [diff] [blame] | 170 | notifyBackButtonAlphaChanged(alpha, animate); |
Matthew Ng | 96985e7 | 2018-05-08 15:46:13 -0700 | [diff] [blame] | 171 | }); |
| 172 | } finally { |
| 173 | Binder.restoreCallingIdentity(token); |
| 174 | } |
| 175 | } |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
Matthew Ng | e3c5af4 | 2018-05-02 16:58:44 -0700 | [diff] [blame] | 178 | private final Runnable mDeferredConnectionCallback = () -> { |
| 179 | Log.w(TAG_OPS, "Binder supposed established connection but actual connection to service " |
| 180 | + "timed out, trying again"); |
| 181 | internalConnectToCurrentUser(); |
| 182 | }; |
| 183 | |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 184 | private final BroadcastReceiver mLauncherStateChangedReceiver = new BroadcastReceiver() { |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 185 | @Override |
| 186 | public void onReceive(Context context, Intent intent) { |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 187 | updateEnabledState(); |
| 188 | |
| 189 | // When launcher service is disabled, reset interaction flags because it is inactive |
| 190 | if (!isEnabled()) { |
| 191 | mInteractionFlags = 0; |
| 192 | Prefs.remove(mContext, Prefs.Key.QUICK_STEP_INTERACTION_FLAGS); |
| 193 | } |
| 194 | |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 195 | // Reconnect immediately, instead of waiting for resume to arrive. |
| 196 | startConnectionToCurrentUser(); |
| 197 | } |
| 198 | }; |
| 199 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 200 | private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() { |
| 201 | @Override |
| 202 | public void onServiceConnected(ComponentName name, IBinder service) { |
Matthew Ng | e3c5af4 | 2018-05-02 16:58:44 -0700 | [diff] [blame] | 203 | mHandler.removeCallbacks(mDeferredConnectionCallback); |
| 204 | mConnectionBackoffAttempts = 0; |
| 205 | mOverviewProxy = IOverviewProxy.Stub.asInterface(service); |
| 206 | // Listen for launcher's death |
| 207 | try { |
| 208 | service.linkToDeath(mOverviewServiceDeathRcpt, 0); |
| 209 | } catch (RemoteException e) { |
| 210 | Log.e(TAG_OPS, "Lost connection to launcher service", e); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 211 | } |
Matthew Ng | e3c5af4 | 2018-05-02 16:58:44 -0700 | [diff] [blame] | 212 | try { |
| 213 | mOverviewProxy.onBind(mSysUiProxy); |
| 214 | } catch (RemoteException e) { |
| 215 | Log.e(TAG_OPS, "Failed to call onBind()", e); |
| 216 | } |
| 217 | notifyConnectionChanged(); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | public void onNullBinding(ComponentName name) { |
| 222 | Log.w(TAG_OPS, "Null binding of '" + name + "', try reconnecting"); |
| 223 | internalConnectToCurrentUser(); |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void onBindingDied(ComponentName name) { |
| 228 | Log.w(TAG_OPS, "Binding died of '" + name + "', try reconnecting"); |
| 229 | internalConnectToCurrentUser(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void onServiceDisconnected(ComponentName name) { |
| 234 | // Do nothing |
| 235 | } |
| 236 | }; |
| 237 | |
| 238 | private final DeviceProvisionedListener mDeviceProvisionedCallback = |
| 239 | new DeviceProvisionedListener() { |
| 240 | @Override |
Matthew Ng | dfab86c | 2017-11-07 15:46:51 -0800 | [diff] [blame] | 241 | public void onUserSetupChanged() { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 242 | if (mDeviceProvisionedController.isCurrentUserSetup()) { |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 243 | internalConnectToCurrentUser(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public void onUserSwitched() { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 249 | mConnectionBackoffAttempts = 0; |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 250 | internalConnectToCurrentUser(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 251 | } |
| 252 | }; |
| 253 | |
| 254 | // This is the death handler for the binder from the launcher service |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 255 | private final IBinder.DeathRecipient mOverviewServiceDeathRcpt |
| 256 | = this::startConnectionToCurrentUser; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 257 | |
| 258 | public OverviewProxyService(Context context) { |
| 259 | mContext = context; |
| 260 | mHandler = new Handler(); |
| 261 | mConnectionBackoffAttempts = 0; |
Sunny Goyal | 09c12d7 | 2018-03-27 10:14:49 -0700 | [diff] [blame] | 262 | mRecentsComponentName = ComponentName.unflattenFromString(context.getString( |
| 263 | com.android.internal.R.string.config_recentsComponentName)); |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 264 | mQuickStepIntent = new Intent(ACTION_QUICKSTEP) |
| 265 | .setPackage(mRecentsComponentName.getPackageName()); |
| 266 | mInteractionFlags = Prefs.getInt(mContext, Prefs.Key.QUICK_STEP_INTERACTION_FLAGS, 0); |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 267 | |
| 268 | // Listen for the package update changes. |
Matthew Ng | 1b1d346 | 2018-03-02 11:43:38 -0800 | [diff] [blame] | 269 | if (SystemServicesProxy.getInstance(context) |
| 270 | .isSystemUser(mDeviceProvisionedController.getCurrentUser())) { |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 271 | updateEnabledState(); |
Matthew Ng | 1b1d346 | 2018-03-02 11:43:38 -0800 | [diff] [blame] | 272 | mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback); |
| 273 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 274 | filter.addDataScheme("package"); |
Sunny Goyal | 09c12d7 | 2018-03-27 10:14:49 -0700 | [diff] [blame] | 275 | filter.addDataSchemeSpecificPart(mRecentsComponentName.getPackageName(), |
Matthew Ng | 1b1d346 | 2018-03-02 11:43:38 -0800 | [diff] [blame] | 276 | PatternMatcher.PATTERN_LITERAL); |
| 277 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 278 | mContext.registerReceiver(mLauncherStateChangedReceiver, filter); |
Matthew Ng | 1b1d346 | 2018-03-02 11:43:38 -0800 | [diff] [blame] | 279 | } |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | public void startConnectionToCurrentUser() { |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 283 | if (mHandler.getLooper() != Looper.myLooper()) { |
| 284 | mHandler.post(mConnectionRunnable); |
| 285 | } else { |
| 286 | internalConnectToCurrentUser(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private void internalConnectToCurrentUser() { |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 291 | disconnectFromLauncherService(); |
| 292 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 293 | // If user has not setup yet or already connected, do not try to connect |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 294 | if (!mDeviceProvisionedController.isCurrentUserSetup() || !isEnabled()) { |
Matthew Ng | e3c5af4 | 2018-05-02 16:58:44 -0700 | [diff] [blame] | 295 | Log.v(TAG_OPS, "Cannot attempt connection, is setup " |
| 296 | + mDeviceProvisionedController.isCurrentUserSetup() + ", is enabled " |
| 297 | + isEnabled()); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | mHandler.removeCallbacks(mConnectionRunnable); |
Sunny Goyal | 09c12d7 | 2018-03-27 10:14:49 -0700 | [diff] [blame] | 301 | Intent launcherServiceIntent = new Intent(ACTION_QUICKSTEP) |
| 302 | .setPackage(mRecentsComponentName.getPackageName()); |
Matthew Ng | 10b6c41a | 2018-03-26 18:01:37 -0700 | [diff] [blame] | 303 | boolean bound = false; |
| 304 | try { |
| 305 | bound = mContext.bindServiceAsUser(launcherServiceIntent, |
| 306 | mOverviewServiceConnection, Context.BIND_AUTO_CREATE, |
| 307 | UserHandle.of(mDeviceProvisionedController.getCurrentUser())); |
| 308 | } catch (SecurityException e) { |
| 309 | Log.e(TAG_OPS, "Unable to bind because of security error", e); |
| 310 | } |
Matthew Ng | e3c5af4 | 2018-05-02 16:58:44 -0700 | [diff] [blame] | 311 | if (bound) { |
| 312 | // Ensure that connection has been established even if it thinks it is bound |
| 313 | mHandler.postDelayed(mDeferredConnectionCallback, DEFERRED_CALLBACK_MILLIS); |
| 314 | } else { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 315 | // Retry after exponential backoff timeout |
| 316 | final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts); |
| 317 | mHandler.postDelayed(mConnectionRunnable, timeoutMs); |
| 318 | mConnectionBackoffAttempts++; |
Matthew Ng | e3c5af4 | 2018-05-02 16:58:44 -0700 | [diff] [blame] | 319 | Log.w(TAG_OPS, "Failed to connect on attempt " + mConnectionBackoffAttempts |
| 320 | + " will try again in " + timeoutMs + "ms"); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 324 | @Override |
| 325 | public void addCallback(OverviewProxyListener listener) { |
| 326 | mConnectionCallbacks.add(listener); |
| 327 | listener.onConnectionChanged(mOverviewProxy != null); |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 328 | listener.onInteractionFlagsChanged(mInteractionFlags); |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public void removeCallback(OverviewProxyListener listener) { |
| 333 | mConnectionCallbacks.remove(listener); |
| 334 | } |
| 335 | |
Matthew Ng | c1a97b1 | 2018-03-28 14:02:00 -0700 | [diff] [blame] | 336 | public boolean shouldShowSwipeUpUI() { |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 337 | return isEnabled() && ((mInteractionFlags & FLAG_DISABLE_SWIPE_UP) == 0); |
| 338 | } |
| 339 | |
| 340 | public boolean isEnabled() { |
| 341 | return mIsEnabled; |
Matthew Ng | c1a97b1 | 2018-03-28 14:02:00 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 344 | public IOverviewProxy getProxy() { |
| 345 | return mOverviewProxy; |
| 346 | } |
| 347 | |
Matthew Ng | 8f25fb96 | 2018-01-16 17:17:24 -0800 | [diff] [blame] | 348 | public int getInteractionFlags() { |
| 349 | return mInteractionFlags; |
| 350 | } |
| 351 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 352 | private void disconnectFromLauncherService() { |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 353 | if (mOverviewProxy != null) { |
Matthew Ng | eb6893b | 2017-11-09 17:15:33 -0800 | [diff] [blame] | 354 | mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0); |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 355 | mContext.unbindService(mOverviewServiceConnection); |
| 356 | mOverviewProxy = null; |
Matthew Ng | 53896454 | 2018-05-30 11:27:39 -0700 | [diff] [blame] | 357 | notifyBackButtonAlphaChanged(1f, false /* animate */); |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 358 | notifyConnectionChanged(); |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 359 | } |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 360 | } |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 361 | |
Matthew Ng | 53896454 | 2018-05-30 11:27:39 -0700 | [diff] [blame] | 362 | private void notifyBackButtonAlphaChanged(float alpha, boolean animate) { |
| 363 | for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) { |
| 364 | mConnectionCallbacks.get(i).onBackButtonAlphaChanged(alpha, animate); |
| 365 | } |
| 366 | } |
| 367 | |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 368 | private void notifyConnectionChanged() { |
| 369 | for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) { |
| 370 | mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null); |
| 371 | } |
| 372 | } |
| 373 | |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 374 | public void notifyQuickStepStarted() { |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 375 | for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) { |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 376 | mConnectionCallbacks.get(i).onQuickStepStarted(); |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Tracy Zhou | 2759905 | 2018-04-16 15:47:29 -0700 | [diff] [blame] | 380 | public void notifyQuickScrubStarted() { |
| 381 | for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) { |
| 382 | mConnectionCallbacks.get(i).onQuickScrubStarted(); |
| 383 | } |
| 384 | } |
| 385 | |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 386 | private void updateEnabledState() { |
Winson Chung | 6e4b0b56 | 2018-04-04 09:13:37 -0700 | [diff] [blame] | 387 | mIsEnabled = mContext.getPackageManager().resolveServiceAsUser(mQuickStepIntent, |
| 388 | MATCH_DIRECT_BOOT_UNAWARE, |
Winson Chung | d95a225 | 2018-04-04 17:02:29 +0000 | [diff] [blame] | 389 | ActivityManagerWrapper.getInstance().getCurrentUserId()) != null; |
| 390 | } |
| 391 | |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 392 | @Override |
| 393 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 394 | pw.println(TAG_OPS + " state:"); |
Winson Chung | 387e38b | 2018-07-09 14:30:54 -0700 | [diff] [blame] | 395 | pw.print(" recentsComponentName="); pw.println(mRecentsComponentName); |
| 396 | pw.print(" isConnected="); pw.println(mOverviewProxy != null); |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 397 | pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController |
| 398 | .isCurrentUserSetup()); |
Winson Chung | 387e38b | 2018-07-09 14:30:54 -0700 | [diff] [blame] | 399 | pw.print(" connectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts); |
| 400 | pw.print(" interactionFlags="); pw.println(mInteractionFlags); |
| 401 | |
| 402 | pw.print(" quickStepIntent="); pw.println(mQuickStepIntent); |
| 403 | pw.print(" quickStepIntentResolved="); pw.println(isEnabled()); |
| 404 | |
| 405 | final int swipeUpDefaultValue = mContext.getResources() |
| 406 | .getBoolean(com.android.internal.R.bool.config_swipe_up_gesture_default) ? 1 : 0; |
| 407 | final int swipeUpEnabled = Settings.Secure.getInt(mContext.getContentResolver(), |
| 408 | Settings.Secure.SWIPE_UP_TO_SWITCH_APPS_ENABLED, swipeUpDefaultValue); |
| 409 | pw.print(" swipeUpSetting="); pw.println(swipeUpEnabled != 0); |
| 410 | pw.print(" swipeUpSettingDefault="); pw.println(swipeUpDefaultValue != 0); |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 413 | public interface OverviewProxyListener { |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 414 | default void onConnectionChanged(boolean isConnected) {} |
Matthew Ng | 2ea93b7 | 2018-03-14 19:43:18 +0000 | [diff] [blame] | 415 | default void onQuickStepStarted() {} |
Matthew Ng | 8f25fb96 | 2018-01-16 17:17:24 -0800 | [diff] [blame] | 416 | default void onInteractionFlagsChanged(@InteractionType int flags) {} |
Tracy Zhou | 2759905 | 2018-04-16 15:47:29 -0700 | [diff] [blame] | 417 | default void onOverviewShown(boolean fromHome) {} |
| 418 | default void onQuickScrubStarted() {} |
Matthew Ng | 96985e7 | 2018-05-08 15:46:13 -0700 | [diff] [blame] | 419 | default void onBackButtonAlphaChanged(float alpha, boolean animate) {} |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 420 | } |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 421 | } |