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