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.window; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.app.Activity; 22 import android.app.ActivityThread; 23 import android.graphics.Rect; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 /** 28 * Stores the window information about a particular Activity. 29 * It contains the info that is not part of {@link android.content.res.Configuration}. 30 * @hide 31 */ 32 public final class ActivityWindowInfo implements Parcelable { 33 34 private boolean mIsEmbedded; 35 36 @NonNull 37 private final Rect mTaskBounds = new Rect(); 38 39 @NonNull 40 private final Rect mTaskFragmentBounds = new Rect(); 41 ActivityWindowInfo()42 public ActivityWindowInfo() {} 43 ActivityWindowInfo(@onNull ActivityWindowInfo info)44 public ActivityWindowInfo(@NonNull ActivityWindowInfo info) { 45 set(info); 46 } 47 48 /** Copies fields from {@code info}. */ set(@onNull ActivityWindowInfo info)49 public void set(@NonNull ActivityWindowInfo info) { 50 set(info.mIsEmbedded, info.mTaskBounds, info.mTaskFragmentBounds); 51 } 52 53 /** Sets to the given values. */ set(boolean isEmbedded, @NonNull Rect taskBounds, @NonNull Rect taskFragmentBounds)54 public void set(boolean isEmbedded, @NonNull Rect taskBounds, 55 @NonNull Rect taskFragmentBounds) { 56 mIsEmbedded = isEmbedded; 57 mTaskBounds.set(taskBounds); 58 mTaskFragmentBounds.set(taskFragmentBounds); 59 } 60 61 /** 62 * Whether this activity is embedded, which means it is a TaskFragment that doesn't fill the 63 * leaf Task. 64 */ isEmbedded()65 public boolean isEmbedded() { 66 return mIsEmbedded; 67 } 68 69 /** 70 * The bounds of the leaf Task window in display space. 71 */ 72 @NonNull getTaskBounds()73 public Rect getTaskBounds() { 74 return mTaskBounds; 75 } 76 77 /** 78 * The bounds of the leaf TaskFragment window in display space. 79 * This can be referring to the bounds of the same window as {@link #getTaskBounds()} when 80 * the activity is not embedded. 81 */ 82 @NonNull getTaskFragmentBounds()83 public Rect getTaskFragmentBounds() { 84 return mTaskFragmentBounds; 85 } 86 ActivityWindowInfo(@onNull Parcel in)87 private ActivityWindowInfo(@NonNull Parcel in) { 88 mIsEmbedded = in.readBoolean(); 89 mTaskBounds.readFromParcel(in); 90 mTaskFragmentBounds.readFromParcel(in); 91 } 92 93 @Override writeToParcel(@onNull Parcel dest, int flags)94 public void writeToParcel(@NonNull Parcel dest, int flags) { 95 dest.writeBoolean(mIsEmbedded); 96 mTaskBounds.writeToParcel(dest, flags); 97 mTaskFragmentBounds.writeToParcel(dest, flags); 98 } 99 100 @NonNull 101 public static final Creator<ActivityWindowInfo> CREATOR = 102 new Creator<>() { 103 @Override 104 public ActivityWindowInfo createFromParcel(@NonNull Parcel in) { 105 return new ActivityWindowInfo(in); 106 } 107 108 @Override 109 public ActivityWindowInfo[] newArray(int size) { 110 return new ActivityWindowInfo[size]; 111 } 112 }; 113 114 @Override describeContents()115 public int describeContents() { 116 return 0; 117 } 118 119 @Override equals(@ullable Object o)120 public boolean equals(@Nullable Object o) { 121 if (this == o) { 122 return true; 123 } 124 if (o == null || getClass() != o.getClass()) { 125 return false; 126 } 127 final ActivityWindowInfo other = (ActivityWindowInfo) o; 128 return mIsEmbedded == other.mIsEmbedded 129 && mTaskBounds.equals(other.mTaskBounds) 130 && mTaskFragmentBounds.equals(other.mTaskFragmentBounds); 131 } 132 133 @Override hashCode()134 public int hashCode() { 135 int result = 17; 136 result = 31 * result + (mIsEmbedded ? 1 : 0); 137 result = 31 * result + mTaskBounds.hashCode(); 138 result = 31 * result + mTaskFragmentBounds.hashCode(); 139 return result; 140 } 141 142 @Override toString()143 public String toString() { 144 return "ActivityWindowInfo{isEmbedded=" + mIsEmbedded 145 + ", taskBounds=" + mTaskBounds 146 + ", taskFragmentBounds=" + mTaskFragmentBounds 147 + "}"; 148 } 149 150 /** Gets the {@link ActivityWindowInfo} of the given activity. */ 151 @Nullable getActivityWindowInfo(@onNull Activity activity)152 public static ActivityWindowInfo getActivityWindowInfo(@NonNull Activity activity) { 153 if (activity.isFinishing()) { 154 return null; 155 } 156 final ActivityThread.ActivityClientRecord record = ActivityThread.currentActivityThread() 157 .getActivityClient(activity.getActivityToken()); 158 return record != null ? record.getActivityWindowInfo() : null; 159 } 160 } 161