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