2011-01-04 01:34:44 +03:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2010-06-30 08:13:44 +04:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Android code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Michael Wu <mwu@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
package org.mozilla.gecko;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import java.util.concurrent.atomic.*;
|
|
|
|
|
|
|
|
import android.os.*;
|
|
|
|
import android.app.*;
|
|
|
|
import android.text.*;
|
2010-08-04 23:47:26 +04:00
|
|
|
import android.text.style.*;
|
2010-06-30 08:13:44 +04:00
|
|
|
import android.view.*;
|
|
|
|
import android.view.inputmethod.*;
|
|
|
|
import android.content.*;
|
2010-11-12 01:07:59 +03:00
|
|
|
import android.R;
|
2010-06-30 08:13:44 +04:00
|
|
|
|
|
|
|
import android.util.*;
|
|
|
|
|
|
|
|
public class GeckoInputConnection
|
|
|
|
extends BaseInputConnection
|
2010-11-23 09:56:37 +03:00
|
|
|
implements TextWatcher
|
2010-06-30 08:13:44 +04:00
|
|
|
{
|
|
|
|
public GeckoInputConnection (View targetView) {
|
|
|
|
super(targetView, true);
|
|
|
|
mQueryResult = new SynchronousQueue<String>();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean beginBatchEdit() {
|
2010-11-12 01:07:59 +03:00
|
|
|
//Log.d("GeckoAppJava", "IME: beginBatchEdit");
|
2010-08-04 23:47:26 +04:00
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
@Override
|
|
|
|
public boolean commitCompletion(CompletionInfo text) {
|
2010-11-12 01:07:59 +03:00
|
|
|
//Log.d("GeckoAppJava", "IME: commitCompletion");
|
2010-08-04 23:47:26 +04:00
|
|
|
|
|
|
|
return commitText(text.getText(), 1);
|
2010-06-30 08:13:44 +04:00
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
@Override
|
|
|
|
public boolean commitText(CharSequence text, int newCursorPosition) {
|
2010-08-04 23:47:26 +04:00
|
|
|
//Log.d("GeckoAppJava", "IME: commitText");
|
|
|
|
|
|
|
|
setComposingText(text, newCursorPosition);
|
|
|
|
finishComposingText();
|
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
@Override
|
|
|
|
public boolean deleteSurroundingText(int leftLength, int rightLength) {
|
2010-11-12 01:07:59 +03:00
|
|
|
//Log.d("GeckoAppJava", "IME: deleteSurroundingText");
|
2010-08-04 23:47:26 +04:00
|
|
|
|
|
|
|
/* deleteSurroundingText is supposed to ignore the composing text,
|
|
|
|
so we cancel any pending composition, delete the text, and then
|
|
|
|
restart the composition */
|
|
|
|
|
|
|
|
if (mComposing) {
|
|
|
|
// Cancel current composition
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(0, 0, 0, 0, 0, 0, null));
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_COMPOSITION_END, 0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select text to be deleted
|
|
|
|
int delStart, delLen;
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_GET_SELECTION, 0, 0));
|
|
|
|
try {
|
|
|
|
mQueryResult.take();
|
|
|
|
} catch (InterruptedException e) {
|
2011-01-04 01:34:44 +03:00
|
|
|
Log.e("GeckoAppJava", "IME: deleteSurroundingText interrupted", e);
|
2010-08-04 23:47:26 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
delStart = mSelectionStart > leftLength ?
|
|
|
|
mSelectionStart - leftLength : 0;
|
|
|
|
delLen = mSelectionStart + rightLength - delStart;
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_SET_SELECTION, delStart, delLen));
|
|
|
|
|
|
|
|
// Restore composition / delete text
|
|
|
|
if (mComposing) {
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_COMPOSITION_BEGIN, 0, 0));
|
|
|
|
if (mComposingText.length() > 0) {
|
|
|
|
/* IME_SET_TEXT doesn't work well with empty strings */
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(0, mComposingText.length(),
|
|
|
|
GeckoEvent.IME_RANGE_RAWINPUT,
|
|
|
|
GeckoEvent.IME_RANGE_UNDERLINE, 0, 0,
|
|
|
|
mComposingText.toString()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_DELETE_TEXT, 0, 0));
|
|
|
|
}
|
2010-06-30 08:13:44 +04:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
@Override
|
|
|
|
public boolean endBatchEdit() {
|
2010-11-12 01:07:59 +03:00
|
|
|
//Log.d("GeckoAppJava", "IME: endBatchEdit");
|
2010-08-04 23:47:26 +04:00
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
@Override
|
|
|
|
public boolean finishComposingText() {
|
2010-08-04 23:47:26 +04:00
|
|
|
//Log.d("GeckoAppJava", "IME: finishComposingText");
|
|
|
|
|
|
|
|
if (mComposing) {
|
|
|
|
// Set style to none
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(0, mComposingText.length(),
|
|
|
|
GeckoEvent.IME_RANGE_RAWINPUT, 0, 0, 0,
|
|
|
|
mComposingText));
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_COMPOSITION_END, 0, 0));
|
|
|
|
mComposing = false;
|
2011-04-11 16:05:00 +04:00
|
|
|
mComposingText = "";
|
2010-08-04 23:47:26 +04:00
|
|
|
|
|
|
|
// Make sure caret stays at the same position
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_SET_SELECTION,
|
|
|
|
mCompositionStart + mCompositionSelStart, 0));
|
|
|
|
}
|
2010-06-30 08:13:44 +04:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
@Override
|
|
|
|
public int getCursorCapsMode(int reqModes) {
|
2010-08-04 23:47:26 +04:00
|
|
|
//Log.d("GeckoAppJava", "IME: getCursorCapsMode");
|
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Editable getEditable() {
|
|
|
|
Log.w("GeckoAppJava", "IME: getEditable called from " +
|
|
|
|
Thread.currentThread().getStackTrace()[0].toString());
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2010-11-12 01:07:59 +03:00
|
|
|
@Override
|
|
|
|
public boolean performContextMenuAction(int id) {
|
|
|
|
//Log.d("GeckoAppJava", "IME: performContextMenuAction");
|
|
|
|
|
|
|
|
// First we need to ask Gecko to tell us the full contents of the
|
|
|
|
// text field we're about to operate on.
|
|
|
|
String text;
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_GET_TEXT, 0, Integer.MAX_VALUE));
|
|
|
|
try {
|
|
|
|
text = mQueryResult.take();
|
|
|
|
} catch (InterruptedException e) {
|
2011-01-04 01:34:44 +03:00
|
|
|
Log.e("GeckoAppJava", "IME: performContextMenuAction interrupted", e);
|
2010-11-12 01:07:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (id) {
|
|
|
|
case R.id.selectAll:
|
2010-12-03 21:10:26 +03:00
|
|
|
setSelection(0, text.length());
|
2010-11-12 01:07:59 +03:00
|
|
|
break;
|
|
|
|
case R.id.cut:
|
|
|
|
// Fill the clipboard
|
|
|
|
GeckoAppShell.setClipboardText(text);
|
|
|
|
// If GET_TEXT returned an empty selection, we'll select everything
|
|
|
|
if (mSelectionLength <= 0)
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_SET_SELECTION, 0, text.length()));
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_DELETE_TEXT, 0, 0));
|
|
|
|
break;
|
|
|
|
case R.id.paste:
|
|
|
|
commitText(GeckoAppShell.getClipboardText(), 1);
|
|
|
|
break;
|
|
|
|
case R.id.copy:
|
|
|
|
// If there is no selection set, we must be doing "Copy All",
|
|
|
|
// otherwise, we need to get the selection from Gecko
|
|
|
|
if (mSelectionLength > 0) {
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_GET_SELECTION, 0, 0));
|
|
|
|
try {
|
|
|
|
text = mQueryResult.take();
|
|
|
|
} catch (InterruptedException e) {
|
2011-01-04 01:34:44 +03:00
|
|
|
Log.e("GeckoAppJava", "IME: performContextMenuAction interrupted", e);
|
2010-11-12 01:07:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GeckoAppShell.setClipboardText(text);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
@Override
|
|
|
|
public ExtractedText getExtractedText(ExtractedTextRequest req, int flags) {
|
2010-08-04 23:47:26 +04:00
|
|
|
if (req == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
//Log.d("GeckoAppJava", "IME: getExtractedText");
|
|
|
|
|
|
|
|
ExtractedText extract = new ExtractedText();
|
|
|
|
extract.flags = 0;
|
|
|
|
extract.partialStartOffset = -1;
|
|
|
|
extract.partialEndOffset = -1;
|
|
|
|
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_GET_SELECTION, 0, 0));
|
|
|
|
try {
|
|
|
|
mQueryResult.take();
|
|
|
|
} catch (InterruptedException e) {
|
2011-01-04 01:34:44 +03:00
|
|
|
Log.e("GeckoAppJava", "IME: getExtractedText interrupted", e);
|
2010-08-04 23:47:26 +04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
extract.selectionStart = mSelectionStart;
|
|
|
|
extract.selectionEnd = mSelectionStart + mSelectionLength;
|
|
|
|
|
2011-04-30 06:25:55 +04:00
|
|
|
// bug 617298 - IME_GET_TEXT sometimes gives the wrong result due to
|
|
|
|
// a stale cache. Use a set of three workarounds:
|
|
|
|
// 1. Sleep for 20 milliseconds and hope the child updates us with the new text.
|
|
|
|
// Very evil and, consequentially, most effective.
|
|
|
|
try {
|
|
|
|
Thread.sleep(20);
|
|
|
|
} catch (InterruptedException e) {}
|
|
|
|
|
2010-08-04 23:47:26 +04:00
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_GET_TEXT, 0, Integer.MAX_VALUE));
|
2010-06-30 08:13:44 +04:00
|
|
|
try {
|
2010-08-04 23:47:26 +04:00
|
|
|
extract.startOffset = 0;
|
|
|
|
extract.text = mQueryResult.take();
|
|
|
|
|
2011-04-30 06:25:55 +04:00
|
|
|
// 2. Make a guess about what the text actually is
|
|
|
|
if (mComposing && extract.selectionEnd > extract.text.length())
|
2011-06-14 18:48:00 +04:00
|
|
|
extract.text = extract.text.subSequence(0, Math.min(extract.text.length(), mCompositionStart)) + mComposingText;
|
2011-04-30 06:25:55 +04:00
|
|
|
|
|
|
|
// 3. If all else fails, make sure our selection indexes make sense
|
|
|
|
extract.selectionStart = Math.min(extract.selectionStart, extract.text.length());
|
|
|
|
extract.selectionEnd = Math.min(extract.selectionEnd, extract.text.length());
|
|
|
|
|
2010-08-04 23:47:26 +04:00
|
|
|
if ((flags & GET_EXTRACTED_TEXT_MONITOR) != 0)
|
|
|
|
mUpdateRequest = req;
|
|
|
|
return extract;
|
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
} catch (InterruptedException e) {
|
2011-01-04 01:34:44 +03:00
|
|
|
Log.e("GeckoAppJava", "IME: getExtractedText interrupted", e);
|
2010-08-04 23:47:26 +04:00
|
|
|
return null;
|
2010-06-30 08:13:44 +04:00
|
|
|
}
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
@Override
|
|
|
|
public CharSequence getTextAfterCursor(int length, int flags) {
|
2010-08-04 23:47:26 +04:00
|
|
|
//Log.d("GeckoAppJava", "IME: getTextAfterCursor");
|
|
|
|
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_GET_SELECTION, 0, 0));
|
2010-06-30 08:13:44 +04:00
|
|
|
try {
|
2010-08-04 23:47:26 +04:00
|
|
|
mQueryResult.take();
|
2010-06-30 08:13:44 +04:00
|
|
|
} catch (InterruptedException e) {
|
2011-01-04 01:34:44 +03:00
|
|
|
Log.e("GeckoAppJava", "IME: getTextBefore/AfterCursor interrupted", e);
|
2010-08-04 23:47:26 +04:00
|
|
|
return null;
|
2010-06-30 08:13:44 +04:00
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
|
|
|
/* Compatible with both positive and negative length
|
|
|
|
(no need for separate code for getTextBeforeCursor) */
|
2010-08-23 18:54:00 +04:00
|
|
|
int textStart = mSelectionStart;
|
|
|
|
int textLength = length;
|
|
|
|
|
|
|
|
if (length < 0) {
|
|
|
|
textStart += length;
|
|
|
|
textLength = -length;
|
|
|
|
if (textStart < 0) {
|
|
|
|
textStart = 0;
|
|
|
|
textLength = mSelectionStart;
|
|
|
|
}
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_GET_TEXT, textStart, textLength));
|
2010-06-30 08:13:44 +04:00
|
|
|
try {
|
2010-08-04 23:47:26 +04:00
|
|
|
return mQueryResult.take();
|
2010-06-30 08:13:44 +04:00
|
|
|
} catch (InterruptedException e) {
|
2011-01-04 01:34:44 +03:00
|
|
|
Log.e("GeckoAppJava", "IME: getTextBefore/AfterCursor: Interrupted!", e);
|
2010-08-04 23:47:26 +04:00
|
|
|
return null;
|
2010-06-30 08:13:44 +04:00
|
|
|
}
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public CharSequence getTextBeforeCursor(int length, int flags) {
|
|
|
|
//Log.d("GeckoAppJava", "IME: getTextBeforeCursor");
|
|
|
|
|
|
|
|
return getTextAfterCursor(-length, flags);
|
|
|
|
}
|
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
@Override
|
|
|
|
public boolean setComposingText(CharSequence text, int newCursorPosition) {
|
2010-08-04 23:47:26 +04:00
|
|
|
//Log.d("GeckoAppJava", "IME: setComposingText");
|
|
|
|
|
|
|
|
// Set new composing text
|
|
|
|
mComposingText = text != null ? text.toString() : "";
|
|
|
|
|
|
|
|
if (!mComposing) {
|
2011-07-22 05:24:34 +04:00
|
|
|
if (mComposingText.length() == 0) {
|
|
|
|
// Some IMEs such as iWnn sometimes call with empty composing
|
|
|
|
// text. (See bug 664364)
|
|
|
|
// If composing text is empty, ignore this and don't start
|
|
|
|
// compositing.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-04 23:47:26 +04:00
|
|
|
// Get current selection
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_GET_SELECTION, 0, 0));
|
|
|
|
try {
|
|
|
|
mQueryResult.take();
|
|
|
|
} catch (InterruptedException e) {
|
2011-01-04 01:34:44 +03:00
|
|
|
Log.e("GeckoAppJava", "IME: setComposingText interrupted", e);
|
2010-08-04 23:47:26 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Make sure we are in a composition
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_COMPOSITION_BEGIN, 0, 0));
|
|
|
|
mComposing = true;
|
|
|
|
mCompositionStart = mSelectionLength >= 0 ?
|
|
|
|
mSelectionStart : mSelectionStart + mSelectionLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set new selection
|
|
|
|
// New selection should be within the composition
|
|
|
|
mCompositionSelStart = newCursorPosition > 0 ? mComposingText.length() : 0;
|
|
|
|
mCompositionSelLen = 0;
|
|
|
|
|
|
|
|
// Handle composition text styles
|
|
|
|
if (text != null && text instanceof Spanned) {
|
|
|
|
Spanned span = (Spanned) text;
|
|
|
|
int spanStart = 0, spanEnd = 0;
|
|
|
|
boolean pastSelStart = false, pastSelEnd = false;
|
|
|
|
|
|
|
|
do {
|
|
|
|
int rangeType = GeckoEvent.IME_RANGE_CONVERTEDTEXT;
|
|
|
|
int rangeStyles = 0, rangeForeColor = 0, rangeBackColor = 0;
|
|
|
|
|
|
|
|
// Find next offset where there is a style transition
|
|
|
|
spanEnd = span.nextSpanTransition(spanStart + 1, text.length(),
|
|
|
|
CharacterStyle.class);
|
|
|
|
|
|
|
|
// We need to count the selection as a transition
|
|
|
|
if (mCompositionSelLen >= 0) {
|
|
|
|
if (!pastSelStart && spanEnd >= mCompositionSelStart) {
|
|
|
|
spanEnd = mCompositionSelStart;
|
|
|
|
pastSelStart = true;
|
|
|
|
} else if (!pastSelEnd && spanEnd >=
|
|
|
|
mCompositionSelStart + mCompositionSelLen) {
|
|
|
|
spanEnd = mCompositionSelStart + mCompositionSelLen;
|
|
|
|
pastSelEnd = true;
|
|
|
|
rangeType = GeckoEvent.IME_RANGE_SELECTEDRAWTEXT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!pastSelEnd && spanEnd >=
|
|
|
|
mCompositionSelStart + mCompositionSelLen) {
|
|
|
|
spanEnd = mCompositionSelStart + mCompositionSelLen;
|
|
|
|
pastSelEnd = true;
|
|
|
|
} else if (!pastSelStart &&
|
|
|
|
spanEnd >= mCompositionSelStart) {
|
|
|
|
spanEnd = mCompositionSelStart;
|
|
|
|
pastSelStart = true;
|
|
|
|
rangeType = GeckoEvent.IME_RANGE_SELECTEDRAWTEXT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Empty range, continue
|
|
|
|
if (spanEnd <= spanStart)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Get and iterate through list of span objects within range
|
|
|
|
CharacterStyle styles[] = span.getSpans(
|
|
|
|
spanStart, spanEnd, CharacterStyle.class);
|
|
|
|
|
|
|
|
for (CharacterStyle style : styles) {
|
|
|
|
if (style instanceof UnderlineSpan) {
|
|
|
|
// Text should be underlined
|
|
|
|
rangeStyles |= GeckoEvent.IME_RANGE_UNDERLINE;
|
|
|
|
|
|
|
|
} else if (style instanceof ForegroundColorSpan) {
|
|
|
|
// Text should be of a different foreground color
|
|
|
|
rangeStyles |= GeckoEvent.IME_RANGE_FORECOLOR;
|
|
|
|
rangeForeColor =
|
|
|
|
((ForegroundColorSpan)style).getForegroundColor();
|
|
|
|
|
|
|
|
} else if (style instanceof BackgroundColorSpan) {
|
|
|
|
// Text should be of a different background color
|
|
|
|
rangeStyles |= GeckoEvent.IME_RANGE_BACKCOLOR;
|
|
|
|
rangeBackColor =
|
|
|
|
((BackgroundColorSpan)style).getBackgroundColor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add range to array, the actual styles are
|
|
|
|
// applied when IME_SET_TEXT is sent
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(spanStart, spanEnd - spanStart,
|
|
|
|
rangeType, rangeStyles,
|
|
|
|
rangeForeColor, rangeBackColor));
|
|
|
|
|
|
|
|
spanStart = spanEnd;
|
|
|
|
} while (spanStart < text.length());
|
|
|
|
} else {
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(0, text == null ? 0 : text.length(),
|
|
|
|
GeckoEvent.IME_RANGE_RAWINPUT,
|
|
|
|
GeckoEvent.IME_RANGE_UNDERLINE, 0, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change composition (treating selection end as where the caret is)
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(mCompositionSelStart + mCompositionSelLen, 0,
|
|
|
|
GeckoEvent.IME_RANGE_CARETPOSITION, 0, 0, 0,
|
|
|
|
mComposingText));
|
2010-06-30 08:13:44 +04:00
|
|
|
return true;
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|
2010-06-30 08:13:44 +04:00
|
|
|
@Override
|
|
|
|
public boolean setSelection(int start, int end) {
|
2010-08-04 23:47:26 +04:00
|
|
|
//Log.d("GeckoAppJava", "IME: setSelection");
|
|
|
|
|
|
|
|
if (mComposing) {
|
|
|
|
/* Translate to fake selection positions */
|
|
|
|
start -= mCompositionStart;
|
|
|
|
end -= mCompositionStart;
|
|
|
|
|
|
|
|
if (start < 0)
|
|
|
|
start = 0;
|
|
|
|
else if (start > mComposingText.length())
|
|
|
|
start = mComposingText.length();
|
|
|
|
|
|
|
|
if (end < 0)
|
|
|
|
end = 0;
|
|
|
|
else if (end > mComposingText.length())
|
|
|
|
end = mComposingText.length();
|
|
|
|
|
|
|
|
mCompositionSelStart = start;
|
|
|
|
mCompositionSelLen = end - start;
|
|
|
|
} else {
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_SET_SELECTION,
|
|
|
|
start, end - start));
|
|
|
|
}
|
2010-06-30 08:13:44 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-04 23:47:26 +04:00
|
|
|
public boolean onKeyDel() {
|
|
|
|
// Some IMEs don't update us on deletions
|
|
|
|
// In that case we are not updated when a composition
|
|
|
|
// is destroyed, and Bad Things happen
|
|
|
|
|
|
|
|
if (!mComposing)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (mComposingText.length() > 0) {
|
|
|
|
mComposingText = mComposingText.substring(0,
|
|
|
|
mComposingText.length() - 1);
|
|
|
|
if (mComposingText.length() > 0)
|
|
|
|
return false;
|
2010-06-30 08:13:44 +04:00
|
|
|
}
|
|
|
|
|
2010-08-04 23:47:26 +04:00
|
|
|
commitText(null, 1);
|
|
|
|
return true;
|
2010-06-30 08:13:44 +04:00
|
|
|
}
|
|
|
|
|
2010-08-04 23:47:26 +04:00
|
|
|
public void notifyTextChange(InputMethodManager imm, String text,
|
|
|
|
int start, int oldEnd, int newEnd) {
|
|
|
|
//Log.d("GeckoAppJava", "IME: notifyTextChange");
|
2010-06-30 08:13:44 +04:00
|
|
|
|
2010-11-23 09:56:37 +03:00
|
|
|
if (!text.contentEquals(GeckoApp.surfaceView.mEditable))
|
2011-01-07 04:15:24 +03:00
|
|
|
GeckoApp.surfaceView.setEditable(text);
|
2010-11-23 09:56:37 +03:00
|
|
|
|
2010-08-04 23:47:26 +04:00
|
|
|
if (mUpdateRequest == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mUpdateExtract.flags = 0;
|
2010-11-25 01:09:24 +03:00
|
|
|
|
|
|
|
// We update from (0, oldEnd) to (0, newEnd) because some Android IMEs
|
|
|
|
// assume that updates start at zero, according to jchen.
|
2010-08-04 23:47:26 +04:00
|
|
|
mUpdateExtract.partialStartOffset = 0;
|
|
|
|
mUpdateExtract.partialEndOffset = oldEnd;
|
|
|
|
|
|
|
|
// Faster to not query for selection
|
|
|
|
mUpdateExtract.selectionStart = newEnd;
|
|
|
|
mUpdateExtract.selectionEnd = newEnd;
|
|
|
|
|
2010-11-25 01:09:24 +03:00
|
|
|
mUpdateExtract.text = text.substring(0, newEnd);
|
2010-08-04 23:47:26 +04:00
|
|
|
mUpdateExtract.startOffset = 0;
|
|
|
|
|
|
|
|
imm.updateExtractedText(GeckoApp.surfaceView,
|
|
|
|
mUpdateRequest.token, mUpdateExtract);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void notifySelectionChange(InputMethodManager imm,
|
|
|
|
int start, int end) {
|
|
|
|
//Log.d("GeckoAppJava", "IME: notifySelectionChange");
|
|
|
|
|
|
|
|
if (mComposing)
|
|
|
|
imm.updateSelection(GeckoApp.surfaceView,
|
|
|
|
mCompositionStart + mCompositionSelStart,
|
|
|
|
mCompositionStart + mCompositionSelStart + mCompositionSelLen,
|
|
|
|
mCompositionStart,
|
|
|
|
mCompositionStart + mComposingText.length());
|
|
|
|
else
|
|
|
|
imm.updateSelection(GeckoApp.surfaceView, start, end, -1, -1);
|
2010-11-23 09:56:37 +03:00
|
|
|
|
|
|
|
int maxLen = GeckoApp.surfaceView.mEditable.length();
|
|
|
|
Selection.setSelection(GeckoApp.surfaceView.mEditable,
|
|
|
|
Math.min(start, maxLen),
|
|
|
|
Math.min(end, maxLen));
|
2010-08-04 23:47:26 +04:00
|
|
|
}
|
|
|
|
|
2010-08-21 04:07:26 +04:00
|
|
|
public void reset() {
|
|
|
|
mComposing = false;
|
2011-04-11 16:05:00 +04:00
|
|
|
mComposingText = "";
|
2010-08-21 04:07:26 +04:00
|
|
|
mUpdateRequest = null;
|
|
|
|
}
|
|
|
|
|
2010-11-23 09:56:37 +03:00
|
|
|
// TextWatcher
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count)
|
|
|
|
{
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_SET_SELECTION, start, before));
|
|
|
|
|
2010-11-25 01:07:58 +03:00
|
|
|
if (count == 0) {
|
2010-11-23 09:56:37 +03:00
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_DELETE_TEXT, 0, 0));
|
2010-11-25 01:07:58 +03:00
|
|
|
} else {
|
|
|
|
// Start and stop composition to force UI updates.
|
|
|
|
finishComposingText();
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_COMPOSITION_BEGIN, 0, 0));
|
|
|
|
|
2010-11-23 09:56:37 +03:00
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(0, count,
|
|
|
|
GeckoEvent.IME_RANGE_RAWINPUT, 0, 0, 0,
|
|
|
|
s.subSequence(start, start + count).toString()));
|
2010-11-25 01:07:58 +03:00
|
|
|
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_COMPOSITION_END, 0, 0));
|
2011-02-23 19:22:43 +03:00
|
|
|
|
|
|
|
GeckoAppShell.sendEventToGecko(
|
|
|
|
new GeckoEvent(GeckoEvent.IME_SET_SELECTION, start + count, 0));
|
2010-11-25 01:07:58 +03:00
|
|
|
}
|
2011-03-02 20:23:02 +03:00
|
|
|
|
|
|
|
// Block this thread until all pending events are processed
|
|
|
|
GeckoAppShell.geckoEventSync();
|
2010-11-23 09:56:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void afterTextChanged(Editable s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-08-04 23:47:26 +04:00
|
|
|
// Is a composition active?
|
|
|
|
boolean mComposing;
|
|
|
|
// Composition text when a composition is active
|
2011-04-11 16:05:00 +04:00
|
|
|
String mComposingText = "";
|
2010-08-04 23:47:26 +04:00
|
|
|
// Start index of the composition within the text body
|
|
|
|
int mCompositionStart;
|
|
|
|
/* During a composition, we should not alter the real selection,
|
|
|
|
therefore we keep our own offsets to emulate selection */
|
|
|
|
// Start of fake selection, relative to start of composition
|
|
|
|
int mCompositionSelStart;
|
|
|
|
// Length of fake selection
|
|
|
|
int mCompositionSelLen;
|
|
|
|
|
|
|
|
ExtractedTextRequest mUpdateRequest;
|
|
|
|
final ExtractedText mUpdateExtract = new ExtractedText();
|
|
|
|
|
|
|
|
int mSelectionStart, mSelectionLength;
|
2010-06-30 08:13:44 +04:00
|
|
|
SynchronousQueue<String> mQueryResult;
|
|
|
|
}
|
2010-08-04 23:47:26 +04:00
|
|
|
|