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