• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.form;
18 
19 import android.content.Intent;
20 import android.os.Bundle;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 /**
27  * Implements a FormPage. This is a form page that can be used by MultiPagedForm
28  * to create fragments
29  */
30 public class FormPage implements Parcelable {
31 
32     public static String DATA_KEY_SUMMARY_STRING =
33             "com.android.tv.settings.form.FormPage.summaryString";
34 
35     enum Type {
36         MULTIPLE_CHOICE, TEXT_INPUT, PASSWORD_INPUT, INTENT
37     }
38 
39     public final static Parcelable.Creator<FormPage> CREATOR = new Parcelable.Creator<FormPage>() {
40 
41         @Override
42         public FormPage createFromParcel(Parcel parcel) {
43             return new FormPage(parcel);
44         }
45 
46         @Override
47         public FormPage[] newArray(int size) {
48             return new FormPage[size];
49         }
50     };
51 
52     /**
53      * Create a form page resulting in a password input field.
54      *
55      * @param title the title of this form page. Should be unique for this
56      *            Activity.
57      * @return an incomplete form page suitable for use in MultiPagedForm.
58      */
createPasswordInputForm(String title)59     public static FormPage createPasswordInputForm(String title) {
60         return new FormPage(title, Type.PASSWORD_INPUT, null, null);
61     }
62 
63     /**
64      * Create a form page resulting in a text input field.
65      *
66      * @param title the title of this form page. Should be unique for this
67      *            Activity.
68      * @return an incomplete form page suitable for use in MultiPagedForm.
69      */
createTextInputForm(String title)70     public static FormPage createTextInputForm(String title) {
71         return new FormPage(title, Type.TEXT_INPUT, null, null);
72     }
73 
74     /**
75      * Create a form page resulting in a list of choices.
76      *
77      * @param title the title of this form page. Should be unique for this
78      *            Activity.
79      * @param formChoices the choices for this form page.
80      * @return an incomplete form page suitable for use in MultiPagedForm.
81      */
createMultipleChoiceForm(String title, ArrayList<String> formChoices)82     public static FormPage createMultipleChoiceForm(String title, ArrayList<String> formChoices) {
83         return new FormPage(title, Type.MULTIPLE_CHOICE, formChoices, null);
84     }
85 
86     /**
87      * Create a form page which launches an intent to get its results.
88      *
89      * @param title the title of this form page. Should be unique for this
90      *            Activity.
91      * @param formIntent the intent to launch for this form page. This intent
92      *            should return the form page's results via the result intent's
93      *            extras bundle. This bundle must contain the
94      *            DATA_KEY_SUMMARY_STRING key as this key is used to display the
95      *            results of the intent to the user.
96      * @return an incomplete form page suitable for use in MultiPagedForm.
97      */
createIntentForm(String title, Intent formIntent)98     public static FormPage createIntentForm(String title, Intent formIntent) {
99         return new FormPage(title, Type.INTENT, null, formIntent);
100     }
101 
102     private final String mPageTitle;
103     private final Type mFormType;
104     private final ArrayList<String> mFormChoices;
105     private final Intent mFormIntent;
106     private Bundle mFormData;
107     private String mError;
108     private String mErrorIconUri;
109     private boolean mCompleted;
110 
FormPage(String pageTitle, Type formType, ArrayList<String> formChoices, Intent formIntent)111     public FormPage(String pageTitle, Type formType, ArrayList<String> formChoices,
112             Intent formIntent) {
113         mPageTitle = pageTitle;
114         mFormType = formType;
115         mFormChoices = formChoices;
116         mFormIntent = formIntent;
117         mFormData = null;
118         mError = null;
119         mErrorIconUri = null;
120         mCompleted = false;
121     }
122 
FormPage(Parcel in)123     public FormPage(Parcel in) {
124         mPageTitle = in.readString();
125         mFormType = Type.valueOf(in.readString());
126         mFormChoices = new ArrayList<String>();
127         in.readStringList(mFormChoices);
128         mFormIntent = in.readParcelable(null);
129         mFormData = in.readParcelable(null);
130         mError = in.readString();
131         mErrorIconUri = in.readString();
132         mCompleted = in.readByte() == 1;
133     }
134 
135     @Override
describeContents()136     public int describeContents() {
137         return 0;
138     }
139 
140     @Override
writeToParcel(Parcel out, int flags)141     public void writeToParcel(Parcel out, int flags) {
142         out.writeString(mPageTitle);
143         out.writeString(mFormType.name());
144         out.writeStringList(mFormChoices);
145         out.writeParcelable(mFormIntent, 0);
146         out.writeParcelable(mFormData, 0);
147         out.writeString(mError);
148         out.writeString(mErrorIconUri);
149         out.writeByte((byte) (mCompleted ? 1 : 0));
150     }
151 
152     @Override
toString()153     public String toString() {
154         StringBuilder sb = new StringBuilder();
155         sb.append("PageTitle: " + mPageTitle + "\n");
156         sb.append("FormType: " + mFormType.name() + "\n");
157         sb.append("FormChoices: " + mFormChoices + "\n");
158         sb.append("FormIntent: " + mFormIntent + "\n");
159         sb.append("FormData: " + mFormData + "\n");
160         sb.append("FormError: " + mError + "\n");
161         sb.append("FormErrorIconUri: " + mErrorIconUri + "\n");
162         sb.append("FormCompleted: " + mCompleted + "\n");
163         return sb.toString();
164     }
165 
getTitle()166     public String getTitle() {
167         return mPageTitle;
168     }
169 
getData()170     public Bundle getData() {
171         return mFormData;
172     }
173 
getDataSummary()174     public String getDataSummary() {
175         return (mFormData != null && mFormData.containsKey(DATA_KEY_SUMMARY_STRING)) ? mFormData
176                 .getString(DATA_KEY_SUMMARY_STRING) : "";
177     }
178 
clearData()179     public void clearData() {
180         mFormData = null;
181     }
182 
getError()183     public String getError() {
184         return mError;
185     }
186 
getErrorIconUri()187     public String getErrorIconUri() {
188         return mErrorIconUri;
189     }
190 
complete(Bundle data)191     public void complete(Bundle data) {
192         mFormData = data;
193         mCompleted = true;
194     }
195 
setError(String error, String errorIconUri)196     public void setError(String error, String errorIconUri) {
197         mError = error;
198         mErrorIconUri = errorIconUri;
199         mCompleted = false;
200     }
201 
getChoices()202     public List<String> getChoices() {
203         return mFormChoices;
204     }
205 
getType()206     Type getType() {
207         return mFormType;
208     }
209 
getIntent()210     Intent getIntent() {
211         return mFormIntent;
212     }
213 
isComplete()214     boolean isComplete() {
215         return mCompleted;
216     }
217 }
218