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