Debian Codegraph @

core/java/android/service/chooser/ChooserAction.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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 package android.service.chooser;
18
19 import android.annotation.NonNull;
20 import android.app.PendingIntent;
21 import android.graphics.drawable.Icon;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 import android.text.TextUtils;
25
26 import java.util.Objects;
27
28 /**
29 * A ChooserAction is an app-defined action that can be provided to the Android Sharesheet to
30 * be shown to the user when {@link android.content.Intent.ACTION_CHOOSER} is invoked.
31 *
32 * @see android.content.Intent.EXTRA_CHOOSER_CUSTOM_ACTIONS
33 * @see android.content.Intent.EXTRA_CHOOSER_PAYLOAD_RESELECTION_ACTION
34 * @hide
35 */
36 public final class ChooserAction implements Parcelable {
37 private final Icon mIcon;
38 private final CharSequence mLabel;
39 private final PendingIntent mAction;
40
41 private ChooserAction(
42 Icon icon,
43 CharSequence label,
44 PendingIntent action) {
45 mIcon = icon;
46 mLabel = label;
47 mAction = action;
48 }
49
50 /**
51 * Return a user-readable label for this action.
52 */
53 @NonNull
54 public CharSequence getLabel() {
55 return mLabel;
56 }
57
58 /**
59 * Return an {@link Icon} representing this action.
60 */
61 @NonNull
62 public Icon getIcon() {
63 return mIcon;
64 }
65
66 /**
67 * Return the action intent.
68 */
69 @NonNull
70 public PendingIntent getAction() {
71 return mAction;
72 }
73
74 @Override
75 public int describeContents() {
76 return 0;
77 }
78
79 @Override
80 public void writeToParcel(@NonNull Parcel dest, int flags) {
81 mIcon.writeToParcel(dest, flags);
82 TextUtils.writeToParcel(mLabel, dest, flags);
83 mAction.writeToParcel(dest, flags);
84 }
85
86 @Override
87 public String toString() {
88 return "ChooserAction {" + "label=" + mLabel + ", intent=" + mAction + "}";
89 }
90
91 public static final Parcelable.Creator<ChooserAction> CREATOR =
92 new Creator<ChooserAction>() {
93 @Override
94 public ChooserAction createFromParcel(Parcel source) {
95 return new ChooserAction(
96 Icon.CREATOR.createFromParcel(source),
97 TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source),
98 PendingIntent.CREATOR.createFromParcel(source));
99 }
100
101 @Override
102 public ChooserAction[] newArray(int size) {
103 return new ChooserAction[size];
104 }
105 };
106
107 /**
108 * Builder class for {@link ChooserAction} objects
109 */
110 public static final class Builder {
111 private final Icon mIcon;
112 private final CharSequence mLabel;
113 private final PendingIntent mAction;
114
115 /**
116 * Construct a new builder for {@link ChooserAction} object.
117 *
118 * @param icon an {@link Icon} representing this action, consisting of a white foreground
119 * atop a transparent background.
120 * @param label label the user-readable label for this action.
121 * @param action {@link PendingIntent} to be invoked when the action is selected.
122 */
123 public Builder(
124 @NonNull Icon icon,
125 @NonNull CharSequence label,
126 @NonNull PendingIntent action) {
127 Objects.requireNonNull(icon, "icon can not be null");
128 Objects.requireNonNull(label, "label can not be null");
129 Objects.requireNonNull(action, "pending intent can not be null");
130 mIcon = icon;
131 mLabel = label;
132 mAction = action;
133 }
134
135 /**
136 * Combine all of the options that have been set and return a new {@link ChooserAction}
137 * object.
138 * @return the built action
139 */
140 public ChooserAction build() {
141 return new ChooserAction(mIcon, mLabel, mAction);
142 }
143 }
144 }