Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 1 | package android.app; |
| 2 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 3 | import com.android.internal.view.IInputMethodCallback; |
| 4 | import com.android.internal.view.IInputMethodSession; |
| 5 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 6 | import android.content.Context; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 7 | import android.content.pm.ActivityInfo; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 8 | import android.content.pm.PackageManager; |
Christopher Tate | 6cce32b | 2010-07-12 18:21:36 -0700 | [diff] [blame] | 9 | import android.content.res.AssetManager; |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 10 | import android.content.res.Configuration; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 11 | import android.graphics.PixelFormat; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 12 | import android.os.Build; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 13 | import android.os.Bundle; |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 14 | import android.os.Environment; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 15 | import android.os.Looper; |
| 16 | import android.os.MessageQueue; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 17 | import android.util.AttributeSet; |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 18 | import android.view.InputChannel; |
Dianne Hackborn | 1e4b9f3 | 2010-06-23 14:10:57 -0700 | [diff] [blame] | 19 | import android.view.InputQueue; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 20 | import android.view.KeyEvent; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 21 | import android.view.Surface; |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 22 | import android.view.SurfaceHolder; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 23 | import android.view.View; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 24 | import android.view.WindowManager; |
| 25 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
| 26 | import android.view.inputmethod.InputMethodManager; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 27 | |
| 28 | import java.io.File; |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 29 | import java.lang.ref.WeakReference; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Convenience for implementing an activity that will be implemented |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 33 | * purely in native code. That is, a game (or game-like thing). There |
| 34 | * is no need to derive from this class; you can simply declare it in your |
| 35 | * manifest, and use the NDK APIs from there. |
| 36 | * |
| 37 | * <p>A typical manifest would look like: |
| 38 | * |
| 39 | * {@sample development/ndk/platforms/android-9/samples/native-activity/AndroidManifest.xml |
| 40 | * manifest} |
| 41 | * |
| 42 | * <p>A very simple example of native code that is run by NativeActivity |
| 43 | * follows. This reads input events from the user and uses OpenGLES to |
| 44 | * draw into the native activity's window. |
| 45 | * |
| 46 | * {@sample development/ndk/platforms/android-9/samples/native-activity/jni/main.c all} |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 47 | */ |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 48 | public class NativeActivity extends Activity implements SurfaceHolder.Callback2, |
| 49 | InputQueue.Callback, OnGlobalLayoutListener { |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 50 | /** |
| 51 | * Optional meta-that can be in the manifest for this component, specifying |
| 52 | * the name of the native shared library to load. If not specified, |
| 53 | * "main" is used. |
| 54 | */ |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 55 | public static final String META_DATA_LIB_NAME = "android.app.lib_name"; |
| 56 | |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 57 | /** |
| 58 | * Optional meta-that can be in the manifest for this component, specifying |
| 59 | * the name of the main entry point for this native activity in the |
| 60 | * {@link #META_DATA_LIB_NAME} native code. If not specified, |
| 61 | * "ANativeActivity_onCreate" is used. |
| 62 | */ |
| 63 | public static final String META_DATA_FUNC_NAME = "android.app.func_name"; |
| 64 | |
| 65 | private static final String KEY_NATIVE_SAVED_STATE = "android:native_state"; |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 66 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 67 | private NativeContentView mNativeContentView; |
| 68 | private InputMethodManager mIMM; |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 69 | private InputMethodCallback mInputMethodCallback; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 70 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 71 | private int mNativeHandle; |
| 72 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 73 | private InputQueue mCurInputQueue; |
| 74 | private SurfaceHolder mCurSurfaceHolder; |
| 75 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 76 | final int[] mLocation = new int[2]; |
| 77 | int mLastContentX; |
| 78 | int mLastContentY; |
| 79 | int mLastContentWidth; |
| 80 | int mLastContentHeight; |
| 81 | |
| 82 | private boolean mDispatchingUnhandledKey; |
| 83 | |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 84 | private boolean mDestroyed; |
| 85 | |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 86 | private native int loadNativeCode(String path, String funcname, MessageQueue queue, |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 87 | String internalDataPath, String obbPath, String externalDataPath, int sdkVersion, |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 88 | AssetManager assetMgr, byte[] savedState); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 89 | private native void unloadNativeCode(int handle); |
| 90 | |
| 91 | private native void onStartNative(int handle); |
| 92 | private native void onResumeNative(int handle); |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 93 | private native byte[] onSaveInstanceStateNative(int handle); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 94 | private native void onPauseNative(int handle); |
| 95 | private native void onStopNative(int handle); |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 96 | private native void onConfigurationChangedNative(int handle); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 97 | private native void onLowMemoryNative(int handle); |
| 98 | private native void onWindowFocusChangedNative(int handle, boolean focused); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 99 | private native void onSurfaceCreatedNative(int handle, Surface surface); |
| 100 | private native void onSurfaceChangedNative(int handle, Surface surface, |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 101 | int format, int width, int height); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 102 | private native void onSurfaceRedrawNeededNative(int handle, Surface surface); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 103 | private native void onSurfaceDestroyedNative(int handle); |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 104 | private native void onInputChannelCreatedNative(int handle, InputChannel channel); |
| 105 | private native void onInputChannelDestroyedNative(int handle, InputChannel channel); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 106 | private native void onContentRectChangedNative(int handle, int x, int y, int w, int h); |
| 107 | private native void dispatchKeyEventNative(int handle, KeyEvent event); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 108 | private native void finishPreDispatchKeyEventNative(int handle, int seq, boolean handled); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 109 | |
| 110 | static class NativeContentView extends View { |
| 111 | NativeActivity mActivity; |
| 112 | |
| 113 | public NativeContentView(Context context) { |
| 114 | super(context); |
| 115 | } |
| 116 | |
| 117 | public NativeContentView(Context context, AttributeSet attrs) { |
| 118 | super(context, attrs); |
| 119 | } |
| 120 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 121 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 122 | static class InputMethodCallback extends IInputMethodCallback.Stub { |
| 123 | WeakReference<NativeActivity> mNa; |
| 124 | |
| 125 | InputMethodCallback(NativeActivity na) { |
| 126 | mNa = new WeakReference<NativeActivity>(na); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void finishedEvent(int seq, boolean handled) { |
| 131 | NativeActivity na = mNa.get(); |
| 132 | if (na != null) { |
| 133 | na.finishPreDispatchKeyEventNative(na.mNativeHandle, seq, handled); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void sessionCreated(IInputMethodSession session) { |
| 139 | // Stub -- not for use in the client. |
| 140 | } |
| 141 | } |
| 142 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 143 | @Override |
| 144 | protected void onCreate(Bundle savedInstanceState) { |
| 145 | String libname = "main"; |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 146 | String funcname = "ANativeActivity_onCreate"; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 147 | ActivityInfo ai; |
| 148 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 149 | mIMM = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 150 | mInputMethodCallback = new InputMethodCallback(this); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 151 | |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 152 | getWindow().takeSurface(this); |
Dianne Hackborn | 1e4b9f3 | 2010-06-23 14:10:57 -0700 | [diff] [blame] | 153 | getWindow().takeInputQueue(this); |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 154 | getWindow().setFormat(PixelFormat.RGB_565); |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 155 | getWindow().setSoftInputMode( |
| 156 | WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED |
| 157 | | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); |
| 158 | |
| 159 | mNativeContentView = new NativeContentView(this); |
| 160 | mNativeContentView.mActivity = this; |
| 161 | setContentView(mNativeContentView); |
| 162 | mNativeContentView.requestFocus(); |
| 163 | mNativeContentView.getViewTreeObserver().addOnGlobalLayoutListener(this); |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 164 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 165 | try { |
| 166 | ai = getPackageManager().getActivityInfo( |
| 167 | getIntent().getComponent(), PackageManager.GET_META_DATA); |
| 168 | if (ai.metaData != null) { |
| 169 | String ln = ai.metaData.getString(META_DATA_LIB_NAME); |
| 170 | if (ln != null) libname = ln; |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 171 | ln = ai.metaData.getString(META_DATA_FUNC_NAME); |
| 172 | if (ln != null) funcname = ln; |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 173 | } |
| 174 | } catch (PackageManager.NameNotFoundException e) { |
| 175 | throw new RuntimeException("Error getting activity info", e); |
| 176 | } |
| 177 | |
| 178 | String path = null; |
| 179 | |
Dianne Hackborn | 74b1ed3 | 2010-10-20 10:22:45 -0700 | [diff] [blame] | 180 | File libraryFile = new File(ai.applicationInfo.nativeLibraryDir, |
| 181 | System.mapLibraryName(libname)); |
| 182 | if (libraryFile.exists()) { |
| 183 | path = libraryFile.getPath(); |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | if (path == null) { |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 187 | throw new IllegalArgumentException("Unable to find native library: " + libname); |
| 188 | } |
| 189 | |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 190 | byte[] nativeSavedState = savedInstanceState != null |
| 191 | ? savedInstanceState.getByteArray(KEY_NATIVE_SAVED_STATE) : null; |
| 192 | |
Dianne Hackborn | e21d91c6 | 2010-10-24 14:56:38 -0700 | [diff] [blame] | 193 | mNativeHandle = loadNativeCode(path, funcname, Looper.myQueue(), |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 194 | getFilesDir().toString(), getObbDir().toString(), |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 195 | Environment.getExternalStorageAppFilesDirectory(ai.packageName).toString(), |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 196 | Build.VERSION.SDK_INT, getAssets(), nativeSavedState); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 197 | |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 198 | if (mNativeHandle == 0) { |
| 199 | throw new IllegalArgumentException("Unable to load native library: " + path); |
| 200 | } |
| 201 | super.onCreate(savedInstanceState); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | protected void onDestroy() { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 206 | mDestroyed = true; |
| 207 | if (mCurSurfaceHolder != null) { |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 208 | onSurfaceDestroyedNative(mNativeHandle); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 209 | mCurSurfaceHolder = null; |
| 210 | } |
| 211 | if (mCurInputQueue != null) { |
| 212 | onInputChannelDestroyedNative(mNativeHandle, mCurInputQueue.getInputChannel()); |
| 213 | mCurInputQueue = null; |
| 214 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 215 | unloadNativeCode(mNativeHandle); |
| 216 | super.onDestroy(); |
| 217 | } |
| 218 | |
| 219 | @Override |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 220 | protected void onPause() { |
| 221 | super.onPause(); |
| 222 | onPauseNative(mNativeHandle); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | protected void onResume() { |
| 227 | super.onResume(); |
| 228 | onResumeNative(mNativeHandle); |
| 229 | } |
| 230 | |
| 231 | @Override |
| 232 | protected void onSaveInstanceState(Bundle outState) { |
| 233 | super.onSaveInstanceState(outState); |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 234 | byte[] state = onSaveInstanceStateNative(mNativeHandle); |
| 235 | if (state != null) { |
| 236 | outState.putByteArray(KEY_NATIVE_SAVED_STATE, state); |
| 237 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override |
| 241 | protected void onStart() { |
| 242 | super.onStart(); |
| 243 | onStartNative(mNativeHandle); |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | protected void onStop() { |
| 248 | super.onStop(); |
| 249 | onStopNative(mNativeHandle); |
| 250 | } |
| 251 | |
| 252 | @Override |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 253 | public void onConfigurationChanged(Configuration newConfig) { |
| 254 | super.onConfigurationChanged(newConfig); |
| 255 | if (!mDestroyed) { |
| 256 | onConfigurationChangedNative(mNativeHandle); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | @Override |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 261 | public void onLowMemory() { |
| 262 | super.onLowMemory(); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 263 | if (!mDestroyed) { |
| 264 | onLowMemoryNative(mNativeHandle); |
| 265 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 269 | public void onWindowFocusChanged(boolean hasFocus) { |
| 270 | super.onWindowFocusChanged(hasFocus); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 271 | if (!mDestroyed) { |
| 272 | onWindowFocusChangedNative(mNativeHandle, hasFocus); |
| 273 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 274 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 275 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 276 | @Override |
| 277 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 278 | if (mDispatchingUnhandledKey) { |
| 279 | return super.dispatchKeyEvent(event); |
| 280 | } else { |
| 281 | // Key events from the IME do not go through the input channel; |
| 282 | // we need to intercept them here to hand to the application. |
| 283 | dispatchKeyEventNative(mNativeHandle, event); |
| 284 | return true; |
| 285 | } |
| 286 | } |
| 287 | |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 288 | public void surfaceCreated(SurfaceHolder holder) { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 289 | if (!mDestroyed) { |
| 290 | mCurSurfaceHolder = holder; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 291 | onSurfaceCreatedNative(mNativeHandle, holder.getSurface()); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 292 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 296 | if (!mDestroyed) { |
| 297 | mCurSurfaceHolder = holder; |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 298 | onSurfaceChangedNative(mNativeHandle, holder.getSurface(), format, width, height); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 299 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 302 | public void surfaceRedrawNeeded(SurfaceHolder holder) { |
| 303 | if (!mDestroyed) { |
| 304 | mCurSurfaceHolder = holder; |
| 305 | onSurfaceRedrawNeededNative(mNativeHandle, holder.getSurface()); |
| 306 | } |
| 307 | } |
| 308 | |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 309 | public void surfaceDestroyed(SurfaceHolder holder) { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 310 | mCurSurfaceHolder = null; |
| 311 | if (!mDestroyed) { |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 312 | onSurfaceDestroyedNative(mNativeHandle); |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 313 | } |
Dianne Hackborn | 74323fd | 2010-05-18 17:56:23 -0700 | [diff] [blame] | 314 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 315 | |
Dianne Hackborn | 1e4b9f3 | 2010-06-23 14:10:57 -0700 | [diff] [blame] | 316 | public void onInputQueueCreated(InputQueue queue) { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 317 | if (!mDestroyed) { |
| 318 | mCurInputQueue = queue; |
| 319 | onInputChannelCreatedNative(mNativeHandle, queue.getInputChannel()); |
| 320 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Dianne Hackborn | 1e4b9f3 | 2010-06-23 14:10:57 -0700 | [diff] [blame] | 323 | public void onInputQueueDestroyed(InputQueue queue) { |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 324 | mCurInputQueue = null; |
| 325 | if (!mDestroyed) { |
| 326 | onInputChannelDestroyedNative(mNativeHandle, queue.getInputChannel()); |
| 327 | } |
| 328 | } |
| 329 | |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 330 | public void onGlobalLayout() { |
| 331 | mNativeContentView.getLocationInWindow(mLocation); |
| 332 | int w = mNativeContentView.getWidth(); |
| 333 | int h = mNativeContentView.getHeight(); |
| 334 | if (mLocation[0] != mLastContentX || mLocation[1] != mLastContentY |
| 335 | || w != mLastContentWidth || h != mLastContentHeight) { |
| 336 | mLastContentX = mLocation[0]; |
| 337 | mLastContentY = mLocation[1]; |
| 338 | mLastContentWidth = w; |
| 339 | mLastContentHeight = h; |
| 340 | if (!mDestroyed) { |
| 341 | onContentRectChangedNative(mNativeHandle, mLastContentX, |
| 342 | mLastContentY, mLastContentWidth, mLastContentHeight); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 347 | boolean dispatchUnhandledKeyEvent(KeyEvent event) { |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 348 | try { |
| 349 | mDispatchingUnhandledKey = true; |
| 350 | View decor = getWindow().getDecorView(); |
| 351 | if (decor != null) { |
Jeff Brown | 3915bb8 | 2010-11-05 15:02:16 -0700 | [diff] [blame] | 352 | return decor.dispatchKeyEvent(event); |
| 353 | } else { |
| 354 | return false; |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 355 | } |
| 356 | } finally { |
| 357 | mDispatchingUnhandledKey = false; |
Dianne Hackborn | 3c80a4a | 2010-06-29 19:20:40 -0700 | [diff] [blame] | 358 | } |
Dianne Hackborn | a95e4cb | 2010-06-18 18:09:33 -0700 | [diff] [blame] | 359 | } |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 360 | |
Dianne Hackborn | 2c6081c | 2010-07-15 17:44:53 -0700 | [diff] [blame] | 361 | void preDispatchKeyEvent(KeyEvent event, int seq) { |
| 362 | mIMM.dispatchKeyEvent(this, seq, event, |
| 363 | mInputMethodCallback); |
| 364 | } |
| 365 | |
Dianne Hackborn | 54a181b | 2010-06-30 18:35:14 -0700 | [diff] [blame] | 366 | void setWindowFlags(int flags, int mask) { |
| 367 | getWindow().setFlags(flags, mask); |
| 368 | } |
| 369 | |
| 370 | void setWindowFormat(int format) { |
| 371 | getWindow().setFormat(format); |
| 372 | } |
Dianne Hackborn | d76b67c | 2010-07-13 17:48:30 -0700 | [diff] [blame] | 373 | |
| 374 | void showIme(int mode) { |
| 375 | mIMM.showSoftInput(mNativeContentView, mode); |
| 376 | } |
| 377 | |
| 378 | void hideIme(int mode) { |
| 379 | mIMM.hideSoftInputFromWindow(mNativeContentView.getWindowToken(), mode); |
| 380 | } |
Dianne Hackborn | 69969e4 | 2010-05-04 11:40:40 -0700 | [diff] [blame] | 381 | } |