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