1 /*
2  * Copyright 2017 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.ActivityThread.ActivityClientRecord;
24 import android.app.ClientTransactionHandler;
25 import android.os.IBinder;
26 import android.os.Parcel;
27 import android.os.Trace;
28 
29 /**
30  * Request to destroy an activity.
31  * @hide
32  */
33 public class DestroyActivityItem extends ActivityLifecycleItem {
34 
35     private boolean mFinished;
36     private int mConfigChanges;
37 
38     @Override
preExecute(ClientTransactionHandler client, IBinder token)39     public void preExecute(ClientTransactionHandler client, IBinder token) {
40         client.getActivitiesToBeDestroyed().put(token, this);
41     }
42 
43     @Override
execute(ClientTransactionHandler client, ActivityClientRecord r, PendingTransactionActions pendingActions)44     public void execute(ClientTransactionHandler client, ActivityClientRecord r,
45             PendingTransactionActions pendingActions) {
46         Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityDestroy");
47         client.handleDestroyActivity(r, mFinished, mConfigChanges,
48                 false /* getNonConfigInstance */, "DestroyActivityItem");
49         Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
50     }
51 
52     @Override
getTargetState()53     public int getTargetState() {
54         return ON_DESTROY;
55     }
56 
57 
58     // ObjectPoolItem implementation
59 
DestroyActivityItem()60     private DestroyActivityItem() {}
61 
62     /** Obtain an instance initialized with provided params. */
obtain(boolean finished, int configChanges)63     public static DestroyActivityItem obtain(boolean finished, int configChanges) {
64         DestroyActivityItem instance = ObjectPool.obtain(DestroyActivityItem.class);
65         if (instance == null) {
66             instance = new DestroyActivityItem();
67         }
68         instance.mFinished = finished;
69         instance.mConfigChanges = configChanges;
70 
71         return instance;
72     }
73 
74     @Override
recycle()75     public void recycle() {
76         super.recycle();
77         mFinished = false;
78         mConfigChanges = 0;
79         ObjectPool.recycle(this);
80     }
81 
82 
83     // Parcelable implementation
84 
85     /** Write to Parcel. */
86     @Override
writeToParcel(Parcel dest, int flags)87     public void writeToParcel(Parcel dest, int flags) {
88         dest.writeBoolean(mFinished);
89         dest.writeInt(mConfigChanges);
90     }
91 
92     /** Read from Parcel. */
DestroyActivityItem(Parcel in)93     private DestroyActivityItem(Parcel in) {
94         mFinished = in.readBoolean();
95         mConfigChanges = in.readInt();
96     }
97 
98     public static final @NonNull Creator<DestroyActivityItem> CREATOR =
99             new Creator<DestroyActivityItem>() {
100         public DestroyActivityItem createFromParcel(Parcel in) {
101             return new DestroyActivityItem(in);
102         }
103 
104         public DestroyActivityItem[] newArray(int size) {
105             return new DestroyActivityItem[size];
106         }
107     };
108 
109     @Override
equals(@ullable Object o)110     public boolean equals(@Nullable Object o) {
111         if (this == o) {
112             return true;
113         }
114         if (o == null || getClass() != o.getClass()) {
115             return false;
116         }
117         final DestroyActivityItem other = (DestroyActivityItem) o;
118         return mFinished == other.mFinished && mConfigChanges == other.mConfigChanges;
119     }
120 
121     @Override
hashCode()122     public int hashCode() {
123         int result = 17;
124         result = 31 * result + (mFinished ? 1 : 0);
125         result = 31 * result + mConfigChanges;
126         return result;
127     }
128 
129     @Override
toString()130     public String toString() {
131         return "DestroyActivityItem{finished=" + mFinished + ",mConfigChanges="
132                 + mConfigChanges + "}";
133     }
134 }
135