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; |
| 33 | import android.util.Log; |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 34 | import android.view.SurfaceControl; |
| 35 | |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 36 | import com.android.systemui.OverviewProxyService.OverviewProxyListener; |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 37 | import com.android.systemui.shared.recents.IOverviewProxy; |
| 38 | import com.android.systemui.shared.recents.ISystemUiProxy; |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 39 | import com.android.systemui.shared.system.GraphicBufferCompat; |
Winson Chung | caf2b81 | 2018-01-26 10:29:46 -0800 | [diff] [blame] | 40 | import com.android.systemui.statusbar.phone.StatusBar; |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 41 | import com.android.systemui.statusbar.policy.CallbackController; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 42 | import com.android.systemui.statusbar.policy.DeviceProvisionedController; |
| 43 | import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener; |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 44 | |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 45 | import java.io.FileDescriptor; |
| 46 | import java.io.PrintWriter; |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 47 | import java.util.ArrayList; |
| 48 | import java.util.List; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Class to send information from overview to launcher with a binder. |
| 52 | */ |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 53 | public class OverviewProxyService implements CallbackController<OverviewProxyListener>, Dumpable { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 54 | |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 55 | public static final String TAG_OPS = "OverviewProxyService"; |
| 56 | public static final boolean DEBUG_OVERVIEW_PROXY = false; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 57 | private static final long BACKOFF_MILLIS = 5000; |
| 58 | |
| 59 | private final Context mContext; |
| 60 | private final Handler mHandler; |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 61 | private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser; |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 62 | private final ComponentName mLauncherComponentName; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 63 | private final DeviceProvisionedController mDeviceProvisionedController |
| 64 | = Dependency.get(DeviceProvisionedController.class); |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 65 | private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 66 | |
| 67 | private IOverviewProxy mOverviewProxy; |
| 68 | private int mConnectionBackoffAttempts; |
| 69 | |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 70 | private ISystemUiProxy mSysUiProxy = new ISystemUiProxy.Stub() { |
Winson Chung | caf2b81 | 2018-01-26 10:29:46 -0800 | [diff] [blame] | 71 | |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 72 | public GraphicBufferCompat screenshot(Rect sourceCrop, int width, int height, int minLayer, |
| 73 | int maxLayer, boolean useIdentityTransform, int rotation) { |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 74 | long token = Binder.clearCallingIdentity(); |
| 75 | try { |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 76 | return new GraphicBufferCompat(SurfaceControl.screenshotToBuffer(sourceCrop, width, |
| 77 | height, minLayer, maxLayer, useIdentityTransform, rotation)); |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 78 | } finally { |
| 79 | Binder.restoreCallingIdentity(token); |
| 80 | } |
| 81 | } |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 82 | |
Winson Chung | caf2b81 | 2018-01-26 10:29:46 -0800 | [diff] [blame] | 83 | public void startScreenPinning(int taskId) { |
| 84 | long token = Binder.clearCallingIdentity(); |
| 85 | try { |
| 86 | mHandler.post(() -> { |
| 87 | StatusBar statusBar = ((SystemUIApplication) mContext).getComponent( |
| 88 | StatusBar.class); |
| 89 | if (statusBar != null) { |
| 90 | statusBar.showScreenPinningRequest(taskId, false /* allowCancel */); |
| 91 | } |
| 92 | }); |
| 93 | } finally { |
| 94 | Binder.restoreCallingIdentity(token); |
| 95 | } |
| 96 | } |
| 97 | |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 98 | public void onRecentsAnimationStarted() { |
| 99 | long token = Binder.clearCallingIdentity(); |
| 100 | try { |
Winson Chung | caf2b81 | 2018-01-26 10:29:46 -0800 | [diff] [blame] | 101 | mHandler.post(() -> { |
| 102 | notifyRecentsAnimationStarted(); |
| 103 | }); |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 104 | } finally { |
| 105 | Binder.restoreCallingIdentity(token); |
| 106 | } |
| 107 | } |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 110 | private final BroadcastReceiver mLauncherAddedReceiver = new BroadcastReceiver() { |
| 111 | @Override |
| 112 | public void onReceive(Context context, Intent intent) { |
| 113 | // Reconnect immediately, instead of waiting for resume to arrive. |
| 114 | startConnectionToCurrentUser(); |
| 115 | } |
| 116 | }; |
| 117 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 118 | private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() { |
| 119 | @Override |
| 120 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 121 | if (service != null) { |
| 122 | mConnectionBackoffAttempts = 0; |
| 123 | mOverviewProxy = IOverviewProxy.Stub.asInterface(service); |
| 124 | // Listen for launcher's death |
| 125 | try { |
| 126 | service.linkToDeath(mOverviewServiceDeathRcpt, 0); |
| 127 | } catch (RemoteException e) { |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 128 | Log.e(TAG_OPS, "Lost connection to launcher service", e); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 129 | } |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 130 | try { |
| 131 | mOverviewProxy.onBind(mSysUiProxy); |
| 132 | } catch (RemoteException e) { |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 133 | Log.e(TAG_OPS, "Failed to call onBind()", e); |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 134 | } |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 135 | notifyConnectionChanged(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void onServiceDisconnected(ComponentName name) { |
| 141 | // Do nothing |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | private final DeviceProvisionedListener mDeviceProvisionedCallback = |
| 146 | new DeviceProvisionedListener() { |
| 147 | @Override |
Matthew Ng | dfab86c | 2017-11-07 15:46:51 -0800 | [diff] [blame] | 148 | public void onUserSetupChanged() { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 149 | if (mDeviceProvisionedController.isCurrentUserSetup()) { |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 150 | internalConnectToCurrentUser(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void onUserSwitched() { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 156 | mConnectionBackoffAttempts = 0; |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 157 | internalConnectToCurrentUser(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 158 | } |
| 159 | }; |
| 160 | |
| 161 | // This is the death handler for the binder from the launcher service |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 162 | private final IBinder.DeathRecipient mOverviewServiceDeathRcpt |
| 163 | = this::startConnectionToCurrentUser; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 164 | |
| 165 | public OverviewProxyService(Context context) { |
| 166 | mContext = context; |
| 167 | mHandler = new Handler(); |
| 168 | mConnectionBackoffAttempts = 0; |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 169 | mLauncherComponentName = ComponentName |
| 170 | .unflattenFromString(context.getString(R.string.config_overviewServiceComponent)); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 171 | mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback); |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 172 | |
| 173 | // Listen for the package update changes. |
| 174 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 175 | filter.addDataScheme("package"); |
| 176 | filter.addDataSchemeSpecificPart(mLauncherComponentName.getPackageName(), |
| 177 | PatternMatcher.PATTERN_LITERAL); |
Matthew Ng | c361fa1 | 2018-01-08 12:50:27 -0800 | [diff] [blame] | 178 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 179 | mContext.registerReceiver(mLauncherAddedReceiver, filter); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | public void startConnectionToCurrentUser() { |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 183 | if (mHandler.getLooper() != Looper.myLooper()) { |
| 184 | mHandler.post(mConnectionRunnable); |
| 185 | } else { |
| 186 | internalConnectToCurrentUser(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | private void internalConnectToCurrentUser() { |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 191 | disconnectFromLauncherService(); |
| 192 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 193 | // If user has not setup yet or already connected, do not try to connect |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 194 | if (!mDeviceProvisionedController.isCurrentUserSetup()) { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | mHandler.removeCallbacks(mConnectionRunnable); |
| 198 | Intent launcherServiceIntent = new Intent(); |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 199 | launcherServiceIntent.setComponent(mLauncherComponentName); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 200 | boolean bound = mContext.bindServiceAsUser(launcherServiceIntent, |
| 201 | mOverviewServiceConnection, Context.BIND_AUTO_CREATE, |
Matthew Ng | 09e6fbd | 2018-02-01 16:54:16 -0800 | [diff] [blame^] | 202 | UserHandle.of(mDeviceProvisionedController.getCurrentUser())); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 203 | if (!bound) { |
| 204 | // Retry after exponential backoff timeout |
| 205 | final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts); |
| 206 | mHandler.postDelayed(mConnectionRunnable, timeoutMs); |
| 207 | mConnectionBackoffAttempts++; |
| 208 | } |
| 209 | } |
| 210 | |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 211 | @Override |
| 212 | public void addCallback(OverviewProxyListener listener) { |
| 213 | mConnectionCallbacks.add(listener); |
| 214 | listener.onConnectionChanged(mOverviewProxy != null); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void removeCallback(OverviewProxyListener listener) { |
| 219 | mConnectionCallbacks.remove(listener); |
| 220 | } |
| 221 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 222 | public IOverviewProxy getProxy() { |
| 223 | return mOverviewProxy; |
| 224 | } |
| 225 | |
Tony Wickham | fb63fe8 | 2018-01-16 12:14:06 -0800 | [diff] [blame] | 226 | public ComponentName getLauncherComponent() { |
| 227 | return mLauncherComponentName; |
| 228 | } |
| 229 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 230 | private void disconnectFromLauncherService() { |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 231 | if (mOverviewProxy != null) { |
Matthew Ng | eb6893b | 2017-11-09 17:15:33 -0800 | [diff] [blame] | 232 | mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0); |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 233 | mContext.unbindService(mOverviewServiceConnection); |
| 234 | mOverviewProxy = null; |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 235 | notifyConnectionChanged(); |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 236 | } |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 237 | } |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 238 | |
| 239 | private void notifyConnectionChanged() { |
| 240 | for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) { |
| 241 | mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null); |
| 242 | } |
| 243 | } |
| 244 | |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 245 | private void notifyRecentsAnimationStarted() { |
| 246 | for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) { |
| 247 | mConnectionCallbacks.get(i).onRecentsAnimationStarted(); |
| 248 | } |
| 249 | } |
| 250 | |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 251 | @Override |
| 252 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 253 | pw.println(TAG_OPS + " state:"); |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 254 | pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts); |
| 255 | pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController |
| 256 | .isCurrentUserSetup()); |
| 257 | pw.print(" isConnected="); pw.println(mOverviewProxy != null); |
| 258 | } |
| 259 | |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 260 | public interface OverviewProxyListener { |
Winson Chung | cbb15a9 | 2018-01-25 17:46:16 +0000 | [diff] [blame] | 261 | default void onConnectionChanged(boolean isConnected) {} |
| 262 | default void onRecentsAnimationStarted() {} |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 263 | } |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 264 | } |