Added API to read text Android clipboard

This commit is contained in:
Sergii Pylypenko
2014-05-13 03:24:09 +03:00
parent 572506a18b
commit 9accde7d6a
4 changed files with 57 additions and 20 deletions

View File

@@ -32,33 +32,32 @@ import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import android.app.Activity;
import android.content.Context;
import java.io.File;
import java.util.concurrent.locks.ReentrantLock;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import android.os.Bundle;
import android.os.Build;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.AssetManager;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.KeyEvent;
import android.view.InputDevice;
import android.view.Window;
import android.view.WindowManager;
import android.os.Environment;
import java.io.File;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.content.res.Resources;
import android.content.res.AssetManager;
import android.widget.Toast;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.TextView;
import java.lang.Thread;
import java.util.concurrent.locks.ReentrantLock;
import android.os.Build;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import android.text.ClipboardManager;
class Mouse
@@ -617,6 +616,7 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer
public DemoRenderer(MainActivity _context)
{
context = _context;
clipboard = (android.text.ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
@@ -790,6 +790,18 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer
accelerometer.stop();
}
public String getClipboardText() // Called from native code
{
String ret = "";
try {
if( clipboard != null && clipboard.getText() != null )
ret = clipboard.getText().toString();
} catch (Exception e) {
Log.i("SDL", "getClipboardText() exception: " + e.toString());
}
return ret;
}
public void exitApp()
{
nativeDone();
@@ -900,6 +912,7 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer
private boolean mFirstTimeStart = true;
public int mWidth = 0;
public int mHeight = 0;
private ClipboardManager clipboard = null;
public static final boolean mRatelimitTouchEvents = true; //(Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO);
}

View File

@@ -141,6 +141,9 @@ extern DECLSPEC int SDLCALL SDL_IsScreenKeyboardShown(void *unused);
On OUYA: O = A, U = X, Y = Y, A = B */
extern DECLSPEC void SDLCALL SDL_ANDROID_SetGamepadKeymap(int A, int B, int X, int Y, int L1, int R1, int L2, int R2, int LThumb, int RThumb);
/* Copy contents of Android clipboard into supplied buffer */
extern DECLSPEC void SDLCALL SDL_ANDROID_GetClipboardText(char * buf, int len);
#ifdef __cplusplus
}
#endif

View File

@@ -70,6 +70,7 @@ static jmethodID JavaHideScreenKeyboard = NULL;
static jmethodID JavaIsScreenKeyboardShown = NULL;
static jmethodID JavaSetScreenKeyboardHintMessage = NULL;
static jmethodID JavaStartAccelerometerGyroscope = NULL;
static jmethodID JavaGetClipboardText = NULL;
static jmethodID JavaGetAdvertisementParams = NULL;
static jmethodID JavaSetAdvertisementVisible = NULL;
static jmethodID JavaSetAdvertisementPosition = NULL;
@@ -335,6 +336,7 @@ JAVA_EXPORT_NAME(DemoRenderer_nativeInitJavaCallbacks) ( JNIEnv* env, jobject t
JavaIsScreenKeyboardShown = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "isScreenKeyboardShown", "()I");
JavaSetScreenKeyboardHintMessage = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "setScreenKeyboardHintMessage", "(Ljava/lang/String;)V");
JavaStartAccelerometerGyroscope = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "startAccelerometerGyroscope", "(I)V");
JavaGetClipboardText = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "getClipboardText", "()Ljava/lang/String;");
JavaGetAdvertisementParams = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "getAdvertisementParams", "([I)V");
JavaSetAdvertisementVisible = (*JavaEnv)->GetMethodID(JavaEnv, JavaRendererClass, "setAdvertisementVisible", "(I)V");
@@ -402,6 +404,25 @@ JAVA_EXPORT_NAME(Settings_nativeSetVideoDepth) (JNIEnv* env, jobject thiz, jint
SDL_ANDROID_UseGles2 = UseGles2;
}
void SDLCALL SDL_ANDROID_GetClipboardText(char * buf, int len)
{
buf[0] = 0;
(*JavaEnv)->PushLocalFrame( JavaEnv, 1 );
jstring s = (jstring) (*JavaEnv)->CallObjectMethod( JavaEnv, JavaRenderer, JavaGetClipboardText );
if( s )
{
const char *c = (*JavaEnv)->GetStringUTFChars( JavaEnv, s, NULL );
if( c )
{
strncpy(buf, c, len);
buf[len-1] = 0;
(*JavaEnv)->ReleaseStringUTFChars( JavaEnv, s, c );
}
(*JavaEnv)->DeleteLocalRef( JavaEnv, s );
}
(*JavaEnv)->PopLocalFrame( JavaEnv, NULL );
}
int SDLCALL SDL_ANDROID_GetAdvertisementParams(int * visible, SDL_Rect * position)
{
jint arr[5];