blob: c1746655da5e8b0c24b3020c212368eeb298b91d [file] [log] [blame]
Winson Chunga29eb982016-12-14 12:01:27 -08001/*
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
17package android.app;
18
19import android.annotation.NonNull;
20import android.graphics.drawable.Icon;
Winson Chunga29eb982016-12-14 12:01:27 -080021import android.os.Parcel;
22import android.os.Parcelable;
Winson Chunga29eb982016-12-14 12:01:27 -080023import android.text.TextUtils;
Winson Chunga29eb982016-12-14 12:01:27 -080024
25import 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 */
31public final class RemoteAction implements Parcelable {
32
Winson Chunga29eb982016-12-14 12:01:27 -080033 private static final String TAG = "RemoteAction";
34
Winson Chunga29eb982016-12-14 12:01:27 -080035 private final Icon mIcon;
36 private final CharSequence mTitle;
37 private final CharSequence mContentDescription;
Winson Chung9c2a6862017-02-03 12:11:00 -080038 private final PendingIntent mActionIntent;
Winson Chung6b88baf2017-03-16 17:10:21 -070039 private boolean mEnabled;
Jan Althaus20d346e2018-03-23 14:03:52 +010040 private boolean mShouldShowIcon;
Winson Chunga29eb982016-12-14 12:01:27 -080041
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 Chung9c2a6862017-02-03 12:11:00 -080046 mActionIntent = PendingIntent.CREATOR.createFromParcel(in);
Winson Chung6b88baf2017-03-16 17:10:21 -070047 mEnabled = in.readBoolean();
Jan Althaus20d346e2018-03-23 14:03:52 +010048 mShouldShowIcon = in.readBoolean();
Winson Chunga29eb982016-12-14 12:01:27 -080049 }
50
51 public RemoteAction(@NonNull Icon icon, @NonNull CharSequence title,
Winson Chung9c2a6862017-02-03 12:11:00 -080052 @NonNull CharSequence contentDescription, @NonNull PendingIntent intent) {
53 if (icon == null || title == null || contentDescription == null || intent == null) {
Winson Chunga29eb982016-12-14 12:01:27 -080054 throw new IllegalArgumentException("Expected icon, title, content description and " +
55 "action callback");
56 }
57 mIcon = icon;
58 mTitle = title;
59 mContentDescription = contentDescription;
Winson Chung9c2a6862017-02-03 12:11:00 -080060 mActionIntent = intent;
Winson Chung6b88baf2017-03-16 17:10:21 -070061 mEnabled = true;
Jan Althaus20d346e2018-03-23 14:03:52 +010062 mShouldShowIcon = true;
Winson Chung6b88baf2017-03-16 17:10:21 -070063 }
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 Chunga29eb982016-12-14 12:01:27 -080077 }
78
79 /**
Jan Althaus20d346e2018-03-23 14:03:52 +010080 * 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 Chunga29eb982016-12-14 12:01:27 -080094 * 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 Chung9c2a6862017-02-03 12:11:00 -0800115 * Return the action intent.
Winson Chunga29eb982016-12-14 12:01:27 -0800116 */
Winson Chung9c2a6862017-02-03 12:11:00 -0800117 public @NonNull PendingIntent getActionIntent() {
118 return mActionIntent;
Winson Chunga29eb982016-12-14 12:01:27 -0800119 }
120
121 @Override
122 public RemoteAction clone() {
Winson Chung6b88baf2017-03-16 17:10:21 -0700123 RemoteAction action = new RemoteAction(mIcon, mTitle, mContentDescription, mActionIntent);
124 action.setEnabled(mEnabled);
Jan Althausab780472018-04-05 17:43:02 +0200125 action.setShouldShowIcon(mShouldShowIcon);
Winson Chung6b88baf2017-03-16 17:10:21 -0700126 return action;
Winson Chunga29eb982016-12-14 12:01:27 -0800127 }
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 Chung9c2a6862017-02-03 12:11:00 -0800139 mActionIntent.writeToParcel(out, flags);
Winson Chung6b88baf2017-03-16 17:10:21 -0700140 out.writeBoolean(mEnabled);
Jan Althaus20d346e2018-03-23 14:03:52 +0100141 out.writeBoolean(mShouldShowIcon);
Winson Chunga29eb982016-12-14 12:01:27 -0800142 }
143
144 public void dump(String prefix, PrintWriter pw) {
145 pw.print(prefix);
146 pw.print("title=" + mTitle);
Winson Chung6b88baf2017-03-16 17:10:21 -0700147 pw.print(" enabled=" + mEnabled);
Winson Chunga29eb982016-12-14 12:01:27 -0800148 pw.print(" contentDescription=" + mContentDescription);
149 pw.print(" icon=" + mIcon);
Winson Chung9c2a6862017-02-03 12:11:00 -0800150 pw.print(" action=" + mActionIntent.getIntent());
Jan Althaus20d346e2018-03-23 14:03:52 +0100151 pw.print(" shouldShowIcon=" + mShouldShowIcon);
Winson Chunga29eb982016-12-14 12:01:27 -0800152 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}