blob: 2e4a5a41bfab2a28e1a426466ce5660a5667fbdf [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.Bitmap;
26import android.graphics.Rect;
Matthew Ng30c0a022017-11-10 14:06:29 -080027import android.net.Uri;
Winson Chung38d31c22017-11-08 14:32:32 -080028import android.os.Binder;
Matthew Ng30c0a022017-11-10 14:06:29 -080029import android.os.Build;
Matthew Ng13dbf872017-10-27 11:02:14 -070030import android.os.Handler;
31import android.os.IBinder;
Matthew Ngfac87832017-11-10 11:27:29 -080032import android.os.Looper;
Matthew Ng30c0a022017-11-10 14:06:29 -080033import android.os.PatternMatcher;
Matthew Ng13dbf872017-10-27 11:02:14 -070034import android.os.RemoteException;
35import android.os.UserHandle;
36import android.util.Log;
Winson Chung38d31c22017-11-08 14:32:32 -080037import android.view.SurfaceControl;
38
39import com.android.systemui.shared.recents.IOverviewProxy;
40import com.android.systemui.shared.recents.ISystemUiProxy;
Matthew Ng7d05e772017-11-09 14:41:07 -080041import com.android.systemui.OverviewProxyService.OverviewProxyListener;
42import com.android.systemui.statusbar.policy.CallbackController;
Matthew Ng13dbf872017-10-27 11:02:14 -070043import com.android.systemui.statusbar.policy.DeviceProvisionedController;
44import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener;
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
55 private static final String TAG = "OverviewProxyService";
56 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() {
70 public Bitmap screenshot(Rect sourceCrop, int width, int height, int minLayer, int maxLayer,
71 boolean useIdentityTransform, int rotation) {
72 long token = Binder.clearCallingIdentity();
73 try {
74 return SurfaceControl.screenshot(sourceCrop, width, height, minLayer, maxLayer,
75 useIdentityTransform, rotation);
76 } 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) {
100 Log.e(TAG, "Lost connection to launcher service", e);
101 }
Winson Chung38d31c22017-11-08 14:32:32 -0800102 try {
103 mOverviewProxy.onBind(mSysUiProxy);
104 } catch (RemoteException e) {
105 Log.e(TAG, "Failed to call onBind()", e);
106 }
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);
150 mContext.registerReceiver(mLauncherAddedReceiver, filter);
Matthew Ng13dbf872017-10-27 11:02:14 -0700151 }
152
153 public void startConnectionToCurrentUser() {
Matthew Ngfac87832017-11-10 11:27:29 -0800154 if (mHandler.getLooper() != Looper.myLooper()) {
155 mHandler.post(mConnectionRunnable);
156 } else {
157 internalConnectToCurrentUser();
158 }
159 }
160
161 private void internalConnectToCurrentUser() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800162 disconnectFromLauncherService();
163
Matthew Ng13dbf872017-10-27 11:02:14 -0700164 // If user has not setup yet or already connected, do not try to connect
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800165 if (!mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ng13dbf872017-10-27 11:02:14 -0700166 return;
167 }
168 mHandler.removeCallbacks(mConnectionRunnable);
169 Intent launcherServiceIntent = new Intent();
Matthew Ng30c0a022017-11-10 14:06:29 -0800170 launcherServiceIntent.setComponent(mLauncherComponentName);
Matthew Ng13dbf872017-10-27 11:02:14 -0700171 boolean bound = mContext.bindServiceAsUser(launcherServiceIntent,
172 mOverviewServiceConnection, Context.BIND_AUTO_CREATE,
173 UserHandle.getUserHandleForUid(mDeviceProvisionedController.getCurrentUser()));
174 if (!bound) {
175 // Retry after exponential backoff timeout
176 final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts);
177 mHandler.postDelayed(mConnectionRunnable, timeoutMs);
178 mConnectionBackoffAttempts++;
179 }
180 }
181
Matthew Ng7d05e772017-11-09 14:41:07 -0800182 @Override
183 public void addCallback(OverviewProxyListener listener) {
184 mConnectionCallbacks.add(listener);
185 listener.onConnectionChanged(mOverviewProxy != null);
186 }
187
188 @Override
189 public void removeCallback(OverviewProxyListener listener) {
190 mConnectionCallbacks.remove(listener);
191 }
192
Matthew Ng13dbf872017-10-27 11:02:14 -0700193 public IOverviewProxy getProxy() {
194 return mOverviewProxy;
195 }
196
197 private void disconnectFromLauncherService() {
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800198 if (mOverviewProxy != null) {
Matthew Ngeb6893b2017-11-09 17:15:33 -0800199 mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0);
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800200 mContext.unbindService(mOverviewServiceConnection);
201 mOverviewProxy = null;
Matthew Ng7d05e772017-11-09 14:41:07 -0800202 notifyConnectionChanged();
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800203 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700204 }
Matthew Ng7d05e772017-11-09 14:41:07 -0800205
206 private void notifyConnectionChanged() {
207 for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) {
208 mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null);
209 }
210 }
211
Matthew Ng1e43ebd2017-11-14 14:47:05 -0800212 @Override
213 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
214 pw.println(TAG + " state:");
215 pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts);
216 pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController
217 .isCurrentUserSetup());
218 pw.print(" isConnected="); pw.println(mOverviewProxy != null);
219 }
220
Matthew Ng7d05e772017-11-09 14:41:07 -0800221 public interface OverviewProxyListener {
222 void onConnectionChanged(boolean isConnected);
223 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700224}