1 /* 2 * Copyright 2019 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 android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER; 20 21 import android.annotation.NonNull; 22 import android.annotation.Nullable; 23 import android.app.ActivityClient; 24 import android.app.ActivityThread.ActivityClientRecord; 25 import android.app.ClientTransactionHandler; 26 import android.os.IBinder; 27 import android.os.Parcel; 28 import android.os.Trace; 29 30 /** 31 * Top resumed activity changed callback. 32 * 33 * @hide 34 */ 35 public class TopResumedActivityChangeItem extends ActivityTransactionItem { 36 37 private final boolean mOnTop; 38 TopResumedActivityChangeItem(@onNull IBinder activityToken, boolean onTop)39 public TopResumedActivityChangeItem(@NonNull IBinder activityToken, boolean onTop) { 40 super(activityToken); 41 mOnTop = onTop; 42 } 43 44 @Override execute(@onNull ClientTransactionHandler client, @NonNull ActivityClientRecord r, @NonNull PendingTransactionActions pendingActions)45 public void execute(@NonNull ClientTransactionHandler client, @NonNull ActivityClientRecord r, 46 @NonNull PendingTransactionActions pendingActions) { 47 Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "topResumedActivityChangeItem"); 48 client.handleTopResumedActivityChanged(r, mOnTop, "topResumedActivityChangeItem"); 49 Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER); 50 } 51 52 @Override postExecute(@onNull ClientTransactionHandler client, @NonNull PendingTransactionActions pendingActions)53 public void postExecute(@NonNull ClientTransactionHandler client, 54 @NonNull PendingTransactionActions pendingActions) { 55 if (mOnTop) { 56 return; 57 } 58 59 // The loss of top resumed state can always be reported immediately in postExecute 60 // because only three cases are possible: 61 // 1. Activity is in RESUMED state now and it just handled the callback in #execute(). 62 // 2. Activity wasn't RESUMED yet, which means that it didn't receive the top state yet. 63 // 3. Activity is PAUSED or in other lifecycle state after PAUSED. In this case top resumed 64 // state loss was already called right before pausing. 65 ActivityClient.getInstance().activityTopResumedStateLost(); 66 } 67 68 // Parcelable implementation 69 70 /** Writes to Parcel. */ 71 @Override writeToParcel(@onNull Parcel dest, int flags)72 public void writeToParcel(@NonNull Parcel dest, int flags) { 73 super.writeToParcel(dest, flags); 74 dest.writeBoolean(mOnTop); 75 } 76 77 /** Reads from Parcel. */ TopResumedActivityChangeItem(@onNull Parcel in)78 private TopResumedActivityChangeItem(@NonNull Parcel in) { 79 super(in); 80 mOnTop = in.readBoolean(); 81 } 82 83 public static final @NonNull Creator<TopResumedActivityChangeItem> CREATOR = new Creator<>() { 84 public TopResumedActivityChangeItem createFromParcel(@NonNull Parcel in) { 85 return new TopResumedActivityChangeItem(in); 86 } 87 88 public TopResumedActivityChangeItem[] newArray(int size) { 89 return new TopResumedActivityChangeItem[size]; 90 } 91 }; 92 93 @Override equals(@ullable Object o)94 public boolean equals(@Nullable Object o) { 95 if (this == o) { 96 return true; 97 } 98 if (!super.equals(o)) { 99 return false; 100 } 101 final TopResumedActivityChangeItem other = (TopResumedActivityChangeItem) o; 102 return mOnTop == other.mOnTop; 103 } 104 105 @Override hashCode()106 public int hashCode() { 107 int result = 17; 108 result = 31 * result + super.hashCode(); 109 result = 31 * result + (mOnTop ? 1 : 0); 110 return result; 111 } 112 113 @Override toString()114 public String toString() { 115 return "TopResumedActivityChangeItem{" + super.toString() + ",onTop=" + mOnTop + "}"; 116 } 117 } 118