Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui; |
| 18 | |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 23 | import android.content.IntentFilter; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 24 | import android.content.ServiceConnection; |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 25 | import android.graphics.Rect; |
| 26 | import android.os.Binder; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 27 | import android.os.Handler; |
| 28 | import android.os.IBinder; |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 29 | import android.os.Looper; |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 30 | import android.os.PatternMatcher; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 31 | import android.os.RemoteException; |
| 32 | import android.os.UserHandle; |
| 33 | import android.util.Log; |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 34 | import android.view.SurfaceControl; |
| 35 | |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 36 | import com.android.systemui.OverviewProxyService.OverviewProxyListener; |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 37 | import com.android.systemui.shared.recents.IOverviewProxy; |
| 38 | import com.android.systemui.shared.recents.ISystemUiProxy; |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 39 | import com.android.systemui.shared.system.GraphicBufferCompat; |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 40 | import com.android.systemui.statusbar.policy.CallbackController; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 41 | import com.android.systemui.statusbar.policy.DeviceProvisionedController; |
| 42 | import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener; |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 43 | |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 44 | import java.io.FileDescriptor; |
| 45 | import java.io.PrintWriter; |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 46 | import java.util.ArrayList; |
| 47 | import java.util.List; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Class to send information from overview to launcher with a binder. |
| 51 | */ |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 52 | public class OverviewProxyService implements CallbackController<OverviewProxyListener>, Dumpable { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 53 | |
| 54 | private static final String TAG = "OverviewProxyService"; |
| 55 | private static final long BACKOFF_MILLIS = 5000; |
| 56 | |
| 57 | private final Context mContext; |
| 58 | private final Handler mHandler; |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 59 | private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser; |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 60 | private final ComponentName mLauncherComponentName; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 61 | private final DeviceProvisionedController mDeviceProvisionedController |
| 62 | = Dependency.get(DeviceProvisionedController.class); |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 63 | private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 64 | |
| 65 | private IOverviewProxy mOverviewProxy; |
| 66 | private int mConnectionBackoffAttempts; |
| 67 | |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 68 | private ISystemUiProxy mSysUiProxy = new ISystemUiProxy.Stub() { |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 69 | public GraphicBufferCompat screenshot(Rect sourceCrop, int width, int height, int minLayer, |
| 70 | int maxLayer, boolean useIdentityTransform, int rotation) { |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 71 | long token = Binder.clearCallingIdentity(); |
| 72 | try { |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 73 | return new GraphicBufferCompat(SurfaceControl.screenshotToBuffer(sourceCrop, width, |
| 74 | height, minLayer, maxLayer, useIdentityTransform, rotation)); |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 75 | } finally { |
| 76 | Binder.restoreCallingIdentity(token); |
| 77 | } |
| 78 | } |
| 79 | }; |
| 80 | |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 81 | private final BroadcastReceiver mLauncherAddedReceiver = new BroadcastReceiver() { |
| 82 | @Override |
| 83 | public void onReceive(Context context, Intent intent) { |
| 84 | // Reconnect immediately, instead of waiting for resume to arrive. |
| 85 | startConnectionToCurrentUser(); |
| 86 | } |
| 87 | }; |
| 88 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 89 | private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() { |
| 90 | @Override |
| 91 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 92 | if (service != null) { |
| 93 | mConnectionBackoffAttempts = 0; |
| 94 | mOverviewProxy = IOverviewProxy.Stub.asInterface(service); |
| 95 | // Listen for launcher's death |
| 96 | try { |
| 97 | service.linkToDeath(mOverviewServiceDeathRcpt, 0); |
| 98 | } catch (RemoteException e) { |
| 99 | Log.e(TAG, "Lost connection to launcher service", e); |
| 100 | } |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 101 | try { |
| 102 | mOverviewProxy.onBind(mSysUiProxy); |
| 103 | } catch (RemoteException e) { |
| 104 | Log.e(TAG, "Failed to call onBind()", e); |
| 105 | } |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 106 | notifyConnectionChanged(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void onServiceDisconnected(ComponentName name) { |
| 112 | // Do nothing |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | private final DeviceProvisionedListener mDeviceProvisionedCallback = |
| 117 | new DeviceProvisionedListener() { |
| 118 | @Override |
Matthew Ng | dfab86c | 2017-11-07 15:46:51 -0800 | [diff] [blame] | 119 | public void onUserSetupChanged() { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 120 | if (mDeviceProvisionedController.isCurrentUserSetup()) { |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 121 | internalConnectToCurrentUser(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void onUserSwitched() { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 127 | mConnectionBackoffAttempts = 0; |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 128 | internalConnectToCurrentUser(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 129 | } |
| 130 | }; |
| 131 | |
| 132 | // This is the death handler for the binder from the launcher service |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 133 | private final IBinder.DeathRecipient mOverviewServiceDeathRcpt |
| 134 | = this::startConnectionToCurrentUser; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 135 | |
| 136 | public OverviewProxyService(Context context) { |
| 137 | mContext = context; |
| 138 | mHandler = new Handler(); |
| 139 | mConnectionBackoffAttempts = 0; |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 140 | mLauncherComponentName = ComponentName |
| 141 | .unflattenFromString(context.getString(R.string.config_overviewServiceComponent)); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 142 | mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback); |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 143 | |
| 144 | // Listen for the package update changes. |
| 145 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 146 | filter.addDataScheme("package"); |
| 147 | filter.addDataSchemeSpecificPart(mLauncherComponentName.getPackageName(), |
| 148 | PatternMatcher.PATTERN_LITERAL); |
Matthew Ng | c361fa1 | 2018-01-08 12:50:27 -0800 | [diff] [blame^] | 149 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 150 | mContext.registerReceiver(mLauncherAddedReceiver, filter); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | public void startConnectionToCurrentUser() { |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 154 | if (mHandler.getLooper() != Looper.myLooper()) { |
| 155 | mHandler.post(mConnectionRunnable); |
| 156 | } else { |
| 157 | internalConnectToCurrentUser(); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private void internalConnectToCurrentUser() { |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 162 | disconnectFromLauncherService(); |
| 163 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 164 | // If user has not setup yet or already connected, do not try to connect |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 165 | if (!mDeviceProvisionedController.isCurrentUserSetup()) { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | mHandler.removeCallbacks(mConnectionRunnable); |
| 169 | Intent launcherServiceIntent = new Intent(); |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 170 | launcherServiceIntent.setComponent(mLauncherComponentName); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 171 | 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 Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 182 | @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 Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 193 | public IOverviewProxy getProxy() { |
| 194 | return mOverviewProxy; |
| 195 | } |
| 196 | |
| 197 | private void disconnectFromLauncherService() { |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 198 | if (mOverviewProxy != null) { |
Matthew Ng | eb6893b | 2017-11-09 17:15:33 -0800 | [diff] [blame] | 199 | mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0); |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 200 | mContext.unbindService(mOverviewServiceConnection); |
| 201 | mOverviewProxy = null; |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 202 | notifyConnectionChanged(); |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 203 | } |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 204 | } |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 205 | |
| 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 Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 212 | @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 Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 221 | public interface OverviewProxyListener { |
| 222 | void onConnectionChanged(boolean isConnected); |
| 223 | } |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 224 | } |