Andrii Kulian | 446e824 | 2017-10-26 15:17:29 -0700 | [diff] [blame] | 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.am; |
| 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.IBinder; |
| 25 | import android.os.RemoteException; |
| 26 | |
| 27 | /** |
| 28 | * Class that is able to combine multiple client lifecycle transition requests and/or callbacks, |
| 29 | * and execute them as a single transaction. |
| 30 | * |
| 31 | * @see ClientTransaction |
| 32 | */ |
| 33 | class ClientLifecycleManager { |
| 34 | // TODO(lifecycler): Implement building transactions or global transaction. |
| 35 | // TODO(lifecycler): Use object pools for transactions and transaction items. |
| 36 | |
| 37 | /** |
| 38 | * Schedule a transaction, which may consist of multiple callbacks and a lifecycle request. |
| 39 | * @param transaction A sequence of client transaction items. |
| 40 | * @throws RemoteException |
| 41 | * |
| 42 | * @see ClientTransaction |
| 43 | */ |
| 44 | void scheduleTransaction(ClientTransaction transaction) throws RemoteException { |
| 45 | transaction.schedule(); |
Andrii Kulian | 9c5ea9c | 2017-12-07 09:31:01 -0800 | [diff] [blame^] | 46 | transaction.recycle(); |
Andrii Kulian | 446e824 | 2017-10-26 15:17:29 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Schedule a single lifecycle request or callback to client activity. |
| 51 | * @param client Target client. |
| 52 | * @param activityToken Target activity token. |
| 53 | * @param stateRequest A request to move target activity to a desired lifecycle state. |
| 54 | * @throws RemoteException |
| 55 | * |
| 56 | * @see ClientTransactionItem |
| 57 | */ |
| 58 | void scheduleTransaction(@NonNull IApplicationThread client, @NonNull IBinder activityToken, |
| 59 | @NonNull ActivityLifecycleItem stateRequest) throws RemoteException { |
| 60 | final ClientTransaction clientTransaction = transactionWithState(client, activityToken, |
| 61 | stateRequest); |
| 62 | scheduleTransaction(clientTransaction); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Schedule a single callback delivery to client activity. |
| 67 | * @param client Target client. |
| 68 | * @param activityToken Target activity token. |
| 69 | * @param callback A request to deliver a callback. |
| 70 | * @throws RemoteException |
| 71 | * |
| 72 | * @see ClientTransactionItem |
| 73 | */ |
| 74 | void scheduleTransaction(@NonNull IApplicationThread client, @NonNull IBinder activityToken, |
| 75 | @NonNull ClientTransactionItem callback) throws RemoteException { |
| 76 | final ClientTransaction clientTransaction = transactionWithCallback(client, activityToken, |
| 77 | callback); |
| 78 | scheduleTransaction(clientTransaction); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Schedule a single callback delivery to client application. |
| 83 | * @param client Target client. |
| 84 | * @param callback A request to deliver a callback. |
| 85 | * @throws RemoteException |
| 86 | * |
| 87 | * @see ClientTransactionItem |
| 88 | */ |
| 89 | void scheduleTransaction(@NonNull IApplicationThread client, |
| 90 | @NonNull ClientTransactionItem callback) throws RemoteException { |
| 91 | final ClientTransaction clientTransaction = transactionWithCallback(client, |
| 92 | null /* activityToken */, callback); |
| 93 | scheduleTransaction(clientTransaction); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return A new instance of {@link ClientTransaction} with a single lifecycle state request. |
| 98 | * |
| 99 | * @see ClientTransaction |
| 100 | * @see ClientTransactionItem |
| 101 | */ |
| 102 | private static ClientTransaction transactionWithState(@NonNull IApplicationThread client, |
| 103 | @NonNull IBinder activityToken, @NonNull ActivityLifecycleItem stateRequest) { |
Andrii Kulian | 9c5ea9c | 2017-12-07 09:31:01 -0800 | [diff] [blame^] | 104 | final ClientTransaction clientTransaction = ClientTransaction.obtain(client, activityToken); |
Andrii Kulian | 446e824 | 2017-10-26 15:17:29 -0700 | [diff] [blame] | 105 | clientTransaction.setLifecycleStateRequest(stateRequest); |
| 106 | return clientTransaction; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @return A new instance of {@link ClientTransaction} with a single callback invocation. |
| 111 | * |
| 112 | * @see ClientTransaction |
| 113 | * @see ClientTransactionItem |
| 114 | */ |
| 115 | private static ClientTransaction transactionWithCallback(@NonNull IApplicationThread client, |
| 116 | IBinder activityToken, @NonNull ClientTransactionItem callback) { |
Andrii Kulian | 9c5ea9c | 2017-12-07 09:31:01 -0800 | [diff] [blame^] | 117 | final ClientTransaction clientTransaction = ClientTransaction.obtain(client, activityToken); |
Andrii Kulian | 446e824 | 2017-10-26 15:17:29 -0700 | [diff] [blame] | 118 | clientTransaction.addCallback(callback); |
| 119 | return clientTransaction; |
| 120 | } |
| 121 | } |