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