• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * Get 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 also find the {@link ActivityClientRecord} in launching
66      *                         activity list. It should be noted that there is no activity in
67      *                         {@link ActivityClientRecord} from the launching activity list.
68      * @return The {@link ActivityClientRecord} instance that corresponds to the provided token.
69      */
getActivityClientRecord( @onNull ClientTransactionHandler client, IBinder token, boolean includeLaunching)70     @NonNull ActivityClientRecord getActivityClientRecord(
71             @NonNull ClientTransactionHandler client, IBinder token, boolean includeLaunching) {
72         ActivityClientRecord r = client.getActivityClient(token);
73         if (r != null) {
74             if (client.getActivity(token) == null) {
75                 throw new IllegalArgumentException("Activity must not be null to execute "
76                         + "transaction item");
77             }
78             return r;
79         }
80         // The activity may not be launched yet. Fallback to check launching activity.
81         if (includeLaunching) {
82             r = client.getLaunchingActivity(token);
83         }
84         if (r == null) {
85             throw new IllegalArgumentException("Activity client record must not be null to execute "
86                     + "transaction item");
87         }
88 
89         // We don't need to check the activity of launching activity client records because they
90         // have not been launched yet.
91 
92         return r;
93     }
94 }
95