Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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.telecomm; |
| 18 | |
| 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 22 | import android.content.ServiceConnection; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ResolveInfo; |
| 25 | import android.content.pm.ServiceInfo; |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 26 | import android.os.Handler; |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 27 | import android.os.IBinder; |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 28 | import android.os.Looper; |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame] | 29 | import android.os.RemoteException; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 30 | import android.telecomm.ICallService; |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame] | 31 | import android.telecomm.ICallServiceLookupResponse; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 32 | import android.telecomm.ICallServiceProvider; |
| 33 | import android.util.Log; |
| 34 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 35 | import com.google.common.base.Preconditions; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 36 | import com.google.common.collect.Lists; |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 37 | import com.google.common.collect.Sets; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 38 | |
| 39 | import java.util.List; |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 40 | import java.util.Set; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Finds {@link ICallService} and {@link ICallServiceProvider} implementations on the device. |
| 44 | * Uses binder APIs to find ICallServiceProviders and calls method on ICallServiceProvider to |
| 45 | * find ICallService implementations. |
| 46 | * TODO(santoscordon): Add performance timing to async calls. |
| 47 | */ |
| 48 | final class CallServiceFinder { |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 49 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 50 | private static final String TAG = CallServiceFinder.class.getSimpleName(); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 51 | |
| 52 | /** |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 53 | * The longest period in milliseconds each lookup cycle is allowed to span over, see |
| 54 | * {@link #mLookupTerminator}. |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 55 | * TODO(gilad): Likely requires tuning. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 56 | */ |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 57 | private static final int LOOKUP_TIMEOUT_MS = 100; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 58 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 59 | /** |
| 60 | * Used to retrieve all known ICallServiceProvider implementations from the framework. |
| 61 | * TODO(gilad): Move to a more logical place for this to be shared. |
| 62 | */ |
| 63 | static final String CALL_SERVICE_PROVIDER_CLASS_NAME = ICallServiceProvider.class.getName(); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 64 | |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 65 | private final Switchboard mSwitchboard; |
| 66 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 67 | /** |
| 68 | * Determines whether or not a lookup cycle is already running. |
| 69 | */ |
| 70 | private boolean mIsLookupInProgress = false; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 71 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Used to generate unique lookup-cycle identifiers. Incremented upon initiateLookup calls. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 74 | * TODO(gilad): If at all useful, consider porting the cycle ID concept to switchboard and |
| 75 | * have it centralized/shared between the two finders. |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 76 | */ |
| 77 | private int mNextLookupId = 0; |
| 78 | |
| 79 | /** |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame^] | 80 | * The set of bound call-services. Only populated via initiateLookup scenarios. Entries should |
| 81 | * only be removed upon unbinding. |
| 82 | * TODO(gilad): Add the necessary logic to keep this set up to date. |
| 83 | */ |
| 84 | private Set<ICallService> mCallServiceRegistry = Sets.newHashSet(); |
| 85 | |
| 86 | /** |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 87 | * The set of bound call-service providers. Only populated via initiateLookup scenarios. |
| 88 | * Providers should only be removed upon unbinding. |
| 89 | */ |
| 90 | private Set<ICallServiceProvider> mProviderRegistry = Sets.newHashSet(); |
| 91 | |
| 92 | /** |
| 93 | * Stores the names of the providers to bind to in one lookup cycle. The set size represents |
| 94 | * the number of call-service providers this finder expects to hear back from upon initiating |
| 95 | * call-service lookups, see initiateLookup. Whenever all providers respond before the lookup |
| 96 | * timeout occurs, the complete set of (available) call services is passed to the switchboard |
| 97 | * for further processing of outgoing calls etc. When the timeout occurs before all responds |
| 98 | * are received, the partial (potentially empty) set gets passed (to the switchboard) instead. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 99 | * Cached providers do not require finding and hence are excluded from this set. Entries are |
| 100 | * removed from this set as providers register. |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 101 | */ |
| 102 | private Set<ComponentName> mUnregisteredProviders; |
| 103 | |
| 104 | /** |
| 105 | * Used to interrupt lookup cycles that didn't terminate naturally within the allowed |
| 106 | * period, see LOOKUP_TIMEOUT. |
| 107 | */ |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 108 | private final Runnable mLookupTerminator = new Runnable() { |
| 109 | @Override |
| 110 | public void run() { |
| 111 | terminateLookup(); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | /** Used to run code (e.g. messages, Runnables) on the main (UI) thread. */ |
| 116 | private final Handler mHandler = new Handler(Looper.getMainLooper()); |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 117 | |
| 118 | /** |
Ben Gilad | 0407fb2 | 2014-01-09 16:18:41 -0800 | [diff] [blame] | 119 | * Persists the specified parameters. |
| 120 | * |
| 121 | * @param switchboard The switchboard for this finer to work against. |
| 122 | */ |
| 123 | CallServiceFinder(Switchboard switchboard) { |
| 124 | mSwitchboard = switchboard; |
| 125 | } |
| 126 | |
| 127 | /** |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 128 | * Initiates a lookup cycle for call-service providers. Must be called from the UI thread. |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 129 | * TODO(gilad): Expand this comment to describe the lookup flow in more detail. |
| 130 | * |
| 131 | * @param context The relevant application context. |
| 132 | */ |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 133 | void initiateLookup(Context context) { |
| 134 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 135 | if (mIsLookupInProgress) { |
| 136 | // At most one active lookup is allowed at any given time, bail out. |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | List<ComponentName> providerNames = getProviderNames(context); |
| 141 | if (providerNames.isEmpty()) { |
| 142 | Log.i(TAG, "No ICallServiceProvider implementations found."); |
| 143 | updateSwitchboard(); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | mIsLookupInProgress = true; |
| 148 | mUnregisteredProviders = Sets.newHashSet(); |
| 149 | |
| 150 | int lookupId = mNextLookupId++; |
| 151 | for (ComponentName name : providerNames) { |
| 152 | if (!mProviderRegistry.contains(name)) { |
| 153 | // The provider is either not yet registered or has been unregistered |
| 154 | // due to unbinding etc. |
Ben Gilad | 80ddb5d | 2014-01-15 14:48:29 -0800 | [diff] [blame] | 155 | bindProvider(name, lookupId, context); |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 156 | mUnregisteredProviders.add(name); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | int providerCount = providerNames.size(); |
| 161 | int unregisteredProviderCount = mUnregisteredProviders.size(); |
| 162 | |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 163 | Log.i(TAG, "Found " + providerCount + " implementations of ICallServiceProvider, " |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 164 | + unregisteredProviderCount + " of which are not currently registered."); |
| 165 | |
| 166 | if (unregisteredProviderCount == 0) { |
| 167 | // All known (provider) implementations are already registered, pass control |
| 168 | // back to the switchboard. |
| 169 | updateSwitchboard(); |
| 170 | } else { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 171 | // Schedule a lookup terminator to run after LOOKUP_TIMEOUT_MS milliseconds. |
| 172 | mHandler.removeCallbacks(mLookupTerminator); |
| 173 | mHandler.postDelayed(mLookupTerminator, LOOKUP_TIMEOUT_MS); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 178 | * Returns the all-inclusive list of call-service-provider names. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 179 | * |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 180 | * @param context The relevant/application context to query against. |
| 181 | * @return The list containing the (component) names of all known ICallServiceProvider |
| 182 | * implementations or the empty list upon no available providers. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 183 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 184 | private List<ComponentName> getProviderNames(Context context) { |
| 185 | // The list of provider names to return to the caller, may be populated below. |
| 186 | List<ComponentName> providerNames = Lists.newArrayList(); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 187 | |
| 188 | PackageManager packageManager = context.getPackageManager(); |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 189 | Intent intent = new Intent(CALL_SERVICE_PROVIDER_CLASS_NAME); |
| 190 | for (ResolveInfo entry : packageManager.queryIntentServices(intent, 0)) { |
| 191 | ServiceInfo serviceInfo = entry.serviceInfo; |
| 192 | if (serviceInfo != null) { |
| 193 | // The entry resolves to a proper service, add it to the list of provider names. |
| 194 | providerNames.add( |
| 195 | new ComponentName(serviceInfo.packageName, serviceInfo.name)); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 196 | } |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 199 | return providerNames; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
Ben Gilad | 80ddb5d | 2014-01-15 14:48:29 -0800 | [diff] [blame] | 203 | * Attempts to bind the specified provider and have it register upon successful binding. Also |
| 204 | * performs the necessary wiring to unregister the provider upon un-binding. |
| 205 | * |
| 206 | * @param providerName The component name of the relevant provider. |
| 207 | * @param lookupId The lookup-cycle ID. |
| 208 | * @param context The relevant application context. |
| 209 | */ |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 210 | private void bindProvider( |
Ben Gilad | 80ddb5d | 2014-01-15 14:48:29 -0800 | [diff] [blame] | 211 | final ComponentName providerName, final int lookupId, Context context) { |
| 212 | |
| 213 | Preconditions.checkNotNull(providerName); |
| 214 | Preconditions.checkNotNull(context); |
| 215 | |
| 216 | Intent serviceIntent = |
| 217 | new Intent(CALL_SERVICE_PROVIDER_CLASS_NAME).setComponent(providerName); |
| 218 | Log.i(TAG, "Binding to ICallServiceProvider through " + serviceIntent); |
| 219 | |
| 220 | // Connection object for the service binding. |
| 221 | ServiceConnection connection = new ServiceConnection() { |
| 222 | @Override |
| 223 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 224 | ICallServiceProvider provider = ICallServiceProvider.Stub.asInterface(service); |
| 225 | registerProvider(lookupId, providerName, provider); |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void onServiceDisconnected(ComponentName className) { |
| 230 | unregisterProvider(providerName); |
| 231 | } |
| 232 | }; |
| 233 | |
| 234 | if (!context.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) { |
| 235 | // TODO(santoscordon): Handle error. |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /** |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame] | 240 | * Queries the supplied provider asynchronously for its CallServices and passes the list through |
| 241 | * to {@link #registerCallServices} which will relinquish control back to switchboard. |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 242 | * |
| 243 | * @param lookupId The lookup-cycle ID. |
| 244 | * @param providerName The component name of the relevant provider. |
| 245 | * @param provider The provider object to register. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 246 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 247 | private void registerProvider( |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame] | 248 | final int lookupId, |
| 249 | final ComponentName providerName, |
| 250 | final ICallServiceProvider provider) { |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 251 | |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame] | 252 | // Query the provider for {@link ICallService} implementations. |
| 253 | try { |
| 254 | provider.lookupCallServices(new ICallServiceLookupResponse.Stub() { |
| 255 | @Override |
| 256 | public void onResult(List<IBinder> binderList) { |
| 257 | List<ICallService> callServices = Lists.newArrayList(); |
| 258 | for (IBinder binder : binderList) { |
| 259 | callServices.add(ICallService.Stub.asInterface(binder)); |
| 260 | } |
| 261 | registerCallServices(lookupId, providerName, provider, callServices); |
| 262 | } |
| 263 | }); |
| 264 | } catch (RemoteException e) { |
| 265 | Log.e(TAG, "Could not retrieve call services from: " + providerName); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Registers the {@link CallService}s for the specified provider and performs the necessary |
| 271 | * bookkeeping to potentially return control to the switchboard before the timeout for the |
| 272 | * current lookup cycle. |
| 273 | * TODO(santoscordon): Consider replacing this method's use of synchronized with a Handler |
| 274 | * queue. |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 275 | * TODO(gilad): Santos has some POC code to make synchronized (below) unnecessary, either |
| 276 | * move to use that or remove this to-do. |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame^] | 277 | * TODO(gilad): callServices should probably be a set, consider refactoring. |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame] | 278 | * |
| 279 | * @param lookupId The lookup-cycle ID. |
| 280 | * @param providerName The component name of the relevant provider. |
| 281 | * @param provider The provider associated with callServices. |
| 282 | * @param callServices The {@link CallService}s to register. |
| 283 | */ |
| 284 | synchronized private void registerCallServices( |
| 285 | int lookupId, |
| 286 | ComponentName providerName, |
| 287 | ICallServiceProvider provider, |
| 288 | List<ICallService> callServices) { |
| 289 | |
| 290 | // TODO(santoscordon): When saving the call services into this class, also add code to |
Ben Gilad | b59769e | 2014-01-16 11:41:10 -0800 | [diff] [blame] | 291 | // unregister (remove) the call services upon disconnect. Potentially use RemoteCallbackList. |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame] | 292 | |
| 293 | if (mUnregisteredProviders.remove(providerName)) { |
| 294 | mProviderRegistry.add(provider); |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame^] | 295 | mCallServiceRegistry.addAll(callServices); |
| 296 | |
| 297 | // TODO(gilad): Introduce a map to retain the association between call services |
| 298 | // and the corresponding provider such that mCallServiceRegistry can be updated |
| 299 | // upon unregisterProvider calls. |
| 300 | |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame] | 301 | if (mUnregisteredProviders.size() < 1) { |
| 302 | terminateLookup(); // No other providers to wait for. |
| 303 | } |
| 304 | } else { |
| 305 | Log.i(TAG, "Received multiple lists of call services in lookup " + lookupId + |
| 306 | " from " + providerName); |
| 307 | } |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Unregisters the specified provider. |
| 312 | * |
| 313 | * @param providerName The component name of the relevant provider. |
| 314 | */ |
| 315 | private void unregisterProvider(ComponentName providerName) { |
| 316 | mProviderRegistry.remove(providerName); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Timeouts the current lookup cycle, see LookupTerminator. |
| 321 | */ |
| 322 | private void terminateLookup() { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 323 | mHandler.removeCallbacks(mLookupTerminator); |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 324 | |
| 325 | updateSwitchboard(); |
| 326 | mIsLookupInProgress = false; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Updates the switchboard passing the relevant call services (as opposed |
| 331 | * to call-service providers). |
| 332 | */ |
| 333 | private void updateSwitchboard() { |
Evan Charlton | 0958f53 | 2014-01-10 16:58:02 -0800 | [diff] [blame] | 334 | ThreadUtil.checkOnMainThread(); |
Ben Gilad | 03292d4 | 2014-01-16 15:06:16 -0800 | [diff] [blame^] | 335 | mSwitchboard.setCallServices(mCallServiceRegistry); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 336 | } |
| 337 | } |