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