blob: 3443334e833a0b1741973e91de16169254de93b3 [file] [log] [blame]
Matthew Ng13dbf872017-10-27 11:02:14 -07001/*
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
17package com.android.systemui;
18
Matthew Ng30c0a022017-11-10 14:06:29 -080019import android.content.BroadcastReceiver;
Matthew Ng13dbf872017-10-27 11:02:14 -070020import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
Matthew Ng30c0a022017-11-10 14:06:29 -080023import android.content.IntentFilter;
Matthew Ng13dbf872017-10-27 11:02:14 -070024import android.content.ServiceConnection;
Winson Chungd95a2252018-04-04 17:02:29 +000025import android.content.pm.PackageManager;
Winson Chung38d31c22017-11-08 14:32:32 -080026import android.graphics.Rect;
27import android.os.Binder;
Matthew Ng13dbf872017-10-27 11:02:14 -070028import android.os.Handler;
29import android.os.IBinder;
Matthew Ngfac87832017-11-10 11:27:29 -080030import android.os.Looper;
Matthew Ng30c0a022017-11-10 14:06:29 -080031import android.os.PatternMatcher;
Matthew Ng13dbf872017-10-27 11:02:14 -070032import android.os.RemoteException;
33import android.os.UserHandle;
34import android.util.Log;
Winson Chung38d31c22017-11-08 14:32:32 -080035import android.view.SurfaceControl;
36
Winson Chung11f53e92017-11-13 17:45:12 -080037import com.android.systemui.OverviewProxyService.OverviewProxyListener;
Winson Chungc1674272018-02-21 10:15:17 -080038import com.android.systemui.recents.events.EventBus;
39import com.android.systemui.recents.events.activity.DockedFirstAnimationFrameEvent;
Matthew Ng1b1d3462018-03-02 11:43:38 -080040import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung38d31c22017-11-08 14:32:32 -080041import com.android.systemui.shared.recents.IOverviewProxy;
42import com.android.systemui.shared.recents.ISystemUiProxy;
Winson Chungd95a2252018-04-04 17:02:29 +000043import com.android.systemui.shared.system.ActivityManagerWrapper;
Winson Chung11f53e92017-11-13 17:45:12 -080044import com.android.systemui.shared.system.GraphicBufferCompat;
Winson Chungcaf2b812018-01-26 10:29:46 -080045import com.android.systemui.statusbar.phone.StatusBar;
Matthew Ng7d05e772017-11-09 14:41:07 -080046import com.android.systemui.statusbar.policy.CallbackController;
Matthew Ng13dbf872017-10-27 11:02:14 -070047import com.android.systemui.statusbar.policy.DeviceProvisionedController;
48import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener;
Winson Chung11f53e92017-11-13 17:45:12 -080049
Matthew Ng1e43ebd2017-11-14 14:47:05 -080050import java.io.FileDescriptor;
51import java.io.PrintWriter;
Matthew Ng7d05e772017-11-09 14:41:07 -080052import java.util.ArrayList;
53import java.util.List;
Matthew Ng13dbf872017-10-27 11:02:14 -070054
Winson Chung6e4b0b562018-04-04 09:13:37 -070055import static android.content.pm.PackageManager.MATCH_DIRECT_BOOT_UNAWARE;
Matthew Ngc1a97b12018-03-28 14:02:00 -070056import static com.android.systemui.shared.system.NavigationBarCompat.FLAG_DISABLE_SWIPE_UP;
Matthew Ng8f25fb962018-01-16 17:17:24 -080057import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType;
58
Matthew Ng13dbf872017-10-27 11:02:14 -070059/**
60 * Class to send information from overview to launcher with a binder.
61 */
Matthew Ng1e43ebd2017-11-14 14:47:05 -080062public class OverviewProxyService implements CallbackController<OverviewProxyListener>, Dumpable {
Matthew Ng13dbf872017-10-27 11:02:14 -070063
Sunny Goyal09c12d72018-03-27 10:14:49 -070064 private static final String ACTION_QUICKSTEP = "android.intent.action.QUICKSTEP_SERVICE";
65
Matthew Ngbd824572018-01-17 16:25:56 -080066 public static final String TAG_OPS = "OverviewProxyService";
67 public static final boolean DEBUG_OVERVIEW_PROXY = false;
Matthew Ng13dbf872017-10-27 11:02:14 -070068 private static final long BACKOFF_MILLIS = 5000;
69
70 private final Context mContext;
71 private final Handler mHandler;
Matthew Ngfac87832017-11-10 11:27:29 -080072 private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser;
Sunny Goyal09c12d72018-03-27 10:14:49 -070073 private final ComponentName mRecentsComponentName;
Matthew Ng13dbf872017-10-27 11:02:14 -070074 private final DeviceProvisionedController mDeviceProvisionedController
75 = Dependency.get(DeviceProvisionedController.class);
Matthew Ng7d05e772017-11-09 14:41:07 -080076 private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>();
Winson Chungd95a2252018-04-04 17:02:29 +000077 private final Intent mQuickStepIntent;
Matthew Ng13dbf872017-10-27 11:02:14 -070078
79 private IOverviewProxy mOverviewProxy;
80 private int mConnectionBackoffAttempts;
Matthew Ng8f25fb962018-01-16 17:17:24 -080081 private @InteractionType int mInteractionFlags;
Winson Chungd95a2252018-04-04 17:02:29 +000082 private boolean mIsEnabled;
Matthew Ng13dbf872017-10-27 11:02:14 -070083
Winson Chung38d31c22017-11-08 14:32:32 -080084 private ISystemUiProxy mSysUiProxy = new ISystemUiProxy.Stub() {
Winson Chungcaf2b812018-01-26 10:29:46 -080085
Winson Chung11f53e92017-11-13 17:45:12 -080086 public GraphicBufferCompat screenshot(Rect sourceCrop, int width, int height, int minLayer,
87 int maxLayer, boolean useIdentityTransform, int rotation) {
Winson Chung38d31c22017-11-08 14:32:32 -080088 long token = Binder.clearCallingIdentity();
89 try {
Winson Chung11f53e92017-11-13 17:45:12 -080090 return new GraphicBufferCompat(SurfaceControl.screenshotToBuffer(sourceCrop, width,
91 height, minLayer, maxLayer, useIdentityTransform, rotation));
Winson Chung38d31c22017-11-08 14:32:32 -080092 } finally {
93 Binder.restoreCallingIdentity(token);
94 }
95 }
Winson Chungcbb15a92018-01-25 17:46:16 +000096
Winson Chungcaf2b812018-01-26 10:29:46 -080097 public void startScreenPinning(int taskId) {
98 long token = Binder.clearCallingIdentity();
99 try {
100 mHandler.post(() -> {
101 StatusBar statusBar = ((SystemUIApplication) mContext).getComponent(
102 StatusBar.class);
103 if (statusBar != null) {
104 statusBar.showScreenPinningRequest(taskId, false /* allowCancel */);
105 }
106 });
107 } finally {
108 Binder.restoreCallingIdentity(token);
109 }
110 }
111
Winson Chungc1674272018-02-21 10:15:17 -0800112 public void onSplitScreenInvoked() {
113 long token = Binder.clearCallingIdentity();
114 try {
115 EventBus.getDefault().post(new DockedFirstAnimationFrameEvent());
116 } finally {
117 Binder.restoreCallingIdentity(token);
118 }
119 }
120
Matthew Ng8f25fb962018-01-16 17:17:24 -0800121 public void setInteractionState(@InteractionType int flags) {
122 long token = Binder.clearCallingIdentity();
123 try {
124 if (mInteractionFlags != flags) {
125 mInteractionFlags = flags;
126 mHandler.post(() -> {
127 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
128 mConnectionCallbacks.get(i).onInteractionFlagsChanged(flags);
129 }
130 });
131 }
132 } finally {
Winson Chungd95a2252018-04-04 17:02:29 +0000133 Prefs.putInt(mContext, Prefs.Key.QUICK_STEP_INTERACTION_FLAGS, mInteractionFlags);
Matthew Ng8f25fb962018-01-16 17:17:24 -0800134 Binder.restoreCallingIdentity(token);
135 }
136 }
Winson Chung38d31c22017-11-08 14:32:32 -0800137 };
138
Winson Chungd95a2252018-04-04 17:02:29 +0000139 private final BroadcastReceiver mLauncherStateChangedReceiver = new BroadcastReceiver() {
Matthew Ng30c0a022017-11-10 14:06:29 -0800140 @Override
141 public void onReceive(Context context, Intent intent) {
Winson Chungd95a2252018-04-04 17:02:29 +0000142 updateEnabledState();
143
144 // When launcher service is disabled, reset interaction flags because it is inactive
145 if (!isEnabled()) {
146 mInteractionFlags = 0;
147 Prefs.remove(mContext, Prefs.Key.QUICK_STEP_INTERACTION_FLAGS);
148 }
149
Matthew Ng30c0a022017-11-10 14:06:29 -0800150 // Reconnect immediately, instead of waiting for resume to arrive.
151 startConnectionToCurrentUser();
152 }
153 };
154
Matthew Ng13dbf872017-10-27 11:02:14 -0700155 private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() {
156 @Override
157 public void onServiceConnected(ComponentName name, IBinder service) {
158 if (service != null) {
159 mConnectionBackoffAttempts = 0;
160 mOverviewProxy = IOverviewProxy.Stub.asInterface(service);
161 // Listen for launcher's death
162 try {
163 service.linkToDeath(mOverviewServiceDeathRcpt, 0);
164 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800165 Log.e(TAG_OPS, "Lost connection to launcher service", e);
Matthew Ng13dbf872017-10-27 11:02:14 -0700166 }
Winson Chung38d31c22017-11-08 14:32:32 -0800167 try {
168 mOverviewProxy.onBind(mSysUiProxy);
169 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800170 Log.e(TAG_OPS, "Failed to call onBind()", e);
Winson Chung38d31c22017-11-08 14:32:32 -0800171 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800172 notifyConnectionChanged();
Matthew Ng13dbf872017-10-27 11:02:14 -0700173 }
174 }
175
176 @Override
177 public void onServiceDisconnected(ComponentName name) {
178 // Do nothing
179 }
180 };
181
182 private final DeviceProvisionedListener mDeviceProvisionedCallback =
183 new DeviceProvisionedListener() {
184 @Override
Matthew Ngdfab86c2017-11-07 15:46:51 -0800185 public void onUserSetupChanged() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700186 if (mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ngfac87832017-11-10 11:27:29 -0800187 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700188 }
189 }
190
191 @Override
192 public void onUserSwitched() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700193 mConnectionBackoffAttempts = 0;
Matthew Ngfac87832017-11-10 11:27:29 -0800194 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700195 }
196 };
197
198 // This is the death handler for the binder from the launcher service
Matthew Ng30c0a022017-11-10 14:06:29 -0800199 private final IBinder.DeathRecipient mOverviewServiceDeathRcpt
200 = this::startConnectionToCurrentUser;
Matthew Ng13dbf872017-10-27 11:02:14 -0700201
202 public OverviewProxyService(Context context) {
203 mContext = context;
204 mHandler = new Handler();
205 mConnectionBackoffAttempts = 0;
Sunny Goyal09c12d72018-03-27 10:14:49 -0700206 mRecentsComponentName = ComponentName.unflattenFromString(context.getString(
207 com.android.internal.R.string.config_recentsComponentName));
Winson Chungd95a2252018-04-04 17:02:29 +0000208 mQuickStepIntent = new Intent(ACTION_QUICKSTEP)
209 .setPackage(mRecentsComponentName.getPackageName());
210 mInteractionFlags = Prefs.getInt(mContext, Prefs.Key.QUICK_STEP_INTERACTION_FLAGS, 0);
Matthew Ng30c0a022017-11-10 14:06:29 -0800211
212 // Listen for the package update changes.
Matthew Ng1b1d3462018-03-02 11:43:38 -0800213 if (SystemServicesProxy.getInstance(context)
214 .isSystemUser(mDeviceProvisionedController.getCurrentUser())) {
Winson Chungd95a2252018-04-04 17:02:29 +0000215 updateEnabledState();
Matthew Ng1b1d3462018-03-02 11:43:38 -0800216 mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback);
217 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
218 filter.addDataScheme("package");
Sunny Goyal09c12d72018-03-27 10:14:49 -0700219 filter.addDataSchemeSpecificPart(mRecentsComponentName.getPackageName(),
Matthew Ng1b1d3462018-03-02 11:43:38 -0800220 PatternMatcher.PATTERN_LITERAL);
221 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Winson Chungd95a2252018-04-04 17:02:29 +0000222 mContext.registerReceiver(mLauncherStateChangedReceiver, filter);
Matthew Ng1b1d3462018-03-02 11:43:38 -0800223 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700224 }
225
226 public void startConnectionToCurrentUser() {
Matthew Ngfac87832017-11-10 11:27:29 -0800227 if (mHandler.getLooper() != Looper.myLooper()) {
228 mHandler.post(mConnectionRunnable);
229 } else {
230 internalConnectToCurrentUser();
231 }
232 }
233
234 private void internalConnectToCurrentUser() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800235 disconnectFromLauncherService();
236
Matthew Ng13dbf872017-10-27 11:02:14 -0700237 // If user has not setup yet or already connected, do not try to connect
Winson Chungd95a2252018-04-04 17:02:29 +0000238 if (!mDeviceProvisionedController.isCurrentUserSetup() || !isEnabled()) {
Matthew Ng13dbf872017-10-27 11:02:14 -0700239 return;
240 }
241 mHandler.removeCallbacks(mConnectionRunnable);
Sunny Goyal09c12d72018-03-27 10:14:49 -0700242 Intent launcherServiceIntent = new Intent(ACTION_QUICKSTEP)
243 .setPackage(mRecentsComponentName.getPackageName());
Matthew Ng10b6c41a2018-03-26 18:01:37 -0700244 boolean bound = false;
245 try {
246 bound = mContext.bindServiceAsUser(launcherServiceIntent,
247 mOverviewServiceConnection, Context.BIND_AUTO_CREATE,
248 UserHandle.of(mDeviceProvisionedController.getCurrentUser()));
249 } catch (SecurityException e) {
250 Log.e(TAG_OPS, "Unable to bind because of security error", e);
251 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700252 if (!bound) {
253 // Retry after exponential backoff timeout
254 final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts);
255 mHandler.postDelayed(mConnectionRunnable, timeoutMs);
256 mConnectionBackoffAttempts++;
257 }
258 }
259
Matthew Ng7d05e772017-11-09 14:41:07 -0800260 @Override
261 public void addCallback(OverviewProxyListener listener) {
262 mConnectionCallbacks.add(listener);
263 listener.onConnectionChanged(mOverviewProxy != null);
Winson Chungd95a2252018-04-04 17:02:29 +0000264 listener.onInteractionFlagsChanged(mInteractionFlags);
Matthew Ng7d05e772017-11-09 14:41:07 -0800265 }
266
267 @Override
268 public void removeCallback(OverviewProxyListener listener) {
269 mConnectionCallbacks.remove(listener);
270 }
271
Matthew Ngc1a97b12018-03-28 14:02:00 -0700272 public boolean shouldShowSwipeUpUI() {
Winson Chungd95a2252018-04-04 17:02:29 +0000273 return isEnabled() && ((mInteractionFlags & FLAG_DISABLE_SWIPE_UP) == 0);
274 }
275
276 public boolean isEnabled() {
277 return mIsEnabled;
Matthew Ngc1a97b12018-03-28 14:02:00 -0700278 }
279
Matthew Ng13dbf872017-10-27 11:02:14 -0700280 public IOverviewProxy getProxy() {
281 return mOverviewProxy;
282 }
283
Matthew Ng8f25fb962018-01-16 17:17:24 -0800284 public int getInteractionFlags() {
285 return mInteractionFlags;
286 }
287
Matthew Ng13dbf872017-10-27 11:02:14 -0700288 private void disconnectFromLauncherService() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800289 if (mOverviewProxy != null) {
Matthew Ngeb6893b2017-11-09 17:15:33 -0800290 mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0);
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800291 mContext.unbindService(mOverviewServiceConnection);
292 mOverviewProxy = null;
Matthew Ng7d05e772017-11-09 14:41:07 -0800293 notifyConnectionChanged();
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800294 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700295 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800296
297 private void notifyConnectionChanged() {
298 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
299 mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null);
300 }
301 }
302
Matthew Ng2ea93b72018-03-14 19:43:18 +0000303 public void notifyQuickStepStarted() {
Winson Chungcbb15a92018-01-25 17:46:16 +0000304 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000305 mConnectionCallbacks.get(i).onQuickStepStarted();
Winson Chungcbb15a92018-01-25 17:46:16 +0000306 }
307 }
308
Winson Chungd95a2252018-04-04 17:02:29 +0000309 private void updateEnabledState() {
Winson Chung6e4b0b562018-04-04 09:13:37 -0700310 mIsEnabled = mContext.getPackageManager().resolveServiceAsUser(mQuickStepIntent,
311 MATCH_DIRECT_BOOT_UNAWARE,
Winson Chungd95a2252018-04-04 17:02:29 +0000312 ActivityManagerWrapper.getInstance().getCurrentUserId()) != null;
313 }
314
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800315 @Override
316 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Matthew Ngbd824572018-01-17 16:25:56 -0800317 pw.println(TAG_OPS + " state:");
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800318 pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts);
319 pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController
320 .isCurrentUserSetup());
321 pw.print(" isConnected="); pw.println(mOverviewProxy != null);
322 }
323
Matthew Ng7d05e772017-11-09 14:41:07 -0800324 public interface OverviewProxyListener {
Winson Chungcbb15a92018-01-25 17:46:16 +0000325 default void onConnectionChanged(boolean isConnected) {}
Matthew Ng2ea93b72018-03-14 19:43:18 +0000326 default void onQuickStepStarted() {}
Matthew Ng8f25fb962018-01-16 17:17:24 -0800327 default void onInteractionFlagsChanged(@InteractionType int flags) {}
Matthew Ng7d05e772017-11-09 14:41:07 -0800328 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700329}