Home | History | Annotate | Download | only in base
      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.base;
     18 
     19 import android.app.Activity;
     20 import android.app.Fragment;
     21 import android.content.Context;
     22 import android.os.Bundle;
     23 import android.os.Handler;
     24 import android.text.InputType;
     25 import android.text.method.PasswordTransformationMethod;
     26 import android.view.KeyEvent;
     27 import android.view.LayoutInflater;
     28 import android.view.View;
     29 import android.view.ViewGroup;
     30 import android.view.inputmethod.InputMethodManager;
     31 import android.widget.EditText;
     32 import android.widget.TextView;
     33 
     34 import com.android.tv.settings.R;
     35 import com.android.tv.settings.util.AccessibilityHelper;
     36 
     37 /**
     38  * Displays a UI for text input in the "wizard" style.
     39  * TODO: Merge with EditTextFragment
     40  */
     41 public class TextInputWizardFragment extends Fragment {
     42 
     43     public interface Listener {
     44         /**
     45          * Called when text input is complete.
     46          *
     47          * @param text the text that was input.
     48          * @return true if the text is acceptable; false if not.
     49          */
     50         boolean onTextInputComplete(String text);
     51     }
     52 
     53     private static final String EXTRA_TITLE = "title";
     54     private static final String EXTRA_IS_PASSWORD = "is_password";
     55     private static final String EXTRA_PREFILL = "prefill";
     56     private static final String EXTRA_IS_NUMERIC = "is_numeric";
     57 
     58     public static TextInputWizardFragment newInstance(String title, boolean isPassword,
     59             String prefill) {
     60         return newInstance(title, isPassword, prefill, false);
     61     }
     62 
     63     public static TextInputWizardFragment newInstance(String title, boolean isPassword,
     64             String prefill, boolean isNumeric) {
     65         TextInputWizardFragment fragment = new TextInputWizardFragment();
     66         Bundle args = new Bundle();
     67         args.putString(EXTRA_TITLE, title);
     68         args.putBoolean(EXTRA_IS_PASSWORD, isPassword);
     69         args.putString(EXTRA_PREFILL, prefill);
     70         args.putBoolean(EXTRA_IS_NUMERIC, isNumeric);
     71         fragment.setArguments(args);
     72         return fragment;
     73     }
     74 
     75     private Handler mHandler;
     76     private EditText mTextInput;
     77 
     78     @Override
     79     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle) {
     80         mHandler = new Handler();
     81         View view = inflater.inflate(R.layout.setup_content_area, container, false);
     82         View content = inflater.inflate(R.layout.wifi_content, null);
     83         View action = inflater.inflate(R.layout.wifi_text_input, null);
     84         ((ViewGroup) view.findViewById(R.id.description)).addView(content);
     85         ((ViewGroup) view.findViewById(R.id.action)).addView(action);
     86 
     87         TextView titleText = (TextView) content.findViewById(R.id.title_text);
     88         mTextInput = (EditText) action.findViewById(R.id.text_input);
     89 
     90         Bundle args = getArguments();
     91         String title = args.getString(EXTRA_TITLE);
     92         boolean isPassword = args.getBoolean(EXTRA_IS_PASSWORD);
     93         String prefill = args.getString(EXTRA_PREFILL);
     94         boolean isNumeric = args.getBoolean(EXTRA_IS_NUMERIC);
     95 
     96         if (title != null) {
     97             titleText.setText(title);
     98             titleText.setVisibility(View.VISIBLE);
     99             if (AccessibilityHelper.forceFocusableViews(getActivity())) {
    100                 titleText.setFocusable(true);
    101                 titleText.setFocusableInTouchMode(true);
    102             }
    103         } else {
    104             titleText.setVisibility(View.GONE);
    105         }
    106 
    107         if (isNumeric) {
    108             mTextInput.setInputType(InputType.TYPE_CLASS_NUMBER);
    109         }
    110 
    111         if (isPassword) {
    112             mTextInput.setTransformationMethod(new PasswordTransformationMethod());
    113         }
    114 
    115         if (prefill != null) {
    116             mTextInput.setText(prefill);
    117             mTextInput.setSelection(mTextInput.getText().length(), mTextInput.getText().length());
    118         }
    119 
    120         mTextInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    121             @Override
    122             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    123                 if (event == null || event.getAction() == KeyEvent.ACTION_UP) {
    124                     Activity a = getActivity();
    125                     if (a instanceof Listener) {
    126                         return ((Listener) a).onTextInputComplete(v.getText().toString());
    127                     }
    128                     return false;
    129                 }
    130                 return true;  // If we don't return true on ACTION_DOWN, we don't get the ACTION_UP.
    131             }
    132         });
    133 
    134         return view;
    135     }
    136 
    137     @Override
    138     public void onResume() {
    139         super.onResume();
    140         mHandler.post(new Runnable() {
    141             @Override
    142             public void run() {
    143                 InputMethodManager inputMethodManager = (InputMethodManager) getActivity()
    144                     .getSystemService(Context.INPUT_METHOD_SERVICE);
    145                 inputMethodManager.showSoftInput(mTextInput, 0);
    146                 mTextInput.requestFocus();
    147             }
    148         });
    149     }
    150 }
    151