Home | History | Annotate | Download | only in app
      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 
     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 
     33     private static final String TAG = "RemoteAction";
     34 
     35     private final Icon mIcon;
     36     private final CharSequence mTitle;
     37     private final CharSequence mContentDescription;
     38     private final PendingIntent mActionIntent;
     39     private boolean mEnabled;
     40     private boolean mShouldShowIcon;
     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);
     46         mActionIntent = PendingIntent.CREATOR.createFromParcel(in);
     47         mEnabled = in.readBoolean();
     48         mShouldShowIcon = in.readBoolean();
     49     }
     50 
     51     public RemoteAction(@NonNull Icon icon, @NonNull CharSequence title,
     52             @NonNull CharSequence contentDescription, @NonNull PendingIntent intent) {
     53         if (icon == null || title == null || contentDescription == null || intent == null) {
     54             throw new IllegalArgumentException("Expected icon, title, content description and " +
     55                     "action callback");
     56         }
     57         mIcon = icon;
     58         mTitle = title;
     59         mContentDescription = contentDescription;
     60         mActionIntent = intent;
     61         mEnabled = true;
     62         mShouldShowIcon = true;
     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;
     77     }
     78 
     79     /**
     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     /**
     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     /**
    115      * Return the action intent.
    116      */
    117     public @NonNull PendingIntent getActionIntent() {
    118         return mActionIntent;
    119     }
    120 
    121     @Override
    122     public RemoteAction clone() {
    123         RemoteAction action = new RemoteAction(mIcon, mTitle, mContentDescription, mActionIntent);
    124         action.setEnabled(mEnabled);
    125         action.setShouldShowIcon(mShouldShowIcon);
    126         return action;
    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);
    139         mActionIntent.writeToParcel(out, flags);
    140         out.writeBoolean(mEnabled);
    141         out.writeBoolean(mShouldShowIcon);
    142     }
    143 
    144     public void dump(String prefix, PrintWriter pw) {
    145         pw.print(prefix);
    146         pw.print("title=" + mTitle);
    147         pw.print(" enabled=" + mEnabled);
    148         pw.print(" contentDescription=" + mContentDescription);
    149         pw.print(" icon=" + mIcon);
    150         pw.print(" action=" + mActionIntent.getIntent());
    151         pw.print(" shouldShowIcon=" + mShouldShowIcon);
    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 }