Santos Cordon | 63aeb16 | 2014-02-10 09:20:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.os.IInterface; |
| 25 | |
| 26 | import com.google.common.base.Preconditions; |
| 27 | import com.google.common.base.Strings; |
| 28 | |
| 29 | /** |
| 30 | * Abstract class to perform the work of binding and unbinding to the specified service interface. |
| 31 | * Subclasses supply the service intent and component name and this class will invoke protected |
| 32 | * methods when the class is bound, unbound, or upon failure. |
| 33 | */ |
| 34 | abstract class ServiceBinder<ServiceInterface extends IInterface> { |
| 35 | |
| 36 | private final class ServiceBinderConnection implements ServiceConnection { |
| 37 | @Override |
| 38 | public void onServiceConnected(ComponentName componentName, IBinder binder) { |
| 39 | ThreadUtil.checkOnMainThread(); |
| 40 | |
| 41 | // Unbind request was queued so unbind immediately. |
| 42 | if (mIsBindingAborted) { |
| 43 | clearAbort(); |
| 44 | mContext.unbindService(this); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | mServiceConnection = this; |
| 49 | mBinder = binder; |
| 50 | handleSuccessfulConnection(binder); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public void onServiceDisconnected(ComponentName componentName) { |
| 55 | mServiceConnection = null; |
| 56 | clearAbort(); |
| 57 | |
| 58 | handleServiceDisconnected(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** The application context. */ |
| 63 | private final Context mContext; |
| 64 | |
| 65 | /** The intent action to use when binding through {@link Context#bindService}. */ |
| 66 | private final String mServiceAction; |
| 67 | |
| 68 | /** The component name of the service to bind to. */ |
| 69 | private final ComponentName mComponentName; |
| 70 | |
| 71 | /** Used to bind and unbind from the service. */ |
| 72 | private ServiceConnection mServiceConnection; |
| 73 | |
| 74 | /** The binder provided by {@link ServiceConnection#onServiceConnected} */ |
| 75 | private IBinder mBinder; |
| 76 | |
| 77 | /** |
| 78 | * Indicates that an unbind request was made when the service was not yet bound. If the service |
| 79 | * successfully connects when this is true, it should be unbound immediately. |
| 80 | */ |
| 81 | private boolean mIsBindingAborted; |
| 82 | |
| 83 | /** |
| 84 | * Persists the specified parameters and initializes the new instance. |
| 85 | * |
| 86 | * @param serviceAction The intent-action used with {@link Context#bindService}. |
| 87 | * @param componentName The component name of the service with which to bind. |
| 88 | */ |
| 89 | protected ServiceBinder(String serviceAction, ComponentName componentName) { |
| 90 | Preconditions.checkState(!Strings.isNullOrEmpty(serviceAction)); |
| 91 | Preconditions.checkNotNull(componentName); |
| 92 | |
| 93 | mContext = TelecommApp.getInstance(); |
| 94 | mServiceAction = serviceAction; |
| 95 | mComponentName = componentName; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Performs an asynchronous bind to the service if not already bound. |
| 100 | * |
| 101 | * @return The result of {#link Context#bindService} or true if already bound. |
| 102 | */ |
| 103 | final boolean bind() { |
| 104 | ThreadUtil.checkOnMainThread(); |
| 105 | |
| 106 | // Reset any abort request if we're asked to bind again. |
| 107 | clearAbort(); |
| 108 | |
| 109 | if (mServiceConnection == null) { |
| 110 | Intent serviceIntent = new Intent(mServiceAction).setComponent(mComponentName); |
| 111 | ServiceConnection connection = new ServiceBinderConnection(); |
| 112 | |
| 113 | if (!mContext.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE)) { |
| 114 | handleFailedConnection(); |
| 115 | return false; |
| 116 | } |
| 117 | } else { |
| 118 | Preconditions.checkNotNull(mBinder); |
| 119 | handleSuccessfulConnection(mBinder); |
| 120 | } |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Unbinds from the service if already bound, no-op otherwise. |
| 127 | */ |
| 128 | final void unbind() { |
| 129 | ThreadUtil.checkOnMainThread(); |
| 130 | |
| 131 | if (mServiceConnection == null) { |
| 132 | // We're not yet bound, so queue up an abort request. |
| 133 | mIsBindingAborted = true; |
| 134 | } else { |
| 135 | mContext.unbindService(mServiceConnection); |
| 136 | mServiceConnection = null; |
| 137 | mBinder = null; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | ComponentName getComponentName() { |
| 142 | return mComponentName; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Handles a successful attempt to bind to service. See {@link Context#bindService}. |
| 147 | * |
| 148 | * @param service The actual bound service implementation. |
| 149 | */ |
| 150 | protected abstract void handleSuccessfulConnection(IBinder binder); |
| 151 | |
| 152 | /** |
| 153 | * Handles a failed attempt to bind to service. See {@link Context#bindService}. |
| 154 | */ |
| 155 | protected abstract void handleFailedConnection(); |
| 156 | |
| 157 | /** |
| 158 | * Handles a service disconnection. |
| 159 | */ |
| 160 | protected abstract void handleServiceDisconnected(); |
| 161 | |
| 162 | private void clearAbort() { |
| 163 | mIsBindingAborted = false; |
| 164 | } |
| 165 | } |