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.ActivityClient;
24 import android.app.ActivityManager;
25 import android.app.ActivityThread.ActivityClientRecord;
26 import android.app.ClientTransactionHandler;
27 import android.os.IBinder;
28 import android.os.Parcel;
29 import android.os.Trace;
30 
31 /**
32  * Request to move an activity to resumed state.
33  * @hide
34  */
35 public class ResumeActivityItem extends ActivityLifecycleItem {
36 
37     private static final String TAG = "ResumeActivityItem";
38 
39     private int mProcState;
40     private boolean mUpdateProcState;
41     private boolean mIsForward;
42 
43     @Override
preExecute(ClientTransactionHandler client, IBinder token)44     public void preExecute(ClientTransactionHandler client, IBinder token) {
45         if (mUpdateProcState) {
46             client.updateProcessState(mProcState, false);
47         }
48     }
49 
50     @Override
execute(ClientTransactionHandler client, ActivityClientRecord r, PendingTransactionActions pendingActions)51     public void execute(ClientTransactionHandler client, ActivityClientRecord r,
52             PendingTransactionActions pendingActions) {
53         Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityResume");
54         client.handleResumeActivity(r, true /* finalStateRequest */, mIsForward,
55                 "RESUME_ACTIVITY");
56         Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
57     }
58 
59     @Override
postExecute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions)60     public void postExecute(ClientTransactionHandler client, IBinder token,
61             PendingTransactionActions pendingActions) {
62         // TODO(lifecycler): Use interface callback instead of actual implementation.
63         ActivityClient.getInstance().activityResumed(token, client.isHandleSplashScreenExit(token));
64     }
65 
66     @Override
getTargetState()67     public int getTargetState() {
68         return ON_RESUME;
69     }
70 
71 
72     // ObjectPoolItem implementation
73 
ResumeActivityItem()74     private ResumeActivityItem() {}
75 
76     /** Obtain an instance initialized with provided params. */
obtain(int procState, boolean isForward)77     public static ResumeActivityItem obtain(int procState, boolean isForward) {
78         ResumeActivityItem instance = ObjectPool.obtain(ResumeActivityItem.class);
79         if (instance == null) {
80             instance = new ResumeActivityItem();
81         }
82         instance.mProcState = procState;
83         instance.mUpdateProcState = true;
84         instance.mIsForward = isForward;
85 
86         return instance;
87     }
88 
89     /** Obtain an instance initialized with provided params. */
obtain(boolean isForward)90     public static ResumeActivityItem obtain(boolean isForward) {
91         ResumeActivityItem instance = ObjectPool.obtain(ResumeActivityItem.class);
92         if (instance == null) {
93             instance = new ResumeActivityItem();
94         }
95         instance.mProcState = ActivityManager.PROCESS_STATE_UNKNOWN;
96         instance.mUpdateProcState = false;
97         instance.mIsForward = isForward;
98 
99         return instance;
100     }
101 
102     @Override
recycle()103     public void recycle() {
104         super.recycle();
105         mProcState = ActivityManager.PROCESS_STATE_UNKNOWN;
106         mUpdateProcState = false;
107         mIsForward = false;
108         ObjectPool.recycle(this);
109     }
110 
111 
112     // Parcelable implementation
113 
114     /** Write to Parcel. */
115     @Override
writeToParcel(Parcel dest, int flags)116     public void writeToParcel(Parcel dest, int flags) {
117         dest.writeInt(mProcState);
118         dest.writeBoolean(mUpdateProcState);
119         dest.writeBoolean(mIsForward);
120     }
121 
122     /** Read from Parcel. */
ResumeActivityItem(Parcel in)123     private ResumeActivityItem(Parcel in) {
124         mProcState = in.readInt();
125         mUpdateProcState = in.readBoolean();
126         mIsForward = in.readBoolean();
127     }
128 
129     public static final @NonNull Creator<ResumeActivityItem> CREATOR =
130             new Creator<ResumeActivityItem>() {
131         public ResumeActivityItem createFromParcel(Parcel in) {
132             return new ResumeActivityItem(in);
133         }
134 
135         public ResumeActivityItem[] newArray(int size) {
136             return new ResumeActivityItem[size];
137         }
138     };
139 
140     @Override
equals(@ullable Object o)141     public boolean equals(@Nullable Object o) {
142         if (this == o) {
143             return true;
144         }
145         if (o == null || getClass() != o.getClass()) {
146             return false;
147         }
148         final ResumeActivityItem other = (ResumeActivityItem) o;
149         return mProcState == other.mProcState && mUpdateProcState == other.mUpdateProcState
150                 && mIsForward == other.mIsForward;
151     }
152 
153     @Override
hashCode()154     public int hashCode() {
155         int result = 17;
156         result = 31 * result + mProcState;
157         result = 31 * result + (mUpdateProcState ? 1 : 0);
158         result = 31 * result + (mIsForward ? 1 : 0);
159         return result;
160     }
161 
162     @Override
toString()163     public String toString() {
164         return "ResumeActivityItem{procState=" + mProcState
165                 + ",updateProcState=" + mUpdateProcState + ",isForward=" + mIsForward + "}";
166     }
167 }
168