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 paused state.
31  * @hide
32  */
33 public class PauseActivityItem extends ActivityLifecycleItem {
34 
35     private static final String TAG = "PauseActivityItem";
36 
37     private boolean mFinished;
38     private boolean mUserLeaving;
39     private int mConfigChanges;
40     private boolean mDontReport;
41 
42     @Override
execute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions)43     public void execute(ClientTransactionHandler client, IBinder token,
44             PendingTransactionActions pendingActions) {
45         Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityPause");
46         client.handlePauseActivity(token, mFinished, mUserLeaving, mConfigChanges, pendingActions,
47                 "PAUSE_ACTIVITY_ITEM");
48         Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
49     }
50 
51     @Override
getTargetState()52     public int getTargetState() {
53         return ON_PAUSE;
54     }
55 
56     @Override
postExecute(ClientTransactionHandler client, IBinder token, PendingTransactionActions pendingActions)57     public void postExecute(ClientTransactionHandler client, IBinder token,
58             PendingTransactionActions pendingActions) {
59         if (mDontReport) {
60             return;
61         }
62         try {
63             // TODO(lifecycler): Use interface callback instead of AMS.
64             ActivityTaskManager.getService().activityPaused(token);
65         } catch (RemoteException ex) {
66             throw ex.rethrowFromSystemServer();
67         }
68     }
69 
70 
71     // ObjectPoolItem implementation
72 
PauseActivityItem()73     private PauseActivityItem() {}
74 
75     /** Obtain an instance initialized with provided params. */
obtain(boolean finished, boolean userLeaving, int configChanges, boolean dontReport)76     public static PauseActivityItem obtain(boolean finished, boolean userLeaving, int configChanges,
77             boolean dontReport) {
78         PauseActivityItem instance = ObjectPool.obtain(PauseActivityItem.class);
79         if (instance == null) {
80             instance = new PauseActivityItem();
81         }
82         instance.mFinished = finished;
83         instance.mUserLeaving = userLeaving;
84         instance.mConfigChanges = configChanges;
85         instance.mDontReport = dontReport;
86 
87         return instance;
88     }
89 
90     /** Obtain an instance initialized with default params. */
obtain()91     public static PauseActivityItem obtain() {
92         PauseActivityItem instance = ObjectPool.obtain(PauseActivityItem.class);
93         if (instance == null) {
94             instance = new PauseActivityItem();
95         }
96         instance.mFinished = false;
97         instance.mUserLeaving = false;
98         instance.mConfigChanges = 0;
99         instance.mDontReport = true;
100 
101         return instance;
102     }
103 
104     @Override
recycle()105     public void recycle() {
106         super.recycle();
107         mFinished = false;
108         mUserLeaving = false;
109         mConfigChanges = 0;
110         mDontReport = false;
111         ObjectPool.recycle(this);
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.writeBoolean(mFinished);
120         dest.writeBoolean(mUserLeaving);
121         dest.writeInt(mConfigChanges);
122         dest.writeBoolean(mDontReport);
123     }
124 
125     /** Read from Parcel. */
PauseActivityItem(Parcel in)126     private PauseActivityItem(Parcel in) {
127         mFinished = in.readBoolean();
128         mUserLeaving = in.readBoolean();
129         mConfigChanges = in.readInt();
130         mDontReport = in.readBoolean();
131     }
132 
133     public static final @android.annotation.NonNull Creator<PauseActivityItem> CREATOR =
134             new Creator<PauseActivityItem>() {
135         public PauseActivityItem createFromParcel(Parcel in) {
136             return new PauseActivityItem(in);
137         }
138 
139         public PauseActivityItem[] newArray(int size) {
140             return new PauseActivityItem[size];
141         }
142     };
143 
144     @Override
equals(Object o)145     public boolean equals(Object o) {
146         if (this == o) {
147             return true;
148         }
149         if (o == null || getClass() != o.getClass()) {
150             return false;
151         }
152         final PauseActivityItem other = (PauseActivityItem) o;
153         return mFinished == other.mFinished && mUserLeaving == other.mUserLeaving
154                 && mConfigChanges == other.mConfigChanges && mDontReport == other.mDontReport;
155     }
156 
157     @Override
hashCode()158     public int hashCode() {
159         int result = 17;
160         result = 31 * result + (mFinished ? 1 : 0);
161         result = 31 * result + (mUserLeaving ? 1 : 0);
162         result = 31 * result + mConfigChanges;
163         result = 31 * result + (mDontReport ? 1 : 0);
164         return result;
165     }
166 
167     @Override
toString()168     public String toString() {
169         return "PauseActivityItem{finished=" + mFinished + ",userLeaving=" + mUserLeaving
170                 + ",configChanges=" + mConfigChanges + ",dontReport=" + mDontReport + "}";
171     }
172 }
173