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