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