blob: a4af6b26a8548abb904931ef26b606725ca55e5d [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
Matthew Ngbd824572018-01-17 16:25:56 -080060 public static final String TAG_OPS = "OverviewProxyService";
61 public static final boolean DEBUG_OVERVIEW_PROXY = false;
Matthew Ng13dbf872017-10-27 11:02:14 -070062 private static final long BACKOFF_MILLIS = 5000;
63
64 private final Context mContext;
65 private final Handler mHandler;
Matthew Ngfac87832017-11-10 11:27:29 -080066 private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser;
Matthew Ng30c0a022017-11-10 14:06:29 -080067 private final ComponentName mLauncherComponentName;
Matthew Ng13dbf872017-10-27 11:02:14 -070068 private final DeviceProvisionedController mDeviceProvisionedController
69 = Dependency.get(DeviceProvisionedController.class);
Matthew Ng7d05e772017-11-09 14:41:07 -080070 private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>();
Matthew Ng13dbf872017-10-27 11:02:14 -070071
72 private IOverviewProxy mOverviewProxy;
73 private int mConnectionBackoffAttempts;
Tony Wickham05c1f852018-02-06 12:32:54 -080074 private CharSequence mOnboardingText;
Matthew Ng8f25fb962018-01-16 17:17:24 -080075 private @InteractionType int mInteractionFlags;
Matthew Ng13dbf872017-10-27 11:02:14 -070076
Winson Chung38d31c22017-11-08 14:32:32 -080077 private ISystemUiProxy mSysUiProxy = new ISystemUiProxy.Stub() {
Winson Chungcaf2b812018-01-26 10:29:46 -080078
Winson Chung11f53e92017-11-13 17:45:12 -080079 public GraphicBufferCompat screenshot(Rect sourceCrop, int width, int height, int minLayer,
80 int maxLayer, boolean useIdentityTransform, int rotation) {
Winson Chung38d31c22017-11-08 14:32:32 -080081 long token = Binder.clearCallingIdentity();
82 try {
Winson Chung11f53e92017-11-13 17:45:12 -080083 return new GraphicBufferCompat(SurfaceControl.screenshotToBuffer(sourceCrop, width,
84 height, minLayer, maxLayer, useIdentityTransform, rotation));
Winson Chung38d31c22017-11-08 14:32:32 -080085 } finally {
86 Binder.restoreCallingIdentity(token);
87 }
88 }
Winson Chungcbb15a92018-01-25 17:46:16 +000089
Winson Chungcaf2b812018-01-26 10:29:46 -080090 public void startScreenPinning(int taskId) {
91 long token = Binder.clearCallingIdentity();
92 try {
93 mHandler.post(() -> {
94 StatusBar statusBar = ((SystemUIApplication) mContext).getComponent(
95 StatusBar.class);
96 if (statusBar != null) {
97 statusBar.showScreenPinningRequest(taskId, false /* allowCancel */);
98 }
99 });
100 } finally {
101 Binder.restoreCallingIdentity(token);
102 }
103 }
104
Winson Chungc1674272018-02-21 10:15:17 -0800105 public void onSplitScreenInvoked() {
106 long token = Binder.clearCallingIdentity();
107 try {
108 EventBus.getDefault().post(new DockedFirstAnimationFrameEvent());
109 } finally {
110 Binder.restoreCallingIdentity(token);
111 }
112 }
113
Tony Wickham05c1f852018-02-06 12:32:54 -0800114 public void setRecentsOnboardingText(CharSequence text) {
115 mOnboardingText = text;
116 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800117
118 public void setInteractionState(@InteractionType int flags) {
119 long token = Binder.clearCallingIdentity();
120 try {
121 if (mInteractionFlags != flags) {
122 mInteractionFlags = flags;
123 mHandler.post(() -> {
124 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
125 mConnectionCallbacks.get(i).onInteractionFlagsChanged(flags);
126 }
127 });
128 }
129 } finally {
130 Binder.restoreCallingIdentity(token);
131 }
132 }
Winson Chung38d31c22017-11-08 14:32:32 -0800133 };
134
Matthew Ng30c0a022017-11-10 14:06:29 -0800135 private final BroadcastReceiver mLauncherAddedReceiver = new BroadcastReceiver() {
136 @Override
137 public void onReceive(Context context, Intent intent) {
138 // Reconnect immediately, instead of waiting for resume to arrive.
139 startConnectionToCurrentUser();
140 }
141 };
142
Matthew Ng13dbf872017-10-27 11:02:14 -0700143 private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() {
144 @Override
145 public void onServiceConnected(ComponentName name, IBinder service) {
146 if (service != null) {
147 mConnectionBackoffAttempts = 0;
148 mOverviewProxy = IOverviewProxy.Stub.asInterface(service);
149 // Listen for launcher's death
150 try {
151 service.linkToDeath(mOverviewServiceDeathRcpt, 0);
152 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800153 Log.e(TAG_OPS, "Lost connection to launcher service", e);
Matthew Ng13dbf872017-10-27 11:02:14 -0700154 }
Winson Chung38d31c22017-11-08 14:32:32 -0800155 try {
156 mOverviewProxy.onBind(mSysUiProxy);
157 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800158 Log.e(TAG_OPS, "Failed to call onBind()", e);
Winson Chung38d31c22017-11-08 14:32:32 -0800159 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800160 notifyConnectionChanged();
Matthew Ng13dbf872017-10-27 11:02:14 -0700161 }
162 }
163
164 @Override
165 public void onServiceDisconnected(ComponentName name) {
166 // Do nothing
167 }
168 };
169
170 private final DeviceProvisionedListener mDeviceProvisionedCallback =
171 new DeviceProvisionedListener() {
172 @Override
Matthew Ngdfab86c2017-11-07 15:46:51 -0800173 public void onUserSetupChanged() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700174 if (mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ngfac87832017-11-10 11:27:29 -0800175 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700176 }
177 }
178
179 @Override
180 public void onUserSwitched() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700181 mConnectionBackoffAttempts = 0;
Matthew Ngfac87832017-11-10 11:27:29 -0800182 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700183 }
184 };
185
186 // This is the death handler for the binder from the launcher service
Matthew Ng30c0a022017-11-10 14:06:29 -0800187 private final IBinder.DeathRecipient mOverviewServiceDeathRcpt
188 = this::startConnectionToCurrentUser;
Matthew Ng13dbf872017-10-27 11:02:14 -0700189
190 public OverviewProxyService(Context context) {
191 mContext = context;
192 mHandler = new Handler();
193 mConnectionBackoffAttempts = 0;
Matthew Ng30c0a022017-11-10 14:06:29 -0800194 mLauncherComponentName = ComponentName
195 .unflattenFromString(context.getString(R.string.config_overviewServiceComponent));
Matthew Ng30c0a022017-11-10 14:06:29 -0800196
197 // Listen for the package update changes.
Matthew Ng1b1d3462018-03-02 11:43:38 -0800198 if (SystemServicesProxy.getInstance(context)
199 .isSystemUser(mDeviceProvisionedController.getCurrentUser())) {
200 mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback);
201 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
202 filter.addDataScheme("package");
203 filter.addDataSchemeSpecificPart(mLauncherComponentName.getPackageName(),
204 PatternMatcher.PATTERN_LITERAL);
205 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
206 mContext.registerReceiver(mLauncherAddedReceiver, filter);
207 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700208 }
209
210 public void startConnectionToCurrentUser() {
Matthew Ngfac87832017-11-10 11:27:29 -0800211 if (mHandler.getLooper() != Looper.myLooper()) {
212 mHandler.post(mConnectionRunnable);
213 } else {
214 internalConnectToCurrentUser();
215 }
216 }
217
218 private void internalConnectToCurrentUser() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800219 disconnectFromLauncherService();
220
Matthew Ng13dbf872017-10-27 11:02:14 -0700221 // If user has not setup yet or already connected, do not try to connect
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800222 if (!mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ng13dbf872017-10-27 11:02:14 -0700223 return;
224 }
225 mHandler.removeCallbacks(mConnectionRunnable);
226 Intent launcherServiceIntent = new Intent();
Matthew Ng30c0a022017-11-10 14:06:29 -0800227 launcherServiceIntent.setComponent(mLauncherComponentName);
Matthew Ng13dbf872017-10-27 11:02:14 -0700228 boolean bound = mContext.bindServiceAsUser(launcherServiceIntent,
229 mOverviewServiceConnection, Context.BIND_AUTO_CREATE,
Matthew Ng09e6fbd2018-02-01 16:54:16 -0800230 UserHandle.of(mDeviceProvisionedController.getCurrentUser()));
Matthew Ng13dbf872017-10-27 11:02:14 -0700231 if (!bound) {
232 // Retry after exponential backoff timeout
233 final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts);
234 mHandler.postDelayed(mConnectionRunnable, timeoutMs);
235 mConnectionBackoffAttempts++;
236 }
237 }
238
Matthew Ng7d05e772017-11-09 14:41:07 -0800239 @Override
240 public void addCallback(OverviewProxyListener listener) {
241 mConnectionCallbacks.add(listener);
242 listener.onConnectionChanged(mOverviewProxy != null);
243 }
244
245 @Override
246 public void removeCallback(OverviewProxyListener listener) {
247 mConnectionCallbacks.remove(listener);
248 }
249
Matthew Ng13dbf872017-10-27 11:02:14 -0700250 public IOverviewProxy getProxy() {
251 return mOverviewProxy;
252 }
253
Tony Wickham05c1f852018-02-06 12:32:54 -0800254 public CharSequence getOnboardingText() {
255 return mOnboardingText;
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800256 }
257
Matthew Ng8f25fb962018-01-16 17:17:24 -0800258 public int getInteractionFlags() {
259 return mInteractionFlags;
260 }
261
Matthew Ng13dbf872017-10-27 11:02:14 -0700262 private void disconnectFromLauncherService() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800263 if (mOverviewProxy != null) {
Matthew Ngeb6893b2017-11-09 17:15:33 -0800264 mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0);
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800265 mContext.unbindService(mOverviewServiceConnection);
266 mOverviewProxy = null;
Matthew Ng7d05e772017-11-09 14:41:07 -0800267 notifyConnectionChanged();
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800268 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700269 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800270
271 private void notifyConnectionChanged() {
272 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
273 mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null);
274 }
275 }
276
Matthew Ng2ea93b72018-03-14 19:43:18 +0000277 public void notifyQuickStepStarted() {
Winson Chungcbb15a92018-01-25 17:46:16 +0000278 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
Matthew Ng2ea93b72018-03-14 19:43:18 +0000279 mConnectionCallbacks.get(i).onQuickStepStarted();
Winson Chungcbb15a92018-01-25 17:46:16 +0000280 }
281 }
282
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800283 @Override
284 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Matthew Ngbd824572018-01-17 16:25:56 -0800285 pw.println(TAG_OPS + " state:");
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800286 pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts);
287 pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController
288 .isCurrentUserSetup());
289 pw.print(" isConnected="); pw.println(mOverviewProxy != null);
290 }
291
Matthew Ng7d05e772017-11-09 14:41:07 -0800292 public interface OverviewProxyListener {
Winson Chungcbb15a92018-01-25 17:46:16 +0000293 default void onConnectionChanged(boolean isConnected) {}
Matthew Ng2ea93b72018-03-14 19:43:18 +0000294 default void onQuickStepStarted() {}
Matthew Ng8f25fb962018-01-16 17:17:24 -0800295 default void onInteractionFlagsChanged(@InteractionType int flags) {}
Matthew Ng7d05e772017-11-09 14:41:07 -0800296 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700297}