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