Home | History | Annotate | Download | only in setup
      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.connectivity.setup;
     18 
     19 import com.android.tv.settings.R;
     20 
     21 import android.app.Activity;
     22 import android.app.Fragment;
     23 import android.content.Context;
     24 import android.os.Bundle;
     25 import android.os.Handler;
     26 import android.text.InputType;
     27 import android.text.TextUtils;
     28 import android.text.method.PasswordTransformationMethod;
     29 import android.view.KeyEvent;
     30 import android.view.LayoutInflater;
     31 import android.view.View.OnClickListener;
     32 import android.view.View;
     33 import android.view.ViewGroup;
     34 import android.view.inputmethod.InputMethodManager;
     35 import android.widget.EditText;
     36 import android.widget.ImageView;
     37 import android.widget.TextView;
     38 import android.widget.CheckBox;
     39 
     40 /**
     41  * Displays a UI for text input in the "wizard" style.
     42  * TODO: Merge with EditTextFragment
     43  */
     44 public class PasswordInputWizardFragment extends Fragment {
     45 
     46     public interface Listener {
     47         /**
     48          * Called when text input is complete.
     49          *
     50          * @param text the text that was input.
     51          * @return true if the text is acceptable; false if not.
     52          */
     53         boolean onPasswordInputComplete(String text);
     54     }
     55 
     56     private static final String EXTRA_TITLE = "title";
     57     private static final String EXTRA_DESCRIPTION = "description";
     58     private static final String EXTRA_INPUT_TYPE = "input_type";
     59     private static final String EXTRA_PREFILL = "prefill";
     60     private static final String EXTRA_EDIT_SUFFIX = "edit_suffix";
     61 
     62     public static PasswordInputWizardFragment newInstance(
     63             String title, String description, int inputType, String prefill) {
     64         return newInstance(title, description, inputType, prefill, null);
     65     }
     66 
     67     //TODO: Add a boolean parameter that controls whether the default is show or hide
     68     private static PasswordInputWizardFragment newInstance(
     69             String title, String description, int inputType, String prefill, String editSuffix) {
     70         PasswordInputWizardFragment fragment = new PasswordInputWizardFragment();
     71         Bundle args = new Bundle();
     72         args.putString(EXTRA_TITLE, title);
     73         args.putString(EXTRA_DESCRIPTION, description);
     74         args.putInt(EXTRA_INPUT_TYPE, inputType);
     75         args.putString(EXTRA_PREFILL, prefill);
     76         args.putString(EXTRA_EDIT_SUFFIX, editSuffix);
     77         fragment.setArguments(args);
     78         return fragment;
     79     }
     80 
     81     private Handler mHandler;
     82     private EditText mTextInput;
     83     private CheckBox textObsufactionToggle;
     84 
     85     @Override
     86     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle) {
     87         mHandler = new Handler();
     88         View view = inflater.inflate(R.layout.account_content_area, container, false);
     89         View content = inflater.inflate(R.layout.wifi_content, null);
     90         View action = inflater.inflate(R.layout.password_text_input, null);
     91         ((ViewGroup) view.findViewById(R.id.description)).addView(content);
     92         ((ViewGroup) view.findViewById(R.id.action)).addView(action);
     93 
     94         TextView titleText = (TextView) content.findViewById(R.id.title_text);
     95         TextView descriptionText = (TextView) content.findViewById(R.id.description_text);
     96         mTextInput = (EditText) action.findViewById(R.id.text_input);
     97         textObsufactionToggle = (CheckBox) action.findViewById(R.id.text_obfuscation_toggle);
     98 
     99         TextView editSuffixText = (TextView) action.findViewById(R.id.edit_suffix_text);
    100 
    101         Bundle args = getArguments();
    102         String title = args.getString(EXTRA_TITLE);
    103         String description = args.getString(EXTRA_DESCRIPTION);
    104         int inputType = args.getInt(EXTRA_INPUT_TYPE);
    105         String prefill = args.getString(EXTRA_PREFILL);
    106         String editSuffix = args.getString(EXTRA_EDIT_SUFFIX);
    107 
    108         if (title != null) {
    109             titleText.setText(title);
    110             titleText.setVisibility(View.VISIBLE);
    111         } else {
    112             titleText.setVisibility(View.GONE);
    113         }
    114 
    115         if (description != null) {
    116             descriptionText.setText(description);
    117             descriptionText.setVisibility(View.VISIBLE);
    118         } else {
    119             descriptionText.setVisibility(View.GONE);
    120         }
    121 
    122         if (editSuffix != null) {
    123             ViewGroup.LayoutParams params = mTextInput.getLayoutParams();
    124             params.width = getActivity()
    125                     .getResources().getDimensionPixelSize(R.dimen.edit_text_width_small);
    126             editSuffixText.setText(editSuffix);
    127             editSuffixText.setVisibility(View.VISIBLE);
    128         } else {
    129             editSuffixText.setVisibility(View.GONE);
    130         }
    131 
    132         textObsufactionToggle.setOnClickListener(new OnClickListener() {
    133 
    134             @Override
    135             public void onClick(View v) {
    136                 boolean hidePassword = textObsufactionToggle.isChecked();
    137                 if (hidePassword) {
    138                     mTextInput.setTransformationMethod(new PasswordTransformationMethod());
    139                 } else {
    140                     mTextInput.setTransformationMethod(null);
    141                 }
    142             }
    143         });
    144 
    145         if (textObsufactionToggle.isChecked()) {
    146             mTextInput.setTransformationMethod(new PasswordTransformationMethod());
    147         }
    148         mTextInput.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS |
    149             InputType.TYPE_CLASS_TEXT);
    150 
    151         if (prefill != null) {
    152             mTextInput.setText(prefill);
    153             mTextInput.setSelection(mTextInput.getText().length(), mTextInput.getText().length());
    154         }
    155 
    156         mTextInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    157             @Override
    158             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    159                 if (event == null || event.getAction() == KeyEvent.ACTION_UP) {
    160                     Activity a = getActivity();
    161                     if (a instanceof Listener) {
    162                         return ((Listener) a).onPasswordInputComplete(v.getText().toString());
    163                     }
    164                     return false;
    165                 }
    166                 return true;  // If we don't return true on ACTION_DOWN, we don't get the ACTION_UP.
    167             }
    168         });
    169 
    170         return view;
    171     }
    172 
    173     @Override
    174     public void onResume() {
    175         super.onResume();
    176         mHandler.post(new Runnable() {
    177             @Override
    178             public void run() {
    179                 Activity a = getActivity();
    180                 if (a != null) {
    181                     InputMethodManager inputMethodManager = (InputMethodManager) a.getSystemService(
    182                             Context.INPUT_METHOD_SERVICE);
    183                     inputMethodManager.showSoftInput(mTextInput, 0);
    184                     mTextInput.requestFocus();
    185                 }
    186             }
    187         });
    188     }
    189 
    190     @Override
    191     public void onPause() {
    192         InputMethodManager imm = (InputMethodManager) getActivity()
    193                 .getSystemService(Context.INPUT_METHOD_SERVICE);
    194         imm.hideSoftInputFromWindow(mTextInput.getWindowToken(), 0);
    195         super.onPause();
    196     }
    197 }
    198