1 /*
2  * Copyright 2024 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 
20 import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE;
21 
22 import static java.util.Objects.requireNonNull;
23 
24 import android.annotation.CallSuper;
25 import android.annotation.NonNull;
26 import android.annotation.Nullable;
27 import android.app.ClientTransactionHandler;
28 import android.os.Parcel;
29 import android.view.IWindow;
30 
31 import com.android.internal.annotations.VisibleForTesting;
32 
33 import java.util.Objects;
34 
35 /**
36  * {@link ClientTransactionItem} to report changes to a window.
37  *
38  * @hide
39  */
40 public abstract class WindowStateTransactionItem extends ClientTransactionItem {
41 
42     /** The interface for IWindow to perform callback directly if possible. */
43     public interface TransactionListener {
44         /** Notifies that the transaction item is going to be executed. */
onExecutingWindowStateTransactionItem()45         void onExecutingWindowStateTransactionItem();
46     }
47 
48     /** Target window. */
49     private IWindow mWindow;
50 
WindowStateTransactionItem()51     WindowStateTransactionItem() {}
52 
53     @Override
execute(@onNull ClientTransactionHandler client, @NonNull PendingTransactionActions pendingActions)54     public final void execute(@NonNull ClientTransactionHandler client,
55             @NonNull PendingTransactionActions pendingActions) {
56         if (mWindow instanceof TransactionListener listener) {
57             listener.onExecutingWindowStateTransactionItem();
58         }
59         execute(client, mWindow, pendingActions);
60     }
61 
62     /**
63      * Like {@link #execute(ClientTransactionHandler, PendingTransactionActions)},
64      * but take non-null {@link IWindow} as a parameter.
65      */
66     @VisibleForTesting(visibility = PACKAGE)
execute(@onNull ClientTransactionHandler client, @NonNull IWindow window, @NonNull PendingTransactionActions pendingActions)67     public abstract void execute(@NonNull ClientTransactionHandler client,
68             @NonNull IWindow window, @NonNull PendingTransactionActions pendingActions);
69 
setWindow(@onNull IWindow window)70     void setWindow(@NonNull IWindow window) {
71         mWindow = requireNonNull(window);
72     }
73 
74     // To be overridden
75 
WindowStateTransactionItem(@onNull Parcel in)76     WindowStateTransactionItem(@NonNull Parcel in) {
77         mWindow = IWindow.Stub.asInterface(in.readStrongBinder());
78     }
79 
80     @CallSuper
81     @Override
writeToParcel(@onNull Parcel dest, int flags)82     public void writeToParcel(@NonNull Parcel dest, int flags) {
83         dest.writeStrongBinder(mWindow.asBinder());
84     }
85 
86     @CallSuper
87     @Override
recycle()88     public void recycle() {
89         mWindow = null;
90     }
91 
92     // Subclass must override and call super.equals to compare the mActivityToken.
93     @SuppressWarnings("EqualsGetClass")
94     @CallSuper
95     @Override
equals(@ullable Object o)96     public boolean equals(@Nullable Object o) {
97         if (this == o) {
98             return true;
99         }
100         if (o == null || getClass() != o.getClass()) {
101             return false;
102         }
103         final WindowStateTransactionItem other = (WindowStateTransactionItem) o;
104         return Objects.equals(mWindow, other.mWindow);
105     }
106 
107     @CallSuper
108     @Override
hashCode()109     public int hashCode() {
110         return Objects.hashCode(mWindow);
111     }
112 
113     @CallSuper
114     @Override
toString()115     public String toString() {
116         return "mWindow=" + mWindow;
117     }
118 }
119