blob: b6e49ae6cc2cf4bdbaedea66f534cac6f65bdc7a [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;
Matthew Ng7d05e772017-11-09 14:41:07 -080040import com.android.systemui.statusbar.policy.CallbackController;
Matthew Ng13dbf872017-10-27 11:02:14 -070041import com.android.systemui.statusbar.policy.DeviceProvisionedController;
42import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener;
Winson Chung11f53e92017-11-13 17:45:12 -080043
Matthew Ng1e43ebd2017-11-14 14:47:05 -080044import java.io.FileDescriptor;
45import java.io.PrintWriter;
Matthew Ng7d05e772017-11-09 14:41:07 -080046import java.util.ArrayList;
47import java.util.List;
Matthew Ng13dbf872017-10-27 11:02:14 -070048
49/**
50 * Class to send information from overview to launcher with a binder.
51 */
Matthew Ng1e43ebd2017-11-14 14:47:05 -080052public class OverviewProxyService implements CallbackController<OverviewProxyListener>, Dumpable {
Matthew Ng13dbf872017-10-27 11:02:14 -070053
Matthew Ngbd824572018-01-17 16:25:56 -080054 public static final String TAG_OPS = "OverviewProxyService";
55 public static final boolean DEBUG_OVERVIEW_PROXY = false;
Matthew Ng13dbf872017-10-27 11:02:14 -070056 private static final long BACKOFF_MILLIS = 5000;
57
58 private final Context mContext;
59 private final Handler mHandler;
Matthew Ngfac87832017-11-10 11:27:29 -080060 private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser;
Matthew Ng30c0a022017-11-10 14:06:29 -080061 private final ComponentName mLauncherComponentName;
Matthew Ng13dbf872017-10-27 11:02:14 -070062 private final DeviceProvisionedController mDeviceProvisionedController
63 = Dependency.get(DeviceProvisionedController.class);
Matthew Ng7d05e772017-11-09 14:41:07 -080064 private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>();
Matthew Ng13dbf872017-10-27 11:02:14 -070065
66 private IOverviewProxy mOverviewProxy;
67 private int mConnectionBackoffAttempts;
68
Winson Chung38d31c22017-11-08 14:32:32 -080069 private ISystemUiProxy mSysUiProxy = new ISystemUiProxy.Stub() {
Winson Chung11f53e92017-11-13 17:45:12 -080070 public GraphicBufferCompat screenshot(Rect sourceCrop, int width, int height, int minLayer,
71 int maxLayer, boolean useIdentityTransform, int rotation) {
Winson Chung38d31c22017-11-08 14:32:32 -080072 long token = Binder.clearCallingIdentity();
73 try {
Winson Chung11f53e92017-11-13 17:45:12 -080074 return new GraphicBufferCompat(SurfaceControl.screenshotToBuffer(sourceCrop, width,
75 height, minLayer, maxLayer, useIdentityTransform, rotation));
Winson Chung38d31c22017-11-08 14:32:32 -080076 } finally {
77 Binder.restoreCallingIdentity(token);
78 }
79 }
Winson Chungcbb15a92018-01-25 17:46:16 +000080
81 public void onRecentsAnimationStarted() {
82 long token = Binder.clearCallingIdentity();
83 try {
84 notifyRecentsAnimationStarted();
85 } finally {
86 Binder.restoreCallingIdentity(token);
87 }
88 }
Winson Chung38d31c22017-11-08 14:32:32 -080089 };
90
Matthew Ng30c0a022017-11-10 14:06:29 -080091 private final BroadcastReceiver mLauncherAddedReceiver = new BroadcastReceiver() {
92 @Override
93 public void onReceive(Context context, Intent intent) {
94 // Reconnect immediately, instead of waiting for resume to arrive.
95 startConnectionToCurrentUser();
96 }
97 };
98
Matthew Ng13dbf872017-10-27 11:02:14 -070099 private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() {
100 @Override
101 public void onServiceConnected(ComponentName name, IBinder service) {
102 if (service != null) {
103 mConnectionBackoffAttempts = 0;
104 mOverviewProxy = IOverviewProxy.Stub.asInterface(service);
105 // Listen for launcher's death
106 try {
107 service.linkToDeath(mOverviewServiceDeathRcpt, 0);
108 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800109 Log.e(TAG_OPS, "Lost connection to launcher service", e);
Matthew Ng13dbf872017-10-27 11:02:14 -0700110 }
Winson Chung38d31c22017-11-08 14:32:32 -0800111 try {
112 mOverviewProxy.onBind(mSysUiProxy);
113 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800114 Log.e(TAG_OPS, "Failed to call onBind()", e);
Winson Chung38d31c22017-11-08 14:32:32 -0800115 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800116 notifyConnectionChanged();
Matthew Ng13dbf872017-10-27 11:02:14 -0700117 }
118 }
119
120 @Override
121 public void onServiceDisconnected(ComponentName name) {
122 // Do nothing
123 }
124 };
125
126 private final DeviceProvisionedListener mDeviceProvisionedCallback =
127 new DeviceProvisionedListener() {
128 @Override
Matthew Ngdfab86c2017-11-07 15:46:51 -0800129 public void onUserSetupChanged() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700130 if (mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ngfac87832017-11-10 11:27:29 -0800131 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700132 }
133 }
134
135 @Override
136 public void onUserSwitched() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700137 mConnectionBackoffAttempts = 0;
Matthew Ngfac87832017-11-10 11:27:29 -0800138 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700139 }
140 };
141
142 // This is the death handler for the binder from the launcher service
Matthew Ng30c0a022017-11-10 14:06:29 -0800143 private final IBinder.DeathRecipient mOverviewServiceDeathRcpt
144 = this::startConnectionToCurrentUser;
Matthew Ng13dbf872017-10-27 11:02:14 -0700145
146 public OverviewProxyService(Context context) {
147 mContext = context;
148 mHandler = new Handler();
149 mConnectionBackoffAttempts = 0;
Matthew Ng30c0a022017-11-10 14:06:29 -0800150 mLauncherComponentName = ComponentName
151 .unflattenFromString(context.getString(R.string.config_overviewServiceComponent));
Matthew Ng13dbf872017-10-27 11:02:14 -0700152 mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback);
Matthew Ng30c0a022017-11-10 14:06:29 -0800153
154 // Listen for the package update changes.
155 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
156 filter.addDataScheme("package");
157 filter.addDataSchemeSpecificPart(mLauncherComponentName.getPackageName(),
158 PatternMatcher.PATTERN_LITERAL);
Matthew Ngc361fa12018-01-08 12:50:27 -0800159 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Matthew Ng30c0a022017-11-10 14:06:29 -0800160 mContext.registerReceiver(mLauncherAddedReceiver, filter);
Matthew Ng13dbf872017-10-27 11:02:14 -0700161 }
162
163 public void startConnectionToCurrentUser() {
Matthew Ngfac87832017-11-10 11:27:29 -0800164 if (mHandler.getLooper() != Looper.myLooper()) {
165 mHandler.post(mConnectionRunnable);
166 } else {
167 internalConnectToCurrentUser();
168 }
169 }
170
171 private void internalConnectToCurrentUser() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800172 disconnectFromLauncherService();
173
Matthew Ng13dbf872017-10-27 11:02:14 -0700174 // If user has not setup yet or already connected, do not try to connect
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800175 if (!mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ng13dbf872017-10-27 11:02:14 -0700176 return;
177 }
178 mHandler.removeCallbacks(mConnectionRunnable);
179 Intent launcherServiceIntent = new Intent();
Matthew Ng30c0a022017-11-10 14:06:29 -0800180 launcherServiceIntent.setComponent(mLauncherComponentName);
Matthew Ng13dbf872017-10-27 11:02:14 -0700181 boolean bound = mContext.bindServiceAsUser(launcherServiceIntent,
182 mOverviewServiceConnection, Context.BIND_AUTO_CREATE,
183 UserHandle.getUserHandleForUid(mDeviceProvisionedController.getCurrentUser()));
184 if (!bound) {
185 // Retry after exponential backoff timeout
186 final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts);
187 mHandler.postDelayed(mConnectionRunnable, timeoutMs);
188 mConnectionBackoffAttempts++;
189 }
190 }
191
Matthew Ng7d05e772017-11-09 14:41:07 -0800192 @Override
193 public void addCallback(OverviewProxyListener listener) {
194 mConnectionCallbacks.add(listener);
195 listener.onConnectionChanged(mOverviewProxy != null);
196 }
197
198 @Override
199 public void removeCallback(OverviewProxyListener listener) {
200 mConnectionCallbacks.remove(listener);
201 }
202
Matthew Ng13dbf872017-10-27 11:02:14 -0700203 public IOverviewProxy getProxy() {
204 return mOverviewProxy;
205 }
206
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800207 public ComponentName getLauncherComponent() {
208 return mLauncherComponentName;
209 }
210
Matthew Ng13dbf872017-10-27 11:02:14 -0700211 private void disconnectFromLauncherService() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800212 if (mOverviewProxy != null) {
Matthew Ngeb6893b2017-11-09 17:15:33 -0800213 mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0);
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800214 mContext.unbindService(mOverviewServiceConnection);
215 mOverviewProxy = null;
Matthew Ng7d05e772017-11-09 14:41:07 -0800216 notifyConnectionChanged();
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800217 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700218 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800219
220 private void notifyConnectionChanged() {
221 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
222 mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null);
223 }
224 }
225
Winson Chungcbb15a92018-01-25 17:46:16 +0000226 private void notifyRecentsAnimationStarted() {
227 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
228 mConnectionCallbacks.get(i).onRecentsAnimationStarted();
229 }
230 }
231
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800232 @Override
233 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Matthew Ngbd824572018-01-17 16:25:56 -0800234 pw.println(TAG_OPS + " state:");
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800235 pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts);
236 pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController
237 .isCurrentUserSetup());
238 pw.print(" isConnected="); pw.println(mOverviewProxy != null);
239 }
240
Matthew Ng7d05e772017-11-09 14:41:07 -0800241 public interface OverviewProxyListener {
Winson Chungcbb15a92018-01-25 17:46:16 +0000242 default void onConnectionChanged(boolean isConnected) {}
243 default void onRecentsAnimationStarted() {}
Matthew Ng7d05e772017-11-09 14:41:07 -0800244 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700245}