1 /*
2  * Copyright (C) 2019 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 package android.app;
17 
18 import android.annotation.NonNull;
19 import android.annotation.Nullable;
20 import android.content.LocusId;
21 import android.os.Bundle;
22 import android.os.IBinder;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 import android.view.accessibility.AccessibilityNodeInfo;
26 
27 import com.android.internal.util.Preconditions;
28 
29 import java.util.Objects;
30 
31 /**
32  * Represents a abstract action that can be perform on this app. This are requested from
33  * outside the app's UI (eg by SystemUI or assistant). The semantics of these actions are
34  * not specified by the OS. This allows open-ended and scalable approach for defining how
35  * an app interacts with components that expose alternative interaction models to the user
36  * such as the assistant, SystemUI, etc. You can use {@link #equals(Object)} to compare
37  * instances of this class.
38  */
39 public final class DirectAction implements Parcelable {
40 
41     /**
42      * @hide
43      */
44     public static final String KEY_ACTIONS_LIST = "actions_list";
45 
46     private int mTaskId;
47     private IBinder mActivityId;
48 
49     @NonNull
50     private final String mID;
51     @Nullable
52     private final Bundle mExtras;
53     @Nullable
54     private final LocusId mLocusId;
55 
56     /** @hide */
DirectAction(@onNull String id, @Nullable Bundle extras, @Nullable LocusId locusId)57     public DirectAction(@NonNull String id, @Nullable Bundle extras,
58             @Nullable LocusId locusId) {
59         mID = Preconditions.checkStringNotEmpty(id);
60         mExtras = extras;
61         mLocusId = locusId;
62     }
63 
64     /** @hide */
setSource(int taskId, IBinder activityId)65     public void setSource(int taskId, IBinder activityId) {
66         mTaskId = taskId;
67         mActivityId = activityId;
68     }
69 
70     /**
71      * @hide
72      */
DirectAction(@onNull DirectAction original)73     public DirectAction(@NonNull DirectAction original) {
74         mTaskId = original.mTaskId;
75         mActivityId = original.mActivityId;
76         mID = original.mID;
77         mExtras = original.mExtras;
78         mLocusId = original.mLocusId;
79     }
80 
DirectAction(Parcel in)81     private DirectAction(Parcel in) {
82         mTaskId = in.readInt();
83         mActivityId = in.readStrongBinder();
84         mID = in.readString();
85         mExtras = in.readBundle();
86         final String idString = in.readString();
87         mLocusId = (idString != null) ? new LocusId(idString) : null;
88     }
89 
90     /** @hide */
getTaskId()91     public int getTaskId() {
92         return mTaskId;
93     }
94 
95     /** @hide */
getActivityId()96     public IBinder getActivityId() {
97         return mActivityId;
98     }
99 
100     /**
101      * @return the ID for this action.
102      */
103     @NonNull
getId()104     public String getId() {
105         return mID;
106     }
107 
108     /**
109      * @return any extras associated with this action.
110      */
111     @Nullable
getExtras()112     public Bundle getExtras() {
113         return mExtras;
114     }
115 
116     /**
117      * @return the LocusId for the current state for the app
118      */
119     @Nullable
getLocusId()120     public LocusId getLocusId() {
121         return mLocusId;
122     }
123 
124     @Override
describeContents()125     public int describeContents() {
126         return 0;
127     }
128 
129     @Override
hashCode()130     public int hashCode() {
131         return mID.hashCode();
132     }
133 
134     @Override
equals(Object other)135     public boolean equals(Object other) {
136         if (other == null) {
137             return false;
138         }
139 
140         if (other == this) {
141             return true;
142         }
143 
144         if (getClass() != other.getClass()) {
145             return false;
146         }
147 
148         return mID.equals(((DirectAction) other).mID);
149     }
150 
151     @Override
writeToParcel(Parcel dest, int flags)152     public void writeToParcel(Parcel dest, int flags) {
153         dest.writeInt(mTaskId);
154         dest.writeStrongBinder(mActivityId);
155         dest.writeString(mID);
156         dest.writeBundle(mExtras);
157         dest.writeString(mLocusId.getId());
158     }
159 
160     /**
161      * Builder for construction of DirectAction.
162      */
163     public static final class Builder {
164         private @NonNull String mId;
165         private @Nullable Bundle mExtras;
166         private @Nullable LocusId mLocusId;
167 
168         /**
169          * Creates a new instance.
170          *
171          * @param id The mandatory action id which must be unique in the
172          *     current application state.
173          */
Builder(@onNull String id)174         public Builder(@NonNull String id) {
175             Preconditions.checkNotNull(id);
176             mId = id;
177         }
178 
179         /**
180          * Sets the optional action extras. These extras are action specific
181          * and their semantics are open-ended potentially representing how
182          * the action is visualized, interpreted, what its arguments are, etc.
183          *
184          * @param extras The extras.
185          * @return This builder.
186          */
setExtras(@ullable Bundle extras)187         public @NonNull Builder setExtras(@Nullable Bundle extras) {
188             mExtras = extras;
189             return this;
190         }
191 
192         /**
193          * Sets the optional locus id. This is an identifier of the application
194          * state from a user perspective. For example, a specific chat in a
195          * messaging app.
196          *
197          * @param locusId The locus id.
198          * @return This builder.
199          */
setLocusId(@ullable LocusId locusId)200         public @NonNull Builder setLocusId(@Nullable LocusId locusId) {
201             mLocusId = locusId;
202             return this;
203         }
204 
205         /**
206          * @return A newly constructed instance.
207          */
build()208         public @NonNull DirectAction build() {
209             return new DirectAction(mId, mExtras, mLocusId);
210         }
211     }
212 
213     public static final @NonNull Parcelable.Creator<DirectAction> CREATOR =
214             new Parcelable.Creator<DirectAction>() {
215                 public DirectAction createFromParcel(Parcel in) {
216                     return new DirectAction(in);
217                 }
218                 public DirectAction[] newArray(int size) {
219                     return new DirectAction[size];
220                 }
221             };
222 }
223