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