| 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.content.Context; |
| 20 |
import android.content.Intent; |
| 21 |
import android.content.ServiceConnection; |
| 22 |
import android.os.Handler; |
| 23 |
import android.os.SystemClock; |
| 24 |
|
| 25 |
import com.android.internal.annotations.GuardedBy; |
| 26 |
import com.android.internal.annotations.VisibleForTesting; |
| 27 |
|
| 28 |
import java.util.concurrent.Executor; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class PersistentServiceConnection<T> extends ObservableServiceConnection<T> { |
| 46 |
private final Callback<T> mConnectionCallback = new Callback<T>() { |
| 47 |
private long mConnectedTime; |
| 48 |
|
| 49 |
@Override |
| 50 |
public void onConnected(ObservableServiceConnection<T> connection, T service) { |
| 51 |
mConnectedTime = mInjector.uptimeMillis(); |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void onDisconnected(ObservableServiceConnection<T> connection, |
| 56 |
@DisconnectReason int reason) { |
| 57 |
if (reason == DISCONNECT_REASON_UNBIND) return; |
| 58 |
synchronized (mLock) { |
| 59 |
if ((mInjector.uptimeMillis() - mConnectedTime) > mMinConnectionDurationMs) { |
| 60 |
mReconnectAttempts = 0; |
| 61 |
bindInternalLocked(); |
| 62 |
} else { |
| 63 |
scheduleConnectionAttemptLocked(); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
private final Object mLock = new Object(); |
| 70 |
private final Injector mInjector; |
| 71 |
private final Handler mHandler; |
| 72 |
private final int mMinConnectionDurationMs; |
| 73 |
private final int mMaxReconnectAttempts; |
| 74 |
private final int mBaseReconnectDelayMs; |
| 75 |
@GuardedBy("mLock") |
| 76 |
private int mReconnectAttempts; |
| 77 |
@GuardedBy("mLock") |
| 78 |
private Object mCancelToken; |
| 79 |
|
| 80 |
private final Runnable mConnectRunnable = new Runnable() { |
| 81 |
@Override |
| 82 |
public void run() { |
| 83 |
synchronized (mLock) { |
| 84 |
mCancelToken = null; |
| 85 |
bindInternalLocked(); |
| 86 |
} |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public PersistentServiceConnection(Context context, |
| 98 |
Executor executor, |
| 99 |
Handler handler, |
| 100 |
ServiceTransformer<T> transformer, |
| 101 |
Intent serviceIntent, |
| 102 |
int flags, |
| 103 |
int minConnectionDurationMs, |
| 104 |
int maxReconnectAttempts, |
| 105 |
int baseReconnectDelayMs) { |
| 106 |
this(context, |
| 107 |
executor, |
| 108 |
handler, |
| 109 |
transformer, |
| 110 |
serviceIntent, |
| 111 |
flags, |
| 112 |
minConnectionDurationMs, |
| 113 |
maxReconnectAttempts, |
| 114 |
baseReconnectDelayMs, |
| 115 |
new Injector()); |
| 116 |
} |
| 117 |
|
| 118 |
@VisibleForTesting |
| 119 |
public PersistentServiceConnection( |
| 120 |
Context context, |
| 121 |
Executor executor, |
| 122 |
Handler handler, |
| 123 |
ServiceTransformer<T> transformer, |
| 124 |
Intent serviceIntent, |
| 125 |
int flags, |
| 126 |
int minConnectionDurationMs, |
| 127 |
int maxReconnectAttempts, |
| 128 |
int baseReconnectDelayMs, |
| 129 |
Injector injector) { |
| 130 |
super(context, executor, transformer, serviceIntent, flags); |
| 131 |
mHandler = handler; |
| 132 |
mMinConnectionDurationMs = minConnectionDurationMs; |
| 133 |
mMaxReconnectAttempts = maxReconnectAttempts; |
| 134 |
mBaseReconnectDelayMs = baseReconnectDelayMs; |
| 135 |
mInjector = injector; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
@Override |
| 140 |
public boolean bind() { |
| 141 |
synchronized (mLock) { |
| 142 |
addCallback(mConnectionCallback); |
| 143 |
mReconnectAttempts = 0; |
| 144 |
return bindInternalLocked(); |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
@GuardedBy("mLock") |
| 149 |
private boolean bindInternalLocked() { |
| 150 |
return super.bind(); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
@Override |
| 155 |
public void unbind() { |
| 156 |
synchronized (mLock) { |
| 157 |
removeCallback(mConnectionCallback); |
| 158 |
cancelPendingConnectionAttemptLocked(); |
| 159 |
super.unbind(); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
@GuardedBy("mLock") |
| 164 |
private void cancelPendingConnectionAttemptLocked() { |
| 165 |
if (mCancelToken != null) { |
| 166 |
mHandler.removeCallbacksAndMessages(mCancelToken); |
| 167 |
mCancelToken = null; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
@GuardedBy("mLock") |
| 172 |
private void scheduleConnectionAttemptLocked() { |
| 173 |
cancelPendingConnectionAttemptLocked(); |
| 174 |
|
| 175 |
if (mReconnectAttempts >= mMaxReconnectAttempts) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
final long reconnectDelayMs = |
| 180 |
(long) Math.scalb(mBaseReconnectDelayMs, mReconnectAttempts); |
| 181 |
|
| 182 |
mCancelToken = new Object(); |
| 183 |
mHandler.postDelayed(mConnectRunnable, mCancelToken, reconnectDelayMs); |
| 184 |
mReconnectAttempts++; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
@VisibleForTesting |
| 191 |
public static class Injector { |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public long uptimeMillis() { |
| 197 |
return SystemClock.uptimeMillis(); |
| 198 |
} |
| 199 |
} |
| 200 |
} |