1 /* 2 * Copyright (C) 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 import static java.util.Objects.requireNonNull; 20 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.app.ClientTransactionHandler; 24 import android.os.Parcel; 25 import android.os.RemoteException; 26 import android.os.Trace; 27 import android.util.Log; 28 import android.view.IWindow; 29 import android.view.InsetsSourceControl; 30 import android.view.InsetsState; 31 32 import com.android.internal.annotations.VisibleForTesting; 33 34 import java.util.Objects; 35 36 /** 37 * Message to deliver window insets control change info. 38 * 39 * @hide 40 */ 41 public class WindowStateInsetsControlChangeItem extends WindowStateTransactionItem { 42 43 private static final String TAG = "WindowStateInsetsControlChangeItem"; 44 45 @NonNull 46 private final InsetsState mInsetsState; 47 48 @NonNull 49 private final InsetsSourceControl.Array mActiveControls; 50 WindowStateInsetsControlChangeItem(@onNull IWindow window, @NonNull InsetsState insetsState, @NonNull InsetsSourceControl.Array activeControls)51 public WindowStateInsetsControlChangeItem(@NonNull IWindow window, 52 @NonNull InsetsState insetsState, @NonNull InsetsSourceControl.Array activeControls) { 53 this(window, insetsState, activeControls, true /* copyActiveControls */); 54 } 55 56 @VisibleForTesting WindowStateInsetsControlChangeItem(@onNull IWindow window, @NonNull InsetsState insetsState, @NonNull InsetsSourceControl.Array activeControls, boolean copyActiveControls)57 public WindowStateInsetsControlChangeItem(@NonNull IWindow window, 58 @NonNull InsetsState insetsState, 59 @NonNull InsetsSourceControl.Array activeControls, boolean copyActiveControls) { 60 super(window); 61 mInsetsState = new InsetsState(insetsState, true /* copySources */); 62 if (copyActiveControls) { 63 mActiveControls = copy(requireNonNull(activeControls)); 64 } else { 65 mActiveControls = requireNonNull(activeControls); 66 } 67 } 68 69 @NonNull copy(@onNull InsetsSourceControl.Array controls)70 private static InsetsSourceControl.Array copy(@NonNull InsetsSourceControl.Array controls) { 71 final InsetsSourceControl.Array copiedControls = new InsetsSourceControl.Array( 72 controls, true /* copyControls */); 73 // This source control is an extra copy if the client is not local. By setting 74 // PARCELABLE_WRITE_RETURN_VALUE, the leash will be released at the end of 75 // SurfaceControl.writeToParcel. 76 copiedControls.setParcelableFlags(PARCELABLE_WRITE_RETURN_VALUE); 77 return copiedControls; 78 } 79 80 @Override execute(@onNull ClientTransactionHandler client, @NonNull IWindow window, @NonNull PendingTransactionActions pendingActions)81 public void execute(@NonNull ClientTransactionHandler client, @NonNull IWindow window, 82 @NonNull PendingTransactionActions pendingActions) { 83 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "windowInsetsControlChanged"); 84 try { 85 window.insetsControlChanged(mInsetsState, mActiveControls); 86 } catch (RemoteException e) { 87 // Should be a local call. 88 // An exception could happen if the process is restarted. It is safe to ignore since 89 // the window should no longer exist. 90 Log.w(TAG, "The original window no longer exists in the new process", e); 91 // Prevent leak 92 mActiveControls.release(); 93 } 94 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER); 95 } 96 97 // Parcelable implementation 98 99 /** Writes to Parcel. */ 100 @Override writeToParcel(@onNull Parcel dest, int flags)101 public void writeToParcel(@NonNull Parcel dest, int flags) { 102 super.writeToParcel(dest, flags); 103 dest.writeTypedObject(mInsetsState, flags); 104 dest.writeTypedObject(mActiveControls, flags); 105 } 106 107 /** Reads from Parcel. */ WindowStateInsetsControlChangeItem(@onNull Parcel in)108 private WindowStateInsetsControlChangeItem(@NonNull Parcel in) { 109 super(in); 110 mInsetsState = requireNonNull(in.readTypedObject(InsetsState.CREATOR)); 111 mActiveControls = requireNonNull(in.readTypedObject(InsetsSourceControl.Array.CREATOR)); 112 } 113 114 public static final @NonNull Creator<WindowStateInsetsControlChangeItem> CREATOR = 115 new Creator<>() { 116 public WindowStateInsetsControlChangeItem createFromParcel(@NonNull Parcel in) { 117 return new WindowStateInsetsControlChangeItem(in); 118 } 119 120 public WindowStateInsetsControlChangeItem[] newArray(int size) { 121 return new WindowStateInsetsControlChangeItem[size]; 122 } 123 }; 124 125 @Override equals(@ullable Object o)126 public boolean equals(@Nullable Object o) { 127 if (this == o) { 128 return true; 129 } 130 if (!super.equals(o)) { 131 return false; 132 } 133 final WindowStateInsetsControlChangeItem other = (WindowStateInsetsControlChangeItem) o; 134 return Objects.equals(mInsetsState, other.mInsetsState) 135 && Objects.equals(mActiveControls, other.mActiveControls); 136 } 137 138 @Override hashCode()139 public int hashCode() { 140 int result = 17; 141 result = 31 * result + super.hashCode(); 142 result = 31 * result + Objects.hashCode(mInsetsState); 143 result = 31 * result + Objects.hashCode(mActiveControls); 144 return result; 145 } 146 147 @Override toString()148 public String toString() { 149 return "WindowStateInsetsControlChangeItem{" + super.toString() + "}"; 150 } 151 } 152