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