1 /*
2  * Copyright (C) 2020 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 android.app.servertransaction;
18 
19 import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE;
20 
21 import android.annotation.NonNull;
22 import android.app.ActivityThread.ActivityClientRecord;
23 import android.app.ClientTransactionHandler;
24 import android.os.IBinder;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 
28 /**
29  * An activity-targeting callback message to a client that can be scheduled and executed.
30  * It also provides nullity-free version of
31  * {@link #execute(ClientTransactionHandler, IBinder, PendingTransactionActions)} for child class
32  * to inherit.
33  *
34  * @see ClientTransaction
35  * @see ClientTransactionItem
36  * @see com.android.server.wm.ClientLifecycleManager
37  * @hide
38  */
39 public abstract class ActivityTransactionItem extends ClientTransactionItem {
40     @Override
execute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions)41     public final void execute(ClientTransactionHandler client, IBinder token,
42             PendingTransactionActions pendingActions) {
43         final ActivityClientRecord r = getActivityClientRecord(client, token);
44 
45         execute(client, r, pendingActions);
46     }
47 
48     /**
49      * Like {@link #execute(ClientTransactionHandler, IBinder, PendingTransactionActions)},
50      * but take non-null {@link ActivityClientRecord} as a parameter.
51      */
52     @VisibleForTesting(visibility = PACKAGE)
execute(@onNull ClientTransactionHandler client, @NonNull ActivityClientRecord r, PendingTransactionActions pendingActions)53     public abstract void execute(@NonNull ClientTransactionHandler client,
54             @NonNull ActivityClientRecord r, PendingTransactionActions pendingActions);
55 
56     /**
57      * Gets the {@link ActivityClientRecord} instance that corresponds to the provided token.
58      * @param client Target client handler.
59      * @param token Target activity token.
60      * @return The {@link ActivityClientRecord} instance that corresponds to the provided token.
61      */
getActivityClientRecord( @onNull ClientTransactionHandler client, IBinder token)62     @NonNull ActivityClientRecord getActivityClientRecord(
63             @NonNull ClientTransactionHandler client, IBinder token) {
64         final ActivityClientRecord r = client.getActivityClient(token);
65         if (r == null) {
66             throw new IllegalArgumentException("Activity client record must not be null to execute "
67                     + "transaction item: " + this);
68         }
69         if (client.getActivity(token) == null) {
70             throw new IllegalArgumentException("Activity must not be null to execute "
71                     + "transaction item: " + this);
72         }
73         return r;
74     }
75 }
76