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