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.ClientTransactionHandler;
22import android.app.ResultInfo;
23import android.os.IBinder;
24import android.os.Parcel;
25import android.os.Parcelable;
26import android.os.Trace;
27
28import java.util.List;
29import java.util.Objects;
30
31/**
32 * Activity result delivery callback.
33 * @hide
34 */
35public class ActivityResultItem extends ClientTransactionItem {
36
37    private List<ResultInfo> mResultInfoList;
38
39    /* TODO(b/78294732)
40    @Override
41    public int getPostExecutionState() {
42        return ON_RESUME;
43    }*/
44
45    @Override
46    public void execute(ClientTransactionHandler client, IBinder token,
47            PendingTransactionActions pendingActions) {
48        Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
49        client.handleSendResult(token, mResultInfoList, "ACTIVITY_RESULT");
50        Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
51    }
52
53
54    // ObjectPoolItem implementation
55
56    private ActivityResultItem() {}
57
58    /** Obtain an instance initialized with provided params. */
59    public static ActivityResultItem obtain(List<ResultInfo> resultInfoList) {
60        ActivityResultItem instance = ObjectPool.obtain(ActivityResultItem.class);
61        if (instance == null) {
62            instance = new ActivityResultItem();
63        }
64        instance.mResultInfoList = resultInfoList;
65
66        return instance;
67    }
68
69    @Override
70    public void recycle() {
71        mResultInfoList = null;
72        ObjectPool.recycle(this);
73    }
74
75
76    // Parcelable implementation
77
78    /** Write to Parcel. */
79    @Override
80    public void writeToParcel(Parcel dest, int flags) {
81        dest.writeTypedList(mResultInfoList, flags);
82    }
83
84    /** Read from Parcel. */
85    private ActivityResultItem(Parcel in) {
86        mResultInfoList = in.createTypedArrayList(ResultInfo.CREATOR);
87    }
88
89    public static final Parcelable.Creator<ActivityResultItem> CREATOR =
90            new Parcelable.Creator<ActivityResultItem>() {
91        public ActivityResultItem createFromParcel(Parcel in) {
92            return new ActivityResultItem(in);
93        }
94
95        public ActivityResultItem[] newArray(int size) {
96            return new ActivityResultItem[size];
97        }
98    };
99
100    @Override
101    public boolean equals(Object o) {
102        if (this == o) {
103            return true;
104        }
105        if (o == null || getClass() != o.getClass()) {
106            return false;
107        }
108        final ActivityResultItem other = (ActivityResultItem) o;
109        return Objects.equals(mResultInfoList, other.mResultInfoList);
110    }
111
112    @Override
113    public int hashCode() {
114        return mResultInfoList.hashCode();
115    }
116
117    @Override
118    public String toString() {
119        return "ActivityResultItem{resultInfoList=" + mResultInfoList + "}";
120    }
121}
122