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; |
| 22 | import android.content.ServiceConnection; |
| 23 | import android.os.IBinder; |
| 24 | import android.telecomm.ICallServiceProvider; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | import com.google.common.base.Preconditions; |
| 28 | |
| 29 | /** |
| 30 | * A proxy to a bound CallServiceProvider implementation. Given a {@link ComponentName}, this class |
| 31 | * will bind, maintain and unbind a connection with a CallServiceProvider. |
| 32 | */ |
| 33 | class CallServiceProviderProxy { |
| 34 | /** |
| 35 | * Interface used for notifying when the {@link ICallServiceProvider} is bound. |
| 36 | */ |
| 37 | public interface CallServiceProviderConnectionCallback { |
| 38 | public void onConnected(ICallServiceProvider provider); |
| 39 | } |
| 40 | |
| 41 | /** Used to identify log entries by this class */ |
| 42 | static final String TAG = CallServiceFinder.class.getSimpleName(); |
| 43 | |
| 44 | /** Context used to bind with ICallServiceProvider. */ |
| 45 | private final Context mContext; |
| 46 | |
| 47 | /** |
| 48 | * Explicit component name of of the ICallServiceProvider implementation with which to bind. |
| 49 | */ |
| 50 | private final ComponentName mComponentName; |
| 51 | |
| 52 | /** |
| 53 | * Persists the specified parameters. |
| 54 | */ |
| 55 | public CallServiceProviderProxy(ComponentName componentName, Context context) { |
| 56 | mComponentName = Preconditions.checkNotNull(componentName); |
| 57 | mContext = Preconditions.checkNotNull(context); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Binds with the {@link ICallServiceProvider} implementation specified by |
| 62 | * {@link #mComponentName}. |
| 63 | */ |
| 64 | public void connect(final CallServiceProviderConnectionCallback connectionCallback) { |
| 65 | // TODO(santoscordon): Are there cases where we are already connected and should return |
| 66 | // early with a saved instance? |
| 67 | |
| 68 | Intent serviceIntent = getServiceIntent(); |
| 69 | Log.i(TAG, "Binding to ICallService through " + serviceIntent); |
| 70 | |
| 71 | // Connection object for the service binding. |
| 72 | ServiceConnection connection = new ServiceConnection() { |
| 73 | @Override |
| 74 | public void onServiceConnected(ComponentName className, IBinder service) { |
| 75 | onConnected(ICallServiceProvider.Stub.asInterface(service), this, |
| 76 | connectionCallback); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void onServiceDisconnected(ComponentName className) { |
| 81 | onDisconnected(this); |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) { |
| 86 | // TODO(santoscordon): Handle error |
| 87 | } |
| 88 | |
| 89 | // At this point, we should get called on onServiceConnected asynchronously |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Returns the service {@link Intent} used to bind to {@link ICallServiceProvider} instances. |
| 94 | */ |
| 95 | private Intent getServiceIntent() { |
| 96 | Intent serviceIntent = new Intent(ICallServiceProvider.class.getName()); |
| 97 | serviceIntent.setComponent(mComponentName); |
| 98 | return serviceIntent; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Called when an instance of ICallServiceProvider is bound to this process. |
| 103 | * |
| 104 | * @param serviceProvider The {@link ICallServiceProvider} instance that was bound. |
| 105 | * @param connection The service connection used to bind to serviceProvider. |
| 106 | */ |
| 107 | private void onConnected(ICallServiceProvider serviceProvider, ServiceConnection connection, |
| 108 | CallServiceProviderConnectionCallback connectionCallback) { |
| 109 | // TODO(santoscordon): add some error conditions |
| 110 | |
| 111 | connectionCallback.onConnected(serviceProvider); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Called when ICallServiceProvider is disconnected. This could be for any reason including |
| 116 | * the host process dying. |
| 117 | * |
| 118 | * @param connection The service connection used to bind initially. |
| 119 | */ |
| 120 | private void onDisconnected(ServiceConnection connection) { |
| 121 | // TODO(santoscordon): How to handle disconnection? |
| 122 | } |
| 123 | } |