1 /* 2 * Copyright (C) 2017 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.server.wm; 18 19 import android.annotation.NonNull; 20 import android.app.IApplicationThread; 21 import android.app.servertransaction.ClientTransaction; 22 import android.app.servertransaction.ClientTransactionItem; 23 import android.app.servertransaction.ActivityLifecycleItem; 24 import android.os.Binder; 25 import android.os.IBinder; 26 import android.os.RemoteException; 27 28 /** 29 * Class that is able to combine multiple client lifecycle transition requests and/or callbacks, 30 * and execute them as a single transaction. 31 * 32 * @see ClientTransaction 33 */ 34 class ClientLifecycleManager { 35 // TODO(lifecycler): Implement building transactions or global transaction. 36 // TODO(lifecycler): Use object pools for transactions and transaction items. 37 38 /** 39 * Schedule a transaction, which may consist of multiple callbacks and a lifecycle request. 40 * @param transaction A sequence of client transaction items. 41 * @throws RemoteException 42 * 43 * @see ClientTransaction 44 */ scheduleTransaction(ClientTransaction transaction)45 void scheduleTransaction(ClientTransaction transaction) throws RemoteException { 46 final IApplicationThread client = transaction.getClient(); 47 transaction.schedule(); 48 if (!(client instanceof Binder)) { 49 // If client is not an instance of Binder - it's a remote call and at this point it is 50 // safe to recycle the object. All objects used for local calls will be recycled after 51 // the transaction is executed on client in ActivityThread. 52 transaction.recycle(); 53 } 54 } 55 56 /** 57 * Schedule a single lifecycle request or callback to client activity. 58 * @param client Target client. 59 * @param activityToken Target activity token. 60 * @param stateRequest A request to move target activity to a desired lifecycle state. 61 * @throws RemoteException 62 * 63 * @see ClientTransactionItem 64 */ scheduleTransaction(@onNull IApplicationThread client, @NonNull IBinder activityToken, @NonNull ActivityLifecycleItem stateRequest)65 void scheduleTransaction(@NonNull IApplicationThread client, @NonNull IBinder activityToken, 66 @NonNull ActivityLifecycleItem stateRequest) throws RemoteException { 67 final ClientTransaction clientTransaction = transactionWithState(client, activityToken, 68 stateRequest); 69 scheduleTransaction(clientTransaction); 70 } 71 72 /** 73 * Schedule a single callback delivery to client activity. 74 * @param client Target client. 75 * @param activityToken Target activity token. 76 * @param callback A request to deliver a callback. 77 * @throws RemoteException 78 * 79 * @see ClientTransactionItem 80 */ scheduleTransaction(@onNull IApplicationThread client, @NonNull IBinder activityToken, @NonNull ClientTransactionItem callback)81 void scheduleTransaction(@NonNull IApplicationThread client, @NonNull IBinder activityToken, 82 @NonNull ClientTransactionItem callback) throws RemoteException { 83 final ClientTransaction clientTransaction = transactionWithCallback(client, activityToken, 84 callback); 85 scheduleTransaction(clientTransaction); 86 } 87 88 /** 89 * Schedule a single callback delivery to client application. 90 * @param client Target client. 91 * @param callback A request to deliver a callback. 92 * @throws RemoteException 93 * 94 * @see ClientTransactionItem 95 */ scheduleTransaction(@onNull IApplicationThread client, @NonNull ClientTransactionItem callback)96 void scheduleTransaction(@NonNull IApplicationThread client, 97 @NonNull ClientTransactionItem callback) throws RemoteException { 98 final ClientTransaction clientTransaction = transactionWithCallback(client, 99 null /* activityToken */, callback); 100 scheduleTransaction(clientTransaction); 101 } 102 103 /** 104 * @return A new instance of {@link ClientTransaction} with a single lifecycle state request. 105 * 106 * @see ClientTransaction 107 * @see ClientTransactionItem 108 */ transactionWithState(@onNull IApplicationThread client, @NonNull IBinder activityToken, @NonNull ActivityLifecycleItem stateRequest)109 private static ClientTransaction transactionWithState(@NonNull IApplicationThread client, 110 @NonNull IBinder activityToken, @NonNull ActivityLifecycleItem stateRequest) { 111 final ClientTransaction clientTransaction = ClientTransaction.obtain(client, activityToken); 112 clientTransaction.setLifecycleStateRequest(stateRequest); 113 return clientTransaction; 114 } 115 116 /** 117 * @return A new instance of {@link ClientTransaction} with a single callback invocation. 118 * 119 * @see ClientTransaction 120 * @see ClientTransactionItem 121 */ transactionWithCallback(@onNull IApplicationThread client, IBinder activityToken, @NonNull ClientTransactionItem callback)122 private static ClientTransaction transactionWithCallback(@NonNull IApplicationThread client, 123 IBinder activityToken, @NonNull ClientTransactionItem callback) { 124 final ClientTransaction clientTransaction = ClientTransaction.obtain(client, activityToken); 125 clientTransaction.addCallback(callback); 126 return clientTransaction; 127 } 128 } 129