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; |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 26 | import android.os.IBinder; |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame^] | 27 | import android.os.Bundle; |
| 28 | import android.os.RemoteException; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 29 | import android.telecomm.ICallService; |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame^] | 30 | import android.telecomm.ICallServiceLookupResponse; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 31 | import android.telecomm.ICallServiceProvider; |
| 32 | import android.util.Log; |
| 33 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 34 | import com.google.common.base.Preconditions; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 35 | import com.google.common.collect.Lists; |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 36 | import com.google.common.collect.Sets; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 37 | |
| 38 | import java.util.List; |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 39 | import java.util.Set; |
| 40 | import java.util.Timer; |
| 41 | import java.util.TimerTask; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Finds {@link ICallService} and {@link ICallServiceProvider} implementations on the device. |
| 45 | * Uses binder APIs to find ICallServiceProviders and calls method on ICallServiceProvider to |
| 46 | * find ICallService implementations. |
| 47 | * TODO(santoscordon): Add performance timing to async calls. |
| 48 | */ |
| 49 | final class CallServiceFinder { |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 50 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 51 | /** |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 52 | * Helper class to register/unregister call-service providers. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 53 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 54 | private class ProviderRegistrar { |
| 55 | |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 56 | /** |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 57 | * The name of the call-service provider that is expected to register with this finder. |
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 | private ComponentName mProviderName; |
| 60 | |
| 61 | /** |
| 62 | * A unique identifier for a given lookup cycle, see nextLookupId. |
| 63 | * TODO(gilad): Potentially unnecessary, consider removing. |
| 64 | */ |
| 65 | int mLookupId; |
| 66 | |
| 67 | /** |
| 68 | * Persists the specified parameters. |
| 69 | * |
| 70 | * @param providerName The component name of the relevant provider. |
| 71 | * @param lookupId The lookup-cycle ID. |
| 72 | */ |
| 73 | ProviderRegistrar(ComponentName providerName, int lookupId) { |
| 74 | this.mProviderName = providerName; |
| 75 | this.mLookupId = lookupId; |
| 76 | } |
| 77 | |
| 78 | ComponentName getProviderName() { |
| 79 | return mProviderName; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Registers the specified call-service provider. |
| 84 | * |
| 85 | * @param provider The provider object to register. |
| 86 | */ |
| 87 | void register(ICallServiceProvider provider) { |
| 88 | registerProvider(mLookupId, mProviderName, provider); |
| 89 | } |
| 90 | |
| 91 | /** Unregisters this provider. */ |
| 92 | void unregister() { |
| 93 | unregisterProvider(mProviderName); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Wrapper around ICallServiceProvider, mostly used for binding etc. |
| 99 | * |
| 100 | * TODO(gilad): Consider making this wrapper unnecessary. |
| 101 | */ |
| 102 | private class ProviderWrapper { |
| 103 | |
| 104 | /** |
| 105 | * Persists the specified parameters and attempts to bind the specified provider. |
| 106 | * |
| 107 | * TODO(gilad): Consider embedding ProviderRegistrar into this class and do away |
| 108 | * with the former, or vice versa. |
| 109 | * |
| 110 | * @param context The relevant application context. |
| 111 | * @param registrar The registrar with which to register and unregister this provider. |
| 112 | */ |
| 113 | ProviderWrapper(Context context, final ProviderRegistrar registrar) { |
| 114 | ComponentName name = registrar.getProviderName(); |
| 115 | Preconditions.checkNotNull(name); |
| 116 | Preconditions.checkNotNull(context); |
| 117 | |
| 118 | Intent serviceIntent = new Intent(CALL_SERVICE_PROVIDER_CLASS_NAME).setComponent(name); |
| 119 | Log.i(TAG, "Binding to ICallServiceProvider through " + serviceIntent); |
| 120 | |
| 121 | // Connection object for the service binding. |
| 122 | ServiceConnection connection = new ServiceConnection() { |
| 123 | @Override |
| 124 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 125 | registrar.register(ICallServiceProvider.Stub.asInterface(service)); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void onServiceDisconnected(ComponentName className) { |
| 130 | registrar.unregister(); |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | if (!context.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) { |
| 135 | // TODO(santoscordon): Handle error. |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * A timer task to ensure each lookup cycle is time-bound, see LOOKUP_TIMEOUT. |
| 142 | */ |
| 143 | private class LookupTerminator extends TimerTask { |
| 144 | @Override |
| 145 | public void run() { |
| 146 | terminateLookup(); |
| 147 | } |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** Used to identify log entries by this class */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 151 | private static final String TAG = CallServiceFinder.class.getSimpleName(); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 152 | |
| 153 | /** |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 154 | * The longest period in milliseconds each lookup cycle is allowed to span over, see mTimer. |
| 155 | * TODO(gilad): Likely requires tuning. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 156 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 157 | private static final int LOOKUP_TIMEOUT = 100; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 158 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 159 | /** |
| 160 | * Used to retrieve all known ICallServiceProvider implementations from the framework. |
| 161 | * TODO(gilad): Move to a more logical place for this to be shared. |
| 162 | */ |
| 163 | static final String CALL_SERVICE_PROVIDER_CLASS_NAME = ICallServiceProvider.class.getName(); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 164 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 165 | /** |
| 166 | * Determines whether or not a lookup cycle is already running. |
| 167 | */ |
| 168 | private boolean mIsLookupInProgress = false; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 169 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 170 | /** |
| 171 | * Used to generate unique lookup-cycle identifiers. Incremented upon initiateLookup calls. |
| 172 | */ |
| 173 | private int mNextLookupId = 0; |
| 174 | |
| 175 | /** |
| 176 | * The set of bound call-service providers. Only populated via initiateLookup scenarios. |
| 177 | * Providers should only be removed upon unbinding. |
| 178 | */ |
| 179 | private Set<ICallServiceProvider> mProviderRegistry = Sets.newHashSet(); |
| 180 | |
| 181 | /** |
| 182 | * Stores the names of the providers to bind to in one lookup cycle. The set size represents |
| 183 | * the number of call-service providers this finder expects to hear back from upon initiating |
| 184 | * call-service lookups, see initiateLookup. Whenever all providers respond before the lookup |
| 185 | * timeout occurs, the complete set of (available) call services is passed to the switchboard |
| 186 | * for further processing of outgoing calls etc. When the timeout occurs before all responds |
| 187 | * are received, the partial (potentially empty) set gets passed (to the switchboard) instead. |
| 188 | * Note that cached providers do not require finding and hence are excluded from this count. |
| 189 | * Also noteworthy is that providers are dynamically removed from this set as they register. |
| 190 | */ |
| 191 | private Set<ComponentName> mUnregisteredProviders; |
| 192 | |
| 193 | /** |
| 194 | * Used to interrupt lookup cycles that didn't terminate naturally within the allowed |
| 195 | * period, see LOOKUP_TIMEOUT. |
| 196 | */ |
| 197 | private Timer mTimer; |
| 198 | |
| 199 | /** |
| 200 | * Initiates a lookup cycle for call-service providers. |
| 201 | * TODO(gilad): Expand this comment to describe the lookup flow in more detail. |
| 202 | * |
| 203 | * @param context The relevant application context. |
| 204 | */ |
| 205 | public synchronized void initiateLookup(Context context) { |
| 206 | if (mIsLookupInProgress) { |
| 207 | // At most one active lookup is allowed at any given time, bail out. |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | List<ComponentName> providerNames = getProviderNames(context); |
| 212 | if (providerNames.isEmpty()) { |
| 213 | Log.i(TAG, "No ICallServiceProvider implementations found."); |
| 214 | updateSwitchboard(); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | mIsLookupInProgress = true; |
| 219 | mUnregisteredProviders = Sets.newHashSet(); |
| 220 | |
| 221 | int lookupId = mNextLookupId++; |
| 222 | for (ComponentName name : providerNames) { |
| 223 | if (!mProviderRegistry.contains(name)) { |
| 224 | // The provider is either not yet registered or has been unregistered |
| 225 | // due to unbinding etc. |
| 226 | ProviderRegistrar registrar = new ProviderRegistrar(name, lookupId); |
| 227 | new ProviderWrapper(context, registrar); |
| 228 | mUnregisteredProviders.add(name); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | int providerCount = providerNames.size(); |
| 233 | int unregisteredProviderCount = mUnregisteredProviders.size(); |
| 234 | |
| 235 | Log.i(TAG, "Found " + providerCount + " implementations for ICallServiceProvider, " |
| 236 | + unregisteredProviderCount + " of which are not currently registered."); |
| 237 | |
| 238 | if (unregisteredProviderCount == 0) { |
| 239 | // All known (provider) implementations are already registered, pass control |
| 240 | // back to the switchboard. |
| 241 | updateSwitchboard(); |
| 242 | } else { |
| 243 | // Start the timeout for this lookup cycle. |
| 244 | // TODO(gilad): Consider reusing the same timer instead of creating new ones. |
| 245 | if (mTimer != null) { |
| 246 | // Shouldn't be running but better safe than sorry. |
| 247 | mTimer.cancel(); |
| 248 | } |
| 249 | mTimer = new Timer(); |
| 250 | mTimer.schedule(new LookupTerminator(), LOOKUP_TIMEOUT); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | /** |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 255 | * Returns the all-inclusive list of call-service-provider names. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 256 | * |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 257 | * @param context The relevant/application context to query against. |
| 258 | * @return The list containing the (component) names of all known ICallServiceProvider |
| 259 | * implementations or the empty list upon no available providers. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 260 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 261 | private List<ComponentName> getProviderNames(Context context) { |
| 262 | // The list of provider names to return to the caller, may be populated below. |
| 263 | List<ComponentName> providerNames = Lists.newArrayList(); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 264 | |
| 265 | PackageManager packageManager = context.getPackageManager(); |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 266 | Intent intent = new Intent(CALL_SERVICE_PROVIDER_CLASS_NAME); |
| 267 | for (ResolveInfo entry : packageManager.queryIntentServices(intent, 0)) { |
| 268 | ServiceInfo serviceInfo = entry.serviceInfo; |
| 269 | if (serviceInfo != null) { |
| 270 | // The entry resolves to a proper service, add it to the list of provider names. |
| 271 | providerNames.add( |
| 272 | new ComponentName(serviceInfo.packageName, serviceInfo.name)); |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 273 | } |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 274 | } |
| 275 | |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 276 | return providerNames; |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /** |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame^] | 280 | * Queries the supplied provider asynchronously for its CallServices and passes the list through |
| 281 | * to {@link #registerCallServices} which will relinquish control back to switchboard. |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 282 | * |
| 283 | * @param lookupId The lookup-cycle ID. |
| 284 | * @param providerName The component name of the relevant provider. |
| 285 | * @param provider The provider object to register. |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 286 | */ |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 287 | private void registerProvider( |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame^] | 288 | final int lookupId, |
| 289 | final ComponentName providerName, |
| 290 | final ICallServiceProvider provider) { |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 291 | |
Santos Cordon | cb83fb6 | 2014-01-06 10:57:57 -0800 | [diff] [blame^] | 292 | // Query the provider for {@link ICallService} implementations. |
| 293 | try { |
| 294 | provider.lookupCallServices(new ICallServiceLookupResponse.Stub() { |
| 295 | @Override |
| 296 | public void onResult(List<IBinder> binderList) { |
| 297 | List<ICallService> callServices = Lists.newArrayList(); |
| 298 | for (IBinder binder : binderList) { |
| 299 | callServices.add(ICallService.Stub.asInterface(binder)); |
| 300 | } |
| 301 | registerCallServices(lookupId, providerName, provider, callServices); |
| 302 | } |
| 303 | }); |
| 304 | } catch (RemoteException e) { |
| 305 | Log.e(TAG, "Could not retrieve call services from: " + providerName); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * Registers the {@link CallService}s for the specified provider and performs the necessary |
| 311 | * bookkeeping to potentially return control to the switchboard before the timeout for the |
| 312 | * current lookup cycle. |
| 313 | * TODO(santoscordon): Consider replacing this method's use of synchronized with a Handler |
| 314 | * queue. |
| 315 | * |
| 316 | * @param lookupId The lookup-cycle ID. |
| 317 | * @param providerName The component name of the relevant provider. |
| 318 | * @param provider The provider associated with callServices. |
| 319 | * @param callServices The {@link CallService}s to register. |
| 320 | */ |
| 321 | synchronized private void registerCallServices( |
| 322 | int lookupId, |
| 323 | ComponentName providerName, |
| 324 | ICallServiceProvider provider, |
| 325 | List<ICallService> callServices) { |
| 326 | |
| 327 | // TODO(santoscordon): When saving the call services into this class, also add code to |
| 328 | // unregister (remove) the call services upon disconnect. Potenially use RemoteCallbackList. |
| 329 | |
| 330 | if (mUnregisteredProviders.remove(providerName)) { |
| 331 | mProviderRegistry.add(provider); |
| 332 | if (mUnregisteredProviders.size() < 1) { |
| 333 | terminateLookup(); // No other providers to wait for. |
| 334 | } |
| 335 | } else { |
| 336 | Log.i(TAG, "Received multiple lists of call services in lookup " + lookupId + |
| 337 | " from " + providerName); |
| 338 | } |
Ben Gilad | d17443c | 2014-01-06 11:04:15 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Unregisters the specified provider. |
| 343 | * |
| 344 | * @param providerName The component name of the relevant provider. |
| 345 | */ |
| 346 | private void unregisterProvider(ComponentName providerName) { |
| 347 | mProviderRegistry.remove(providerName); |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Timeouts the current lookup cycle, see LookupTerminator. |
| 352 | */ |
| 353 | private void terminateLookup() { |
| 354 | if (mTimer != null) { |
| 355 | mTimer.cancel(); // Terminate the timer thread. |
| 356 | } |
| 357 | |
| 358 | updateSwitchboard(); |
| 359 | mIsLookupInProgress = false; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Updates the switchboard passing the relevant call services (as opposed |
| 364 | * to call-service providers). |
| 365 | */ |
| 366 | private void updateSwitchboard() { |
| 367 | synchronized (mProviderRegistry) { |
| 368 | // TODO(gilad): More here. |
| 369 | } |
Santos Cordon | 8e8b8d2 | 2013-12-19 14:14:05 -0800 | [diff] [blame] | 370 | } |
| 371 | } |