blob: 0be522bfb8cbdeeb7b4e700d117b1a36258fdc6b [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 }
80 };
81
Matthew Ng30c0a022017-11-10 14:06:29 -080082 private final BroadcastReceiver mLauncherAddedReceiver = new BroadcastReceiver() {
83 @Override
84 public void onReceive(Context context, Intent intent) {
85 // Reconnect immediately, instead of waiting for resume to arrive.
86 startConnectionToCurrentUser();
87 }
88 };
89
Matthew Ng13dbf872017-10-27 11:02:14 -070090 private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() {
91 @Override
92 public void onServiceConnected(ComponentName name, IBinder service) {
93 if (service != null) {
94 mConnectionBackoffAttempts = 0;
95 mOverviewProxy = IOverviewProxy.Stub.asInterface(service);
96 // Listen for launcher's death
97 try {
98 service.linkToDeath(mOverviewServiceDeathRcpt, 0);
99 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800100 Log.e(TAG_OPS, "Lost connection to launcher service", e);
Matthew Ng13dbf872017-10-27 11:02:14 -0700101 }
Winson Chung38d31c22017-11-08 14:32:32 -0800102 try {
103 mOverviewProxy.onBind(mSysUiProxy);
104 } catch (RemoteException e) {
Matthew Ngbd824572018-01-17 16:25:56 -0800105 Log.e(TAG_OPS, "Failed to call onBind()", e);
Winson Chung38d31c22017-11-08 14:32:32 -0800106 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800107 notifyConnectionChanged();
Matthew Ng13dbf872017-10-27 11:02:14 -0700108 }
109 }
110
111 @Override
112 public void onServiceDisconnected(ComponentName name) {
113 // Do nothing
114 }
115 };
116
117 private final DeviceProvisionedListener mDeviceProvisionedCallback =
118 new DeviceProvisionedListener() {
119 @Override
Matthew Ngdfab86c2017-11-07 15:46:51 -0800120 public void onUserSetupChanged() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700121 if (mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ngfac87832017-11-10 11:27:29 -0800122 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700123 }
124 }
125
126 @Override
127 public void onUserSwitched() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700128 mConnectionBackoffAttempts = 0;
Matthew Ngfac87832017-11-10 11:27:29 -0800129 internalConnectToCurrentUser();
Matthew Ng13dbf872017-10-27 11:02:14 -0700130 }
131 };
132
133 // This is the death handler for the binder from the launcher service
Matthew Ng30c0a022017-11-10 14:06:29 -0800134 private final IBinder.DeathRecipient mOverviewServiceDeathRcpt
135 = this::startConnectionToCurrentUser;
Matthew Ng13dbf872017-10-27 11:02:14 -0700136
137 public OverviewProxyService(Context context) {
138 mContext = context;
139 mHandler = new Handler();
140 mConnectionBackoffAttempts = 0;
Matthew Ng30c0a022017-11-10 14:06:29 -0800141 mLauncherComponentName = ComponentName
142 .unflattenFromString(context.getString(R.string.config_overviewServiceComponent));
Matthew Ng13dbf872017-10-27 11:02:14 -0700143 mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback);
Matthew Ng30c0a022017-11-10 14:06:29 -0800144
145 // Listen for the package update changes.
146 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
147 filter.addDataScheme("package");
148 filter.addDataSchemeSpecificPart(mLauncherComponentName.getPackageName(),
149 PatternMatcher.PATTERN_LITERAL);
Matthew Ngc361fa12018-01-08 12:50:27 -0800150 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Matthew Ng30c0a022017-11-10 14:06:29 -0800151 mContext.registerReceiver(mLauncherAddedReceiver, filter);
Matthew Ng13dbf872017-10-27 11:02:14 -0700152 }
153
154 public void startConnectionToCurrentUser() {
Matthew Ngfac87832017-11-10 11:27:29 -0800155 if (mHandler.getLooper() != Looper.myLooper()) {
156 mHandler.post(mConnectionRunnable);
157 } else {
158 internalConnectToCurrentUser();
159 }
160 }
161
162 private void internalConnectToCurrentUser() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800163 disconnectFromLauncherService();
164
Matthew Ng13dbf872017-10-27 11:02:14 -0700165 // If user has not setup yet or already connected, do not try to connect
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800166 if (!mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ng13dbf872017-10-27 11:02:14 -0700167 return;
168 }
169 mHandler.removeCallbacks(mConnectionRunnable);
170 Intent launcherServiceIntent = new Intent();
Matthew Ng30c0a022017-11-10 14:06:29 -0800171 launcherServiceIntent.setComponent(mLauncherComponentName);
Matthew Ng13dbf872017-10-27 11:02:14 -0700172 boolean bound = mContext.bindServiceAsUser(launcherServiceIntent,
173 mOverviewServiceConnection, Context.BIND_AUTO_CREATE,
174 UserHandle.getUserHandleForUid(mDeviceProvisionedController.getCurrentUser()));
175 if (!bound) {
176 // Retry after exponential backoff timeout
177 final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts);
178 mHandler.postDelayed(mConnectionRunnable, timeoutMs);
179 mConnectionBackoffAttempts++;
180 }
181 }
182
Matthew Ng7d05e772017-11-09 14:41:07 -0800183 @Override
184 public void addCallback(OverviewProxyListener listener) {
185 mConnectionCallbacks.add(listener);
186 listener.onConnectionChanged(mOverviewProxy != null);
187 }
188
189 @Override
190 public void removeCallback(OverviewProxyListener listener) {
191 mConnectionCallbacks.remove(listener);
192 }
193
Matthew Ng13dbf872017-10-27 11:02:14 -0700194 public IOverviewProxy getProxy() {
195 return mOverviewProxy;
196 }
197
198 private void disconnectFromLauncherService() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800199 if (mOverviewProxy != null) {
Matthew Ngeb6893b2017-11-09 17:15:33 -0800200 mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0);
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800201 mContext.unbindService(mOverviewServiceConnection);
202 mOverviewProxy = null;
Matthew Ng7d05e772017-11-09 14:41:07 -0800203 notifyConnectionChanged();
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800204 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700205 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800206
207 private void notifyConnectionChanged() {
208 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
209 mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null);
210 }
211 }
212
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800213 @Override
214 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Matthew Ngbd824572018-01-17 16:25:56 -0800215 pw.println(TAG_OPS + " state:");
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800216 pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts);
217 pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController
218 .isCurrentUserSetup());
219 pw.print(" isConnected="); pw.println(mOverviewProxy != null);
220 }
221
Matthew Ng7d05e772017-11-09 14:41:07 -0800222 public interface OverviewProxyListener {
223 void onConnectionChanged(boolean isConnected);
224 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700225}