Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 package org.mozilla.gecko.toolbar;
8 import org.mozilla.gecko.R;
9 import org.mozilla.gecko.animation.PropertyAnimator;
10 import org.mozilla.gecko.animation.PropertyAnimator.PropertyAnimationListener;
11 import org.mozilla.gecko.toolbar.BrowserToolbar.OnCommitListener;
12 import org.mozilla.gecko.toolbar.BrowserToolbar.OnDismissListener;
13 import org.mozilla.gecko.toolbar.BrowserToolbar.OnFilterListener;
14 import org.mozilla.gecko.widget.ThemedLinearLayout;
16 import android.content.Context;
17 import android.util.AttributeSet;
18 import android.view.KeyEvent;
19 import android.view.LayoutInflater;
20 import android.view.View;
21 import android.view.inputmethod.InputMethodManager;
23 /**
24 * {@code ToolbarEditLayout} is the UI for when the toolbar is in
25 * edit state. It controls a text entry ({@code ToolbarEditText})
26 * and its matching 'go' button which changes depending on the
27 * current type of text in the entry.
28 */
29 public class ToolbarEditLayout extends ThemedLinearLayout {
31 private final ToolbarEditText mEditText;
33 private OnFocusChangeListener mFocusChangeListener;
35 public ToolbarEditLayout(Context context, AttributeSet attrs) {
36 super(context, attrs);
38 setOrientation(HORIZONTAL);
40 LayoutInflater.from(context).inflate(R.layout.toolbar_edit_layout, this);
41 mEditText = (ToolbarEditText) findViewById(R.id.url_edit_text);
42 }
44 @Override
45 public void onAttachedToWindow() {
46 mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
47 @Override
48 public void onFocusChange(View v, boolean hasFocus) {
49 if (mFocusChangeListener != null) {
50 mFocusChangeListener.onFocusChange(ToolbarEditLayout.this, hasFocus);
51 }
52 }
53 });
54 }
56 @Override
57 public void setOnFocusChangeListener(OnFocusChangeListener listener) {
58 mFocusChangeListener = listener;
59 }
61 @Override
62 public void setEnabled(boolean enabled) {
63 super.setEnabled(enabled);
64 mEditText.setEnabled(enabled);
65 }
67 @Override
68 public void setPrivateMode(boolean isPrivate) {
69 super.setPrivateMode(isPrivate);
70 mEditText.setPrivateMode(isPrivate);
71 }
73 private void showSoftInput() {
74 InputMethodManager imm =
75 (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
76 imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
77 }
79 void prepareShowAnimation(final PropertyAnimator animator) {
80 if (animator == null) {
81 mEditText.requestFocus();
82 showSoftInput();
83 return;
84 }
86 animator.addPropertyAnimationListener(new PropertyAnimationListener() {
87 @Override
88 public void onPropertyAnimationStart() {
89 mEditText.requestFocus();
90 }
92 @Override
93 public void onPropertyAnimationEnd() {
94 showSoftInput();
95 }
96 });
97 }
99 void setOnCommitListener(OnCommitListener listener) {
100 mEditText.setOnCommitListener(listener);
101 }
103 void setOnDismissListener(OnDismissListener listener) {
104 mEditText.setOnDismissListener(listener);
105 }
107 void setOnFilterListener(OnFilterListener listener) {
108 mEditText.setOnFilterListener(listener);
109 }
111 void onEditSuggestion(String suggestion) {
112 mEditText.setText(suggestion);
113 mEditText.setSelection(mEditText.getText().length());
114 mEditText.requestFocus();
116 showSoftInput();
117 }
119 void setText(String text) {
120 mEditText.setText(text);
121 }
123 String getText() {
124 return mEditText.getText().toString();
125 }
126 }