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 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.TextUtils; 26 import android.text.method.PasswordTransformationMethod; 27 import android.view.KeyEvent; 28 import android.view.LayoutInflater; 29 import android.view.View; 30 import android.view.ViewGroup; 31 import android.view.inputmethod.InputMethodManager; 32 import android.widget.EditText; 33 import android.widget.ImageView; 34 import android.widget.TextView; 35 36 import com.android.tv.settings.R; 37 import com.android.tv.settings.util.AccessibilityHelper; 38 39 /** 40 * Displays a UI for text input in the "wizard" style. 41 * TODO: Merge with EditTextFragment 42 */ 43 public class TextInputWizardFragment extends Fragment { 44 45 public static final int INPUT_TYPE_NORMAL = InputType.TYPE_CLASS_TEXT 46 | InputType.TYPE_TEXT_VARIATION_NORMAL; 47 public static final int INPUT_TYPE_PASSWORD = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS 48 | InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD; 49 public static final int INPUT_TYPE_EMAIL = InputType.TYPE_CLASS_TEXT 50 | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS; 51 public static final int INPUT_TYPE_NO_SUGGESTIONS = InputType.TYPE_CLASS_TEXT 52 | InputType.TYPE_TEXT_VARIATION_NORMAL | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS; 53 public static final int INPUT_TYPE_NUMERIC = InputType.TYPE_CLASS_NUMBER 54 | InputType.TYPE_NUMBER_VARIATION_NORMAL; 55 56 public interface Listener { 57 /** 58 * Called when text input is complete. 59 * 60 * @param text the text that was input. 61 * @return true if the text is acceptable; false if not. 62 */ 63 boolean onTextInputComplete(String text); 64 } 65 66 private static final String EXTRA_TITLE = "title"; 67 private static final String EXTRA_DESCRIPTION = "description"; 68 private static final String EXTRA_INPUT_TYPE = "input_type"; 69 private static final String EXTRA_PREFILL = "prefill"; 70 71 public static TextInputWizardFragment newInstance( 72 String title, String description, int inputType, String prefill) { 73 TextInputWizardFragment fragment = new TextInputWizardFragment(); 74 Bundle args = new Bundle(); 75 args.putString(EXTRA_TITLE, title); 76 args.putString(EXTRA_DESCRIPTION, description); 77 args.putInt(EXTRA_INPUT_TYPE, inputType); 78 args.putString(EXTRA_PREFILL, prefill); 79 fragment.setArguments(args); 80 return fragment; 81 } 82 83 private Handler mHandler; 84 private EditText mTextInput; 85 86 @Override 87 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle) { 88 mHandler = new Handler(); 89 View view = inflater.inflate(R.layout.account_content_area, container, false); 90 View content = inflater.inflate(R.layout.wifi_content, null); 91 View action = inflater.inflate(R.layout.wifi_text_input, null); 92 ((ViewGroup) view.findViewById(R.id.description)).addView(content); 93 ((ViewGroup) view.findViewById(R.id.action)).addView(action); 94 95 TextView titleText = (TextView) content.findViewById(R.id.title_text); 96 TextView descriptionText = (TextView) content.findViewById(R.id.description_text); 97 mTextInput = (EditText) action.findViewById(R.id.text_input); 98 99 Bundle args = getArguments(); 100 String title = args.getString(EXTRA_TITLE); 101 String description = args.getString(EXTRA_DESCRIPTION); 102 int inputType = args.getInt(EXTRA_INPUT_TYPE); 103 String prefill = args.getString(EXTRA_PREFILL); 104 105 boolean forceFocusable = AccessibilityHelper.forceFocusableViews(getActivity()); 106 if (title != null) { 107 titleText.setText(title); 108 titleText.setVisibility(View.VISIBLE); 109 if (forceFocusable) { 110 titleText.setFocusable(true); 111 titleText.setFocusableInTouchMode(true); 112 } 113 } else { 114 titleText.setVisibility(View.GONE); 115 } 116 117 if (description != null) { 118 descriptionText.setText(description); 119 descriptionText.setVisibility(View.VISIBLE); 120 if (forceFocusable) { 121 descriptionText.setFocusable(true); 122 descriptionText.setFocusableInTouchMode(true); 123 } 124 } else { 125 descriptionText.setVisibility(View.GONE); 126 } 127 128 if ((inputType & InputType.TYPE_TEXT_VARIATION_PASSWORD) != 0) { 129 mTextInput.setTransformationMethod(new PasswordTransformationMethod()); 130 } 131 mTextInput.setInputType(inputType); 132 133 if (prefill != null) { 134 mTextInput.setText(prefill); 135 mTextInput.setSelection(mTextInput.getText().length(), mTextInput.getText().length()); 136 } 137 138 mTextInput.setOnEditorActionListener(new TextView.OnEditorActionListener() { 139 @Override 140 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 141 if (event == null || event.getAction() == KeyEvent.ACTION_UP) { 142 Activity a = getActivity(); 143 if (a instanceof Listener) { 144 return ((Listener) a).onTextInputComplete(v.getText().toString()); 145 } 146 return false; 147 } 148 return true; // If we don't return true on ACTION_DOWN, we don't get the ACTION_UP. 149 } 150 }); 151 152 return view; 153 } 154 155 @Override 156 public void onResume() { 157 super.onResume(); 158 mHandler.post(new Runnable() { 159 @Override 160 public void run() { 161 Activity a = getActivity(); 162 if (a != null) { 163 InputMethodManager inputMethodManager = (InputMethodManager) a.getSystemService( 164 Context.INPUT_METHOD_SERVICE); 165 inputMethodManager.showSoftInput(mTextInput, 0); 166 mTextInput.requestFocus(); 167 } 168 } 169 }); 170 } 171 172 @Override 173 public void onPause() { 174 InputMethodManager imm = (InputMethodManager) getActivity() 175 .getSystemService(Context.INPUT_METHOD_SERVICE); 176 imm.hideSoftInputFromWindow(mTextInput.getWindowToken(), 0); 177 super.onPause(); 178 } 179 } 180