| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.servertransaction; |
| 18 |
|
| 19 |
import static android.app.servertransaction.ActivityLifecycleItem.ON_CREATE; |
| 20 |
import static android.app.servertransaction.ActivityLifecycleItem.ON_DESTROY; |
| 21 |
import static android.app.servertransaction.ActivityLifecycleItem.ON_PAUSE; |
| 22 |
import static android.app.servertransaction.ActivityLifecycleItem.ON_RESTART; |
| 23 |
import static android.app.servertransaction.ActivityLifecycleItem.ON_RESUME; |
| 24 |
import static android.app.servertransaction.ActivityLifecycleItem.ON_START; |
| 25 |
import static android.app.servertransaction.ActivityLifecycleItem.ON_STOP; |
| 26 |
import static android.app.servertransaction.ActivityLifecycleItem.PRE_ON_CREATE; |
| 27 |
import static android.app.servertransaction.ActivityLifecycleItem.UNDEFINED; |
| 28 |
|
| 29 |
import android.app.Activity; |
| 30 |
import android.app.ActivityThread.ActivityClientRecord; |
| 31 |
import android.app.ClientTransactionHandler; |
| 32 |
import android.os.IBinder; |
| 33 |
import android.util.IntArray; |
| 34 |
import android.util.Log; |
| 35 |
|
| 36 |
import com.android.internal.annotations.VisibleForTesting; |
| 37 |
|
| 38 |
import java.io.PrintWriter; |
| 39 |
import java.io.StringWriter; |
| 40 |
import java.util.List; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class TransactionExecutorHelper { |
| 47 |
private static final String TAG = TransactionExecutorHelper.class.getSimpleName(); |
| 48 |
|
| 49 |
private static final int DESTRUCTION_PENALTY = 10; |
| 50 |
|
| 51 |
private static final int[] ON_RESUME_PRE_EXCUTION_STATES = new int[] { ON_START, ON_PAUSE }; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
@ActivityLifecycleItem.LifecycleState |
| 56 |
private IntArray mLifecycleSequence = new IntArray(6); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
@VisibleForTesting |
| 66 |
public IntArray getLifecyclePath(int start, int finish, boolean excludeLastState) { |
| 67 |
if (start == UNDEFINED || finish == UNDEFINED) { |
| 68 |
throw new IllegalArgumentException("Can't resolve lifecycle path for undefined state"); |
| 69 |
} |
| 70 |
if (start == ON_RESTART || finish == ON_RESTART) { |
| 71 |
throw new IllegalArgumentException( |
| 72 |
"Can't start or finish in intermittent RESTART state"); |
| 73 |
} |
| 74 |
if (finish == PRE_ON_CREATE && start != finish) { |
| 75 |
throw new IllegalArgumentException("Can only start in pre-onCreate state"); |
| 76 |
} |
| 77 |
|
| 78 |
mLifecycleSequence.clear(); |
| 79 |
if (finish >= start) { |
| 80 |
if (start == ON_START && finish == ON_STOP) { |
| 81 |
|
| 82 |
|
| 83 |
mLifecycleSequence.add(ON_STOP); |
| 84 |
} else { |
| 85 |
|
| 86 |
for (int i = start + 1; i <= finish; i++) { |
| 87 |
mLifecycleSequence.add(i); |
| 88 |
} |
| 89 |
} |
| 90 |
} else { |
| 91 |
if (start == ON_PAUSE && finish == ON_RESUME) { |
| 92 |
|
| 93 |
mLifecycleSequence.add(ON_RESUME); |
| 94 |
} else if (start <= ON_STOP && finish >= ON_START) { |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
for (int i = start + 1; i <= ON_STOP; i++) { |
| 99 |
mLifecycleSequence.add(i); |
| 100 |
} |
| 101 |
|
| 102 |
mLifecycleSequence.add(ON_RESTART); |
| 103 |
|
| 104 |
for (int i = ON_START; i <= finish; i++) { |
| 105 |
mLifecycleSequence.add(i); |
| 106 |
} |
| 107 |
} else { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
for (int i = start + 1; i <= ON_DESTROY; i++) { |
| 112 |
mLifecycleSequence.add(i); |
| 113 |
} |
| 114 |
|
| 115 |
for (int i = ON_CREATE; i <= finish; i++) { |
| 116 |
mLifecycleSequence.add(i); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
if (excludeLastState && mLifecycleSequence.size() != 0) { |
| 123 |
mLifecycleSequence.remove(mLifecycleSequence.size() - 1); |
| 124 |
} |
| 125 |
|
| 126 |
return mLifecycleSequence; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
@VisibleForTesting |
| 140 |
public int getClosestPreExecutionState(ActivityClientRecord r, |
| 141 |
int postExecutionState) { |
| 142 |
switch (postExecutionState) { |
| 143 |
case UNDEFINED: |
| 144 |
return UNDEFINED; |
| 145 |
case ON_RESUME: |
| 146 |
return getClosestOfStates(r, ON_RESUME_PRE_EXCUTION_STATES); |
| 147 |
default: |
| 148 |
throw new UnsupportedOperationException("Pre-execution states for state: " |
| 149 |
+ postExecutionState + " is not supported."); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
@VisibleForTesting |
| 163 |
public int getClosestOfStates(ActivityClientRecord r, int[] finalStates) { |
| 164 |
if (finalStates == null || finalStates.length == 0) { |
| 165 |
return UNDEFINED; |
| 166 |
} |
| 167 |
if (r == null) { |
| 168 |
|
| 169 |
Log.w(TAG, "ActivityClientRecord was null"); |
| 170 |
return UNDEFINED; |
| 171 |
} |
| 172 |
|
| 173 |
final int currentState = r.getLifecycleState(); |
| 174 |
int closestState = UNDEFINED; |
| 175 |
for (int i = 0, shortestPath = Integer.MAX_VALUE, pathLength; i < finalStates.length; i++) { |
| 176 |
getLifecyclePath(currentState, finalStates[i], false ); |
| 177 |
pathLength = mLifecycleSequence.size(); |
| 178 |
if (pathInvolvesDestruction(mLifecycleSequence)) { |
| 179 |
pathLength += DESTRUCTION_PENALTY; |
| 180 |
} |
| 181 |
if (shortestPath > pathLength) { |
| 182 |
shortestPath = pathLength; |
| 183 |
closestState = finalStates[i]; |
| 184 |
} |
| 185 |
} |
| 186 |
return closestState; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
public static ActivityLifecycleItem getLifecycleRequestForCurrentState(ActivityClientRecord r) { |
| 191 |
final int prevState = r.getLifecycleState(); |
| 192 |
final ActivityLifecycleItem lifecycleItem; |
| 193 |
switch (prevState) { |
| 194 |
|
| 195 |
case ON_START: |
| 196 |
lifecycleItem = StartActivityItem.obtain(null ); |
| 197 |
break; |
| 198 |
case ON_PAUSE: |
| 199 |
lifecycleItem = PauseActivityItem.obtain(); |
| 200 |
break; |
| 201 |
case ON_STOP: |
| 202 |
lifecycleItem = StopActivityItem.obtain(0 ); |
| 203 |
break; |
| 204 |
default: |
| 205 |
lifecycleItem = ResumeActivityItem.obtain(false , |
| 206 |
false ); |
| 207 |
break; |
| 208 |
} |
| 209 |
|
| 210 |
return lifecycleItem; |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
private static boolean pathInvolvesDestruction(IntArray lifecycleSequence) { |
| 218 |
final int size = lifecycleSequence.size(); |
| 219 |
for (int i = 0; i < size; i++) { |
| 220 |
if (lifecycleSequence.get(i) == ON_DESTROY) { |
| 221 |
return true; |
| 222 |
} |
| 223 |
} |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
static int lastCallbackRequestingState(ClientTransaction transaction) { |
| 239 |
final List<ClientTransactionItem> callbacks = transaction.getCallbacks(); |
| 240 |
if (callbacks == null || callbacks.size() == 0) { |
| 241 |
return -1; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
int lastRequestedState = UNDEFINED; |
| 247 |
int lastRequestingCallback = -1; |
| 248 |
for (int i = callbacks.size() - 1; i >= 0; i--) { |
| 249 |
final ClientTransactionItem callback = callbacks.get(i); |
| 250 |
final int postExecutionState = callback.getPostExecutionState(); |
| 251 |
if (postExecutionState != UNDEFINED) { |
| 252 |
|
| 253 |
if (lastRequestedState == UNDEFINED || lastRequestedState == postExecutionState) { |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
lastRequestedState = postExecutionState; |
| 258 |
lastRequestingCallback = i; |
| 259 |
} else { |
| 260 |
break; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
return lastRequestingCallback; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
static String transactionToString(ClientTransaction transaction, |
| 270 |
ClientTransactionHandler transactionHandler) { |
| 271 |
final StringWriter stringWriter = new StringWriter(); |
| 272 |
final PrintWriter pw = new PrintWriter(stringWriter); |
| 273 |
final String prefix = tId(transaction); |
| 274 |
transaction.dump(prefix, pw); |
| 275 |
pw.append(prefix + "Target activity: ") |
| 276 |
.println(getActivityName(transaction.getActivityToken(), transactionHandler)); |
| 277 |
return stringWriter.toString(); |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
static String tId(ClientTransaction transaction) { |
| 282 |
return "tId:" + transaction.hashCode() + " "; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
static String getActivityName(IBinder token, ClientTransactionHandler transactionHandler) { |
| 287 |
final Activity activity = getActivityForToken(token, transactionHandler); |
| 288 |
if (activity != null) { |
| 289 |
return activity.getComponentName().getClassName(); |
| 290 |
} |
| 291 |
return "Not found for token: " + token; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
static String getShortActivityName(IBinder token, ClientTransactionHandler transactionHandler) { |
| 296 |
final Activity activity = getActivityForToken(token, transactionHandler); |
| 297 |
if (activity != null) { |
| 298 |
return activity.getComponentName().getShortClassName(); |
| 299 |
} |
| 300 |
return "Not found for token: " + token; |
| 301 |
} |
| 302 |
|
| 303 |
private static Activity getActivityForToken(IBinder token, |
| 304 |
ClientTransactionHandler transactionHandler) { |
| 305 |
if (token == null) { |
| 306 |
return null; |
| 307 |
} |
| 308 |
return transactionHandler.getActivity(token); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
static String getStateName(int state) { |
| 313 |
switch (state) { |
| 314 |
case UNDEFINED: |
| 315 |
return "UNDEFINED"; |
| 316 |
case PRE_ON_CREATE: |
| 317 |
return "PRE_ON_CREATE"; |
| 318 |
case ON_CREATE: |
| 319 |
return "ON_CREATE"; |
| 320 |
case ON_START: |
| 321 |
return "ON_START"; |
| 322 |
case ON_RESUME: |
| 323 |
return "ON_RESUME"; |
| 324 |
case ON_PAUSE: |
| 325 |
return "ON_PAUSE"; |
| 326 |
case ON_STOP: |
| 327 |
return "ON_STOP"; |
| 328 |
case ON_DESTROY: |
| 329 |
return "ON_DESTROY"; |
| 330 |
case ON_RESTART: |
| 331 |
return "ON_RESTART"; |
| 332 |
default: |
| 333 |
throw new IllegalArgumentException("Unexpected lifecycle state: " + state); |
| 334 |
} |
| 335 |
} |
| 336 |
} |