1 package android.app.assist; 2 3 import android.compat.annotation.UnsupportedAppUsage; 4 import android.content.ClipData; 5 import android.content.Intent; 6 import android.net.Uri; 7 import android.os.Build; 8 import android.os.Bundle; 9 import android.os.Parcel; 10 import android.os.Parcelable; 11 12 /** 13 * Holds information about the content an application is viewing, to hand to an 14 * assistant at the user's request. This is filled in by 15 * {@link android.app.Activity#onProvideAssistContent Activity.onProvideAssistContent}. 16 */ 17 public class AssistContent implements Parcelable { 18 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 19 private boolean mIsAppProvidedIntent = false; 20 private boolean mIsAppProvidedWebUri = false; 21 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 22 private Intent mIntent; 23 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 24 private String mStructuredData; 25 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 26 private ClipData mClipData; 27 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 28 private Uri mUri; 29 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) 30 private final Bundle mExtras; 31 AssistContent()32 public AssistContent() { 33 mExtras = new Bundle(); 34 } 35 36 /** 37 * Create an AssistContent with extras initialized. 38 * 39 * @hide 40 */ AssistContent(@ndroid.annotation.NonNull Bundle extras)41 public AssistContent(@android.annotation.NonNull Bundle extras) { 42 mExtras = extras; 43 } 44 45 /** 46 * Called by {@link android.app.ActivityThread} to set the default Intent based on 47 * {@link android.app.Activity#getIntent Activity.getIntent}. 48 * 49 * <p>Automatically populates {@link #mUri} if that Intent is an {@link Intent#ACTION_VIEW} 50 * of a web (http or https scheme) URI.</p> 51 * 52 * @hide 53 */ setDefaultIntent(Intent intent)54 public void setDefaultIntent(Intent intent) { 55 mIntent = intent; 56 mIsAppProvidedIntent = false; 57 mIsAppProvidedWebUri = false; 58 mUri = null; 59 if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) { 60 Uri uri = intent.getData(); 61 if (uri != null) { 62 if ("http".equals(uri.getScheme()) || "https".equals(uri.getScheme())) { 63 mUri = uri; 64 } 65 } 66 } 67 } 68 69 /** 70 * Sets the Intent associated with the content, describing the current top-level context of 71 * the activity. If this contains a reference to a piece of data related to the activity, 72 * be sure to set {@link Intent#FLAG_GRANT_READ_URI_PERMISSION} so the accessibility 73 * service can access it. 74 */ setIntent(Intent intent)75 public void setIntent(Intent intent) { 76 mIsAppProvidedIntent = true; 77 mIntent = intent; 78 } 79 80 /** 81 * Returns the current {@link #setIntent} if one is set, else the default Intent obtained from 82 * {@link android.app.Activity#getIntent Activity.getIntent}. Can be modified in-place. 83 */ getIntent()84 public Intent getIntent() { 85 return mIntent; 86 } 87 88 /** 89 * Returns whether or not the current Intent was explicitly provided in 90 * {@link android.app.Activity#onProvideAssistContent Activity.onProvideAssistContent}. If not, 91 * the Intent was automatically set based on 92 * {@link android.app.Activity#getIntent Activity.getIntent}. 93 */ isAppProvidedIntent()94 public boolean isAppProvidedIntent() { 95 return mIsAppProvidedIntent; 96 } 97 98 /** 99 * Optional additional content items that are involved with 100 * the current UI. Access to this content will be granted to the assistant as if you 101 * are sending it through an Intent with {@link Intent#FLAG_GRANT_READ_URI_PERMISSION}. 102 */ setClipData(ClipData clip)103 public void setClipData(ClipData clip) { 104 mClipData = clip; 105 } 106 107 /** 108 * Return the current {@link #setClipData}, which you can modify in-place. 109 */ getClipData()110 public ClipData getClipData() { 111 return mClipData; 112 } 113 114 /** 115 * Sets optional structured data regarding the content being viewed. The provided data 116 * must be a string represented with <a href="http://json-ld.org/">JSON-LD</a> using the 117 * <a href="http://schema.org/">schema.org</a> vocabulary. 118 */ setStructuredData(String structuredData)119 public void setStructuredData(String structuredData) { 120 mStructuredData = structuredData; 121 } 122 123 /** 124 * Returns the current {@link #setStructuredData}. 125 */ getStructuredData()126 public String getStructuredData() { 127 return mStructuredData; 128 } 129 130 /** 131 * Set a web URI associated with the current data being shown to the user. 132 * This URI could be opened in a web browser, or in the app as an 133 * {@link Intent#ACTION_VIEW} Intent, to show the same data that is currently 134 * being displayed by it. The URI here should be something that is transportable 135 * off the device into other environments to acesss the same data as is currently 136 * being shown in the app; if the app does not have such a representation, it should 137 * leave the null and only report the local intent and clip data. 138 */ setWebUri(Uri uri)139 public void setWebUri(Uri uri) { 140 mIsAppProvidedWebUri = true; 141 mUri = uri; 142 } 143 144 /** 145 * Return the content's web URI as per {@link #setWebUri(android.net.Uri)}, or null if 146 * there is none. 147 */ getWebUri()148 public Uri getWebUri() { 149 return mUri; 150 } 151 152 /** 153 * Returns whether or not the current {@link #getWebUri} was explicitly provided in 154 * {@link android.app.Activity#onProvideAssistContent Activity.onProvideAssistContent}. If not, 155 * the Intent was automatically set based on 156 * {@link android.app.Activity#getIntent Activity.getIntent}. 157 */ isAppProvidedWebUri()158 public boolean isAppProvidedWebUri() { 159 return mIsAppProvidedWebUri; 160 } 161 162 /** 163 * Return Bundle for extra vendor-specific data that can be modified and examined. 164 */ getExtras()165 public Bundle getExtras() { 166 return mExtras; 167 } 168 169 @UnsupportedAppUsage AssistContent(Parcel in)170 AssistContent(Parcel in) { 171 if (in.readInt() != 0) { 172 mIntent = Intent.CREATOR.createFromParcel(in); 173 } 174 if (in.readInt() != 0) { 175 mClipData = ClipData.CREATOR.createFromParcel(in); 176 } 177 if (in.readInt() != 0) { 178 mUri = Uri.CREATOR.createFromParcel(in); 179 } 180 if (in.readInt() != 0) { 181 mStructuredData = in.readString(); 182 } 183 mIsAppProvidedIntent = in.readInt() == 1; 184 mExtras = in.readBundle(); 185 mIsAppProvidedWebUri = in.readInt() == 1; 186 } 187 188 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) writeToParcelInternal(Parcel dest, int flags)189 void writeToParcelInternal(Parcel dest, int flags) { 190 if (mIntent != null) { 191 dest.writeInt(1); 192 mIntent.writeToParcel(dest, flags); 193 } else { 194 dest.writeInt(0); 195 } 196 if (mClipData != null) { 197 dest.writeInt(1); 198 mClipData.writeToParcel(dest, flags); 199 } else { 200 dest.writeInt(0); 201 } 202 if (mUri != null) { 203 dest.writeInt(1); 204 mUri.writeToParcel(dest, flags); 205 } else { 206 dest.writeInt(0); 207 } 208 if (mStructuredData != null) { 209 dest.writeInt(1); 210 dest.writeString(mStructuredData); 211 } else { 212 dest.writeInt(0); 213 } 214 dest.writeInt(mIsAppProvidedIntent ? 1 : 0); 215 dest.writeBundle(mExtras); 216 dest.writeInt(mIsAppProvidedWebUri ? 1 : 0); 217 } 218 219 @Override describeContents()220 public int describeContents() { 221 return 0; 222 } 223 224 @Override writeToParcel(Parcel dest, int flags)225 public void writeToParcel(Parcel dest, int flags) { 226 writeToParcelInternal(dest, flags); 227 } 228 229 public static final @android.annotation.NonNull Parcelable.Creator<AssistContent> CREATOR 230 = new Parcelable.Creator<AssistContent>() { 231 public AssistContent createFromParcel(Parcel in) { 232 return new AssistContent(in); 233 } 234 235 public AssistContent[] newArray(int size) { 236 return new AssistContent[size]; 237 } 238 }; 239 } 240