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