Home | History | Annotate | Download | only in builders
      1 /*
      2  * Copyright 2018 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 androidx.slice.builders;
     18 
     19 import static androidx.annotation.RestrictTo.Scope.LIBRARY;
     20 import static androidx.slice.builders.ListBuilder.ICON_IMAGE;
     21 
     22 import android.app.PendingIntent;
     23 import android.graphics.drawable.Icon;
     24 
     25 import androidx.annotation.IntRange;
     26 import androidx.annotation.NonNull;
     27 import androidx.annotation.Nullable;
     28 import androidx.annotation.RequiresApi;
     29 import androidx.annotation.RestrictTo;
     30 import androidx.core.graphics.drawable.IconCompat;
     31 import androidx.slice.Slice;
     32 import androidx.slice.core.SliceActionImpl;
     33 
     34 /**
     35  * Class representing an action, supports tappable icons, custom toggle icons, and default toggles.
     36  */
     37 public class SliceAction implements androidx.slice.core.SliceAction {
     38 
     39     private SliceActionImpl mSliceAction;
     40 
     41     /**
     42      * @deprecated TO BE REMOVED
     43      */
     44     @Deprecated
     45     @RequiresApi(23)
     46     public SliceAction(@NonNull PendingIntent action, @NonNull Icon actionIcon,
     47             @NonNull CharSequence actionTitle) {
     48         this(action, actionIcon, ICON_IMAGE, actionTitle);
     49     }
     50 
     51     /**
     52      * @deprecated TO BE REMOVED
     53      */
     54     @Deprecated
     55     @RequiresApi(23)
     56     public SliceAction(@NonNull PendingIntent action, @NonNull Icon actionIcon,
     57             @ListBuilder.ImageMode int imageMode, @NonNull CharSequence actionTitle) {
     58         this(action, IconCompat.createFromIcon(actionIcon), imageMode, actionTitle);
     59     }
     60 
     61     /**
     62      * @deprecated TO BE REMOVED
     63      */
     64     @Deprecated
     65     @RequiresApi(23)
     66     public SliceAction(@NonNull PendingIntent action, @NonNull Icon actionIcon,
     67             @NonNull CharSequence actionTitle, boolean isChecked) {
     68         this(action, IconCompat.createFromIcon(actionIcon), actionTitle, isChecked);
     69     }
     70 
     71     /**
     72      * Construct a SliceAction representing a tappable icon.
     73      *
     74      * @param action the pending intent to invoke for this action.
     75      * @param actionIcon the icon to display for this action.
     76      * @param actionTitle the title for this action, also used for content description if one hasn't
     77      *                    been set via {@link #setContentDescription(CharSequence)}.
     78      */
     79     public SliceAction(@NonNull PendingIntent action, @NonNull IconCompat actionIcon,
     80             @NonNull CharSequence actionTitle) {
     81         this(action, actionIcon, ICON_IMAGE, actionTitle);
     82     }
     83 
     84     /**
     85      * Construct a SliceAction representing a tappable icon. Use this method to specify the
     86      * format of the image, {@link ListBuilder#ICON_IMAGE} will be presented as a tintable icon.
     87      * Note that there is no difference between {@link ListBuilder#SMALL_IMAGE} and
     88      * {@link ListBuilder#LARGE_IMAGE} for actions; these will just be represented as an
     89      * non-tintable image.
     90      *
     91      * @param action the pending intent to invoke for this action.
     92      * @param actionIcon the icon to display for this action.
     93      * @param imageMode the mode this icon should be displayed in.
     94      * @param actionTitle the title for this action, also used for content description if one hasn't
     95      *                    been set via {@link #setContentDescription(CharSequence)}.
     96      *
     97      * @see ListBuilder#ICON_IMAGE
     98      * @see ListBuilder#SMALL_IMAGE
     99      * @see ListBuilder#LARGE_IMAGE
    100      */
    101     public SliceAction(@NonNull PendingIntent action, @NonNull IconCompat actionIcon,
    102             @ListBuilder.ImageMode int imageMode, @NonNull CharSequence actionTitle) {
    103         mSliceAction = new SliceActionImpl(action, actionIcon, imageMode, actionTitle);
    104     }
    105 
    106     /**
    107      * Construct a SliceAction representing a custom toggle icon.
    108      *
    109      * @param action the pending intent to invoke for this toggle.
    110      * @param actionIcon the icon to display for this toggle, should have a checked and unchecked
    111      *                   state.
    112      * @param actionTitle the title for this toggle, also used for content description if one hasn't
    113      *                    been set via {@link #setContentDescription(CharSequence)}.
    114      * @param isChecked the state of the toggle.
    115      */
    116     public SliceAction(@NonNull PendingIntent action, @NonNull IconCompat actionIcon,
    117             @NonNull CharSequence actionTitle, boolean isChecked) {
    118         mSliceAction = new SliceActionImpl(action, actionIcon, actionTitle, isChecked);
    119     }
    120 
    121     /**
    122      * Construct a SliceAction representing a default toggle.
    123      *
    124      * @param action the pending intent to invoke for this toggle.
    125      * @param actionTitle the title for this toggle, also used for content description if one hasn't
    126      *                    been set via {@link #setContentDescription(CharSequence)}.
    127      * @param isChecked the state of the toggle.
    128      */
    129     public SliceAction(@NonNull PendingIntent action, @NonNull CharSequence actionTitle,
    130             boolean isChecked) {
    131         mSliceAction = new SliceActionImpl(action, actionTitle, isChecked);
    132     }
    133 
    134     /**
    135      * @param description the content description for this action.
    136      */
    137     @NonNull
    138     @Override
    139     public SliceAction setContentDescription(@NonNull CharSequence description) {
    140         mSliceAction.setContentDescription(description);
    141         return this;
    142     }
    143 
    144     /**
    145      * @param isChecked whether the state of this action is checked or not; only used for toggle
    146      *                  actions.
    147      */
    148     @NonNull
    149     @Override
    150     public SliceAction setChecked(boolean isChecked) {
    151         mSliceAction.setChecked(isChecked);
    152         return this;
    153     }
    154 
    155     /**
    156      * Sets the priority of this action, with the lowest priority having the highest ranking.
    157      */
    158     @NonNull
    159     @Override
    160     public SliceAction setPriority(@IntRange(from = 0) int priority) {
    161         mSliceAction.setPriority(priority);
    162         return this;
    163     }
    164 
    165     /**
    166      * @return the {@link PendingIntent} associated with this action.
    167      */
    168     @NonNull
    169     @Override
    170     public PendingIntent getAction() {
    171         return mSliceAction.getAction();
    172     }
    173 
    174     /**
    175      * @return the {@link Icon} to display for this action. This can be null when the action
    176      * represented is a default toggle.
    177      */
    178     @Nullable
    179     @Override
    180     public IconCompat getIcon() {
    181         return mSliceAction.getIcon();
    182     }
    183 
    184     /**
    185      * @return the title for this action.
    186      */
    187     @NonNull
    188     @Override
    189     public CharSequence getTitle() {
    190         return mSliceAction.getTitle();
    191     }
    192 
    193     /**
    194      * @return the content description to use for this action.
    195      */
    196     @Nullable
    197     @Override
    198     public CharSequence getContentDescription() {
    199         return mSliceAction.getContentDescription();
    200     }
    201 
    202     /**
    203      * @return the priority associated with this action, -1 if unset.
    204      */
    205     @Override
    206     public int getPriority() {
    207         return mSliceAction.getPriority();
    208     }
    209 
    210     /**
    211      * @return whether this action represents a toggle (i.e. has a checked and unchecked state).
    212      */
    213     @Override
    214     public boolean isToggle() {
    215         return mSliceAction.isToggle();
    216     }
    217 
    218     /**
    219      * @return whether the state of this action is checked or not; only used for toggle actions.
    220      */
    221     @Override
    222     public boolean isChecked() {
    223         return mSliceAction.isChecked();
    224     }
    225 
    226     /**
    227      * @return the image mode to use for this action.
    228      */
    229     @Override
    230     public @ListBuilder.ImageMode int getImageMode() {
    231         return mSliceAction.getImageMode();
    232     }
    233 
    234     /**
    235      * @return whether this action is a toggle using the standard switch control.
    236      */
    237     @Override
    238     public boolean isDefaultToggle() {
    239         return mSliceAction.isDefaultToggle();
    240     }
    241 
    242     /**
    243      * @param builder this should be a new builder that has any additional hints the action might
    244      *                need.
    245      * @return the slice representation of this action.
    246      * @hide
    247      */
    248     @RestrictTo(LIBRARY)
    249     @NonNull
    250     public Slice buildSlice(@NonNull Slice.Builder builder) {
    251         return mSliceAction.buildSlice(builder);
    252     }
    253 
    254     /**
    255      * @hide
    256      */
    257     @RestrictTo(LIBRARY)
    258     @NonNull
    259     public SliceActionImpl getImpl() {
    260         return mSliceAction;
    261     }
    262 }
    263