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