1 /* 2 * Copyright (C) 2016 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.app; 18 19 import android.annotation.NonNull; 20 import android.graphics.drawable.Icon; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 import android.text.TextUtils; 24 25 import java.io.PrintWriter; 26 import java.util.Objects; 27 28 /** 29 * Represents a remote action that can be called from another process. The action can have an 30 * associated visualization including metadata like an icon or title. 31 */ 32 public final class RemoteAction implements Parcelable { 33 34 private static final String TAG = "RemoteAction"; 35 36 private final Icon mIcon; 37 private final CharSequence mTitle; 38 private final CharSequence mContentDescription; 39 private final PendingIntent mActionIntent; 40 private boolean mEnabled; 41 private boolean mShouldShowIcon; 42 RemoteAction(Parcel in)43 RemoteAction(Parcel in) { 44 mIcon = Icon.CREATOR.createFromParcel(in); 45 mTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 46 mContentDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 47 mActionIntent = PendingIntent.CREATOR.createFromParcel(in); 48 mEnabled = in.readBoolean(); 49 mShouldShowIcon = in.readBoolean(); 50 } 51 RemoteAction(@onNull Icon icon, @NonNull CharSequence title, @NonNull CharSequence contentDescription, @NonNull PendingIntent intent)52 public RemoteAction(@NonNull Icon icon, @NonNull CharSequence title, 53 @NonNull CharSequence contentDescription, @NonNull PendingIntent intent) { 54 if (icon == null || title == null || contentDescription == null || intent == null) { 55 throw new IllegalArgumentException("Expected icon, title, content description and " + 56 "action callback"); 57 } 58 mIcon = icon; 59 mTitle = title; 60 mContentDescription = contentDescription; 61 mActionIntent = intent; 62 mEnabled = true; 63 mShouldShowIcon = true; 64 } 65 66 /** 67 * Sets whether this action is enabled. 68 */ setEnabled(boolean enabled)69 public void setEnabled(boolean enabled) { 70 mEnabled = enabled; 71 } 72 73 /** 74 * Return whether this action is enabled. 75 */ isEnabled()76 public boolean isEnabled() { 77 return mEnabled; 78 } 79 80 /** 81 * Sets whether the icon should be shown. 82 */ setShouldShowIcon(boolean shouldShowIcon)83 public void setShouldShowIcon(boolean shouldShowIcon) { 84 mShouldShowIcon = shouldShowIcon; 85 } 86 87 /** 88 * Return whether the icon should be shown. 89 */ shouldShowIcon()90 public boolean shouldShowIcon() { 91 return mShouldShowIcon; 92 } 93 94 /** 95 * Return an icon representing the action. 96 */ getIcon()97 public @NonNull Icon getIcon() { 98 return mIcon; 99 } 100 101 /** 102 * Return an title representing the action. 103 */ getTitle()104 public @NonNull CharSequence getTitle() { 105 return mTitle; 106 } 107 108 /** 109 * Return a content description representing the action. 110 */ getContentDescription()111 public @NonNull CharSequence getContentDescription() { 112 return mContentDescription; 113 } 114 115 /** 116 * Return the action intent. 117 */ getActionIntent()118 public @NonNull PendingIntent getActionIntent() { 119 return mActionIntent; 120 } 121 122 @Override clone()123 public RemoteAction clone() { 124 RemoteAction action = new RemoteAction(mIcon, mTitle, mContentDescription, mActionIntent); 125 action.setEnabled(mEnabled); 126 action.setShouldShowIcon(mShouldShowIcon); 127 return action; 128 } 129 130 @Override equals(Object o)131 public boolean equals(Object o) { 132 if (this == o) return true; 133 if (!(o instanceof RemoteAction)) return false; 134 RemoteAction that = (RemoteAction) o; 135 return mEnabled == that.mEnabled 136 && mShouldShowIcon == that.mShouldShowIcon 137 && mIcon.equals(that.mIcon) 138 && mTitle.equals(that.mTitle) 139 && mContentDescription.equals(that.mContentDescription) 140 && mActionIntent.equals(that.mActionIntent); 141 } 142 143 @Override hashCode()144 public int hashCode() { 145 return Objects.hash(mIcon, mTitle, mContentDescription, mActionIntent, mEnabled, 146 mShouldShowIcon); 147 } 148 149 @Override describeContents()150 public int describeContents() { 151 return 0; 152 } 153 154 @Override writeToParcel(Parcel out, int flags)155 public void writeToParcel(Parcel out, int flags) { 156 mIcon.writeToParcel(out, 0); 157 TextUtils.writeToParcel(mTitle, out, flags); 158 TextUtils.writeToParcel(mContentDescription, out, flags); 159 mActionIntent.writeToParcel(out, flags); 160 out.writeBoolean(mEnabled); 161 out.writeBoolean(mShouldShowIcon); 162 } 163 dump(String prefix, PrintWriter pw)164 public void dump(String prefix, PrintWriter pw) { 165 pw.print(prefix); 166 pw.print("title=" + mTitle); 167 pw.print(" enabled=" + mEnabled); 168 pw.print(" contentDescription=" + mContentDescription); 169 pw.print(" icon=" + mIcon); 170 pw.print(" action=" + mActionIntent.getIntent()); 171 pw.print(" shouldShowIcon=" + mShouldShowIcon); 172 pw.println(); 173 } 174 175 public static final @android.annotation.NonNull Parcelable.Creator<RemoteAction> CREATOR = 176 new Parcelable.Creator<RemoteAction>() { 177 public RemoteAction createFromParcel(Parcel in) { 178 return new RemoteAction(in); 179 } 180 public RemoteAction[] newArray(int size) { 181 return new RemoteAction[size]; 182 } 183 }; 184 } 185