1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/toolbar/ToolbarEditLayout.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.toolbar; 1.10 + 1.11 +import org.mozilla.gecko.R; 1.12 +import org.mozilla.gecko.animation.PropertyAnimator; 1.13 +import org.mozilla.gecko.animation.PropertyAnimator.PropertyAnimationListener; 1.14 +import org.mozilla.gecko.toolbar.BrowserToolbar.OnCommitListener; 1.15 +import org.mozilla.gecko.toolbar.BrowserToolbar.OnDismissListener; 1.16 +import org.mozilla.gecko.toolbar.BrowserToolbar.OnFilterListener; 1.17 +import org.mozilla.gecko.widget.ThemedLinearLayout; 1.18 + 1.19 +import android.content.Context; 1.20 +import android.util.AttributeSet; 1.21 +import android.view.KeyEvent; 1.22 +import android.view.LayoutInflater; 1.23 +import android.view.View; 1.24 +import android.view.inputmethod.InputMethodManager; 1.25 + 1.26 +/** 1.27 +* {@code ToolbarEditLayout} is the UI for when the toolbar is in 1.28 +* edit state. It controls a text entry ({@code ToolbarEditText}) 1.29 +* and its matching 'go' button which changes depending on the 1.30 +* current type of text in the entry. 1.31 +*/ 1.32 +public class ToolbarEditLayout extends ThemedLinearLayout { 1.33 + 1.34 + private final ToolbarEditText mEditText; 1.35 + 1.36 + private OnFocusChangeListener mFocusChangeListener; 1.37 + 1.38 + public ToolbarEditLayout(Context context, AttributeSet attrs) { 1.39 + super(context, attrs); 1.40 + 1.41 + setOrientation(HORIZONTAL); 1.42 + 1.43 + LayoutInflater.from(context).inflate(R.layout.toolbar_edit_layout, this); 1.44 + mEditText = (ToolbarEditText) findViewById(R.id.url_edit_text); 1.45 + } 1.46 + 1.47 + @Override 1.48 + public void onAttachedToWindow() { 1.49 + mEditText.setOnFocusChangeListener(new OnFocusChangeListener() { 1.50 + @Override 1.51 + public void onFocusChange(View v, boolean hasFocus) { 1.52 + if (mFocusChangeListener != null) { 1.53 + mFocusChangeListener.onFocusChange(ToolbarEditLayout.this, hasFocus); 1.54 + } 1.55 + } 1.56 + }); 1.57 + } 1.58 + 1.59 + @Override 1.60 + public void setOnFocusChangeListener(OnFocusChangeListener listener) { 1.61 + mFocusChangeListener = listener; 1.62 + } 1.63 + 1.64 + @Override 1.65 + public void setEnabled(boolean enabled) { 1.66 + super.setEnabled(enabled); 1.67 + mEditText.setEnabled(enabled); 1.68 + } 1.69 + 1.70 + @Override 1.71 + public void setPrivateMode(boolean isPrivate) { 1.72 + super.setPrivateMode(isPrivate); 1.73 + mEditText.setPrivateMode(isPrivate); 1.74 + } 1.75 + 1.76 + private void showSoftInput() { 1.77 + InputMethodManager imm = 1.78 + (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 1.79 + imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT); 1.80 + } 1.81 + 1.82 + void prepareShowAnimation(final PropertyAnimator animator) { 1.83 + if (animator == null) { 1.84 + mEditText.requestFocus(); 1.85 + showSoftInput(); 1.86 + return; 1.87 + } 1.88 + 1.89 + animator.addPropertyAnimationListener(new PropertyAnimationListener() { 1.90 + @Override 1.91 + public void onPropertyAnimationStart() { 1.92 + mEditText.requestFocus(); 1.93 + } 1.94 + 1.95 + @Override 1.96 + public void onPropertyAnimationEnd() { 1.97 + showSoftInput(); 1.98 + } 1.99 + }); 1.100 + } 1.101 + 1.102 + void setOnCommitListener(OnCommitListener listener) { 1.103 + mEditText.setOnCommitListener(listener); 1.104 + } 1.105 + 1.106 + void setOnDismissListener(OnDismissListener listener) { 1.107 + mEditText.setOnDismissListener(listener); 1.108 + } 1.109 + 1.110 + void setOnFilterListener(OnFilterListener listener) { 1.111 + mEditText.setOnFilterListener(listener); 1.112 + } 1.113 + 1.114 + void onEditSuggestion(String suggestion) { 1.115 + mEditText.setText(suggestion); 1.116 + mEditText.setSelection(mEditText.getText().length()); 1.117 + mEditText.requestFocus(); 1.118 + 1.119 + showSoftInput(); 1.120 + } 1.121 + 1.122 + void setText(String text) { 1.123 + mEditText.setText(text); 1.124 + } 1.125 + 1.126 + String getText() { 1.127 + return mEditText.getText().toString(); 1.128 + } 1.129 +}