Skip to content

Commit ca369d7

Browse files
committed
Merge pull request kivy#158 from kivy/keyboard+type
Honor `input_type` Textinput property to choose the type of keyboard
2 parents c1ca120 + b5db42c commit ca369d7

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

recipes/android/src/android/_android.pyx

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,56 @@ def get_dpi():
167167

168168

169169
# Soft keyboard.
170-
cdef extern void android_show_keyboard()
170+
cdef extern void android_show_keyboard(int)
171171
cdef extern void android_hide_keyboard()
172172

173-
def show_keyboard():
174-
android_show_keyboard()
173+
# Flags for input_type, for requesting a particular type of keyboard
174+
#android FLAGS
175+
TYPE_CLASS_DATETIME = 4
176+
TYPE_CLASS_NUMBER = 2
177+
TYPE_NUMBER_VARIATION_NORMAL = 0
178+
TYPE_NUMBER_VARIATION_PASSWORD = 16
179+
TYPE_CLASS_TEXT = 1
180+
TYPE_TEXT_FLAG_AUTO_COMPLETE = 65536
181+
TYPE_TEXT_FLAG_AUTO_CORRECT = 32768
182+
TYPE_TEXT_FLAG_NO_SUGGESTIONS = 524288
183+
TYPE_TEXT_VARIATION_EMAIL_ADDRESS = 32
184+
TYPE_TEXT_VARIATION_NORMAL = 0
185+
TYPE_TEXT_VARIATION_PASSWORD = 128
186+
TYPE_TEXT_VARIATION_POSTAL_ADDRESS = 112
187+
TYPE_TEXT_VARIATION_URI = 16
188+
TYPE_CLASS_PHONE = 3
189+
190+
def show_keyboard(target, input_type):
191+
if input_type == 'text':
192+
_input_type = TYPE_CLASS_TEXT
193+
elif input_type == 'number':
194+
_input_type = TYPE_CLASS_NUMBER
195+
elif input_type == 'url':
196+
_input_type = \
197+
TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_URI
198+
elif input_type == 'mail':
199+
_input_type = \
200+
TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_EMAIL_ADDRESS
201+
elif input_type == 'datetime':
202+
_input_type = TYPE_CLASS_DATETIME
203+
elif input_type == 'tel':
204+
_input_type = TYPE_CLASS_PHONE
205+
elif input_type == 'address':
206+
_input_type = TYPE_TEXT_VARIATION_POSTAL_ADDRESS
207+
208+
if target.password:
209+
if _input_type == TYPE_CLASS_TEXT:
210+
_input_type |= TYPE_TEXT_VARIATION_PASSWORD
211+
elif _input_type == TYPE_CLASS_NUMBER:
212+
_input_type |= TYPE_NUMBER_VARIATION_PASSWORD
213+
214+
if not target.keyboard_suggestions:
215+
if _input_type == TYPE_CLASS_TEXT:
216+
_input_type = TYPE_CLASS_TEXT | \
217+
TYPE_TEXT_FLAG_NO_SUGGESTIONS
218+
219+
android_show_keyboard(_input_type)
175220

176221
def hide_keyboard():
177222
android_hide_keyboard()

recipes/android/src/android/_android_jni.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ int android_get_dpi(void) {
129129
return (*env)->CallStaticIntMethod(env, cls, mid);
130130
}
131131

132-
void android_show_keyboard(void) {
132+
void android_show_keyboard(int input_type) {
133133
static JNIEnv *env = NULL;
134134
static jclass *cls = NULL;
135135
static jmethodID mid = NULL;
@@ -139,11 +139,11 @@ void android_show_keyboard(void) {
139139
aassert(env);
140140
cls = (*env)->FindClass(env, "org/renpy/android/Hardware");
141141
aassert(cls);
142-
mid = (*env)->GetStaticMethodID(env, cls, "showKeyboard", "()V");
142+
mid = (*env)->GetStaticMethodID(env, cls, "showKeyboard", "(I)V");
143143
aassert(mid);
144144
}
145145

146-
(*env)->CallStaticVoidMethod(env, cls, mid);
146+
(*env)->CallStaticVoidMethod(env, cls, mid, (jint) input_type);
147147
}
148148

149149
void android_hide_keyboard(void) {

src/src/org/renpy/android/Hardware.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.hardware.SensorManager;
99
import android.util.DisplayMetrics;
1010
import android.view.inputmethod.InputMethodManager;
11+
import android.view.inputmethod.EditorInfo;
1112
import android.view.View;
1213

1314
import java.util.List;
@@ -21,7 +22,6 @@
2122
import android.net.NetworkInfo;
2223

2324

24-
2525
/**
2626
* Methods that are expected to be called via JNI, to access the
2727
* device's non-screen hardware. (For example, the vibration and
@@ -168,8 +168,20 @@ public static int getDPI() {
168168
/**
169169
* Show the soft keyboard.
170170
*/
171-
public static void showKeyboard() {
171+
public static void showKeyboard(int input_type) {
172+
//Log.i("python", "hardware.Java show_keyword " input_type);
173+
172174
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
175+
176+
SDLSurfaceView vw = (SDLSurfaceView) view;
177+
178+
int inputType = input_type;
179+
180+
if (vw.inputType != inputType){
181+
vw.inputType = inputType;
182+
imm.restartInput(view);
183+
}
184+
173185
imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
174186
}
175187

src/src/org/renpy/android/SDLSurfaceView.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ public interface OnInterceptTouchListener {
283283
// Have we started yet?
284284
public boolean mStarted = false;
285285

286+
// what is the textinput type while calling the keyboard
287+
public int inputType = EditorInfo.TYPE_CLASS_TEXT;
288+
286289
// Is Python ready to receive input events?
287290
static boolean mInputActivated = false;
288291

@@ -449,6 +452,12 @@ public static void setOpenFile(){
449452
}
450453
}
451454

455+
456+
public void closeSoftKeyboard(){
457+
// close the IME overlay(keyboard)
458+
Hardware.hideKeyboard();
459+
}
460+
452461
/**
453462
* Inform the view that the activity is paused. The owner of this view must
454463
* call this method when the activity is paused. Calling this method will
@@ -457,6 +466,7 @@ public static void setOpenFile(){
457466
*/
458467
public void onPause() {
459468

469+
this.closeSoftKeyboard();
460470
synchronized (this) {
461471
if (mPause == PAUSE_NONE) {
462472
mPause = PAUSE_REQUEST;
@@ -496,6 +506,7 @@ public void onResume() {
496506

497507
public void onDestroy() {
498508
Log.w(TAG, "onDestroy() called");
509+
this.closeSoftKeyboard();
499510
synchronized (this) {
500511
this.notifyAll();
501512

@@ -504,9 +515,6 @@ public void onDestroy() {
504515
return;
505516
}
506517

507-
// close the IME overlay(keyboard)
508-
InputMethodManager inputMethodManager = (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
509-
inputMethodManager.hideSoftInputFromInputMethod(this.getWindowToken(), 0);
510518

511519
// application didn't leave, give 10s before closing.
512520
// hopefully, this could be enough for launching the on_stop() trigger within the app.
@@ -1103,7 +1111,7 @@ public boolean onKeyPreIme(int keyCode, final KeyEvent event){
11031111
@Override
11041112
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
11051113
// setting inputtype to TYPE_CLASS_TEXT is necessary for swiftkey to enable
1106-
outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT;
1114+
outAttrs.inputType = inputType;
11071115
// ask IME to avoid taking full screen on landscape mode
11081116
outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
11091117
return new BaseInputConnection(this, false){

0 commit comments

Comments
 (0)