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 
getActivityClientRecord( @onNull ClientTransactionHandler client, IBinder token)56     @NonNull ActivityClientRecord getActivityClientRecord(
57             @NonNull ClientTransactionHandler client, IBinder token) {
58         return getActivityClientRecord(client, token, false /* includeLaunching */);
59     }
60 
61     /**
62      * Gets the {@link ActivityClientRecord} instance that corresponds to the provided token.
63      * @param client Target client handler.
64      * @param token Target activity token.
65      * @param includeLaunching Indicate to find the {@link ActivityClientRecord} in launching
66      *                         activity list.
67      *                         <p>Note that there is no {@link android.app.Activity} instance in
68      *                         {@link ActivityClientRecord} from the launching activity list.
69      * @return The {@link ActivityClientRecord} instance that corresponds to the provided token.
70      */
getActivityClientRecord( @onNull ClientTransactionHandler client, IBinder token, boolean includeLaunching)71     @NonNull ActivityClientRecord getActivityClientRecord(
72             @NonNull ClientTransactionHandler client, IBinder token, boolean includeLaunching) {
73         ActivityClientRecord r = null;
74         // Check launching Activity first to prevent race condition that activity instance has not
75         // yet set to ActivityClientRecord.
76         if (includeLaunching) {
77             r = client.getLaunchingActivity(token);
78         }
79         // Then if we don't want to find launching Activity or the ActivityClientRecord doesn't
80         // exist in launching Activity list. The ActivityClientRecord should have been initialized
81         // and put in the Activity list.
82         if (r == null) {
83             r = client.getActivityClient(token);
84             if (r != null && client.getActivity(token) == null) {
85                 throw new IllegalArgumentException("Activity must not be null to execute "
86                         + "transaction item");
87             }
88         }
89         if (r == null) {
90             throw new IllegalArgumentException("Activity client record must not be null to execute "
91                     + "transaction item");
92         }
93         return r;
94     }
95 }
96