blob: 8e5984244476c544bce63c48c421defa2bdaa9a4 [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
Matthew Ngc1a97b12018-03-28 14:02:00 -070055import static com.android.systemui.shared.system.NavigationBarCompat.FLAG_DISABLE_SWIPE_UP;
Matthew Ng8f25fb962018-01-16 17:17:24 -080056import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType;
57
Matthew Ng13dbf872017-10-27 11:02:14 -070058/**
59 * Class to send information from overview to launcher with a binder.
60 */
Matthew Ng1e43ebd2017-11-14 14:47:05 -080061public class OverviewProxyService implements CallbackController<OverviewProxyListener>, Dumpable {
Matthew Ng13dbf872017-10-27 11:02:14 -070062
Sunny Goyal09c12d72018-03-27 10:14:49 -070063 private static final String ACTION_QUICKSTEP = "android.intent.action.QUICKSTEP_SERVICE";
64
Matthew Ngbd824572018-01-17 16:25:56 -080065 public static final String TAG_OPS = "OverviewProxyService";
66 public static final boolean DEBUG_OVERVIEW_PROXY = false;
Matthew Ng13dbf872017-10-27 11:02:14 -070067 private static final long BACKOFF_MILLIS = 5000;
68
69 private final Context mContext;
70 private final Handler mHandler;
Matthew Ngfac87832017-11-10 11:27:29 -080071 private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser;
Sunny Goyal09c12d72018-03-27 10:14:49 -070072 private final ComponentName mRecentsComponentName;
Matthew Ng13dbf872017-10-27 11:02:14 -070073 private final DeviceProvisionedController mDeviceProvisionedController
74 = Dependency.get(DeviceProvisionedController.class);
Matthew Ng7d05e772017-11-09 14:41:07 -080075 private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>();
Winson Chungd95a2252018-04-04 17:02:29 +000076 private final Intent mQuickStepIntent;
Matthew Ng13dbf872017-10-27 11:02:14 -070077
78 private IOverviewProxy mOverviewProxy;
79 private int mConnectionBackoffAttempts;
Tony Wickham05c1f852018-02-06 12:32:54 -080080 private CharSequence mOnboardingText;
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
Tony Wickham05c1f852018-02-06 12:32:54 -0800121 public void setRecentsOnboardingText(CharSequence text) {
122 mOnboardingText = text;
123 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800124
125 public void setInteractionState(@InteractionType int flags) {
126 long token = Binder.clearCallingIdentity();
127 try {
128 if (mInteractionFlags != flags) {
129 mInteractionFlags = flags;
130 mHandler.post(() -> {
131 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
132 mConnectionCallbacks.get(i).onInteractionFlagsChanged(flags);
133 }
134 });
135 }
136 } finally {
Winson Chungd95a2252018-04-04 17:02:29 +0000137 Prefs.putInt(mContext, Prefs.Key.QUICK_STEP_INTERACTION_FLAGS, mInteractionFlags);
Matthew Ng8f25fb962018-01-16 17:17:24 -0800138 Binder.restoreCallingIdentity(token);
139 }
140 }
Winson Chung38d31c22017-11-08 14:32:32 -0800141 };
142
Winson Chungd95a2252018-04-04 17:02:29 +0000143 private final BroadcastReceiver mLauncherStateChangedReceiver = new BroadcastReceiver() {
Matthew Ng30c0a022017-11-10 14:06:29 -0800144 @Override
145 public void onReceive(Context context, Intent intent) {
Winson Chungd95a2252018-04-04 17:02:29 +0000146 updateEnabledState();
147
148 // When launcher service is disabled, reset interaction flags because it is inactive
149 if (!isEnabled()) {
150 mInteractionFlags = 0;
151 Prefs.remove(mContext, Prefs.Key.QUICK_STEP_INTERACTION_FLAGS);
152 }
153
Matthew Ng30c0a022017-11-10 14:06:29 -0800154 // Reconnect immediately, instead of waiting for resume to arrive.
155 startConnectionToCurrentUser();
156 }
157 };
158
Matthew Ng13dbf872017-10-27 11:02:14 -0700159 private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() {
160 @Override
161 public void onServiceConnected(ComponentName name, IBinder service) {
162 if (service != null) {
163 mConnectionBackoffAttempts = 0;
164 mOverviewProxy = IOverviewProxy.Stub.asInterface(service);
165 // Listen for launcher's death
166 try {
167 service.linkToDeath(mOverviewServiceDeathRcpt, 0);
168 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800169 Log.e(TAG_OPS, "Lost connection to launcher service", e);
Matthew Ng13dbf872017-10-27 11:02:14 -0700170 }
Winson Chung38d31c22017-11-08 14:32:32 -0800171 try {
172 mOverviewProxy.onBind(mSysUiProxy);
173 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800174 Log.e(TAG_OPS, "Failed to call onBind()", e);
Winson Chung38d31c22017-11-08 14:32:32 -0800175 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800176 notifyConnectionChanged();
Matthew Ng13dbf872017-10-27 11:02:14 -0700177 }
178 }
179
180 @Override
181 public void onServiceDisconnected(ComponentName name) {
182 // Do nothing
183 }
184 };
185
186 private final DeviceProvisionedListener mDeviceProvisionedCallback =
187 new DeviceProvisionedListener() {
188 @Override
Matthew Ngdfab86c2017-11-07 15:46:51 -0800189 public void onUserSetupChanged() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700190 if (mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ngfac87832017-11-10 11:27:29 -0800191 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700192 }
193 }
194
195 @Override
196 public void onUserSwitched() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700197 mConnectionBackoffAttempts = 0;
Matthew Ngfac87832017-11-10 11:27:29 -0800198 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700199 }
200 };
201
202 // This is the death handler for the binder from the launcher service
Matthew Ng30c0a022017-11-10 14:06:29 -0800203 private final IBinder.DeathRecipient mOverviewServiceDeathRcpt
204 = this::startConnectionToCurrentUser;
Matthew Ng13dbf872017-10-27 11:02:14 -0700205
206 public OverviewProxyService(Context context) {
207 mContext = context;
208 mHandler = new Handler();
209 mConnectionBackoffAttempts = 0;
Sunny Goyal09c12d72018-03-27 10:14:49 -0700210 mRecentsComponentName = ComponentName.unflattenFromString(context.getString(
211 com.android.internal.R.string.config_recentsComponentName));
Winson Chungd95a2252018-04-04 17:02:29 +0000212 mQuickStepIntent = new Intent(ACTION_QUICKSTEP)
213 .setPackage(mRecentsComponentName.getPackageName());
214 mInteractionFlags = Prefs.getInt(mContext, Prefs.Key.QUICK_STEP_INTERACTION_FLAGS, 0);
Matthew Ng30c0a022017-11-10 14:06:29 -0800215
216 // Listen for the package update changes.
Matthew Ng1b1d3462018-03-02 11:43:38 -0800217 if (SystemServicesProxy.getInstance(context)
218 .isSystemUser(mDeviceProvisionedController.getCurrentUser())) {
Winson Chungd95a2252018-04-04 17:02:29 +0000219 updateEnabledState();
Matthew Ng1b1d3462018-03-02 11:43:38 -0800220 mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback);
221 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
222 filter.addDataScheme("package");
Sunny Goyal09c12d72018-03-27 10:14:49 -0700223 filter.addDataSchemeSpecificPart(mRecentsComponentName.getPackageName(),
Matthew Ng1b1d3462018-03-02 11:43:38 -0800224 PatternMatcher.PATTERN_LITERAL);
225 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Winson Chungd95a2252018-04-04 17:02:29 +0000226 mContext.registerReceiver(mLauncherStateChangedReceiver, filter);
Matthew Ng1b1d3462018-03-02 11:43:38 -0800227 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700228 }
229
230 public void startConnectionToCurrentUser() {
Matthew Ngfac87832017-11-10 11:27:29 -0800231 if (mHandler.getLooper() != Looper.myLooper()) {
232 mHandler.post(mConnectionRunnable);
233 } else {
234 internalConnectToCurrentUser();
235 }
236 }
237
238 private void internalConnectToCurrentUser() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800239 disconnectFromLauncherService();
240
Matthew Ng13dbf872017-10-27 11:02:14 -0700241 // If user has not setup yet or already connected, do not try to connect
Winson Chungd95a2252018-04-04 17:02:29 +0000242 if (!mDeviceProvisionedController.isCurrentUserSetup() || !isEnabled()) {
Matthew Ng13dbf872017-10-27 11:02:14 -0700243 return;
244 }
245 mHandler.removeCallbacks(mConnectionRunnable);
Sunny Goyal09c12d72018-03-27 10:14:49 -0700246 Intent launcherServiceIntent = new Intent(ACTION_QUICKSTEP)
247 .setPackage(mRecentsComponentName.getPackageName());
Matthew Ng10b6c41a2018-03-26 18:01:37 -0700248 boolean bound = false;
249 try {
250 bound = mContext.bindServiceAsUser(launcherServiceIntent,
251 mOverviewServiceConnection, Context.BIND_AUTO_CREATE,
252 UserHandle.of(mDeviceProvisionedController.getCurrentUser()));
253 } catch (SecurityException e) {
254 Log.e(TAG_OPS, "Unable to bind because of security error", e);
255 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700256 if (!bound) {
257 // Retry after exponential backoff timeout
258 final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts);
259 mHandler.postDelayed(mConnectionRunnable, timeoutMs);
260 mConnectionBackoffAttempts++;
261 }
262 }
263
Matthew Ng7d05e772017-11-09 14:41:07 -0800264 @Override
265 public void addCallback(OverviewProxyListener listener) {
266 mConnectionCallbacks.add(listener);
267 listener.onConnectionChanged(mOverviewProxy != null);
Winson Chungd95a2252018-04-04 17:02:29 +0000268 listener.onInteractionFlagsChanged(mInteractionFlags);
Matthew Ng7d05e772017-11-09 14:41:07 -0800269 }
270
271 @Override
272 public void removeCallback(OverviewProxyListener listener) {
273 mConnectionCallbacks.remove(listener);
274 }
275
Matthew Ngc1a97b12018-03-28 14:02:00 -0700276 public boolean shouldShowSwipeUpUI() {
Winson Chungd95a2252018-04-04 17:02:29 +0000277 return isEnabled() && ((mInteractionFlags & FLAG_DISABLE_SWIPE_UP) == 0);
278 }
279
280 public boolean isEnabled() {
281 return mIsEnabled;
Matthew Ngc1a97b12018-03-28 14:02:00 -0700282 }
283
Matthew Ng13dbf872017-10-27 11:02:14 -0700284 public IOverviewProxy getProxy() {
285 return mOverviewProxy;
286 }
287
Tony Wickham05c1f852018-02-06 12:32:54 -0800288 public CharSequence getOnboardingText() {
289 return mOnboardingText;
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800290 }
291
Matthew Ng8f25fb962018-01-16 17:17:24 -0800292 public int getInteractionFlags() {
293 return mInteractionFlags;
294 }
295
Matthew Ng13dbf872017-10-27 11:02:14 -0700296 private void disconnectFromLauncherService() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800297 if (mOverviewProxy != null) {
Matthew Ngeb6893b2017-11-09 17:15:33 -0800298 mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0);
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800299 mContext.unbindService(mOverviewServiceConnection);
300 mOverviewProxy = null;
Matthew Ng7d05e772017-11-09 14:41:07 -0800301 notifyConnectionChanged();
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800302 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700303 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800304
305 private void notifyConnectionChanged() {
306 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
307 mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null);
308 }
309 }
310
Matthew Ng2ea93b72018-03-14 19:43:18 +0000311 public void notifyQuickStepStarted() {
Winson Chungcbb15a92018-01-25 17:46:16 +0000312 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000313 mConnectionCallbacks.get(i).onQuickStepStarted();
Winson Chungcbb15a92018-01-25 17:46:16 +0000314 }
315 }
316
Winson Chungd95a2252018-04-04 17:02:29 +0000317 private void updateEnabledState() {
318 mIsEnabled = mContext.getPackageManager().resolveServiceAsUser(mQuickStepIntent, 0,
319 ActivityManagerWrapper.getInstance().getCurrentUserId()) != null;
320 }
321
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800322 @Override
323 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Matthew Ngbd824572018-01-17 16:25:56 -0800324 pw.println(TAG_OPS + " state:");
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800325 pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts);
326 pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController
327 .isCurrentUserSetup());
328 pw.print(" isConnected="); pw.println(mOverviewProxy != null);
329 }
330
Matthew Ng7d05e772017-11-09 14:41:07 -0800331 public interface OverviewProxyListener {
Winson Chungcbb15a92018-01-25 17:46:16 +0000332 default void onConnectionChanged(boolean isConnected) {}
Matthew Ng2ea93b72018-03-14 19:43:18 +0000333 default void onQuickStepStarted() {}
Matthew Ng8f25fb962018-01-16 17:17:24 -0800334 default void onInteractionFlagsChanged(@InteractionType int flags) {}
Matthew Ng7d05e772017-11-09 14:41:07 -0800335 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700336}