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