summaryrefslogtreecommitdiffstats
path: root/core/java
diff options
context:
space:
mode:
authorsatok <satok@google.com>2011-05-19 21:31:50 +0900
committersatok <satok@google.com>2011-05-26 11:02:31 +0900
commitf9f01008624e2d28c15a90d942fa36f98c8c967d (patch)
tree3e6cfc7fc71c3cf7db7588713c7f0b9a64c1da81 /core/java
parentf30c23d7bfa7a490ce01e2799905f320883c66d3 (diff)
downloadframeworks_base-f9f01008624e2d28c15a90d942fa36f98c8c967d.zip
frameworks_base-f9f01008624e2d28c15a90d942fa36f98c8c967d.tar.gz
frameworks_base-f9f01008624e2d28c15a90d942fa36f98c8c967d.tar.bz2
Add Apis to send notifications when the suggestion was picked
- Due to a strong request from VoiceIME Bug: 4443922 Change-Id: Ia539de0acf66053e0349daec459d75e36805f6bf
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/text/style/SuggestionSpan.java63
-rw-r--r--core/java/android/view/inputmethod/BaseInputConnection.java5
-rw-r--r--core/java/android/view/inputmethod/InputMethodManager.java21
-rw-r--r--core/java/com/android/internal/view/IInputMethodManager.aidl3
-rw-r--r--core/java/com/android/internal/widget/EditableInputConnection.java8
5 files changed, 86 insertions, 14 deletions
diff --git a/core/java/android/text/style/SuggestionSpan.java b/core/java/android/text/style/SuggestionSpan.java
index dcb0898..effa5c8 100644
--- a/core/java/android/text/style/SuggestionSpan.java
+++ b/core/java/android/text/style/SuggestionSpan.java
@@ -19,8 +19,10 @@ package android.text.style;
import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
+import android.os.SystemClock;
import android.text.ParcelableSpan;
import android.text.TextUtils;
+import android.util.Log;
import java.util.Arrays;
import java.util.Locale;
@@ -29,6 +31,7 @@ import java.util.Locale;
* Holds suggestion candidates of words under this span.
*/
public class SuggestionSpan implements ParcelableSpan {
+ private static final String TAG = SuggestionSpan.class.getSimpleName();
/**
* Flag for indicating that the input is verbatim. TextView refers to this flag to determine
@@ -36,7 +39,12 @@ public class SuggestionSpan implements ParcelableSpan {
*/
public static final int FLAG_VERBATIM = 0x0001;
- private static final int SUGGESTIONS_MAX_SIZE = 5;
+ public static final String ACTION_SUGGESTION_PICKED = "android.text.style.SUGGESTION_PICKED";
+ public static final String SUGGESTION_SPAN_PICKED_AFTER = "after";
+ public static final String SUGGESTION_SPAN_PICKED_BEFORE = "before";
+ public static final String SUGGESTION_SPAN_PICKED_HASHCODE = "hashcode";
+
+ public static final int SUGGESTIONS_MAX_SIZE = 5;
/*
* TODO: Needs to check the validity and add a feature that TextView will change
@@ -48,7 +56,9 @@ public class SuggestionSpan implements ParcelableSpan {
private final int mFlags;
private final String[] mSuggestions;
private final String mLocaleString;
- private final String mOriginalString;
+ private final Class<?> mNotificationTargetClass;
+ private final int mHashCode;
+
/*
* TODO: If switching IME is required, needs to add parameters for ids of InputMethodInfo
* and InputMethodSubtype.
@@ -77,10 +87,11 @@ public class SuggestionSpan implements ParcelableSpan {
* @param locale locale Locale of the suggestions
* @param suggestions Suggestions for the string under the span
* @param flags Additional flags indicating how this span is handled in TextView
- * @param originalString originalString for suggestions
+ * @param notificationTargetClass if not null, this class will get notified when the user
+ * selects one of the suggestions.
*/
public SuggestionSpan(Context context, Locale locale, String[] suggestions, int flags,
- String originalString) {
+ Class<?> notificationTargetClass) {
final int N = Math.min(SUGGESTIONS_MAX_SIZE, suggestions.length);
mSuggestions = Arrays.copyOf(suggestions, N);
mFlags = flags;
@@ -89,14 +100,26 @@ public class SuggestionSpan implements ParcelableSpan {
} else {
mLocaleString = locale.toString();
}
- mOriginalString = originalString;
+ mNotificationTargetClass = notificationTargetClass;
+ mHashCode = hashCodeInternal(
+ mFlags, mSuggestions, mLocaleString, mNotificationTargetClass);
}
public SuggestionSpan(Parcel src) {
mSuggestions = src.readStringArray();
mFlags = src.readInt();
mLocaleString = src.readString();
- mOriginalString = src.readString();
+ Class<?> tempClass = null;
+ try {
+ final String className = src.readString();
+ if (!TextUtils.isEmpty(className)) {
+ tempClass = Class.forName(className);
+ }
+ } catch (ClassNotFoundException e) {
+ Log.i(TAG, "Invalid class name was created.");
+ }
+ mNotificationTargetClass = tempClass;
+ mHashCode = src.readInt();
}
/**
@@ -114,10 +137,13 @@ public class SuggestionSpan implements ParcelableSpan {
}
/**
- * @return original string of suggestions
+ * @return The class to notify. The class of the original IME package will receive
+ * a notification when the user selects one of the suggestions. The notification will include
+ * the original string, the suggested replacement string as well as the hashCode of this span.
+ * The class will get notified by an intent that has those information.
*/
- public String getOriginalString() {
- return mOriginalString;
+ public Class<?> getNotificationTargetClass() {
+ return mNotificationTargetClass;
}
public int getFlags() {
@@ -134,7 +160,10 @@ public class SuggestionSpan implements ParcelableSpan {
dest.writeStringArray(mSuggestions);
dest.writeInt(mFlags);
dest.writeString(mLocaleString);
- dest.writeString(mOriginalString);
+ dest.writeString(mNotificationTargetClass != null
+ ? mNotificationTargetClass.getCanonicalName()
+ : "");
+ dest.writeInt(mHashCode);
}
@Override
@@ -142,6 +171,20 @@ public class SuggestionSpan implements ParcelableSpan {
return TextUtils.SUGGESTION_SPAN;
}
+ @Override
+ public int hashCode() {
+ return mHashCode;
+ }
+
+ private static int hashCodeInternal(int flags, String[] suggestions,String locale,
+ Class<?> notificationTargetClass) {
+ final String cls = notificationTargetClass != null
+ ? notificationTargetClass.getCanonicalName()
+ : "";
+ return Arrays.hashCode(
+ new Object[] {SystemClock.uptimeMillis(), flags, suggestions, locale, cls});
+ }
+
public static final Parcelable.Creator<SuggestionSpan> CREATOR =
new Parcelable.Creator<SuggestionSpan>() {
@Override
diff --git a/core/java/android/view/inputmethod/BaseInputConnection.java b/core/java/android/view/inputmethod/BaseInputConnection.java
index b4303f4..abe3c2c 100644
--- a/core/java/android/view/inputmethod/BaseInputConnection.java
+++ b/core/java/android/view/inputmethod/BaseInputConnection.java
@@ -49,8 +49,9 @@ public class BaseInputConnection implements InputConnection {
private static final boolean DEBUG = false;
private static final String TAG = "BaseInputConnection";
static final Object COMPOSING = new ComposingText();
-
- final InputMethodManager mIMM;
+
+ /** @hide */
+ protected final InputMethodManager mIMM;
final View mTargetView;
final boolean mDummyMode;
diff --git a/core/java/android/view/inputmethod/InputMethodManager.java b/core/java/android/view/inputmethod/InputMethodManager.java
index 27cbaf7..ea66d67 100644
--- a/core/java/android/view/inputmethod/InputMethodManager.java
+++ b/core/java/android/view/inputmethod/InputMethodManager.java
@@ -35,6 +35,7 @@ import android.os.Message;
import android.os.RemoteException;
import android.os.ResultReceiver;
import android.os.ServiceManager;
+import android.text.style.SuggestionSpan;
import android.util.Log;
import android.util.PrintWriterPrinter;
import android.util.Printer;
@@ -550,7 +551,25 @@ public final class InputMethodManager {
public void setFullscreenMode(boolean fullScreen) {
mFullscreenMode = fullScreen;
}
-
+
+ /** @hide */
+ public void registerSuggestionSpansForNotification(SuggestionSpan[] spans) {
+ try {
+ mService.registerSuggestionSpansForNotification(spans);
+ } catch (RemoteException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /** @hide */
+ public void notifySuggestionPicked(SuggestionSpan span, String originalString, int index) {
+ try {
+ mService.notifySuggestionPicked(span, originalString, index);
+ } catch (RemoteException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
/**
* Allows you to discover whether the attached input method is running
* in fullscreen mode. Return true if it is fullscreen, entirely covering
diff --git a/core/java/com/android/internal/view/IInputMethodManager.aidl b/core/java/com/android/internal/view/IInputMethodManager.aidl
index 4ffa4e1..8039fda 100644
--- a/core/java/com/android/internal/view/IInputMethodManager.aidl
+++ b/core/java/com/android/internal/view/IInputMethodManager.aidl
@@ -17,6 +17,7 @@
package com.android.internal.view;
import android.os.ResultReceiver;
+import android.text.style.SuggestionSpan;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodSubtype;
import android.view.inputmethod.EditorInfo;
@@ -61,6 +62,8 @@ interface IInputMethodManager {
void showMySoftInput(in IBinder token, int flags);
void updateStatusIcon(in IBinder token, String packageName, int iconId);
void setImeWindowStatus(in IBinder token, int vis, int backDisposition);
+ void registerSuggestionSpansForNotification(in SuggestionSpan[] spans);
+ boolean notifySuggestionPicked(in SuggestionSpan span, String originalString, int index);
InputMethodSubtype getCurrentInputMethodSubtype();
boolean setCurrentInputMethodSubtype(in InputMethodSubtype subtype);
boolean switchToLastInputMethod(in IBinder token);
diff --git a/core/java/com/android/internal/widget/EditableInputConnection.java b/core/java/com/android/internal/widget/EditableInputConnection.java
index 0d32d4b..32e733b 100644
--- a/core/java/com/android/internal/widget/EditableInputConnection.java
+++ b/core/java/com/android/internal/widget/EditableInputConnection.java
@@ -17,9 +17,10 @@
package com.android.internal.widget;
import android.os.Bundle;
-import android.os.IBinder;
import android.text.Editable;
+import android.text.Spanned;
import android.text.method.KeyListener;
+import android.text.style.SuggestionSpan;
import android.util.Log;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.CompletionInfo;
@@ -138,6 +139,11 @@ public class EditableInputConnection extends BaseInputConnection {
if (mTextView == null) {
return super.commitText(text, newCursorPosition);
}
+ if (text instanceof Spanned) {
+ Spanned spanned = ((Spanned) text);
+ SuggestionSpan[] spans = spanned.getSpans(0, text.length(), SuggestionSpan.class);
+ mIMM.registerSuggestionSpansForNotification(spans);
+ }
mTextView.resetErrorChangedFlag();
boolean success = super.commitText(text, newCursorPosition);