• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.servertransaction.ActivityLifecycleItem.ON_RESUME;
20 import static android.app.servertransaction.ActivityLifecycleItem.UNDEFINED;
21 import static android.os.Trace.TRACE_TAG_ACTIVITY_MANAGER;
22 
23 import android.annotation.NonNull;
24 import android.annotation.Nullable;
25 import android.app.ActivityThread.ActivityClientRecord;
26 import android.app.ClientTransactionHandler;
27 import android.app.ResultInfo;
28 import android.app.compat.CompatChanges;
29 import android.compat.annotation.ChangeId;
30 import android.compat.annotation.EnabledAfter;
31 import android.compat.annotation.UnsupportedAppUsage;
32 import android.os.Build;
33 import android.os.IBinder;
34 import android.os.Parcel;
35 import android.os.Parcelable;
36 import android.os.Trace;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Objects;
41 
42 /**
43  * Activity result delivery callback.
44  *
45  * @hide
46  */
47 public class ActivityResultItem extends ActivityTransactionItem {
48 
49     // TODO(b/170729553): Mark this with @NonNull and final once @UnsupportedAppUsage removed.
50     //  We cannot do it now to avoid app compatibility regression.
51     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
52     private List<ResultInfo> mResultInfoList;
53 
54     /**
55      * Correct the lifecycle of activity result after {@link android.os.Build.VERSION_CODES#S} to
56      * guarantee that an activity gets activity result just before resume.
57      */
58     @ChangeId
59     @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.S)
60     public static final long CALL_ACTIVITY_RESULT_BEFORE_RESUME = 78294732L;
61 
ActivityResultItem(@onNull IBinder activityToken, @NonNull List<ResultInfo> resultInfoList)62     public ActivityResultItem(@NonNull IBinder activityToken,
63             @NonNull List<ResultInfo> resultInfoList) {
64         super(activityToken);
65         mResultInfoList = new ArrayList<>(resultInfoList);
66     }
67 
68     @Override
getPostExecutionState()69     public int getPostExecutionState() {
70         return CompatChanges.isChangeEnabled(CALL_ACTIVITY_RESULT_BEFORE_RESUME)
71                 ? ON_RESUME : UNDEFINED;
72     }
73 
74     @Override
execute(@onNull ClientTransactionHandler client, @NonNull ActivityClientRecord r, @NonNull PendingTransactionActions pendingActions)75     public void execute(@NonNull ClientTransactionHandler client, @NonNull ActivityClientRecord r,
76             @NonNull PendingTransactionActions pendingActions) {
77         Trace.traceBegin(TRACE_TAG_ACTIVITY_MANAGER, "activityDeliverResult");
78         client.handleSendResult(r, mResultInfoList, "ACTIVITY_RESULT");
79         Trace.traceEnd(TRACE_TAG_ACTIVITY_MANAGER);
80     }
81 
82     // Parcelable implementation
83 
84     /** Writes to Parcel. */
85     @Override
writeToParcel(@onNull Parcel dest, int flags)86     public void writeToParcel(@NonNull Parcel dest, int flags) {
87         super.writeToParcel(dest, flags);
88         dest.writeTypedList(mResultInfoList, flags);
89     }
90 
91     /** Reads from Parcel. */
ActivityResultItem(@onNull Parcel in)92     private ActivityResultItem(@NonNull Parcel in) {
93         super(in);
94         // TODO(b/170729553): Wrap with requireNonNull once @UnsupportedAppUsage removed.
95         mResultInfoList = in.createTypedArrayList(ResultInfo.CREATOR);
96     }
97 
98     public static final @NonNull Parcelable.Creator<ActivityResultItem> CREATOR =
99             new Parcelable.Creator<>() {
100                 public ActivityResultItem createFromParcel(@NonNull Parcel in) {
101                     return new ActivityResultItem(in);
102                 }
103 
104                 public ActivityResultItem[] newArray(int size) {
105                     return new ActivityResultItem[size];
106                 }
107             };
108 
109     @Override
equals(@ullable Object o)110     public boolean equals(@Nullable Object o) {
111         if (this == o) {
112             return true;
113         }
114         if (!super.equals(o)) {
115             return false;
116         }
117         final ActivityResultItem other = (ActivityResultItem) o;
118         return Objects.equals(mResultInfoList, other.mResultInfoList);
119     }
120 
121     @Override
hashCode()122     public int hashCode() {
123         int result = 17;
124         result = 31 * result + super.hashCode();
125         result = 31 * result + Objects.hashCode(mResultInfoList);
126         return result;
127     }
128 
129     @Override
toString()130     public String toString() {
131         return "ActivityResultItem{" + super.toString()
132                 + ",resultInfoList=" + mResultInfoList + "}";
133     }
134 }
135