blob: 9d960a1ec89f5b54628c0dcd87d3938c306e0d6c [file] [log] [blame]
Matthew Ng13dbf872017-10-27 11:02:14 -07001/*
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
17package com.android.systemui;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
Winson Chung38d31c22017-11-08 14:32:32 -080023import android.graphics.Bitmap;
24import android.graphics.Rect;
25import android.os.Binder;
Matthew Ng13dbf872017-10-27 11:02:14 -070026import android.os.Handler;
27import android.os.IBinder;
28import android.os.RemoteException;
29import android.os.UserHandle;
30import android.util.Log;
Winson Chung38d31c22017-11-08 14:32:32 -080031import android.view.SurfaceControl;
32
33import com.android.systemui.shared.recents.IOverviewProxy;
34import com.android.systemui.shared.recents.ISystemUiProxy;
Matthew Ng13dbf872017-10-27 11:02:14 -070035import com.android.systemui.statusbar.policy.DeviceProvisionedController;
36import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener;
37
38/**
39 * Class to send information from overview to launcher with a binder.
40 */
41public 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 Chung38d31c22017-11-08 14:32:32 -080055 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 Ng13dbf872017-10-27 11:02:14 -070068 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 Chung38d31c22017-11-08 14:32:32 -080080 try {
81 mOverviewProxy.onBind(mSysUiProxy);
82 } catch (RemoteException e) {
83 Log.e(TAG, "Failed to call onBind()", e);
84 }
Matthew Ng13dbf872017-10-27 11:02:14 -070085 }
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 Ngdfab86c2017-11-07 15:46:51 -080097 public void onUserSetupChanged() {
Matthew Ng13dbf872017-10-27 11:02:14 -070098 if (mDeviceProvisionedController.isCurrentUserSetup()) {
99 startConnectionToCurrentUser();
100 }
101 }
102
103 @Override
104 public void onUserSwitched() {
Matthew Ng13dbf872017-10-27 11:02:14 -0700105 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 Ng1fa3f7e2017-11-07 11:50:36 -0800126 disconnectFromLauncherService();
127
Matthew Ng13dbf872017-10-27 11:02:14 -0700128 // If user has not setup yet or already connected, do not try to connect
Matthew Ng1fa3f7e2017-11-07 11:50:36 -0800129 if (!mDeviceProvisionedController.isCurrentUserSetup()) {
Matthew Ng13dbf872017-10-27 11:02:14 -0700130 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 Ng1fa3f7e2017-11-07 11:50:36 -0800152 if (mOverviewProxy != null) {
153 mContext.unbindService(mOverviewServiceConnection);
154 mOverviewProxy = null;
155 }
Matthew Ng13dbf872017-10-27 11:02:14 -0700156 }
157}