Home | History | Annotate | Download | only in dashboard
      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.settings.dashboard;
     18 
     19 import android.content.Intent;
     20 import android.content.res.Resources;
     21 import android.os.Bundle;
     22 import android.os.Parcel;
     23 import android.os.Parcelable;
     24 import android.text.TextUtils;
     25 
     26 /**
     27  * Description of a single dashboard tile that the user can select.
     28  */
     29 public class DashboardTile implements Parcelable {
     30     /**
     31      * Default value for {@link com.android.settings.dashboard.DashboardTile#id DashboardTile.id}
     32      * indicating that no identifier value is set.  All other values (including those below -1)
     33      * are valid.
     34      */
     35     public static final long TILE_ID_UNDEFINED = -1;
     36 
     37     /**
     38      * Identifier for this tile, to correlate with a new list when
     39      * it is updated.  The default value is
     40      * {@link com.android.settings.dashboard.DashboardTile#TILE_ID_UNDEFINED}, meaning no id.
     41      * @attr ref android.R.styleable#PreferenceHeader_id
     42      */
     43     public long id = TILE_ID_UNDEFINED;
     44 
     45     /**
     46      * Resource ID of title of the tile that is shown to the user.
     47      * @attr ref android.R.styleable#PreferenceHeader_title
     48      */
     49     public int titleRes;
     50 
     51     /**
     52      * Title of the tile that is shown to the user.
     53      * @attr ref android.R.styleable#PreferenceHeader_title
     54      */
     55     public CharSequence title;
     56 
     57     /**
     58      * Resource ID of optional summary describing what this tile controls.
     59      * @attr ref android.R.styleable#PreferenceHeader_summary
     60      */
     61     public int summaryRes;
     62 
     63     /**
     64      * Optional summary describing what this tile controls.
     65      * @attr ref android.R.styleable#PreferenceHeader_summary
     66      */
     67     public CharSequence summary;
     68 
     69     /**
     70      * Optional icon resource to show for this tile.
     71      * @attr ref android.R.styleable#PreferenceHeader_icon
     72      */
     73     public int iconRes;
     74 
     75     /**
     76      * Full class name of the fragment to display when this tile is
     77      * selected.
     78      * @attr ref android.R.styleable#PreferenceHeader_fragment
     79      */
     80     public String fragment;
     81 
     82     /**
     83      * Optional arguments to supply to the fragment when it is
     84      * instantiated.
     85      */
     86     public Bundle fragmentArguments;
     87 
     88     /**
     89      * Intent to launch when the preference is selected.
     90      */
     91     public Intent intent;
     92 
     93     /**
     94      * Optional additional data for use by subclasses of the activity
     95      */
     96     public Bundle extras;
     97 
     98     public DashboardTile() {
     99         // Empty
    100     }
    101 
    102     /**
    103      * Return the currently set title.  If {@link #titleRes} is set,
    104      * this resource is loaded from <var>res</var> and returned.  Otherwise
    105      * {@link #title} is returned.
    106      */
    107     public CharSequence getTitle(Resources res) {
    108         if (titleRes != 0) {
    109             return res.getText(titleRes);
    110         }
    111         return title;
    112     }
    113 
    114     /**
    115      * Return the currently set summary.  If {@link #summaryRes} is set,
    116      * this resource is loaded from <var>res</var> and returned.  Otherwise
    117      * {@link #summary} is returned.
    118      */
    119     public CharSequence getSummary(Resources res) {
    120         if (summaryRes != 0) {
    121             return res.getText(summaryRes);
    122         }
    123         return summary;
    124     }
    125 
    126     @Override
    127     public int describeContents() {
    128         return 0;
    129     }
    130 
    131     @Override
    132     public void writeToParcel(Parcel dest, int flags) {
    133         dest.writeLong(id);
    134         dest.writeInt(titleRes);
    135         TextUtils.writeToParcel(title, dest, flags);
    136         dest.writeInt(summaryRes);
    137         TextUtils.writeToParcel(summary, dest, flags);
    138         dest.writeInt(iconRes);
    139         dest.writeString(fragment);
    140         dest.writeBundle(fragmentArguments);
    141         if (intent != null) {
    142             dest.writeInt(1);
    143             intent.writeToParcel(dest, flags);
    144         } else {
    145             dest.writeInt(0);
    146         }
    147         dest.writeBundle(extras);
    148     }
    149 
    150     public void readFromParcel(Parcel in) {
    151         id = in.readLong();
    152         titleRes = in.readInt();
    153         title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
    154         summaryRes = in.readInt();
    155         summary = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
    156         iconRes = in.readInt();
    157         fragment = in.readString();
    158         fragmentArguments = in.readBundle();
    159         if (in.readInt() != 0) {
    160             intent = Intent.CREATOR.createFromParcel(in);
    161         }
    162         extras = in.readBundle();
    163     }
    164 
    165     DashboardTile(Parcel in) {
    166         readFromParcel(in);
    167     }
    168 
    169     public static final Creator<DashboardTile> CREATOR = new Creator<DashboardTile>() {
    170         public DashboardTile createFromParcel(Parcel source) {
    171             return new DashboardTile(source);
    172         }
    173         public DashboardTile[] newArray(int size) {
    174             return new DashboardTile[size];
    175         }
    176     };
    177 }
    178