blob: 3878cd1cf92632202dff4099c7af130125c3f2d2 [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;
23import android.os.Handler;
24import android.os.IBinder;
25import android.os.RemoteException;
26import android.os.UserHandle;
27import android.util.Log;
28import com.android.systemui.shared.recents.model.IOverviewProxy;
29import com.android.systemui.statusbar.policy.DeviceProvisionedController;
30import com.android.systemui.statusbar.policy.DeviceProvisionedController.DeviceProvisionedListener;
31
32/**
33 * Class to send information from overview to launcher with a binder.
34 */
35public class OverviewProxyService {
36
37 private static final String TAG = "OverviewProxyService";
38 private static final long BACKOFF_MILLIS = 5000;
39
40 private final Context mContext;
41 private final Handler mHandler;
42 private final Runnable mConnectionRunnable = this::startConnectionToCurrentUser;
43 private final DeviceProvisionedController mDeviceProvisionedController
44 = Dependency.get(DeviceProvisionedController.class);
45
46 private IOverviewProxy mOverviewProxy;
47 private int mConnectionBackoffAttempts;
48
49 private final ServiceConnection mOverviewServiceConnection = new ServiceConnection() {
50 @Override
51 public void onServiceConnected(ComponentName name, IBinder service) {
52 if (service != null) {
53 mConnectionBackoffAttempts = 0;
54 mOverviewProxy = IOverviewProxy.Stub.asInterface(service);
55 // Listen for launcher's death
56 try {
57 service.linkToDeath(mOverviewServiceDeathRcpt, 0);
58 } catch (RemoteException e) {
59 Log.e(TAG, "Lost connection to launcher service", e);
60 }
61 }
62 }
63
64 @Override
65 public void onServiceDisconnected(ComponentName name) {
66 // Do nothing
67 }
68 };
69
70 private final DeviceProvisionedListener mDeviceProvisionedCallback =
71 new DeviceProvisionedListener() {
72 @Override
73 public void onDeviceProvisionedChanged() {
74 if (mDeviceProvisionedController.isCurrentUserSetup()) {
75 startConnectionToCurrentUser();
76 }
77 }
78
79 @Override
80 public void onUserSwitched() {
81 disconnectFromLauncherService();
82 mConnectionBackoffAttempts = 0;
83 startConnectionToCurrentUser();
84 }
85 };
86
87 // This is the death handler for the binder from the launcher service
88 private final IBinder.DeathRecipient mOverviewServiceDeathRcpt = new IBinder.DeathRecipient() {
89 @Override
90 public void binderDied() {
91 mOverviewProxy = null;
92 }
93 };
94
95 public OverviewProxyService(Context context) {
96 mContext = context;
97 mHandler = new Handler();
98 mConnectionBackoffAttempts = 0;
99 mDeviceProvisionedController.addCallback(mDeviceProvisionedCallback);
100 }
101
102 public void startConnectionToCurrentUser() {
103 // If user has not setup yet or already connected, do not try to connect
104 if (!mDeviceProvisionedController.isCurrentUserSetup() || mOverviewProxy != null) {
105 return;
106 }
107 mHandler.removeCallbacks(mConnectionRunnable);
108 Intent launcherServiceIntent = new Intent();
109 launcherServiceIntent.setComponent(ComponentName.unflattenFromString(
110 mContext.getString(R.string.config_overviewServiceComponent)));
111 boolean bound = mContext.bindServiceAsUser(launcherServiceIntent,
112 mOverviewServiceConnection, Context.BIND_AUTO_CREATE,
113 UserHandle.getUserHandleForUid(mDeviceProvisionedController.getCurrentUser()));
114 if (!bound) {
115 // Retry after exponential backoff timeout
116 final long timeoutMs = (long) Math.scalb(BACKOFF_MILLIS, mConnectionBackoffAttempts);
117 mHandler.postDelayed(mConnectionRunnable, timeoutMs);
118 mConnectionBackoffAttempts++;
119 }
120 }
121
122 public IOverviewProxy getProxy() {
123 return mOverviewProxy;
124 }
125
126 private void disconnectFromLauncherService() {
127 mContext.unbindService(mOverviewServiceConnection);
128 mOverviewProxy = null;
129 }
130}