blob: 3c666e4b11cc8c9ae77cdd4eb4a19e7a9b81d6a6 [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;
Winson Chung38d31c22017-11-08 14:32:32 -080039import com.android.systemui.shared.recents.IOverviewProxy;
40import com.android.systemui.shared.recents.ISystemUiProxy;
Winson Chung11f53e92017-11-13 17:45:12 -080041import com.android.systemui.shared.system.GraphicBufferCompat;
Winson Chungcaf2b812018-01-26 10:29:46 -080042import com.android.systemui.statusbar.phone.StatusBar;
Matthew Ng7d05e772017-11-09 14:41:07 -080043import com.android.systemui.statusbar.policy.CallbackController;
Matthew Ng13dbf872017-10-27 11:02:14 -070044import com.android.systemui.statusbar.policy.DeviceProvisionedController;
45import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener;
Winson Chung11f53e92017-11-13 17:45:12 -080046
Matthew Ng1e43ebd2017-11-14 14:47:05 -080047import java.io.FileDescriptor;
48import java.io.PrintWriter;
Matthew Ng7d05e772017-11-09 14:41:07 -080049import java.util.ArrayList;
50import java.util.List;
Matthew Ng13dbf872017-10-27 11:02:14 -070051
Matthew Ng8f25fb962018-01-16 17:17:24 -080052import static com.android.systemui.shared.system.NavigationBarCompat.InteractionType;
53
Matthew Ng13dbf872017-10-27 11:02:14 -070054/**
55 * Class to send information from overview to launcher with a binder.
56 */
Matthew Ng1e43ebd2017-11-14 14:47:05 -080057public class OverviewProxyService implements CallbackController<OverviewProxyListener>, Dumpable {
Matthew Ng13dbf872017-10-27 11:02:14 -070058
Matthew Ngbd824572018-01-17 16:25:56 -080059 public static final String TAG_OPS = "OverviewProxyService";
60 public static final boolean DEBUG_OVERVIEW_PROXY = false;
Matthew Ng13dbf872017-10-27 11:02:14 -070061 private static final long BACKOFF_MILLIS = 5000;
62
63 private final Context mContext;
64 private final Handler mHandler;
Matthew Ngfac87832017-11-10 11:27:29 -080065 private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser;
Matthew Ng30c0a022017-11-10 14:06:29 -080066 private final ComponentName mLauncherComponentName;
Matthew Ng13dbf872017-10-27 11:02:14 -070067 private final DeviceProvisionedController mDeviceProvisionedController
68 = Dependency.get(DeviceProvisionedController.class);
Matthew Ng7d05e772017-11-09 14:41:07 -080069 private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>();
Matthew Ng13dbf872017-10-27 11:02:14 -070070
71 private IOverviewProxy mOverviewProxy;
72 private int mConnectionBackoffAttempts;
Tony Wickham05c1f852018-02-06 12:32:54 -080073 private CharSequence mOnboardingText;
Matthew Ng8f25fb962018-01-16 17:17:24 -080074 private @InteractionType int mInteractionFlags;
Matthew Ng13dbf872017-10-27 11:02:14 -070075
Winson Chung38d31c22017-11-08 14:32:32 -080076 private ISystemUiProxy mSysUiProxy = new ISystemUiProxy.Stub() {
Winson Chungcaf2b812018-01-26 10:29:46 -080077
Winson Chung11f53e92017-11-13 17:45:12 -080078 public GraphicBufferCompat screenshot(Rect sourceCrop, int width, int height, int minLayer,
79 int maxLayer, boolean useIdentityTransform, int rotation) {
Winson Chung38d31c22017-11-08 14:32:32 -080080 long token = Binder.clearCallingIdentity();
81 try {
Winson Chung11f53e92017-11-13 17:45:12 -080082 return new GraphicBufferCompat(SurfaceControl.screenshotToBuffer(sourceCrop, width,
83 height, minLayer, maxLayer, useIdentityTransform, rotation));
Winson Chung38d31c22017-11-08 14:32:32 -080084 } finally {
85 Binder.restoreCallingIdentity(token);
86 }
87 }
Winson Chungcbb15a92018-01-25 17:46:16 +000088
Winson Chungcaf2b812018-01-26 10:29:46 -080089 public void startScreenPinning(int taskId) {
90 long token = Binder.clearCallingIdentity();
91 try {
92 mHandler.post(() -> {
93 StatusBar statusBar = ((SystemUIApplication) mContext).getComponent(
94 StatusBar.class);
95 if (statusBar != null) {
96 statusBar.showScreenPinningRequest(taskId, false /* allowCancel */);
97 }
98 });
99 } finally {
100 Binder.restoreCallingIdentity(token);
101 }
102 }
103
Winson Chungcbb15a92018-01-25 17:46:16 +0000104 public void onRecentsAnimationStarted() {
105 long token = Binder.clearCallingIdentity();
106 try {
Winson Chung58bd4392018-02-08 17:53:26 -0800107 mHandler.post(OverviewProxyService.this::notifyRecentsAnimationStarted);
Winson Chungcbb15a92018-01-25 17:46:16 +0000108 } finally {
109 Binder.restoreCallingIdentity(token);
110 }
111 }
Tony Wickham05c1f852018-02-06 12:32:54 -0800112
Winson Chungc1674272018-02-21 10:15:17 -0800113 public void onSplitScreenInvoked() {
114 long token = Binder.clearCallingIdentity();
115 try {
116 EventBus.getDefault().post(new DockedFirstAnimationFrameEvent());
117 } finally {
118 Binder.restoreCallingIdentity(token);
119 }
120 }
121
Tony Wickham05c1f852018-02-06 12:32:54 -0800122 public void setRecentsOnboardingText(CharSequence text) {
123 mOnboardingText = text;
124 }
Matthew Ng8f25fb962018-01-16 17:17:24 -0800125
126 public void setInteractionState(@InteractionType int flags) {
127 long token = Binder.clearCallingIdentity();
128 try {
129 if (mInteractionFlags != flags) {
130 mInteractionFlags = flags;
131 mHandler.post(() -> {
132 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
133 mConnectionCallbacks.get(i).onInteractionFlagsChanged(flags);
134 }
135 });
136 }
137 } finally {
138 Binder.restoreCallingIdentity(token);
139 }
140 }
Winson Chung38d31c22017-11-08 14:32:32 -0800141 };
142
Matthew Ng30c0a022017-11-10 14:06:29 -0800143 private final BroadcastReceiver mLauncherAddedReceiver = new BroadcastReceiver() {
144 @Override
145 public void onReceive(Context context, Intent intent) {
146 // Reconnect immediately, instead of waiting for resume to arrive.
147 startConnectionToCurrentUser();
148 }
149 };
150
Matthew Ng13dbf872017-10-27 11:02:14 -0700151 private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() {
152 @Override
153 public void onServiceConnected(ComponentName name, IBinder service) {
154 if (service != null) {
155 mConnectionBackoffAttempts = 0;
156 mOverviewProxy = IOverviewProxy.Stub.asInterface(service);
157 // Listen for launcher's death
158 try {
159 service.linkToDeath(mOverviewServiceDeathRcpt, 0);
160 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800161 Log.e(TAG_OPS, "Lost connection to launcher service", e);
Matthew Ng13dbf872017-10-27 11:02:14 -0700162 }
Winson Chung38d31c22017-11-08 14:32:32 -0800163 try {
164 mOverviewProxy.onBind(mSysUiProxy);
165 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800166 Log.e(TAG_OPS, "Failed to call onBind()", e);
Winson Chung38d31c22017-11-08 14:32:32 -0800167 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800168 notifyConnectionChanged();
Matthew Ng13dbf872017-10-27 11:02:14 -0700169 }
170 }
171
172 @Override
173 public void onServiceDisconnected(ComponentName name) {
174 // Do nothing
175 }
176 };
177
178 private final DeviceProvisionedListener mDeviceProvisionedCallback =
179 new DeviceProvisionedListener() {
180 @Override
Matthew Ngdfab86c2017-11-07 15:46:51 -0800181 public void onUserSetupChanged() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700182 if (mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ngfac87832017-11-10 11:27:29 -0800183 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700184 }
185 }
186
187 @Override
188 public void onUserSwitched() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700189 mConnectionBackoffAttempts = 0;
Matthew Ngfac87832017-11-10 11:27:29 -0800190 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700191 }
192 };
193
194 // This is the death handler for the binder from the launcher service
Matthew Ng30c0a022017-11-10 14:06:29 -0800195 private final IBinder.DeathRecipient mOverviewServiceDeathRcpt
196 = this::startConnectionToCurrentUser;
Matthew Ng13dbf872017-10-27 11:02:14 -0700197
198 public OverviewProxyService(Context context) {
199 mContext = context;
200 mHandler = new Handler();
201 mConnectionBackoffAttempts = 0;
Matthew Ng30c0a022017-11-10 14:06:29 -0800202 mLauncherComponentName = ComponentName
203 .unflattenFromString(context.getString(R.string.config_overviewServiceComponent));
Matthew Ng13dbf872017-10-27 11:02:14 -0700204 mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback);
Matthew Ng30c0a022017-11-10 14:06:29 -0800205
206 // Listen for the package update changes.
207 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
208 filter.addDataScheme("package");
209 filter.addDataSchemeSpecificPart(mLauncherComponentName.getPackageName(),
210 PatternMatcher.PATTERN_LITERAL);
Matthew Ngc361fa12018-01-08 12:50:27 -0800211 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Matthew Ng30c0a022017-11-10 14:06:29 -0800212 mContext.registerReceiver(mLauncherAddedReceiver, filter);
Matthew Ng13dbf872017-10-27 11:02:14 -0700213 }
214
215 public void startConnectionToCurrentUser() {
Matthew Ngfac87832017-11-10 11:27:29 -0800216 if (mHandler.getLooper() != Looper.myLooper()) {
217 mHandler.post(mConnectionRunnable);
218 } else {
219 internalConnectToCurrentUser();
220 }
221 }
222
223 private void internalConnectToCurrentUser() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800224 disconnectFromLauncherService();
225
Matthew Ng13dbf872017-10-27 11:02:14 -0700226 // If user has not setup yet or already connected, do not try to connect
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800227 if (!mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ng13dbf872017-10-27 11:02:14 -0700228 return;
229 }
230 mHandler.removeCallbacks(mConnectionRunnable);
231 Intent launcherServiceIntent = new Intent();
Matthew Ng30c0a022017-11-10 14:06:29 -0800232 launcherServiceIntent.setComponent(mLauncherComponentName);
Matthew Ng13dbf872017-10-27 11:02:14 -0700233 boolean bound = mContext.bindServiceAsUser(launcherServiceIntent,
234 mOverviewServiceConnection, Context.BIND_AUTO_CREATE,
Matthew Ng09e6fbd2018-02-01 16:54:16 -0800235 UserHandle.of(mDeviceProvisionedController.getCurrentUser()));
Matthew Ng13dbf872017-10-27 11:02:14 -0700236 if (!bound) {
237 // Retry after exponential backoff timeout
238 final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts);
239 mHandler.postDelayed(mConnectionRunnable, timeoutMs);
240 mConnectionBackoffAttempts++;
241 }
242 }
243
Matthew Ng7d05e772017-11-09 14:41:07 -0800244 @Override
245 public void addCallback(OverviewProxyListener listener) {
246 mConnectionCallbacks.add(listener);
247 listener.onConnectionChanged(mOverviewProxy != null);
248 }
249
250 @Override
251 public void removeCallback(OverviewProxyListener listener) {
252 mConnectionCallbacks.remove(listener);
253 }
254
Matthew Ng13dbf872017-10-27 11:02:14 -0700255 public IOverviewProxy getProxy() {
256 return mOverviewProxy;
257 }
258
Tony Wickham05c1f852018-02-06 12:32:54 -0800259 public CharSequence getOnboardingText() {
260 return mOnboardingText;
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800261 }
262
Matthew Ng8f25fb962018-01-16 17:17:24 -0800263 public int getInteractionFlags() {
264 return mInteractionFlags;
265 }
266
Matthew Ng13dbf872017-10-27 11:02:14 -0700267 private void disconnectFromLauncherService() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800268 if (mOverviewProxy != null) {
Matthew Ngeb6893b2017-11-09 17:15:33 -0800269 mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0);
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800270 mContext.unbindService(mOverviewServiceConnection);
271 mOverviewProxy = null;
Matthew Ng7d05e772017-11-09 14:41:07 -0800272 notifyConnectionChanged();
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800273 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700274 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800275
276 private void notifyConnectionChanged() {
277 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
278 mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null);
279 }
280 }
281
Winson Chungcbb15a92018-01-25 17:46:16 +0000282 private void notifyRecentsAnimationStarted() {
283 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
284 mConnectionCallbacks.get(i).onRecentsAnimationStarted();
285 }
286 }
287
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800288 @Override
289 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Matthew Ngbd824572018-01-17 16:25:56 -0800290 pw.println(TAG_OPS + " state:");
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800291 pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts);
292 pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController
293 .isCurrentUserSetup());
294 pw.print(" isConnected="); pw.println(mOverviewProxy != null);
295 }
296
Matthew Ng7d05e772017-11-09 14:41:07 -0800297 public interface OverviewProxyListener {
Winson Chungcbb15a92018-01-25 17:46:16 +0000298 default void onConnectionChanged(boolean isConnected) {}
299 default void onRecentsAnimationStarted() {}
Matthew Ng8f25fb962018-01-16 17:17:24 -0800300 default void onInteractionFlagsChanged(@InteractionType int flags) {}
Matthew Ng7d05e772017-11-09 14:41:07 -0800301 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700302}