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 | |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 54 | public static final String TAG_OPS = "OverviewProxyService"; |
| 55 | public static final boolean DEBUG_OVERVIEW_PROXY = false; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 56 | private static final long BACKOFF_MILLIS = 5000; |
| 57 | |
| 58 | private final Context mContext; |
| 59 | private final Handler mHandler; |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 60 | private final Runnable mConnectionRunnable = this::internalConnectToCurrentUser; |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 61 | private final ComponentName mLauncherComponentName; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 62 | private final DeviceProvisionedController mDeviceProvisionedController |
| 63 | = Dependency.get(DeviceProvisionedController.class); |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 64 | private final List<OverviewProxyListener> mConnectionCallbacks = new ArrayList<>(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 65 | |
| 66 | private IOverviewProxy mOverviewProxy; |
| 67 | private int mConnectionBackoffAttempts; |
| 68 | |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 69 | private ISystemUiProxy mSysUiProxy = new ISystemUiProxy.Stub() { |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 70 | public GraphicBufferCompat screenshot(Rect sourceCrop, int width, int height, int minLayer, |
| 71 | int maxLayer, boolean useIdentityTransform, int rotation) { |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 72 | long token = Binder.clearCallingIdentity(); |
| 73 | try { |
Winson Chung | 11f53e9 | 2017-11-13 17:45:12 -0800 | [diff] [blame] | 74 | return new GraphicBufferCompat(SurfaceControl.screenshotToBuffer(sourceCrop, width, |
| 75 | height, minLayer, maxLayer, useIdentityTransform, rotation)); |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 76 | } finally { |
| 77 | Binder.restoreCallingIdentity(token); |
| 78 | } |
| 79 | } |
| 80 | }; |
| 81 | |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 82 | 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 Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 90 | 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 Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 100 | Log.e(TAG_OPS, "Lost connection to launcher service", e); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 101 | } |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 102 | try { |
| 103 | mOverviewProxy.onBind(mSysUiProxy); |
| 104 | } catch (RemoteException e) { |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 105 | Log.e(TAG_OPS, "Failed to call onBind()", e); |
Winson Chung | 38d31c2 | 2017-11-08 14:32:32 -0800 | [diff] [blame] | 106 | } |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 107 | notifyConnectionChanged(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 108 | } |
| 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 Ng | dfab86c | 2017-11-07 15:46:51 -0800 | [diff] [blame] | 120 | public void onUserSetupChanged() { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 121 | if (mDeviceProvisionedController.isCurrentUserSetup()) { |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 122 | internalConnectToCurrentUser(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public void onUserSwitched() { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 128 | mConnectionBackoffAttempts = 0; |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 129 | internalConnectToCurrentUser(); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 130 | } |
| 131 | }; |
| 132 | |
| 133 | // This is the death handler for the binder from the launcher service |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 134 | private final IBinder.DeathRecipient mOverviewServiceDeathRcpt |
| 135 | = this::startConnectionToCurrentUser; |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 136 | |
| 137 | public OverviewProxyService(Context context) { |
| 138 | mContext = context; |
| 139 | mHandler = new Handler(); |
| 140 | mConnectionBackoffAttempts = 0; |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 141 | mLauncherComponentName = ComponentName |
| 142 | .unflattenFromString(context.getString(R.string.config_overviewServiceComponent)); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 143 | mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback); |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 144 | |
| 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 Ng | c361fa1 | 2018-01-08 12:50:27 -0800 | [diff] [blame] | 150 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 151 | mContext.registerReceiver(mLauncherAddedReceiver, filter); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | public void startConnectionToCurrentUser() { |
Matthew Ng | fac8783 | 2017-11-10 11:27:29 -0800 | [diff] [blame] | 155 | if (mHandler.getLooper() != Looper.myLooper()) { |
| 156 | mHandler.post(mConnectionRunnable); |
| 157 | } else { |
| 158 | internalConnectToCurrentUser(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | private void internalConnectToCurrentUser() { |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 163 | disconnectFromLauncherService(); |
| 164 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 165 | // 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] | 166 | if (!mDeviceProvisionedController.isCurrentUserSetup()) { |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 167 | return; |
| 168 | } |
| 169 | mHandler.removeCallbacks(mConnectionRunnable); |
| 170 | Intent launcherServiceIntent = new Intent(); |
Matthew Ng | 30c0a02 | 2017-11-10 14:06:29 -0800 | [diff] [blame] | 171 | launcherServiceIntent.setComponent(mLauncherComponentName); |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 172 | 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 Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 183 | @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 Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 194 | public IOverviewProxy getProxy() { |
| 195 | return mOverviewProxy; |
| 196 | } |
| 197 | |
Tony Wickham | fb63fe8 | 2018-01-16 12:14:06 -0800 | [diff] [blame] | 198 | public ComponentName getLauncherComponent() { |
| 199 | return mLauncherComponentName; |
| 200 | } |
| 201 | |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 202 | private void disconnectFromLauncherService() { |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 203 | if (mOverviewProxy != null) { |
Matthew Ng | eb6893b | 2017-11-09 17:15:33 -0800 | [diff] [blame] | 204 | mOverviewProxy.asBinder().unlinkToDeath(mOverviewServiceDeathRcpt, 0); |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 205 | mContext.unbindService(mOverviewServiceConnection); |
| 206 | mOverviewProxy = null; |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 207 | notifyConnectionChanged(); |
Matthew Ng | 1fa3f7e | 2017-11-07 11:50:36 -0800 | [diff] [blame] | 208 | } |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 209 | } |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 210 | |
| 211 | private void notifyConnectionChanged() { |
| 212 | for (int i = mConnectionCallbacks.size() - 1; i >= 0; --i) { |
| 213 | mConnectionCallbacks.get(i).onConnectionChanged(mOverviewProxy != null); |
| 214 | } |
| 215 | } |
| 216 | |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 217 | @Override |
| 218 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Matthew Ng | bd82457 | 2018-01-17 16:25:56 -0800 | [diff] [blame] | 219 | pw.println(TAG_OPS + " state:"); |
Matthew Ng | 1e43ebd | 2017-11-14 14:47:05 -0800 | [diff] [blame] | 220 | pw.print(" mConnectionBackoffAttempts="); pw.println(mConnectionBackoffAttempts); |
| 221 | pw.print(" isCurrentUserSetup="); pw.println(mDeviceProvisionedController |
| 222 | .isCurrentUserSetup()); |
| 223 | pw.print(" isConnected="); pw.println(mOverviewProxy != null); |
| 224 | } |
| 225 | |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 226 | public interface OverviewProxyListener { |
Jorim Jaggi | f6401c0 | 2018-01-25 15:21:18 +0100 | [diff] [blame] | 227 | void onConnectionChanged(boolean isConnected); |
Matthew Ng | 7d05e77 | 2017-11-09 14:41:07 -0800 | [diff] [blame] | 228 | } |
Matthew Ng | 13dbf87 | 2017-10-27 11:02:14 -0700 | [diff] [blame] | 229 | } |