| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util; |
| 18 |
|
| 19 |
import android.annotation.CallbackExecutor; |
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.content.ComponentName; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.Intent; |
| 25 |
import android.content.ServiceConnection; |
| 26 |
import android.os.IBinder; |
| 27 |
|
| 28 |
import com.android.internal.annotations.GuardedBy; |
| 29 |
import com.android.internal.util.CallbackRegistry.NotifierCallback; |
| 30 |
|
| 31 |
import java.lang.annotation.Retention; |
| 32 |
import java.lang.annotation.RetentionPolicy; |
| 33 |
import java.util.concurrent.Executor; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class ObservableServiceConnection<T> implements ServiceConnection { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public interface ServiceTransformer<T> { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
T convert(IBinder service); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public interface Callback<T> { |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
void onConnected(ObservableServiceConnection<T> connection, T service); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
void onDisconnected(ObservableServiceConnection<T> connection, |
| 79 |
@DisconnectReason int reason); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static final int DISCONNECT_REASON_NONE = 0; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public static final int DISCONNECT_REASON_NULL_BINDING = 1; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static final int DISCONNECT_REASON_DISCONNECTED = 2; |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static final int DISCONNECT_REASON_BINDING_DIED = 3; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public static final int DISCONNECT_REASON_UNBIND = 4; |
| 102 |
|
| 103 |
@Retention(RetentionPolicy.SOURCE) |
| 104 |
@IntDef({ |
| 105 |
DISCONNECT_REASON_NONE, |
| 106 |
DISCONNECT_REASON_NULL_BINDING, |
| 107 |
DISCONNECT_REASON_DISCONNECTED, |
| 108 |
DISCONNECT_REASON_BINDING_DIED, |
| 109 |
DISCONNECT_REASON_UNBIND |
| 110 |
}) |
| 111 |
public @interface DisconnectReason { |
| 112 |
} |
| 113 |
|
| 114 |
private final Object mLock = new Object(); |
| 115 |
private final Context mContext; |
| 116 |
private final Executor mExecutor; |
| 117 |
private final ServiceTransformer<T> mTransformer; |
| 118 |
private final Intent mServiceIntent; |
| 119 |
private final int mFlags; |
| 120 |
|
| 121 |
@GuardedBy("mLock") |
| 122 |
private T mService; |
| 123 |
@GuardedBy("mLock") |
| 124 |
private boolean mBoundCalled = false; |
| 125 |
@GuardedBy("mLock") |
| 126 |
private int mLastDisconnectReason = DISCONNECT_REASON_NONE; |
| 127 |
|
| 128 |
private final CallbackRegistry<Callback<T>, ObservableServiceConnection<T>, T> |
| 129 |
mCallbackRegistry = new CallbackRegistry<>( |
| 130 |
new NotifierCallback<Callback<T>, ObservableServiceConnection<T>, T>() { |
| 131 |
@Override |
| 132 |
public void onNotifyCallback(Callback<T> callback, |
| 133 |
ObservableServiceConnection<T> sender, |
| 134 |
int disconnectReason, T service) { |
| 135 |
mExecutor.execute(() -> { |
| 136 |
synchronized (mLock) { |
| 137 |
if (service != null) { |
| 138 |
callback.onConnected(sender, service); |
| 139 |
} else if (mLastDisconnectReason != DISCONNECT_REASON_NONE) { |
| 140 |
callback.onDisconnected(sender, disconnectReason); |
| 141 |
} |
| 142 |
} |
| 143 |
}); |
| 144 |
} |
| 145 |
}); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public ObservableServiceConnection(@NonNull Context context, |
| 156 |
@NonNull @CallbackExecutor Executor executor, |
| 157 |
@NonNull ServiceTransformer<T> transformer, |
| 158 |
Intent serviceIntent, |
| 159 |
int flags) { |
| 160 |
mContext = context; |
| 161 |
mExecutor = executor; |
| 162 |
mTransformer = transformer; |
| 163 |
mServiceIntent = serviceIntent; |
| 164 |
mFlags = flags; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public boolean bind() { |
| 175 |
synchronized (mLock) { |
| 176 |
if (mBoundCalled) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
final boolean bindResult = |
| 180 |
mContext.bindService(mServiceIntent, mFlags, mExecutor, this); |
| 181 |
mBoundCalled = true; |
| 182 |
return bindResult; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public void unbind() { |
| 190 |
onDisconnected(DISCONNECT_REASON_UNBIND); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public void addCallback(Callback<T> callback) { |
| 199 |
mCallbackRegistry.add(callback); |
| 200 |
mExecutor.execute(() -> { |
| 201 |
synchronized (mLock) { |
| 202 |
if (mService != null) { |
| 203 |
callback.onConnected(this, mService); |
| 204 |
} else if (mLastDisconnectReason != DISCONNECT_REASON_NONE) { |
| 205 |
callback.onDisconnected(this, mLastDisconnectReason); |
| 206 |
} |
| 207 |
} |
| 208 |
}); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public void removeCallback(Callback<T> callback) { |
| 217 |
synchronized (mLock) { |
| 218 |
mCallbackRegistry.remove(callback); |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
private void onDisconnected(@DisconnectReason int reason) { |
| 223 |
synchronized (mLock) { |
| 224 |
if (!mBoundCalled) { |
| 225 |
return; |
| 226 |
} |
| 227 |
mBoundCalled = false; |
| 228 |
mLastDisconnectReason = reason; |
| 229 |
mContext.unbindService(this); |
| 230 |
mService = null; |
| 231 |
mCallbackRegistry.notifyCallbacks(this, reason, null); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
@Override |
| 236 |
public final void onServiceConnected(ComponentName name, IBinder service) { |
| 237 |
synchronized (mLock) { |
| 238 |
mService = mTransformer.convert(service); |
| 239 |
mLastDisconnectReason = DISCONNECT_REASON_NONE; |
| 240 |
mCallbackRegistry.notifyCallbacks(this, mLastDisconnectReason, mService); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
@Override |
| 245 |
public final void onServiceDisconnected(ComponentName name) { |
| 246 |
onDisconnected(DISCONNECT_REASON_DISCONNECTED); |
| 247 |
} |
| 248 |
|
| 249 |
@Override |
| 250 |
public final void onBindingDied(ComponentName name) { |
| 251 |
onDisconnected(DISCONNECT_REASON_BINDING_DIED); |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
public final void onNullBinding(ComponentName name) { |
| 256 |
onDisconnected(DISCONNECT_REASON_NULL_BINDING); |
| 257 |
} |
| 258 |
} |