blob: b30b0c5754155bc694aa5e9ef33fb08e1701acdd [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 Chung38d31c22017-11-08 14:32:32 -080037import com.android.systemui.shared.recents.IOverviewProxy;
38import com.android.systemui.shared.recents.ISystemUiProxy;
Winson Chung11f53e92017-11-13 17:45:12 -080039import com.android.systemui.shared.system.GraphicBufferCompat;
Winson Chungcaf2b812018-01-26 10:29:46 -080040import com.android.systemui.statusbar.phone.StatusBar;
Matthew Ng7d05e772017-11-09 14:41:07 -080041import com.android.systemui.statusbar.policy.CallbackController;
Matthew Ng13dbf872017-10-27 11:02:14 -070042import com.android.systemui.statusbar.policy.DeviceProvisionedController;
43import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener;
Winson Chung11f53e92017-11-13 17:45:12 -080044
Matthew Ng1e43ebd2017-11-14 14:47:05 -080045import java.io.FileDescriptor;
46import java.io.PrintWriter;
Matthew Ng7d05e772017-11-09 14:41:07 -080047import java.util.ArrayList;
48import java.util.List;
Matthew Ng13dbf872017-10-27 11:02:14 -070049
50/**
51 * Class to send information from overview to launcher with a binder.
52 */
Matthew Ng1e43ebd2017-11-14 14:47:05 -080053public class OverviewProxyService implements CallbackController<OverviewProxyListener>, Dumpable {
Matthew Ng13dbf872017-10-27 11:02:14 -070054
Matthew Ngbd824572018-01-17 16:25:56 -080055 public static final String TAG_OPS = "OverviewProxyService";
56 public static final boolean DEBUG_OVERVIEW_PROXY = false;
Matthew Ng13dbf872017-10-27 11:02:14 -070057 private static final long BACKOFF_MILLIS = 5000;
58
59 private final Context mContext;
60 private final Handler mHandler;
Matthew Ngfac87832017-11-10 11:27:29 -080061 private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser;
Matthew Ng30c0a022017-11-10 14:06:29 -080062 private final ComponentName mLauncherComponentName;
Matthew Ng13dbf872017-10-27 11:02:14 -070063 private final DeviceProvisionedController mDeviceProvisionedController
64 = Dependency.get(DeviceProvisionedController.class);
Matthew Ng7d05e772017-11-09 14:41:07 -080065 private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>();
Matthew Ng13dbf872017-10-27 11:02:14 -070066
67 private IOverviewProxy mOverviewProxy;
68 private int mConnectionBackoffAttempts;
Tony Wickham05c1f852018-02-06 12:32:54 -080069 private CharSequence mOnboardingText;
Matthew Ng13dbf872017-10-27 11:02:14 -070070
Winson Chung38d31c22017-11-08 14:32:32 -080071 private ISystemUiProxy mSysUiProxy = new ISystemUiProxy.Stub() {
Winson Chungcaf2b812018-01-26 10:29:46 -080072
Winson Chung11f53e92017-11-13 17:45:12 -080073 public GraphicBufferCompat screenshot(Rect sourceCrop, int width, int height, int minLayer,
74 int maxLayer, boolean useIdentityTransform, int rotation) {
Winson Chung38d31c22017-11-08 14:32:32 -080075 long token = Binder.clearCallingIdentity();
76 try {
Winson Chung11f53e92017-11-13 17:45:12 -080077 return new GraphicBufferCompat(SurfaceControl.screenshotToBuffer(sourceCrop, width,
78 height, minLayer, maxLayer, useIdentityTransform, rotation));
Winson Chung38d31c22017-11-08 14:32:32 -080079 } finally {
80 Binder.restoreCallingIdentity(token);
81 }
82 }
Winson Chungcbb15a92018-01-25 17:46:16 +000083
Winson Chungcaf2b812018-01-26 10:29:46 -080084 public void startScreenPinning(int taskId) {
85 long token = Binder.clearCallingIdentity();
86 try {
87 mHandler.post(() -> {
88 StatusBar statusBar = ((SystemUIApplication) mContext).getComponent(
89 StatusBar.class);
90 if (statusBar != null) {
91 statusBar.showScreenPinningRequest(taskId, false /* allowCancel */);
92 }
93 });
94 } finally {
95 Binder.restoreCallingIdentity(token);
96 }
97 }
98
Winson Chungcbb15a92018-01-25 17:46:16 +000099 public void onRecentsAnimationStarted() {
100 long token = Binder.clearCallingIdentity();
101 try {
Winson Chungcaf2b812018-01-26 10:29:46 -0800102 mHandler.post(() -> {
103 notifyRecentsAnimationStarted();
104 });
Winson Chungcbb15a92018-01-25 17:46:16 +0000105 } finally {
106 Binder.restoreCallingIdentity(token);
107 }
108 }
Tony Wickham05c1f852018-02-06 12:32:54 -0800109
110 public void setRecentsOnboardingText(CharSequence text) {
111 mOnboardingText = text;
112 }
Winson Chung38d31c22017-11-08 14:32:32 -0800113 };
114
Matthew Ng30c0a022017-11-10 14:06:29 -0800115 private final BroadcastReceiver mLauncherAddedReceiver = new BroadcastReceiver() {
116 @Override
117 public void onReceive(Context context, Intent intent) {
118 // Reconnect immediately, instead of waiting for resume to arrive.
119 startConnectionToCurrentUser();
120 }
121 };
122
Matthew Ng13dbf872017-10-27 11:02:14 -0700123 private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() {
124 @Override
125 public void onServiceConnected(ComponentName name, IBinder service) {
126 if (service != null) {
127 mConnectionBackoffAttempts = 0;
128 mOverviewProxy = IOverviewProxy.Stub.asInterface(service);
129 // Listen for launcher's death
130 try {
131 service.linkToDeath(mOverviewServiceDeathRcpt, 0);
132 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800133 Log.e(TAG_OPS, "Lost connection to launcher service", e);
Matthew Ng13dbf872017-10-27 11:02:14 -0700134 }
Winson Chung38d31c22017-11-08 14:32:32 -0800135 try {
136 mOverviewProxy.onBind(mSysUiProxy);
137 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800138 Log.e(TAG_OPS, "Failed to call onBind()", e);
Winson Chung38d31c22017-11-08 14:32:32 -0800139 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800140 notifyConnectionChanged();
Matthew Ng13dbf872017-10-27 11:02:14 -0700141 }
142 }
143
144 @Override
145 public void onServiceDisconnected(ComponentName name) {
146 // Do nothing
147 }
148 };
149
150 private final DeviceProvisionedListener mDeviceProvisionedCallback =
151 new DeviceProvisionedListener() {
152 @Override
Matthew Ngdfab86c2017-11-07 15:46:51 -0800153 public void onUserSetupChanged() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700154 if (mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ngfac87832017-11-10 11:27:29 -0800155 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700156 }
157 }
158
159 @Override
160 public void onUserSwitched() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700161 mConnectionBackoffAttempts = 0;
Matthew Ngfac87832017-11-10 11:27:29 -0800162 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700163 }
164 };
165
166 // This is the death handler for the binder from the launcher service
Matthew Ng30c0a022017-11-10 14:06:29 -0800167 private final IBinder.DeathRecipient mOverviewServiceDeathRcpt
168 = this::startConnectionToCurrentUser;
Matthew Ng13dbf872017-10-27 11:02:14 -0700169
170 public OverviewProxyService(Context context) {
171 mContext = context;
172 mHandler = new Handler();
173 mConnectionBackoffAttempts = 0;
Matthew Ng30c0a022017-11-10 14:06:29 -0800174 mLauncherComponentName = ComponentName
175 .unflattenFromString(context.getString(R.string.config_overviewServiceComponent));
Matthew Ng13dbf872017-10-27 11:02:14 -0700176 mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback);
Matthew Ng30c0a022017-11-10 14:06:29 -0800177
178 // Listen for the package update changes.
179 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
180 filter.addDataScheme("package");
181 filter.addDataSchemeSpecificPart(mLauncherComponentName.getPackageName(),
182 PatternMatcher.PATTERN_LITERAL);
Matthew Ngc361fa12018-01-08 12:50:27 -0800183 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Matthew Ng30c0a022017-11-10 14:06:29 -0800184 mContext.registerReceiver(mLauncherAddedReceiver, filter);
Matthew Ng13dbf872017-10-27 11:02:14 -0700185 }
186
187 public void startConnectionToCurrentUser() {
Matthew Ngfac87832017-11-10 11:27:29 -0800188 if (mHandler.getLooper() != Looper.myLooper()) {
189 mHandler.post(mConnectionRunnable);
190 } else {
191 internalConnectToCurrentUser();
192 }
193 }
194
195 private void internalConnectToCurrentUser() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800196 disconnectFromLauncherService();
197
Matthew Ng13dbf872017-10-27 11:02:14 -0700198 // If user has not setup yet or already connected, do not try to connect
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800199 if (!mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ng13dbf872017-10-27 11:02:14 -0700200 return;
201 }
202 mHandler.removeCallbacks(mConnectionRunnable);
203 Intent launcherServiceIntent = new Intent();
Matthew Ng30c0a022017-11-10 14:06:29 -0800204 launcherServiceIntent.setComponent(mLauncherComponentName);
Matthew Ng13dbf872017-10-27 11:02:14 -0700205 boolean bound = mContext.bindServiceAsUser(launcherServiceIntent,
206 mOverviewServiceConnection, Context.BIND_AUTO_CREATE,
Matthew Ng09e6fbd2018-02-01 16:54:16 -0800207 UserHandle.of(mDeviceProvisionedController.getCurrentUser()));
Matthew Ng13dbf872017-10-27 11:02:14 -0700208 if (!bound) {
209 // Retry after exponential backoff timeout
210 final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts);
211 mHandler.postDelayed(mConnectionRunnable, timeoutMs);
212 mConnectionBackoffAttempts++;
213 }
214 }
215
Matthew Ng7d05e772017-11-09 14:41:07 -0800216 @Override
217 public void addCallback(OverviewProxyListener listener) {
218 mConnectionCallbacks.add(listener);
219 listener.onConnectionChanged(mOverviewProxy != null);
220 }
221
222 @Override
223 public void removeCallback(OverviewProxyListener listener) {
224 mConnectionCallbacks.remove(listener);
225 }
226
Matthew Ng13dbf872017-10-27 11:02:14 -0700227 public IOverviewProxy getProxy() {
228 return mOverviewProxy;
229 }
230
Tony Wickham05c1f852018-02-06 12:32:54 -0800231 public CharSequence getOnboardingText() {
232 return mOnboardingText;
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800233 }
234
Matthew Ng13dbf872017-10-27 11:02:14 -0700235 private void disconnectFromLauncherService() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800236 if (mOverviewProxy != null) {
Matthew Ngeb6893b2017-11-09 17:15:33 -0800237 mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0);
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800238 mContext.unbindService(mOverviewServiceConnection);
239 mOverviewProxy = null;
Matthew Ng7d05e772017-11-09 14:41:07 -0800240 notifyConnectionChanged();
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800241 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700242 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800243
244 private void notifyConnectionChanged() {
245 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
246 mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null);
247 }
248 }
249
Winson Chungcbb15a92018-01-25 17:46:16 +0000250 private void notifyRecentsAnimationStarted() {
251 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
252 mConnectionCallbacks.get(i).onRecentsAnimationStarted();
253 }
254 }
255
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800256 @Override
257 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Matthew Ngbd824572018-01-17 16:25:56 -0800258 pw.println(TAG_OPS + " state:");
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800259 pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts);
260 pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController
261 .isCurrentUserSetup());
262 pw.print(" isConnected="); pw.println(mOverviewProxy != null);
263 }
264
Matthew Ng7d05e772017-11-09 14:41:07 -0800265 public interface OverviewProxyListener {
Winson Chungcbb15a92018-01-25 17:46:16 +0000266 default void onConnectionChanged(boolean isConnected) {}
267 default void onRecentsAnimationStarted() {}
Matthew Ng7d05e772017-11-09 14:41:07 -0800268 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700269}