Home | History | Annotate | Download | only in old
      1 /*
      2  * Copyright (C) 2014 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 com.android.tv.settings.dialog.old;
     18 
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.pm.PackageManager;
     22 import android.content.res.Resources;
     23 import android.graphics.drawable.Drawable;
     24 import android.net.Uri;
     25 import android.os.Parcel;
     26 import android.os.Parcelable;
     27 import android.util.Log;
     28 
     29 import java.util.ArrayList;
     30 
     31 /**
     32  * An action within an {@link ActionAdapter}.
     33  */
     34 public class Action implements Parcelable {
     35 
     36     private static final String TAG = "Action";
     37 
     38     public static final int NO_DRAWABLE = 0;
     39     public static final int NO_CHECK_SET = 0;
     40     public static final int DEFAULT_CHECK_SET_ID = 1;
     41 
     42     private String mKey;
     43     private String mTitle;
     44     private String mDescription;
     45     private Intent mIntent;
     46 
     47     /**
     48      * If not {@code null}, the package name to use to retrieve {{@link #mDrawableResource}.
     49      */
     50     private String mResourcePackageName;
     51 
     52     private int mDrawableResource;
     53     private Uri mIconUri;
     54     private boolean mChecked;
     55     private boolean mMultilineDescription;
     56     private boolean mHasNext;
     57     private boolean mInfoOnly;
     58     private int mCheckSetId;
     59     private boolean mEnabled;
     60 
     61     /**
     62      * Builds a Action object.
     63      */
     64     public static class Builder {
     65         private String mKey;
     66         private String mTitle;
     67         private String mDescription;
     68         private Intent mIntent;
     69         private String mResourcePackageName;
     70         private int mDrawableResource = NO_DRAWABLE;
     71         private Uri mIconUri;
     72         private boolean mChecked;
     73         private boolean mMultilineDescription;
     74         private boolean mHasNext;
     75         private boolean mInfoOnly;
     76         private int mCheckSetId = NO_CHECK_SET;
     77         private boolean mEnabled = true;
     78 
     79         public Action build() {
     80             Action action = new Action();
     81             action.mKey = mKey;
     82             action.mTitle = mTitle;
     83             action.mDescription = mDescription;
     84             action.mIntent = mIntent;
     85             action.mResourcePackageName = mResourcePackageName;
     86             action.mDrawableResource = mDrawableResource;
     87             action.mIconUri = mIconUri;
     88             action.mChecked = mChecked;
     89             action.mMultilineDescription = mMultilineDescription;
     90             action.mHasNext = mHasNext;
     91             action.mInfoOnly = mInfoOnly;
     92             action.mCheckSetId = mCheckSetId;
     93             action.mEnabled = mEnabled;
     94             return action;
     95         }
     96 
     97         public Builder key(String key) {
     98             mKey = key;
     99             return this;
    100         }
    101 
    102         public Builder title(String title) {
    103             mTitle = title;
    104             return this;
    105         }
    106 
    107         public Builder description(String description) {
    108             mDescription = description;
    109             return this;
    110         }
    111 
    112         public Builder intent(Intent intent) {
    113             mIntent = intent;
    114             return this;
    115         }
    116 
    117         public Builder resourcePackageName(String resourcePackageName) {
    118             mResourcePackageName = resourcePackageName;
    119             return this;
    120         }
    121 
    122         public Builder drawableResource(int drawableResource) {
    123             mDrawableResource = drawableResource;
    124             return this;
    125         }
    126 
    127         public Builder iconUri(Uri iconUri) {
    128             mIconUri = iconUri;
    129             return this;
    130         }
    131 
    132         public Builder checked(boolean checked) {
    133             mChecked = checked;
    134             return this;
    135         }
    136 
    137         public Builder multilineDescription(boolean multilineDescription) {
    138             mMultilineDescription = multilineDescription;
    139             return this;
    140         }
    141 
    142         public Builder hasNext(boolean hasNext) {
    143             mHasNext = hasNext;
    144             return this;
    145         }
    146 
    147         public Builder infoOnly(boolean infoOnly) {
    148             mInfoOnly = infoOnly;
    149             return this;
    150         }
    151 
    152         public Builder checkSetId(int checkSetId) {
    153             mCheckSetId = checkSetId;
    154             return this;
    155         }
    156 
    157         public Builder enabled(boolean enabled) {
    158             mEnabled = enabled;
    159             return this;
    160         }
    161     }
    162 
    163     private Action() {
    164     }
    165 
    166     protected Action(String key, String title, String description, String resourcePackageName,
    167             int drawableResource, boolean checked, boolean multilineDescription, boolean hasNext,
    168             boolean infoOnly, Intent intent, int checkSetId) {
    169         mKey = key;
    170         mTitle = title;
    171         mDescription = description;
    172         mResourcePackageName = resourcePackageName;
    173         mDrawableResource = drawableResource;
    174         mChecked = checked;
    175         mMultilineDescription = multilineDescription;
    176         mHasNext = hasNext;
    177         mInfoOnly = infoOnly;
    178         mIntent = intent;
    179         mCheckSetId = checkSetId;
    180         mEnabled = true;
    181     }
    182 
    183     /**
    184      * Returns a list of {@link Action} with the specified keys and titles
    185      * matched up.
    186      * <p>
    187      * The key and title arrays must be of equal length.
    188      */
    189     public static ArrayList<Action> createActionsFromArrays(String[] keys, String[] titles) {
    190         return createActionsFromArrays(keys, titles, NO_CHECK_SET, null);
    191     }
    192 
    193     /**
    194      * Returns a list of {@link Action} with the specified keys and titles
    195      * matched up.
    196      * <p>
    197      * The key and title arrays must be of equal length.
    198      */
    199     public static ArrayList<Action> createActionsFromArrays(
    200             String[] keys, String[] titles, String checkedItemKey) {
    201         return createActionsFromArrays(keys, titles, DEFAULT_CHECK_SET_ID, checkedItemKey);
    202     }
    203 
    204     /**
    205      * Returns a list of {@link Action} with the specified keys and titles
    206      * matched up and a given check set ID so that they are related.
    207      * <p>
    208      * The key and title arrays must be of equal length.
    209      */
    210     public static ArrayList<Action> createActionsFromArrays(String[] keys, String[] titles,
    211             int checkSetId) {
    212         return createActionsFromArrays(keys, titles, checkSetId, null);
    213     }
    214 
    215     /**
    216      * Returns a list of {@link Action} with the specified keys and titles
    217      * matched up and a given check set ID so that they are related.
    218      * <p>
    219      * The key and title arrays must be of equal length.
    220      */
    221     public static ArrayList<Action> createActionsFromArrays(String[] keys, String[] titles,
    222             int checkSetId, String checkedItemKey) {
    223         int keysLength = keys.length;
    224         int titlesLength = titles.length;
    225 
    226         if (keysLength != titlesLength) {
    227             throw new IllegalArgumentException("Keys and titles dimensions must match");
    228         }
    229 
    230         ArrayList<Action> actions = new ArrayList<Action>();
    231         for (int i = 0; i < keysLength; i++) {
    232             Action.Builder builder = new Action.Builder();
    233             builder.key(keys[i]).title(titles[i]).checkSetId(checkSetId);
    234             if (checkedItemKey != null) {
    235                 if (checkedItemKey.equals(keys[i])) {
    236                     builder.checked(true);
    237                 } else {
    238                     builder.checked(false);
    239                 }
    240             }
    241             Action action = builder.build();
    242             actions.add(action);
    243         }
    244         return actions;
    245     }
    246 
    247     public String getKey() {
    248         return mKey;
    249     }
    250 
    251     public String getTitle() {
    252         return mTitle;
    253     }
    254 
    255     public String getDescription() {
    256         return mDescription;
    257     }
    258 
    259     public Intent getIntent() {
    260         return mIntent;
    261     }
    262 
    263     public boolean isChecked() {
    264         return mChecked;
    265     }
    266 
    267     public int getDrawableResource() {
    268         return mDrawableResource;
    269     }
    270 
    271     public Uri getIconUri() {
    272         return mIconUri;
    273     }
    274 
    275     public String getResourcePackageName() {
    276         return mResourcePackageName;
    277     }
    278 
    279     /**
    280      * Returns the check set id this action is a part of.  All actions in the same list with the
    281      * same check set id are considered linked.  When one of the actions within that set is selected
    282      * that action becomes checked while all the other actions become unchecked.
    283      * @return an integer representing the check set this action is a part of or {@NO_CHECK_SET} if
    284      * this action isn't a part of a check set.
    285      */
    286     public int getCheckSetId() {
    287         return mCheckSetId;
    288     }
    289 
    290     public boolean hasMultilineDescription() {
    291         return mMultilineDescription;
    292     }
    293 
    294     public boolean isEnabled() {
    295         return mEnabled;
    296     }
    297 
    298     public void setChecked(boolean checked) {
    299         mChecked = checked;
    300     }
    301 
    302     public void setEnabled(boolean enabled) {
    303         mEnabled = enabled;
    304     }
    305 
    306     /**
    307      * @return true if the action will request further user input when selected
    308      *         (such as showing another dialog or launching a new activity).
    309      *         False, otherwise.
    310      */
    311     public boolean hasNext() {
    312         return mHasNext;
    313     }
    314 
    315     /**
    316      * @return true if the action will only display information and is thus unactionable.
    317      * If both this and {@link #hasNext()} are true, infoOnly takes precedence. (default is false)
    318      * e.g. Play balance, or cost of an app.
    319      */
    320     public boolean infoOnly() {
    321         return mInfoOnly;
    322     }
    323 
    324     /**
    325      * Returns an indicator to be drawn. If null is returned, no space for the
    326      * indicator will be made.
    327      *
    328      * @param context the context of the Activity this Action belongs to
    329      * @return an indicator to draw or null if no indicator space should exist.
    330      */
    331     public Drawable getIndicator(Context context) {
    332         if (mDrawableResource == NO_DRAWABLE) {
    333             return null;
    334         }
    335         if (mResourcePackageName == null) {
    336             return context.getResources().getDrawable(mDrawableResource);
    337         }
    338         // If we get to here, need to load the resources.
    339         Drawable icon = null;
    340         try {
    341             Context packageContext = context.createPackageContext(mResourcePackageName, 0);
    342             icon = packageContext.getResources().getDrawable(mDrawableResource);
    343         } catch (PackageManager.NameNotFoundException e) {
    344             if (Log.isLoggable(TAG, Log.WARN)) {
    345                 Log.w(TAG, "No icon for this action.");
    346             }
    347         } catch (Resources.NotFoundException e) {
    348             if (Log.isLoggable(TAG, Log.WARN)) {
    349                 Log.w(TAG, "No icon for this action.");
    350             }
    351         }
    352         return icon;
    353     }
    354 
    355     public static Parcelable.Creator<Action> CREATOR = new Parcelable.Creator<Action>() {
    356 
    357         @Override
    358         public Action createFromParcel(Parcel source) {
    359 
    360             return new Action.Builder()
    361                     .key(source.readString())
    362                     .title(source.readString())
    363                     .description(source.readString())
    364                     .intent((Intent) source.readParcelable(Intent.class.getClassLoader()))
    365                     .resourcePackageName(source.readString())
    366                     .drawableResource(source.readInt())
    367                     .iconUri((Uri) source.readParcelable(Uri.class.getClassLoader()))
    368                     .checked(source.readInt() != 0)
    369                     .multilineDescription(source.readInt() != 0)
    370                     .checkSetId(source.readInt())
    371                     .build();
    372         }
    373 
    374         @Override
    375         public Action[] newArray(int size) {
    376             return new Action[size];
    377         }
    378     };
    379 
    380     @Override
    381     public int describeContents() {
    382         return 0;
    383     }
    384 
    385     @Override
    386     public void writeToParcel(Parcel dest, int flags) {
    387         dest.writeString(mKey);
    388         dest.writeString(mTitle);
    389         dest.writeString(mDescription);
    390         dest.writeParcelable(mIntent, flags);
    391         dest.writeString(mResourcePackageName);
    392         dest.writeInt(mDrawableResource);
    393         dest.writeParcelable(mIconUri, flags);
    394         dest.writeInt(mChecked ? 1 : 0);
    395         dest.writeInt(mMultilineDescription ? 1 : 0);
    396         dest.writeInt(mCheckSetId);
    397     }
    398 }
    399