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.dialog.old; 18 19 import android.app.Fragment; 20 import android.os.Bundle; 21 import android.text.Editable; 22 import android.view.KeyEvent; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import android.widget.EditText; 27 import android.text.TextUtils; 28 import android.widget.TextView; 29 import android.text.TextWatcher; 30 import android.text.method.PasswordTransformationMethod; 31 32 import com.android.tv.settings.R; 33 34 public class EditTextFragment extends Fragment 35 implements TextWatcher, TextView.OnEditorActionListener { 36 37 private static final String EXTRA_LAYOUT_RES_ID = "layout_res_id"; 38 private static final String EXTRA_EDIT_TEXT_RES_ID = "edit_text_res_id"; 39 private static final String EXTRA_DESC = "description"; 40 private static final String EXTRA_INITIAL_TEXT = "initialText"; 41 private static final String EXTRA_PASSWORD = "password"; 42 private TextWatcher mTextWatcher = null; 43 private TextView.OnEditorActionListener mEditorActionListener = null; 44 newInstance(int layoutResId, int editTextResId)45 public static EditTextFragment newInstance(int layoutResId, int editTextResId) { 46 EditTextFragment fragment = new EditTextFragment(); 47 Bundle args = new Bundle(); 48 if (layoutResId == 0 || editTextResId == 0) { 49 throw new IllegalArgumentException("resource id must be valid values"); 50 } 51 args.putInt(EXTRA_LAYOUT_RES_ID, layoutResId); 52 args.putInt(EXTRA_EDIT_TEXT_RES_ID, editTextResId); 53 fragment.setArguments(args); 54 return fragment; 55 } 56 newInstance(String description)57 public static EditTextFragment newInstance(String description) { 58 return newInstance(description, null); 59 } 60 newInstance(String description, String initialText)61 public static EditTextFragment newInstance(String description, String initialText) { 62 return newInstance(description, initialText, false); 63 } 64 newInstance(String description, String initialText, boolean password)65 public static EditTextFragment newInstance(String description, String initialText, 66 boolean password) { 67 EditTextFragment fragment = new EditTextFragment(); 68 Bundle args = new Bundle(); 69 args.putString(EXTRA_DESC, description); 70 args.putString(EXTRA_INITIAL_TEXT, initialText); 71 args.putBoolean(EXTRA_PASSWORD, password); 72 fragment.setArguments(args); 73 return fragment; 74 75 } 76 setTextWatcher(TextWatcher textWatcher)77 public void setTextWatcher(TextWatcher textWatcher) { 78 mTextWatcher = textWatcher; 79 } 80 setOnEditorActionListener(TextView.OnEditorActionListener listener)81 public void setOnEditorActionListener(TextView.OnEditorActionListener listener) { 82 mEditorActionListener = listener; 83 } 84 85 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)86 public View onCreateView(LayoutInflater inflater, ViewGroup container, 87 Bundle savedInstanceState) { 88 View view = null; 89 EditText editText = null; 90 int layoutResId = getArguments().getInt(EXTRA_LAYOUT_RES_ID, R.layout.edittext_fragment); 91 int editTextResId = getArguments().getInt(EXTRA_EDIT_TEXT_RES_ID, R.id.edittext); 92 93 view = inflater.inflate(layoutResId, container, false); 94 editText = (EditText) view.findViewById(editTextResId); 95 96 String descString = getArguments().getString(EXTRA_DESC); 97 if (!TextUtils.isEmpty(descString)) { 98 TextView description = (TextView) view.findViewById(R.id.description); 99 if (description != null) { 100 description.setText(descString); 101 description.setVisibility(View.VISIBLE); 102 } 103 } 104 105 if (editText != null) { 106 editText.setOnEditorActionListener(this); 107 editText.addTextChangedListener(this); 108 editText.requestFocus(); 109 String initialText = getArguments().getString(EXTRA_INITIAL_TEXT); 110 if(!TextUtils.isEmpty(initialText)) { 111 editText.setText(initialText); 112 editText.setSelection(initialText.length()); 113 } 114 if (getArguments().getBoolean(EXTRA_PASSWORD, false)) { 115 editText.setTransformationMethod(PasswordTransformationMethod.getInstance()); 116 } 117 } 118 return view; 119 } 120 121 @Override afterTextChanged(Editable s)122 public void afterTextChanged(Editable s) { 123 if (mTextWatcher != null) { 124 mTextWatcher.afterTextChanged(s); 125 } 126 } 127 128 @Override beforeTextChanged(CharSequence s, int start, int count, int after)129 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 130 if (mTextWatcher != null) { 131 mTextWatcher.beforeTextChanged(s, start, count, after); 132 } 133 } 134 135 @Override onTextChanged(CharSequence s, int start, int before, int count)136 public void onTextChanged(CharSequence s, int start, int before, int count) { 137 if (mTextWatcher != null) { 138 mTextWatcher.onTextChanged(s, start, before, count); 139 } 140 } 141 142 @Override onEditorAction(TextView v, int actionId, KeyEvent event)143 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 144 if (mEditorActionListener != null) { 145 return mEditorActionListener.onEditorAction(v, actionId, event); 146 } else { 147 return false; 148 } 149 } 150 } 151