blob: 90d1d948d44de69f351b82b766db11e9dd58b0bb [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 Chung38d31c22017-11-08 14:32:32 -080025import android.graphics.Rect;
26import android.os.Binder;
Matthew Ng13dbf872017-10-27 11:02:14 -070027import android.os.Handler;
28import android.os.IBinder;
Matthew Ngfac87832017-11-10 11:27:29 -080029import android.os.Looper;
Matthew Ng30c0a022017-11-10 14:06:29 -080030import android.os.PatternMatcher;
Matthew Ng13dbf872017-10-27 11:02:14 -070031import android.os.RemoteException;
32import android.os.UserHandle;
33import android.util.Log;
Winson Chung38d31c22017-11-08 14:32:32 -080034import android.view.SurfaceControl;
35
Winson Chung11f53e92017-11-13 17:45:12 -080036import com.android.systemui.OverviewProxyService.OverviewProxyListener;
Winson Chungc1674272018-02-21 10:15:17 -080037import com.android.systemui.recents.events.EventBus;
38import com.android.systemui.recents.events.activity.DockedFirstAnimationFrameEvent;
Matthew Ng1b1d3462018-03-02 11:43:38 -080039import com.android.systemui.recents.misc.SystemServicesProxy;
Winson Chung38d31c22017-11-08 14:32:32 -080040import com.android.systemui.shared.recents.IOverviewProxy;
41import com.android.systemui.shared.recents.ISystemUiProxy;
Winson Chung11f53e92017-11-13 17:45:12 -080042import com.android.systemui.shared.system.GraphicBufferCompat;
Winson Chungcaf2b812018-01-26 10:29:46 -080043import com.android.systemui.statusbar.phone.StatusBar;
Matthew Ng7d05e772017-11-09 14:41:07 -080044import com.android.systemui.statusbar.policy.CallbackController;
Matthew Ng13dbf872017-10-27 11:02:14 -070045import com.android.systemui.statusbar.policy.DeviceProvisionedController;
46import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener;
Winson Chung11f53e92017-11-13 17:45:12 -080047
Matthew Ng1e43ebd2017-11-14 14:47:05 -080048import java.io.FileDescriptor;
49import java.io.PrintWriter;
Matthew Ng7d05e772017-11-09 14:41:07 -080050import java.util.ArrayList;
51import java.util.List;
Matthew Ng13dbf872017-10-27 11:02:14 -070052
Matthew Ng8f25fb962018-01-16 17:17:24 -080053import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType;
54
Matthew Ng13dbf872017-10-27 11:02:14 -070055/**
56 * Class to send information from overview to launcher with a binder.
57 */
Matthew Ng1e43ebd2017-11-14 14:47:05 -080058public class OverviewProxyService implements CallbackController<OverviewProxyListener>, Dumpable {
Matthew Ng13dbf872017-10-27 11:02:14 -070059
Sunny Goyal09c12d72018-03-27 10:14:49 -070060 private static final String ACTION_QUICKSTEP = "android.intent.action.QUICKSTEP_SERVICE";
61
Matthew Ngbd824572018-01-17 16:25:56 -080062 public static final String TAG_OPS = "OverviewProxyService";
63 public static final boolean DEBUG_OVERVIEW_PROXY = false;
Matthew Ng13dbf872017-10-27 11:02:14 -070064 private static final long BACKOFF_MILLIS = 5000;
65
66 private final Context mContext;
67 private final Handler mHandler;
Matthew Ngfac87832017-11-10 11:27:29 -080068 private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser;
Sunny Goyal09c12d72018-03-27 10:14:49 -070069 private final ComponentName mRecentsComponentName;
Matthew Ng13dbf872017-10-27 11:02:14 -070070 private final DeviceProvisionedController mDeviceProvisionedController
71 = Dependency.get(DeviceProvisionedController.class);
Matthew Ng7d05e772017-11-09 14:41:07 -080072 private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>();
Matthew Ng13dbf872017-10-27 11:02:14 -070073
74 private IOverviewProxy mOverviewProxy;
75 private int mConnectionBackoffAttempts;
Tony Wickham05c1f852018-02-06 12:32:54 -080076 private CharSequence mOnboardingText;
Matthew Ng8f25fb962018-01-16 17:17:24 -080077 private @InteractionType int mInteractionFlags;
Matthew Ng13dbf872017-10-27 11:02:14 -070078
Winson Chung38d31c22017-11-08 14:32:32 -080079 private ISystemUiProxy mSysUiProxy = new ISystemUiProxy.Stub() {
Winson Chungcaf2b812018-01-26 10:29:46 -080080
Winson Chung11f53e92017-11-13 17:45:12 -080081 public GraphicBufferCompat screenshot(Rect sourceCrop, int width, int height, int minLayer,
82 int maxLayer, boolean useIdentityTransform, int rotation) {
Winson Chung38d31c22017-11-08 14:32:32 -080083 long token = Binder.clearCallingIdentity();
84 try {
Winson Chung11f53e92017-11-13 17:45:12 -080085 return new GraphicBufferCompat(SurfaceControl.screenshotToBuffer(sourceCrop, width,
86 height, minLayer, maxLayer, useIdentityTransform, rotation));
Winson Chung38d31c22017-11-08 14:32:32 -080087 } finally {
88 Binder.restoreCallingIdentity(token);
89 }
90 }
Winson Chungcbb15a92018-01-25 17:46:16 +000091
Winson Chungcaf2b812018-01-26 10:29:46 -080092 public void startScreenPinning(int taskId) {
93 long token = Binder.clearCallingIdentity();
94 try {
95 mHandler.post(() -> {
96 StatusBar statusBar = ((SystemUIApplication) mContext).getComponent(
97 StatusBar.class);
98 if (statusBar != null) {
99 statusBar.showScreenPinningRequest(taskId, false /* allowCancel */);
100 }
101 });
102 } finally {
103 Binder.restoreCallingIdentity(token);
104 }
105 }
106
Winson Chungc1674272018-02-21 10:15:17 -0800107 public void onSplitScreenInvoked() {
108 long token = Binder.clearCallingIdentity();
109 try {
110 EventBus.getDefault().post(new DockedFirstAnimationFrameEvent());
111 } finally {
112 Binder.restoreCallingIdentity(token);
113 }
114 }
115
Tony Wickham05c1f852018-02-06 12:32:54 -0800116 public void setRecentsOnboardingText(CharSequence text) {
117 mOnboardingText = text;
118 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800119
120 public void setInteractionState(@InteractionType int flags) {
121 long token = Binder.clearCallingIdentity();
122 try {
123 if (mInteractionFlags != flags) {
124 mInteractionFlags = flags;
125 mHandler.post(() -> {
126 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
127 mConnectionCallbacks.get(i).onInteractionFlagsChanged(flags);
128 }
129 });
130 }
131 } finally {
132 Binder.restoreCallingIdentity(token);
133 }
134 }
Winson Chung38d31c22017-11-08 14:32:32 -0800135 };
136
Matthew Ng30c0a022017-11-10 14:06:29 -0800137 private final BroadcastReceiver mLauncherAddedReceiver = new BroadcastReceiver() {
138 @Override
139 public void onReceive(Context context, Intent intent) {
140 // Reconnect immediately, instead of waiting for resume to arrive.
141 startConnectionToCurrentUser();
142 }
143 };
144
Matthew Ng13dbf872017-10-27 11:02:14 -0700145 private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() {
146 @Override
147 public void onServiceConnected(ComponentName name, IBinder service) {
148 if (service != null) {
149 mConnectionBackoffAttempts = 0;
150 mOverviewProxy = IOverviewProxy.Stub.asInterface(service);
151 // Listen for launcher's death
152 try {
153 service.linkToDeath(mOverviewServiceDeathRcpt, 0);
154 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800155 Log.e(TAG_OPS, "Lost connection to launcher service", e);
Matthew Ng13dbf872017-10-27 11:02:14 -0700156 }
Winson Chung38d31c22017-11-08 14:32:32 -0800157 try {
158 mOverviewProxy.onBind(mSysUiProxy);
159 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800160 Log.e(TAG_OPS, "Failed to call onBind()", e);
Winson Chung38d31c22017-11-08 14:32:32 -0800161 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800162 notifyConnectionChanged();
Matthew Ng13dbf872017-10-27 11:02:14 -0700163 }
164 }
165
166 @Override
167 public void onServiceDisconnected(ComponentName name) {
168 // Do nothing
169 }
170 };
171
172 private final DeviceProvisionedListener mDeviceProvisionedCallback =
173 new DeviceProvisionedListener() {
174 @Override
Matthew Ngdfab86c2017-11-07 15:46:51 -0800175 public void onUserSetupChanged() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700176 if (mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ngfac87832017-11-10 11:27:29 -0800177 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700178 }
179 }
180
181 @Override
182 public void onUserSwitched() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700183 mConnectionBackoffAttempts = 0;
Matthew Ngfac87832017-11-10 11:27:29 -0800184 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700185 }
186 };
187
188 // This is the death handler for the binder from the launcher service
Matthew Ng30c0a022017-11-10 14:06:29 -0800189 private final IBinder.DeathRecipient mOverviewServiceDeathRcpt
190 = this::startConnectionToCurrentUser;
Matthew Ng13dbf872017-10-27 11:02:14 -0700191
192 public OverviewProxyService(Context context) {
193 mContext = context;
194 mHandler = new Handler();
195 mConnectionBackoffAttempts = 0;
Sunny Goyal09c12d72018-03-27 10:14:49 -0700196 mRecentsComponentName = ComponentName.unflattenFromString(context.getString(
197 com.android.internal.R.string.config_recentsComponentName));
Matthew Ng30c0a022017-11-10 14:06:29 -0800198
199 // Listen for the package update changes.
Matthew Ng1b1d3462018-03-02 11:43:38 -0800200 if (SystemServicesProxy.getInstance(context)
201 .isSystemUser(mDeviceProvisionedController.getCurrentUser())) {
202 mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback);
203 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
204 filter.addDataScheme("package");
Sunny Goyal09c12d72018-03-27 10:14:49 -0700205 filter.addDataSchemeSpecificPart(mRecentsComponentName.getPackageName(),
Matthew Ng1b1d3462018-03-02 11:43:38 -0800206 PatternMatcher.PATTERN_LITERAL);
207 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
208 mContext.registerReceiver(mLauncherAddedReceiver, filter);
209 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700210 }
211
212 public void startConnectionToCurrentUser() {
Matthew Ngfac87832017-11-10 11:27:29 -0800213 if (mHandler.getLooper() != Looper.myLooper()) {
214 mHandler.post(mConnectionRunnable);
215 } else {
216 internalConnectToCurrentUser();
217 }
218 }
219
220 private void internalConnectToCurrentUser() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800221 disconnectFromLauncherService();
222
Matthew Ng13dbf872017-10-27 11:02:14 -0700223 // If user has not setup yet or already connected, do not try to connect
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800224 if (!mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ng13dbf872017-10-27 11:02:14 -0700225 return;
226 }
227 mHandler.removeCallbacks(mConnectionRunnable);
Sunny Goyal09c12d72018-03-27 10:14:49 -0700228 Intent launcherServiceIntent = new Intent(ACTION_QUICKSTEP)
229 .setPackage(mRecentsComponentName.getPackageName());
Matthew Ng10b6c41a2018-03-26 18:01:37 -0700230 boolean bound = false;
231 try {
232 bound = mContext.bindServiceAsUser(launcherServiceIntent,
233 mOverviewServiceConnection, Context.BIND_AUTO_CREATE,
234 UserHandle.of(mDeviceProvisionedController.getCurrentUser()));
235 } catch (SecurityException e) {
236 Log.e(TAG_OPS, "Unable to bind because of security error", e);
237 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700238 if (!bound) {
239 // Retry after exponential backoff timeout
240 final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts);
241 mHandler.postDelayed(mConnectionRunnable, timeoutMs);
242 mConnectionBackoffAttempts++;
243 }
244 }
245
Matthew Ng7d05e772017-11-09 14:41:07 -0800246 @Override
247 public void addCallback(OverviewProxyListener listener) {
248 mConnectionCallbacks.add(listener);
249 listener.onConnectionChanged(mOverviewProxy != null);
250 }
251
252 @Override
253 public void removeCallback(OverviewProxyListener listener) {
254 mConnectionCallbacks.remove(listener);
255 }
256
Matthew Ng13dbf872017-10-27 11:02:14 -0700257 public IOverviewProxy getProxy() {
258 return mOverviewProxy;
259 }
260
Tony Wickham05c1f852018-02-06 12:32:54 -0800261 public CharSequence getOnboardingText() {
262 return mOnboardingText;
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800263 }
264
Matthew Ng8f25fb962018-01-16 17:17:24 -0800265 public int getInteractionFlags() {
266 return mInteractionFlags;
267 }
268
Matthew Ng13dbf872017-10-27 11:02:14 -0700269 private void disconnectFromLauncherService() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800270 if (mOverviewProxy != null) {
Matthew Ngeb6893b2017-11-09 17:15:33 -0800271 mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0);
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800272 mContext.unbindService(mOverviewServiceConnection);
273 mOverviewProxy = null;
Matthew Ng7d05e772017-11-09 14:41:07 -0800274 notifyConnectionChanged();
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800275 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700276 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800277
278 private void notifyConnectionChanged() {
279 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
280 mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null);
281 }
282 }
283
Matthew Ng2ea93b72018-03-14 19:43:18 +0000284 public void notifyQuickStepStarted() {
Winson Chungcbb15a92018-01-25 17:46:16 +0000285 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000286 mConnectionCallbacks.get(i).onQuickStepStarted();
Winson Chungcbb15a92018-01-25 17:46:16 +0000287 }
288 }
289
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800290 @Override
291 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Matthew Ngbd824572018-01-17 16:25:56 -0800292 pw.println(TAG_OPS + " state:");
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800293 pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts);
294 pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController
295 .isCurrentUserSetup());
296 pw.print(" isConnected="); pw.println(mOverviewProxy != null);
297 }
298
Matthew Ng7d05e772017-11-09 14:41:07 -0800299 public interface OverviewProxyListener {
Winson Chungcbb15a92018-01-25 17:46:16 +0000300 default void onConnectionChanged(boolean isConnected) {}
Matthew Ng2ea93b72018-03-14 19:43:18 +0000301 default void onQuickStepStarted() {}
Matthew Ng8f25fb962018-01-16 17:17:24 -0800302 default void onInteractionFlagsChanged(@InteractionType int flags) {}
Matthew Ng7d05e772017-11-09 14:41:07 -0800303 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700304}