1 /*
2  * Copyright (C) 2015 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 
17 
18 package android.service.chooser;
19 
20 import android.annotation.Nullable;
21 import android.content.ComponentName;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.graphics.drawable.Icon;
25 import android.os.Bundle;
26 import android.os.Parcel;
27 import android.os.Parcelable;
28 
29 /**
30  * A ChooserTarget represents a deep-link into an application as returned by a
31  * {@link android.service.chooser.ChooserTargetService}.
32  *
33  * <p>A chooser target represents a specific deep link target into an application exposed
34  * for selection by the user. This might be a frequently emailed contact, a recently active
35  * group messaging conversation, a folder in a cloud storage app, a collection of related
36  * items published on a social media service or any other contextually relevant grouping
37  * of target app + relevant metadata.</p>
38  *
39  * <p>Creators of chooser targets should consult the relevant design guidelines for the type
40  * of target they are presenting. For example, targets involving people should be presented
41  * with a circular icon.</p>
42  *
43  * @deprecated For publishing direct share targets, please follow the instructions in
44  * https://developer.android.com/training/sharing/receive.html#providing-direct-share-targets
45  * instead.
46  */
47 @Deprecated
48 public final class ChooserTarget implements Parcelable {
49     private static final String TAG = "ChooserTarget";
50 
51     /**
52      * The title of this target that will be shown to the user. The title may be truncated
53      * if it is too long to display in the space provided.
54      */
55     private CharSequence mTitle;
56 
57     /**
58      * The icon that will be shown to the user to represent this target.
59      * The system may resize this icon as appropriate.
60      */
61     private Icon mIcon;
62 
63     /**
64      * The ComponentName of the Activity to be invoked. Must be part of the target creator's
65      * own package or an Activity exported by its package.
66      */
67     private ComponentName mComponentName;
68 
69     /**
70      * A Bundle to merge with the extras of the intent sent to this target.
71      * Any extras here will override the extras from the original intent.
72      */
73     private Bundle mIntentExtras;
74 
75     /**
76      * The score given to this item. It can be normalized.
77      */
78     private float mScore;
79 
80     /**
81      * Construct a deep link target for presentation by a chooser UI.
82      *
83      * <p>A target is composed of a title and an icon for presentation to the user.
84      * The UI presenting this target may truncate the title if it is too long to be presented
85      * in the available space, as well as crop, resize or overlay the supplied icon.</p>
86      *
87      * <p>The creator of a target may supply a ranking score. This score is assumed to be relative
88      * to the other targets supplied by the same
89      * {@link ChooserTargetService#onGetChooserTargets(ComponentName, IntentFilter) query}.
90      * Scores should be in the range from 0.0f (unlikely match) to 1.0f (very relevant match).
91      * Scores for a set of targets do not need to sum to 1.</p>
92      *
93      * <p>The ComponentName must be the name of an Activity component in the creator's own
94      * package, or an exported component from any other package. You may provide an optional
95      * Bundle of extras that will be merged into the final intent before it is sent to the
96      * target Activity; use this to add any additional data about the deep link that the target
97      * activity will read. e.g. conversation IDs, email addresses, etc.</p>
98      *
99      * <p>Take care not to place custom {@link android.os.Parcelable} types into
100      * the extras bundle, as the system will not be able to unparcel them to merge them.</p>
101      *
102      * @param title title of this target that will be shown to a user
103      * @param icon icon to represent this target
104      * @param score ranking score for this target between 0.0f and 1.0f, inclusive
105      * @param componentName Name of the component to be launched if this target is chosen
106      * @param intentExtras Bundle of extras to merge with the extras of the launched intent
107      */
ChooserTarget(CharSequence title, Icon icon, float score, ComponentName componentName, @Nullable Bundle intentExtras)108     public ChooserTarget(CharSequence title, Icon icon, float score,
109             ComponentName componentName, @Nullable Bundle intentExtras) {
110         mTitle = title;
111         mIcon = icon;
112         if (score > 1.f || score < 0.f) {
113             throw new IllegalArgumentException("Score " + score + " out of range; "
114                     + "must be between 0.0f and 1.0f");
115         }
116         mScore = score;
117         mComponentName = componentName;
118         mIntentExtras = intentExtras;
119     }
120 
ChooserTarget(Parcel in)121     ChooserTarget(Parcel in) {
122         mTitle = in.readCharSequence();
123         if (in.readInt() != 0) {
124             mIcon = Icon.CREATOR.createFromParcel(in);
125         } else {
126             mIcon = null;
127         }
128         mScore = in.readFloat();
129         mComponentName = ComponentName.readFromParcel(in);
130         mIntentExtras = in.readBundle();
131     }
132 
133     /**
134      * Returns the title of this target for display to a user. The UI displaying the title
135      * may truncate this title if it is too long to be displayed in full.
136      *
137      * @return the title of this target, intended to be shown to a user
138      */
getTitle()139     public CharSequence getTitle() {
140         return mTitle;
141     }
142 
143     /**
144      * Returns the icon representing this target for display to a user. The UI displaying the icon
145      * may crop, resize or overlay this icon.
146      *
147      * @return the icon representing this target, intended to be shown to a user
148      */
getIcon()149     public Icon getIcon() {
150         return mIcon;
151     }
152 
153     /**
154      * Returns the ranking score supplied by the creator of this ChooserTarget.
155      * Values are between 0.0f and 1.0f. The UI displaying the target may
156      * take this score into account when sorting and merging targets from multiple sources.
157      *
158      * @return the ranking score for this target between 0.0f and 1.0f, inclusive
159      */
getScore()160     public float getScore() {
161         return mScore;
162     }
163 
164     /**
165      * Returns the ComponentName of the Activity that should be launched for this ChooserTarget.
166      *
167      * @return the name of the target Activity to launch
168      */
getComponentName()169     public ComponentName getComponentName() {
170         return mComponentName;
171     }
172 
173     /**
174      * Returns the Bundle of extras to be added to an intent launched to this target.
175      *
176      * @return the extras to merge with the extras of the intent being launched
177      */
getIntentExtras()178     public Bundle getIntentExtras() {
179         return mIntentExtras;
180     }
181 
182     @Override
toString()183     public String toString() {
184         return "ChooserTarget{"
185                 + mComponentName
186                 + ", " + mIntentExtras
187                 + ", '" + mTitle
188                 + "', " + mScore + "}";
189     }
190 
191     @Override
describeContents()192     public int describeContents() {
193         return 0;
194     }
195 
196     @Override
writeToParcel(Parcel dest, int flags)197     public void writeToParcel(Parcel dest, int flags) {
198         dest.writeCharSequence(mTitle);
199         if (mIcon != null) {
200             dest.writeInt(1);
201             mIcon.writeToParcel(dest, 0);
202         } else {
203             dest.writeInt(0);
204         }
205         dest.writeFloat(mScore);
206         ComponentName.writeToParcel(mComponentName, dest);
207         dest.writeBundle(mIntentExtras);
208     }
209 
210     public static final @android.annotation.NonNull Creator<ChooserTarget> CREATOR
211             = new Creator<ChooserTarget>() {
212         @Override
213         public ChooserTarget createFromParcel(Parcel source) {
214             return new ChooserTarget(source);
215         }
216 
217         @Override
218         public ChooserTarget[] newArray(int size) {
219             return new ChooserTarget[size];
220         }
221     };
222 }
223