Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 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; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 23 | import android.text.TextUtils; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 24 | |
| 25 | import java.io.PrintWriter; |
| 26 | |
| 27 | /** |
| 28 | * Represents a remote action that can be called from another process. The action can have an |
| 29 | * associated visualization including metadata like an icon or title. |
| 30 | */ |
| 31 | public final class RemoteAction implements Parcelable { |
| 32 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 33 | private static final String TAG = "RemoteAction"; |
| 34 | |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 35 | private final Icon mIcon; |
| 36 | private final CharSequence mTitle; |
| 37 | private final CharSequence mContentDescription; |
Winson Chung | 9c2a686 | 2017-02-03 12:11:00 -0800 | [diff] [blame] | 38 | private final PendingIntent mActionIntent; |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 39 | private boolean mEnabled; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 40 | private boolean mShouldShowIcon; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 41 | |
| 42 | RemoteAction(Parcel in) { |
| 43 | mIcon = Icon.CREATOR.createFromParcel(in); |
| 44 | mTitle = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 45 | mContentDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
Winson Chung | 9c2a686 | 2017-02-03 12:11:00 -0800 | [diff] [blame] | 46 | mActionIntent = PendingIntent.CREATOR.createFromParcel(in); |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 47 | mEnabled = in.readBoolean(); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 48 | mShouldShowIcon = in.readBoolean(); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public RemoteAction(@NonNull Icon icon, @NonNull CharSequence title, |
Winson Chung | 9c2a686 | 2017-02-03 12:11:00 -0800 | [diff] [blame] | 52 | @NonNull CharSequence contentDescription, @NonNull PendingIntent intent) { |
| 53 | if (icon == null || title == null || contentDescription == null || intent == null) { |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 54 | throw new IllegalArgumentException("Expected icon, title, content description and " + |
| 55 | "action callback"); |
| 56 | } |
| 57 | mIcon = icon; |
| 58 | mTitle = title; |
| 59 | mContentDescription = contentDescription; |
Winson Chung | 9c2a686 | 2017-02-03 12:11:00 -0800 | [diff] [blame] | 60 | mActionIntent = intent; |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 61 | mEnabled = true; |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 62 | mShouldShowIcon = true; |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Sets whether this action is enabled. |
| 67 | */ |
| 68 | public void setEnabled(boolean enabled) { |
| 69 | mEnabled = enabled; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Return whether this action is enabled. |
| 74 | */ |
| 75 | public boolean isEnabled() { |
| 76 | return mEnabled; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 80 | * Sets whether the icon should be shown. |
| 81 | */ |
| 82 | public void setShouldShowIcon(boolean shouldShowIcon) { |
| 83 | mShouldShowIcon = shouldShowIcon; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Return whether the icon should be shown. |
| 88 | */ |
| 89 | public boolean shouldShowIcon() { |
| 90 | return mShouldShowIcon; |
| 91 | } |
| 92 | |
| 93 | /** |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 94 | * Return an icon representing the action. |
| 95 | */ |
| 96 | public @NonNull Icon getIcon() { |
| 97 | return mIcon; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Return an title representing the action. |
| 102 | */ |
| 103 | public @NonNull CharSequence getTitle() { |
| 104 | return mTitle; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Return a content description representing the action. |
| 109 | */ |
| 110 | public @NonNull CharSequence getContentDescription() { |
| 111 | return mContentDescription; |
| 112 | } |
| 113 | |
| 114 | /** |
Winson Chung | 9c2a686 | 2017-02-03 12:11:00 -0800 | [diff] [blame] | 115 | * Return the action intent. |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 116 | */ |
Winson Chung | 9c2a686 | 2017-02-03 12:11:00 -0800 | [diff] [blame] | 117 | public @NonNull PendingIntent getActionIntent() { |
| 118 | return mActionIntent; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public RemoteAction clone() { |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 123 | RemoteAction action = new RemoteAction(mIcon, mTitle, mContentDescription, mActionIntent); |
| 124 | action.setEnabled(mEnabled); |
Jan Althaus | ab78047 | 2018-04-05 17:43:02 +0200 | [diff] [blame] | 125 | action.setShouldShowIcon(mShouldShowIcon); |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 126 | return action; |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int describeContents() { |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void writeToParcel(Parcel out, int flags) { |
| 136 | mIcon.writeToParcel(out, 0); |
| 137 | TextUtils.writeToParcel(mTitle, out, flags); |
| 138 | TextUtils.writeToParcel(mContentDescription, out, flags); |
Winson Chung | 9c2a686 | 2017-02-03 12:11:00 -0800 | [diff] [blame] | 139 | mActionIntent.writeToParcel(out, flags); |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 140 | out.writeBoolean(mEnabled); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 141 | out.writeBoolean(mShouldShowIcon); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | public void dump(String prefix, PrintWriter pw) { |
| 145 | pw.print(prefix); |
| 146 | pw.print("title=" + mTitle); |
Winson Chung | 6b88baf | 2017-03-16 17:10:21 -0700 | [diff] [blame] | 147 | pw.print(" enabled=" + mEnabled); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 148 | pw.print(" contentDescription=" + mContentDescription); |
| 149 | pw.print(" icon=" + mIcon); |
Winson Chung | 9c2a686 | 2017-02-03 12:11:00 -0800 | [diff] [blame] | 150 | pw.print(" action=" + mActionIntent.getIntent()); |
Jan Althaus | 20d346e | 2018-03-23 14:03:52 +0100 | [diff] [blame] | 151 | pw.print(" shouldShowIcon=" + mShouldShowIcon); |
Winson Chung | a29eb98 | 2016-12-14 12:01:27 -0800 | [diff] [blame] | 152 | pw.println(); |
| 153 | } |
| 154 | |
| 155 | public static final Parcelable.Creator<RemoteAction> CREATOR = |
| 156 | new Parcelable.Creator<RemoteAction>() { |
| 157 | public RemoteAction createFromParcel(Parcel in) { |
| 158 | return new RemoteAction(in); |
| 159 | } |
| 160 | public RemoteAction[] newArray(int size) { |
| 161 | return new RemoteAction[size]; |
| 162 | } |
| 163 | }; |
| 164 | } |