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