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