summaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
authorThe Android Open Source Project <initial-contribution@android.com>2009-01-09 17:51:19 -0800
committerThe Android Open Source Project <initial-contribution@android.com>2009-01-09 17:51:19 -0800
commit95cf464c5a9f2bb7d6bf3a231862323dcbf854f8 (patch)
tree1d8570ed831feb1053a458a46138d9764dae0c9c /samples
parente943f2fd8e7623ba3ea12bb65294d30446db4174 (diff)
downloadandroid_development-95cf464c5a9f2bb7d6bf3a231862323dcbf854f8.tar.gz
android_development-95cf464c5a9f2bb7d6bf3a231862323dcbf854f8.tar.bz2
android_development-95cf464c5a9f2bb7d6bf3a231862323dcbf854f8.zip
auto import from //branches/cupcake/...@125939
Diffstat (limited to 'samples')
-rw-r--r--samples/ApiDemos/AndroidManifest.xml9
-rw-r--r--samples/ApiDemos/res/layout/intents.xml37
-rw-r--r--samples/ApiDemos/res/values/strings.xml8
-rw-r--r--samples/ApiDemos/src/com/android/samples/app/VoiceRecognition.java54
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/app/Intents.java47
-rw-r--r--samples/Home/res/drawable/ic_menu_gallery.pngbin2379 -> 0 bytes
-rw-r--r--samples/Home/res/drawable/ic_menu_notifications.pngbin1771 -> 0 bytes
-rwxr-xr-xsamples/Home/res/drawable/ic_menu_preferences.pngbin2144 -> 0 bytes
-rw-r--r--samples/Home/src/com/example/android/home/Home.java4
-rw-r--r--samples/PlatformLibrary/Android.mk17
-rw-r--r--samples/PlatformLibrary/README.txt14
-rw-r--r--samples/PlatformLibrary/com.example.android.platform_library.xml5
-rw-r--r--samples/SoftKeyboard/res/values/dimens.xml1
-rwxr-xr-xsamples/SoftKeyboard/src/com/example/android/softkeyboard/CandidateView.java6
-rw-r--r--samples/SoftKeyboard/src/com/example/android/softkeyboard/SoftKeyboard.java224
15 files changed, 303 insertions, 123 deletions
diff --git a/samples/ApiDemos/AndroidManifest.xml b/samples/ApiDemos/AndroidManifest.xml
index bfefbf059..f8fc1b24f 100644
--- a/samples/ApiDemos/AndroidManifest.xml
+++ b/samples/ApiDemos/AndroidManifest.xml
@@ -152,6 +152,15 @@
</intent-filter>
</activity>
+ <!-- Intent Samples -->
+
+ <activity android:name=".app.Intents" android:label="@string/activity_intents">
+ <intent-filter>
+ <action android:name="android.intent.action.MAIN" />
+ <category android:name="android.intent.category.SAMPLE_CODE" />
+ </intent-filter>
+ </activity>
+
<!-- Service Samples -->
<service android:name=".app.LocalService" />
diff --git a/samples/ApiDemos/res/layout/intents.xml b/samples/ApiDemos/res/layout/intents.xml
new file mode 100644
index 000000000..023e4e84d
--- /dev/null
+++ b/samples/ApiDemos/res/layout/intents.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2008 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<!-- Demonstrates launching various intents.
+ See corresponding Java code com.example.android.apis.app.Intents.java. -->
+
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip"
+ android:gravity="center_horizontal"
+ android:layout_width="fill_parent" android:layout_height="fill_parent">
+
+ <TextView
+ android:layout_width="fill_parent" android:layout_height="wrap_content"
+ android:layout_weight="0"
+ android:paddingBottom="4dip"
+ android:text="@string/intents"/>
+
+ <Button android:id="@+id/get_music"
+ android:layout_width="wrap_content" android:layout_height="wrap_content"
+ android:text="@string/get_music">
+ <requestFocus />
+ </Button>
+
+</LinearLayout>
+
diff --git a/samples/ApiDemos/res/values/strings.xml b/samples/ApiDemos/res/values/strings.xml
index 2b2cc164d..7db3e1b7c 100644
--- a/samples/ApiDemos/res/values/strings.xml
+++ b/samples/ApiDemos/res/values/strings.xml
@@ -205,6 +205,14 @@
<string name="activity_themes">Content/Resources/Themes</string>
<string name="activity_resources">Content/Resources/Resources</string>
+ <!-- ============================== -->
+ <!-- app/intents examples strings -->
+ <!-- ============================== -->
+
+ <string name="activity_intents">App/Intents</string>
+ <string name="intents">Example of launching various Intents.</string>
+ <string name="get_music">Get Music</string>
+
<!-- =================================== -->
<!-- app/notification examples strings -->
<!-- =================================== -->
diff --git a/samples/ApiDemos/src/com/android/samples/app/VoiceRecognition.java b/samples/ApiDemos/src/com/android/samples/app/VoiceRecognition.java
index aa45c8eb1..35d94307d 100644
--- a/samples/ApiDemos/src/com/android/samples/app/VoiceRecognition.java
+++ b/samples/ApiDemos/src/com/android/samples/app/VoiceRecognition.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2008 Google Inc.
+ * Copyright (C) 2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,7 +29,10 @@ import com.example.android.apis.R;
import java.util.ArrayList;
-public class VoiceRecognition extends Activity {
+/**
+ * Sample code that invokes the speech recognition intent API.
+ */
+public class VoiceRecognition extends Activity implements OnClickListener {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
@@ -51,34 +54,41 @@ public class VoiceRecognition extends Activity {
mList = (ListView) findViewById(R.id.list);
// Attach actions to buttons
- speakButton.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- startVoiceRecognitionActivity();
- }
- });
+ speakButton.setOnClickListener(this);
+ }
+
+ /**
+ * Handle the click on the start recognition button.
+ */
+ public void onClick(View v) {
+ if (v.getId() == R.id.btn_speak) {
+ startVoiceRecognitionActivity();
+ }
}
- void startVoiceRecognitionActivity() {
- Intent intent = new Intent("android.intent.action.VOICE_RECOGNITION");
- this.startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
+ /**
+ * Fire an intent to start the speech recognition activity.
+ */
+ private void startVoiceRecognitionActivity() {
+ //TODO Get these values from constants
+ Intent intent = new Intent("android.speech.action.RECOGNIZE_SPEECH");
+ intent.putExtra("language_model", "free_form");
+ intent.putExtra("prompt", "Speech recognition demo");
+ startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
+ /**
+ * Handle the results from the recognition activity.
+ */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
-
- android.util.Log.d("mike", "************** onActivityResult" + data );
-
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
- ArrayList<String>matches = data.getStringArrayListExtra("queries");
-
-
- android.util.Log.d("mike", "************** onActivityResult" + matches);
-
- mList.setAdapter(new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1, matches));
+ //TODO get the value from a constant
+ ArrayList<String>matches = data.getStringArrayListExtra("results");
+ mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
+ matches));
}
-
+
super.onActivityResult(requestCode, resultCode, data);
}
-
}
diff --git a/samples/ApiDemos/src/com/example/android/apis/app/Intents.java b/samples/ApiDemos/src/com/example/android/apis/app/Intents.java
new file mode 100644
index 000000000..8f02b8322
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/app/Intents.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.app;
+
+import com.example.android.apis.R;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.view.View.OnClickListener;
+import android.widget.Button;
+
+public class Intents extends Activity {
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+
+ setContentView(R.layout.intents);
+
+ // Watch for button clicks.
+ Button button = (Button)findViewById(R.id.get_music);
+ button.setOnClickListener(mGetMusicListener);
+ }
+
+ private OnClickListener mGetMusicListener = new OnClickListener() {
+ public void onClick(View v) {
+ Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
+ intent.setType("audio/*");
+ startActivity(Intent.createChooser(intent, "Select music"));
+ }
+ };
+}
diff --git a/samples/Home/res/drawable/ic_menu_gallery.png b/samples/Home/res/drawable/ic_menu_gallery.png
deleted file mode 100644
index f61bbd8ba..000000000
--- a/samples/Home/res/drawable/ic_menu_gallery.png
+++ /dev/null
Binary files differ
diff --git a/samples/Home/res/drawable/ic_menu_notifications.png b/samples/Home/res/drawable/ic_menu_notifications.png
deleted file mode 100644
index 866d4e0bd..000000000
--- a/samples/Home/res/drawable/ic_menu_notifications.png
+++ /dev/null
Binary files differ
diff --git a/samples/Home/res/drawable/ic_menu_preferences.png b/samples/Home/res/drawable/ic_menu_preferences.png
deleted file mode 100755
index b8e71412d..000000000
--- a/samples/Home/res/drawable/ic_menu_preferences.png
+++ /dev/null
Binary files differ
diff --git a/samples/Home/src/com/example/android/home/Home.java b/samples/Home/src/com/example/android/home/Home.java
index f2fbefffb..de67ef4d2 100644
--- a/samples/Home/src/com/example/android/home/Home.java
+++ b/samples/Home/src/com/example/android/home/Home.java
@@ -388,13 +388,13 @@ public class Home extends Activity {
super.onCreateOptionsMenu(menu);
menu.add(0, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
- .setIcon(R.drawable.ic_menu_gallery)
+ .setIcon(android.R.drawable.ic_menu_gallery)
.setAlphabeticShortcut('W');
menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
.setIcon(android.R.drawable.ic_search_category_default)
.setAlphabeticShortcut(SearchManager.MENU_KEY);
menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
- .setIcon(R.drawable.ic_menu_preferences)
+ .setIcon(android.R.drawable.ic_menu_preferences)
.setIntent(new Intent(android.provider.Settings.ACTION_SETTINGS));
return true;
diff --git a/samples/PlatformLibrary/Android.mk b/samples/PlatformLibrary/Android.mk
index d70038696..afb54a26e 100644
--- a/samples/PlatformLibrary/Android.mk
+++ b/samples/PlatformLibrary/Android.mk
@@ -35,6 +35,23 @@ LOCAL_SRC_FILES := $(call all-java-files-under, java)
include $(BUILD_JAVA_LIBRARY)
# ============================================================
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := com.example.android.platform_library.xml
+
+LOCAL_MODULE_TAGS := samples
+
+LOCAL_MODULE_CLASS := ETC
+
+# This will install the file in /system/etc/permissions
+#
+LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/permissions
+
+LOCAL_SRC_FILES := $(LOCAL_MODULE)
+
+include $(BUILD_PREBUILT)
+
+# ============================================================
# Also build all of the sub-targets under this one: the library's
# associated JNI code, and a sample client of the library.
diff --git a/samples/PlatformLibrary/README.txt b/samples/PlatformLibrary/README.txt
index e2a7c80b7..5ce9d2f77 100644
--- a/samples/PlatformLibrary/README.txt
+++ b/samples/PlatformLibrary/README.txt
@@ -12,18 +12,14 @@ create a system image that will be delivered on a device which will include
a custom library as shown here. It can not be used to create a third party
shared library, which is not currently supported in Android.
-Note that while this example is almost entirely self-contained -- you can
-build it and run the client app without any changes to the framework -- there
-is one change you must make so that the system will recognize your library:
-
-In frameworks/base/data/etc/permissions.xml is a list of all of the optional
-shared libraries available in the system. You will need to add an entry at
-the bottom of this file for your own shared library, which looks like this
-for our sample code here:
+To declare your library to the framework, you must place a file with a .xml
+extension in the /system/etc/permissions directory with the following contents:
+<?xml version="1.0" encoding="utf-8"?>
+<permissions>
<library name="com.example.android.platform_library"
file="/system/framework/com.example.android.platform_library.jar"/>
-
+</permissions>
There are three major parts of this example, supplying three distinct
build targets and corresponding build outputs:
diff --git a/samples/PlatformLibrary/com.example.android.platform_library.xml b/samples/PlatformLibrary/com.example.android.platform_library.xml
new file mode 100644
index 000000000..b9491d85b
--- /dev/null
+++ b/samples/PlatformLibrary/com.example.android.platform_library.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="utf-8"?>
+<permissions>
+ <library name="com.example.android.platform_library"
+ file="/system/framework/com.example.android.platform_library.jar"/>
+</permissions>
diff --git a/samples/SoftKeyboard/res/values/dimens.xml b/samples/SoftKeyboard/res/values/dimens.xml
index cea6e505a..cdaf27a2d 100644
--- a/samples/SoftKeyboard/res/values/dimens.xml
+++ b/samples/SoftKeyboard/res/values/dimens.xml
@@ -21,4 +21,5 @@
<resources>
<dimen name="key_height">50px</dimen>
<dimen name="candidate_font_height">16sp</dimen>
+ <dimen name="candidate_vertical_padding">6sp</dimen>
</resources> \ No newline at end of file
diff --git a/samples/SoftKeyboard/src/com/example/android/softkeyboard/CandidateView.java b/samples/SoftKeyboard/src/com/example/android/softkeyboard/CandidateView.java
index 1e79cd7c4..7cadead8a 100755
--- a/samples/SoftKeyboard/src/com/example/android/softkeyboard/CandidateView.java
+++ b/samples/SoftKeyboard/src/com/example/android/softkeyboard/CandidateView.java
@@ -55,6 +55,7 @@ public class CandidateView extends View {
private int mColorNormal;
private int mColorRecommended;
private int mColorOther;
+ private int mVerticalPadding;
private Paint mPaint;
private boolean mScrolled;
private int mTargetScrollX;
@@ -86,6 +87,7 @@ public class CandidateView extends View {
mColorNormal = r.getColor(R.color.candidate_normal);
mColorRecommended = r.getColor(R.color.candidate_recommended);
mColorOther = r.getColor(R.color.candidate_other);
+ mVerticalPadding = r.getDimensionPixelSize(R.dimen.candidate_vertical_padding);
mPaint = new Paint();
mPaint.setColor(mColorNormal);
@@ -139,7 +141,7 @@ public class CandidateView extends View {
// not have a divider below)
Rect padding = new Rect();
mSelectionHighlight.getPadding(padding);
- final int desiredHeight = ((int)mPaint.getTextSize()) + 1
+ final int desiredHeight = ((int)mPaint.getTextSize()) + mVerticalPadding
+ padding.top + padding.bottom;
// Maximum possible width and desired height
@@ -174,7 +176,7 @@ public class CandidateView extends View {
final int scrollX = getScrollX();
final boolean scrolled = mScrolled;
final boolean typedWordValid = mTypedWordValid;
- final int y = (int) (height + mPaint.getTextSize()) / 2;
+ final int y = (int) (((height - mPaint.getTextSize()) / 2) - mPaint.ascent());
for (int i = 0; i < count; i++) {
String suggestion = mSuggestions.get(i);
diff --git a/samples/SoftKeyboard/src/com/example/android/softkeyboard/SoftKeyboard.java b/samples/SoftKeyboard/src/com/example/android/softkeyboard/SoftKeyboard.java
index baa45942d..5d68b56f1 100644
--- a/samples/SoftKeyboard/src/com/example/android/softkeyboard/SoftKeyboard.java
+++ b/samples/SoftKeyboard/src/com/example/android/softkeyboard/SoftKeyboard.java
@@ -20,8 +20,9 @@ import android.content.Context;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
-import android.text.TextUtils;
+import android.text.method.MetaKeyKeyListener;
import android.util.Log;
+import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
@@ -48,12 +49,12 @@ public class SoftKeyboard extends InputMethodService
private CompletionInfo[] mCompletions;
private StringBuilder mComposing = new StringBuilder();
- private boolean mPredicting;
private boolean mPredictionOn;
private boolean mCompletionOn;
private int mLastDisplayWidth;
private boolean mCapsLock;
private long mLastShiftTime;
+ private long mMetaState;
private Keyboard mSymbolsKeyboard;
private Keyboard mSymbolsShiftedKeyboard;
@@ -61,8 +62,6 @@ public class SoftKeyboard extends InputMethodService
private String mWordSeparators;
- private InputConnection mOldConnection;
-
private void makeKeyboards() {
// Configuration change is coming after the keyboard gets recreated. So don't rely on that.
// If keyboards have already been made, check if we have a screen width change and
@@ -80,7 +79,6 @@ public class SoftKeyboard extends InputMethodService
@Override public void onCreate() {
super.onCreate();
- //setStatusIcon(R.drawable.ime_qwerty);
makeKeyboards();
mWordSeparators = getResources().getString(R.string.word_separators);
}
@@ -97,7 +95,6 @@ public class SoftKeyboard extends InputMethodService
@Override
public View onCreateCandidatesView() {
- makeKeyboards();
mCandidateView = new CandidateView(this);
mCandidateView.setService(this);
return mCandidateView;
@@ -105,33 +102,28 @@ public class SoftKeyboard extends InputMethodService
@Override
public void onStartInputView(EditorInfo attribute, boolean restarting) {
- // In landscape mode, this method gets called without the input view being created.
- if (mInputView == null) {
- return;
- }
-
- if (mQwertyKeyboard == null) {
- makeKeyboards();
- }
-
- if (mOldConnection != null) {
- commitTyped(mOldConnection);
+ mComposing.setLength(0);
+ updateCandidates();
+
+ if (!restarting) {
+ // Clear shift states.
+ mMetaState = 0;
}
mPredictionOn = false;
mCompletionOn = false;
mCompletions = null;
+ Keyboard keyboard;
switch (attribute.inputType&EditorInfo.TYPE_MASK_CLASS) {
case EditorInfo.TYPE_CLASS_NUMBER:
case EditorInfo.TYPE_CLASS_DATETIME:
- mInputView.setKeyboard(mSymbolsKeyboard);
+ keyboard = mSymbolsKeyboard;
break;
case EditorInfo.TYPE_CLASS_PHONE:
- mInputView.setKeyboard(mSymbolsKeyboard);
+ keyboard = mSymbolsKeyboard;
break;
default:
- mInputView.setKeyboard(mQwertyKeyboard);
- //startPrediction();
+ keyboard = mQwertyKeyboard;
mPredictionOn = true;
// Make sure that passwords are not displayed in candidate view
int variation = attribute.inputType & EditorInfo.TYPE_MASK_VARIATION;
@@ -149,34 +141,49 @@ public class SoftKeyboard extends InputMethodService
updateShiftKeyState(attribute);
break;
}
- mInputView.closing();
+
+ if (mInputView != null) {
+ mInputView.setKeyboard(keyboard);
+ mInputView.closing();
+ }
+
mComposing.setLength(0);
- mCandidateView.setSuggestions(null, false, false);
- mOldConnection = getCurrentInputConnection();
- setCandidatesViewShown(mPredictionOn || mCompletionOn);
+ setSuggestions(null, false, false);
}
@Override
public void onFinishInput() {
super.onFinishInput();
- commitTyped(mOldConnection);
+ mComposing.setLength(0);
+ updateCandidates();
if (mInputView != null) {
mInputView.closing();
}
}
@Override
- public void onDisplayCompletions(CompletionInfo[] completions) {
- if (false) {
- Log.i("foo", "Received completions:");
- for (int i=0; i<(completions != null ? completions.length : 0); i++) {
- Log.i("foo", " #" + i + ": " + completions[i]);
+ public void onUpdateSelection(int oldSelStart, int oldSelEnd,
+ int newSelStart, int newSelEnd,
+ int candidatesStart, int candidatesEnd) {
+ // If the current selection in the text view changes, we should
+ // clear whatever candidate text we have.
+ if (mComposing.length() > 0 && (newSelStart != candidatesEnd
+ || newSelEnd != candidatesEnd)) {
+ mComposing.setLength(0);
+ updateCandidates();
+ InputConnection ic = getCurrentInputConnection();
+ if (ic != null) {
+ ic.finishComposingText();
}
}
+ }
+
+ @Override
+ public void onDisplayCompletions(CompletionInfo[] completions) {
if (mCompletionOn) {
mCompletions = completions;
if (completions == null) {
- mCandidateView.setSuggestions(null, false, false);
+ setSuggestions(null, false, false);
return;
}
@@ -185,8 +192,40 @@ public class SoftKeyboard extends InputMethodService
CompletionInfo ci = completions[i];
if (ci != null) stringList.add(ci.getText().toString());
}
- mCandidateView.setSuggestions(stringList, true, true);
+ setSuggestions(stringList, true, true);
+ }
+ }
+
+ private boolean translateKeyDown(int keyCode, KeyEvent event) {
+ mMetaState = MetaKeyKeyListener.handleKeyDown(mMetaState,
+ keyCode, event);
+ int c = event.getUnicodeChar(MetaKeyKeyListener.getMetaState(mMetaState));
+ mMetaState = MetaKeyKeyListener.adjustMetaAfterKeypress(mMetaState);
+ InputConnection ic = getCurrentInputConnection();
+ if (c == 0 || ic == null) {
+ return false;
+ }
+
+ boolean dead = false;
+
+ if ((c & KeyCharacterMap.COMBINING_ACCENT) != 0) {
+ dead = true;
+ c = c & KeyCharacterMap.COMBINING_ACCENT_MASK;
+ }
+
+ if (mComposing.length() > 0) {
+ char accent = mComposing.charAt(mComposing.length() -1 );
+ int composed = KeyEvent.getDeadChar(accent, c);
+
+ if (composed != 0) {
+ c = composed;
+ mComposing.setLength(mComposing.length()-1);
+ }
}
+
+ onKey(c, null);
+
+ return true;
}
@Override
@@ -199,12 +238,16 @@ public class SoftKeyboard extends InputMethodService
}
}
break;
- case KeyEvent.KEYCODE_DPAD_DOWN:
- case KeyEvent.KEYCODE_DPAD_UP:
- case KeyEvent.KEYCODE_DPAD_LEFT:
- case KeyEvent.KEYCODE_DPAD_RIGHT:
- abortComposition();
+ case KeyEvent.KEYCODE_DEL:
+ if (mComposing.length() > 0) {
+ onKey(Keyboard.KEYCODE_DELETE, null);
+ return true;
+ }
break;
+ default:
+ if (mPredictionOn && translateKeyDown(keyCode, event)) {
+ return true;
+ }
}
return super.onKeyDown(keyCode, event);
}
@@ -225,6 +268,11 @@ public class SoftKeyboard extends InputMethodService
return true;
}
break;
+ default:
+ if (mPredictionOn) {
+ mMetaState = MetaKeyKeyListener.handleKeyUp(mMetaState,
+ keyCode, event);
+ }
}
return super.onKeyUp(keyCode, event);
}
@@ -262,7 +310,7 @@ public class SoftKeyboard extends InputMethodService
private void sendKey(int keyCode) {
switch (keyCode) {
- case 10:
+ case '\n':
keyDownUp(KeyEvent.KEYCODE_ENTER);
break;
default:
@@ -280,11 +328,11 @@ public class SoftKeyboard extends InputMethodService
public void onKey(int primaryCode, int[] keyCodes) {
if (isWordSeparator(primaryCode)) {
// Handle separator
- if (mPredicting) {
+ if (mComposing.length() > 0) {
commitTyped(getCurrentInputConnection());
}
sendKey(primaryCode);
- updateShiftKeyState(getCurrentInputInfo());
+ updateShiftKeyState(getCurrentInputEditorInfo());
} else if (primaryCode == Keyboard.KEYCODE_DELETE) {
handleBackspace();
} else if (primaryCode == Keyboard.KEYCODE_SHIFT) {
@@ -294,7 +342,8 @@ public class SoftKeyboard extends InputMethodService
return;
} else if (primaryCode == LatinKeyboardView.KEYCODE_OPTIONS) {
// Show a menu or somethin'
- } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE) {
+ } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE
+ && mInputView != null) {
Keyboard current = mInputView.getKeyboard();
if (current == mSymbolsKeyboard || current == mSymbolsShiftedKeyboard) {
current = mQwertyKeyboard;
@@ -320,34 +369,47 @@ public class SoftKeyboard extends InputMethodService
if (mComposing.length() > 0) {
ArrayList<String> list = new ArrayList<String>();
list.add(mComposing.toString());
- mCandidateView.setSuggestions(list, true, true);
+ setSuggestions(list, true, true);
} else {
- mCandidateView.setSuggestions(null, false, false);
+ setSuggestions(null, false, false);
}
}
}
+ public void setSuggestions(List<String> suggestions, boolean completions,
+ boolean typedWordValid) {
+ mCandidateView.setSuggestions(suggestions, completions, typedWordValid);
+ if (suggestions != null && suggestions.size() > 0) {
+ setCandidatesViewShown(true);
+ } else if (isFullscreenMode()) {
+ setCandidatesViewShown(true);
+ } else {
+ setCandidatesViewShown(false);
+ }
+ }
+
private void handleBackspace() {
- if (mPredicting) {
- final int length = mComposing.length();
- if (length > 0) {
- mComposing.delete(length - 1, length);
- getCurrentInputConnection().setComposingText(mComposing, mComposing.length());
- updateCandidates();
- if (mComposing.length() == 0) {
- mPredicting = false;
- }
- } else {
- getCurrentInputConnection().deleteSurroundingText(1, 0);
- }
+ final int length = mComposing.length();
+ if (length > 1) {
+ mComposing.delete(length - 1, length);
+ getCurrentInputConnection().setComposingText(mComposing, mComposing.length());
+ updateCandidates();
+ } else if (length > 0) {
+ mComposing.setLength(0);
+ getCurrentInputConnection().commitText("", 0);
+ updateCandidates();
} else {
//getCurrentInputConnection().deleteSurroundingText(1, 0);
keyDownUp(KeyEvent.KEYCODE_DEL);
}
- updateShiftKeyState(getCurrentInputInfo());
+ updateShiftKeyState(getCurrentInputEditorInfo());
}
private void handleShift() {
+ if (mInputView == null) {
+ return;
+ }
+
Keyboard currentKeyboard = mInputView.getKeyboard();
if (mQwertyKeyboard == currentKeyboard) {
// Alphabet keyboard
@@ -365,19 +427,20 @@ public class SoftKeyboard extends InputMethodService
}
private void handleCharacter(int primaryCode, int[] keyCodes) {
- if (isAlphabet(primaryCode) && mPredictionOn && !isCursorTouchingWord()) {
- if (!mPredicting) {
- mPredicting = true;
- mComposing.setLength(0);
+ if (isInputViewShown()) {
+ if (mInputView.isShifted()) {
+ primaryCode = Character.toUpperCase(primaryCode);
}
}
- if (mInputView.isShifted()) {
- primaryCode = Character.toUpperCase(primaryCode);
+ if (isAlphabet(primaryCode) && mPredictionOn) {
+ mComposing.append((char) primaryCode);
+ getCurrentInputConnection().setComposingText(mComposing, mComposing.length());
+ updateShiftKeyState(getCurrentInputEditorInfo());
+ updateCandidates();
+ } else {
+ getCurrentInputConnection().commitText(
+ String.valueOf((char) primaryCode), 0);
}
- mComposing.append((char) primaryCode);
- getCurrentInputConnection().setComposingText(mComposing, mComposing.length());
- updateShiftKeyState(getCurrentInputInfo());
- updateCandidates();
}
private void handleClose() {
@@ -396,20 +459,6 @@ public class SoftKeyboard extends InputMethodService
}
}
- private boolean isCursorTouchingWord() {
- CharSequence toLeft = getCurrentInputConnection().getTextBeforeCursor(1);
- CharSequence toRight = getCurrentInputConnection().getTextAfterCursor(1);
- if (!TextUtils.isEmpty(toLeft)
- && !isWordSeparator(toLeft.charAt(0))) {
- return true;
- }
- if (!TextUtils.isEmpty(toRight)
- && !isWordSeparator(toRight.charAt(0))) {
- return true;
- }
- return false;
- }
-
protected String getWordSeparators() {
return mWordSeparators;
}
@@ -431,7 +480,12 @@ public class SoftKeyboard extends InputMethodService
if (mCandidateView != null) {
mCandidateView.clear();
}
- updateShiftKeyState(getCurrentInputInfo());
+ updateShiftKeyState(getCurrentInputEditorInfo());
+ } else if (mComposing.length() > 0) {
+ // If we were generating candidate suggestions for the current
+ // text, we would commit one of them here. But for this sample,
+ // we will just commit the current text.
+ commitTyped(getCurrentInputConnection());
}
}
@@ -452,10 +506,4 @@ public class SoftKeyboard extends InputMethodService
public void swipeUp() {
// ?
}
-
- private void abortComposition() {
- commitTyped(getCurrentInputConnection());
- mComposing.setLength(0); // Don't want to allow uncommit after this
- updateCandidates();
- }
}