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