Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko; |
michael@0 | 6 | |
michael@0 | 7 | import org.mozilla.gecko.util.GeckoEventListener; |
michael@0 | 8 | import org.mozilla.gecko.util.ThreadUtils; |
michael@0 | 9 | |
michael@0 | 10 | import org.json.JSONObject; |
michael@0 | 11 | |
michael@0 | 12 | import android.content.Context; |
michael@0 | 13 | import android.text.Editable; |
michael@0 | 14 | import android.text.TextUtils; |
michael@0 | 15 | import android.text.TextWatcher; |
michael@0 | 16 | import android.util.AttributeSet; |
michael@0 | 17 | import android.view.KeyEvent; |
michael@0 | 18 | import android.view.LayoutInflater; |
michael@0 | 19 | import android.view.View; |
michael@0 | 20 | import android.view.inputmethod.InputMethodManager; |
michael@0 | 21 | import android.widget.LinearLayout; |
michael@0 | 22 | |
michael@0 | 23 | public class FindInPageBar extends LinearLayout implements TextWatcher, View.OnClickListener, GeckoEventListener { |
michael@0 | 24 | private static final String REQUEST_ID = "FindInPageBar"; |
michael@0 | 25 | |
michael@0 | 26 | private final Context mContext; |
michael@0 | 27 | private CustomEditText mFindText; |
michael@0 | 28 | private boolean mInflated = false; |
michael@0 | 29 | |
michael@0 | 30 | public FindInPageBar(Context context, AttributeSet attrs) { |
michael@0 | 31 | super(context, attrs); |
michael@0 | 32 | mContext = context; |
michael@0 | 33 | setFocusable(true); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | public void inflateContent() { |
michael@0 | 37 | LayoutInflater inflater = LayoutInflater.from(mContext); |
michael@0 | 38 | View content = inflater.inflate(R.layout.find_in_page_content, this); |
michael@0 | 39 | |
michael@0 | 40 | content.findViewById(R.id.find_prev).setOnClickListener(this); |
michael@0 | 41 | content.findViewById(R.id.find_next).setOnClickListener(this); |
michael@0 | 42 | content.findViewById(R.id.find_close).setOnClickListener(this); |
michael@0 | 43 | |
michael@0 | 44 | // Capture clicks on the rest of the view to prevent them from |
michael@0 | 45 | // leaking into other views positioned below. |
michael@0 | 46 | content.setOnClickListener(this); |
michael@0 | 47 | |
michael@0 | 48 | mFindText = (CustomEditText) content.findViewById(R.id.find_text); |
michael@0 | 49 | mFindText.addTextChangedListener(this); |
michael@0 | 50 | mFindText.setOnKeyPreImeListener(new CustomEditText.OnKeyPreImeListener() { |
michael@0 | 51 | @Override |
michael@0 | 52 | public boolean onKeyPreIme(View v, int keyCode, KeyEvent event) { |
michael@0 | 53 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
michael@0 | 54 | hide(); |
michael@0 | 55 | return true; |
michael@0 | 56 | } |
michael@0 | 57 | return false; |
michael@0 | 58 | } |
michael@0 | 59 | }); |
michael@0 | 60 | |
michael@0 | 61 | mInflated = true; |
michael@0 | 62 | GeckoAppShell.getEventDispatcher().registerEventListener("TextSelection:Data", this); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | public void show() { |
michael@0 | 66 | if (!mInflated) |
michael@0 | 67 | inflateContent(); |
michael@0 | 68 | |
michael@0 | 69 | setVisibility(VISIBLE); |
michael@0 | 70 | mFindText.requestFocus(); |
michael@0 | 71 | |
michael@0 | 72 | // handleMessage() receives response message and determines initial state of softInput |
michael@0 | 73 | GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("TextSelection:Get", REQUEST_ID)); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | public void hide() { |
michael@0 | 77 | setVisibility(GONE); |
michael@0 | 78 | getInputMethodManager(mFindText).hideSoftInputFromWindow(mFindText.getWindowToken(), 0); |
michael@0 | 79 | GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FindInPage:Closed", null)); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | private InputMethodManager getInputMethodManager(View view) { |
michael@0 | 83 | Context context = view.getContext(); |
michael@0 | 84 | return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | public void onDestroy() { |
michael@0 | 88 | if (!mInflated) { |
michael@0 | 89 | return; |
michael@0 | 90 | } |
michael@0 | 91 | GeckoAppShell.getEventDispatcher().unregisterEventListener("TextSelection:Data", this); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | // TextWatcher implementation |
michael@0 | 95 | |
michael@0 | 96 | @Override |
michael@0 | 97 | public void afterTextChanged(Editable s) { |
michael@0 | 98 | GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FindInPage:Find", s.toString())); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | @Override |
michael@0 | 102 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
michael@0 | 103 | // ignore |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | @Override |
michael@0 | 107 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
michael@0 | 108 | // ignore |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | // View.OnClickListener implementation |
michael@0 | 112 | |
michael@0 | 113 | @Override |
michael@0 | 114 | public void onClick(View v) { |
michael@0 | 115 | final int viewId = v.getId(); |
michael@0 | 116 | |
michael@0 | 117 | if (viewId == R.id.find_prev) { |
michael@0 | 118 | GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FindInPage:Prev", mFindText.getText().toString())); |
michael@0 | 119 | getInputMethodManager(mFindText).hideSoftInputFromWindow(mFindText.getWindowToken(), 0); |
michael@0 | 120 | return; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | if (viewId == R.id.find_next) { |
michael@0 | 124 | GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("FindInPage:Next", mFindText.getText().toString())); |
michael@0 | 125 | getInputMethodManager(mFindText).hideSoftInputFromWindow(mFindText.getWindowToken(), 0); |
michael@0 | 126 | return; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | if (viewId == R.id.find_close) { |
michael@0 | 130 | hide(); |
michael@0 | 131 | } |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | // GeckoEventListener implementation |
michael@0 | 135 | |
michael@0 | 136 | @Override |
michael@0 | 137 | public void handleMessage(String event, JSONObject message) { |
michael@0 | 138 | if (!event.equals("TextSelection:Data") || !REQUEST_ID.equals(message.optString("requestId"))) { |
michael@0 | 139 | return; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | final String text = message.optString("text"); |
michael@0 | 143 | |
michael@0 | 144 | // Populate an initial find string, virtual keyboard not required. |
michael@0 | 145 | if (!TextUtils.isEmpty(text)) { |
michael@0 | 146 | // Populate initial selection |
michael@0 | 147 | ThreadUtils.postToUiThread(new Runnable() { |
michael@0 | 148 | @Override |
michael@0 | 149 | public void run() { |
michael@0 | 150 | mFindText.setText(text); |
michael@0 | 151 | } |
michael@0 | 152 | }); |
michael@0 | 153 | return; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | // Show the virtual keyboard. |
michael@0 | 157 | if (mFindText.hasWindowFocus()) { |
michael@0 | 158 | getInputMethodManager(mFindText).showSoftInput(mFindText, 0); |
michael@0 | 159 | } else { |
michael@0 | 160 | // showSoftInput won't work until after the window is focused. |
michael@0 | 161 | mFindText.setOnWindowFocusChangeListener(new CustomEditText.OnWindowFocusChangeListener() { |
michael@0 | 162 | @Override |
michael@0 | 163 | public void onWindowFocusChanged(boolean hasFocus) { |
michael@0 | 164 | if (!hasFocus) |
michael@0 | 165 | return; |
michael@0 | 166 | |
michael@0 | 167 | mFindText.setOnWindowFocusChangeListener(null); |
michael@0 | 168 | getInputMethodManager(mFindText).showSoftInput(mFindText, 0); |
michael@0 | 169 | } |
michael@0 | 170 | }); |
michael@0 | 171 | } |
michael@0 | 172 | } |
michael@0 | 173 | } |