blob: 1370000fe092563cf80a431c79fde93573cdd351 [file] [log] [blame]
Jeff Brownf9e989d2013-04-04 23:04:03 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jeff Sharkey1abdb712013-08-11 16:28:14 -070016
Dianne Hackborn69969e42010-05-04 11:40:40 -070017package android.app;
18
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070019import android.content.Context;
Dianne Hackborn69969e42010-05-04 11:40:40 -070020import android.content.pm.ActivityInfo;
Dianne Hackborn69969e42010-05-04 11:40:40 -070021import android.content.pm.PackageManager;
Christopher Tate6cce32b2010-07-12 18:21:36 -070022import android.content.res.AssetManager;
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -070023import android.content.res.Configuration;
Dianne Hackborn54a181b2010-06-30 18:35:14 -070024import android.graphics.PixelFormat;
Dianne Hackborn68267412010-07-02 18:52:01 -070025import android.os.Build;
Dianne Hackborn69969e42010-05-04 11:40:40 -070026import android.os.Bundle;
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070027import android.os.Looper;
28import android.os.MessageQueue;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070029import android.util.AttributeSet;
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -070030import android.view.InputQueue;
Dianne Hackborn54a181b2010-06-30 18:35:14 -070031import android.view.Surface;
Dianne Hackborn74323fd2010-05-18 17:56:23 -070032import android.view.SurfaceHolder;
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070033import android.view.View;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070034import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Jeff Sharkey1abdb712013-08-11 16:28:14 -070035import android.view.WindowManager;
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070036import android.view.inputmethod.InputMethodManager;
Dianne Hackborn69969e42010-05-04 11:40:40 -070037
38import java.io.File;
39
40/**
41 * Convenience for implementing an activity that will be implemented
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -070042 * purely in native code. That is, a game (or game-like thing). There
43 * is no need to derive from this class; you can simply declare it in your
44 * manifest, and use the NDK APIs from there.
45 *
46 * <p>A typical manifest would look like:
47 *
48 * {@sample development/ndk/platforms/android-9/samples/native-activity/AndroidManifest.xml
49 * manifest}
50 *
51 * <p>A very simple example of native code that is run by NativeActivity
52 * follows. This reads input events from the user and uses OpenGLES to
53 * draw into the native activity's window.
54 *
55 * {@sample development/ndk/platforms/android-9/samples/native-activity/jni/main.c all}
Dianne Hackborn69969e42010-05-04 11:40:40 -070056 */
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070057public class NativeActivity extends Activity implements SurfaceHolder.Callback2,
58 InputQueue.Callback, OnGlobalLayoutListener {
Dianne Hackborne21d91c62010-10-24 14:56:38 -070059 /**
60 * Optional meta-that can be in the manifest for this component, specifying
61 * the name of the native shared library to load. If not specified,
62 * "main" is used.
63 */
Dianne Hackborn69969e42010-05-04 11:40:40 -070064 public static final String META_DATA_LIB_NAME = "android.app.lib_name";
65
Dianne Hackborne21d91c62010-10-24 14:56:38 -070066 /**
67 * Optional meta-that can be in the manifest for this component, specifying
68 * the name of the main entry point for this native activity in the
69 * {@link #META_DATA_LIB_NAME} native code. If not specified,
70 * "ANativeActivity_onCreate" is used.
71 */
72 public static final String META_DATA_FUNC_NAME = "android.app.func_name";
73
74 private static final String KEY_NATIVE_SAVED_STATE = "android:native_state";
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -070075
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070076 private NativeContentView mNativeContentView;
77 private InputMethodManager mIMM;
78
Ashok Bhat58b8b242014-01-02 16:52:41 +000079 private long mNativeHandle;
Dianne Hackborn69969e42010-05-04 11:40:40 -070080
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070081 private InputQueue mCurInputQueue;
82 private SurfaceHolder mCurSurfaceHolder;
83
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070084 final int[] mLocation = new int[2];
85 int mLastContentX;
86 int mLastContentY;
87 int mLastContentWidth;
88 int mLastContentHeight;
89
90 private boolean mDispatchingUnhandledKey;
91
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070092 private boolean mDestroyed;
93
Ashok Bhat58b8b242014-01-02 16:52:41 +000094 private native long loadNativeCode(String path, String funcname, MessageQueue queue,
Dianne Hackborn805fd7e2011-01-16 18:30:29 -080095 String internalDataPath, String obbPath, String externalDataPath, int sdkVersion,
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -070096 AssetManager assetMgr, byte[] savedState);
Ashok Bhat58b8b242014-01-02 16:52:41 +000097 private native void unloadNativeCode(long handle);
98 private native void onStartNative(long handle);
99 private native void onResumeNative(long handle);
100 private native byte[] onSaveInstanceStateNative(long handle);
101 private native void onPauseNative(long handle);
102 private native void onStopNative(long handle);
103 private native void onConfigurationChangedNative(long handle);
104 private native void onLowMemoryNative(long handle);
105 private native void onWindowFocusChangedNative(long handle, boolean focused);
106 private native void onSurfaceCreatedNative(long handle, Surface surface);
107 private native void onSurfaceChangedNative(long handle, Surface surface,
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700108 int format, int width, int height);
Ashok Bhat58b8b242014-01-02 16:52:41 +0000109 private native void onSurfaceRedrawNeededNative(long handle, Surface surface);
110 private native void onSurfaceDestroyedNative(long handle);
111 private native void onInputQueueCreatedNative(long handle, long queuePtr);
112 private native void onInputQueueDestroyedNative(long handle, long queuePtr);
113 private native void onContentRectChangedNative(long handle, int x, int y, int w, int h);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700114
115 static class NativeContentView extends View {
116 NativeActivity mActivity;
117
118 public NativeContentView(Context context) {
119 super(context);
120 }
121
122 public NativeContentView(Context context, AttributeSet attrs) {
123 super(context, attrs);
124 }
125 }
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700126
Dianne Hackborn69969e42010-05-04 11:40:40 -0700127 @Override
128 protected void onCreate(Bundle savedInstanceState) {
129 String libname = "main";
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700130 String funcname = "ANativeActivity_onCreate";
Dianne Hackborn69969e42010-05-04 11:40:40 -0700131 ActivityInfo ai;
Yohei Yukawa777ef952015-11-25 20:32:24 -0800132
133 mIMM = getSystemService(InputMethodManager.class);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700134
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700135 getWindow().takeSurface(this);
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700136 getWindow().takeInputQueue(this);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700137 getWindow().setFormat(PixelFormat.RGB_565);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700138 getWindow().setSoftInputMode(
139 WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED
140 | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
141
142 mNativeContentView = new NativeContentView(this);
143 mNativeContentView.mActivity = this;
144 setContentView(mNativeContentView);
145 mNativeContentView.requestFocus();
146 mNativeContentView.getViewTreeObserver().addOnGlobalLayoutListener(this);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700147
Dianne Hackborn69969e42010-05-04 11:40:40 -0700148 try {
149 ai = getPackageManager().getActivityInfo(
150 getIntent().getComponent(), PackageManager.GET_META_DATA);
151 if (ai.metaData != null) {
152 String ln = ai.metaData.getString(META_DATA_LIB_NAME);
153 if (ln != null) libname = ln;
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700154 ln = ai.metaData.getString(META_DATA_FUNC_NAME);
155 if (ln != null) funcname = ln;
Dianne Hackborn69969e42010-05-04 11:40:40 -0700156 }
157 } catch (PackageManager.NameNotFoundException e) {
158 throw new RuntimeException("Error getting activity info", e);
159 }
160
161 String path = null;
162
Dianne Hackborn74b1ed32010-10-20 10:22:45 -0700163 File libraryFile = new File(ai.applicationInfo.nativeLibraryDir,
164 System.mapLibraryName(libname));
165 if (libraryFile.exists()) {
166 path = libraryFile.getPath();
Dianne Hackborn69969e42010-05-04 11:40:40 -0700167 }
168
169 if (path == null) {
Dianne Hackborn69969e42010-05-04 11:40:40 -0700170 throw new IllegalArgumentException("Unable to find native library: " + libname);
171 }
172
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700173 byte[] nativeSavedState = savedInstanceState != null
174 ? savedInstanceState.getByteArray(KEY_NATIVE_SAVED_STATE) : null;
175
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700176 mNativeHandle = loadNativeCode(path, funcname, Looper.myQueue(),
Jeff Sharkeye0475c82013-08-15 11:50:02 -0700177 getAbsolutePath(getFilesDir()), getAbsolutePath(getObbDir()),
178 getAbsolutePath(getExternalFilesDir(null)),
179 Build.VERSION.SDK_INT, getAssets(), nativeSavedState);
180
Dianne Hackborn69969e42010-05-04 11:40:40 -0700181 if (mNativeHandle == 0) {
182 throw new IllegalArgumentException("Unable to load native library: " + path);
183 }
184 super.onCreate(savedInstanceState);
185 }
186
Jeff Sharkeye0475c82013-08-15 11:50:02 -0700187 private static String getAbsolutePath(File file) {
188 return (file != null) ? file.getAbsolutePath() : null;
189 }
190
Dianne Hackborn69969e42010-05-04 11:40:40 -0700191 @Override
192 protected void onDestroy() {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700193 mDestroyed = true;
194 if (mCurSurfaceHolder != null) {
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700195 onSurfaceDestroyedNative(mNativeHandle);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700196 mCurSurfaceHolder = null;
197 }
198 if (mCurInputQueue != null) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700199 onInputQueueDestroyedNative(mNativeHandle, mCurInputQueue.getNativePtr());
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700200 mCurInputQueue = null;
201 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700202 unloadNativeCode(mNativeHandle);
203 super.onDestroy();
204 }
205
206 @Override
Dianne Hackborn69969e42010-05-04 11:40:40 -0700207 protected void onPause() {
208 super.onPause();
209 onPauseNative(mNativeHandle);
210 }
211
212 @Override
213 protected void onResume() {
214 super.onResume();
215 onResumeNative(mNativeHandle);
216 }
217
218 @Override
219 protected void onSaveInstanceState(Bundle outState) {
220 super.onSaveInstanceState(outState);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700221 byte[] state = onSaveInstanceStateNative(mNativeHandle);
222 if (state != null) {
223 outState.putByteArray(KEY_NATIVE_SAVED_STATE, state);
224 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700225 }
226
227 @Override
228 protected void onStart() {
229 super.onStart();
230 onStartNative(mNativeHandle);
231 }
232
233 @Override
234 protected void onStop() {
235 super.onStop();
236 onStopNative(mNativeHandle);
237 }
238
239 @Override
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700240 public void onConfigurationChanged(Configuration newConfig) {
241 super.onConfigurationChanged(newConfig);
242 if (!mDestroyed) {
243 onConfigurationChangedNative(mNativeHandle);
244 }
245 }
246
247 @Override
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700248 public void onLowMemory() {
249 super.onLowMemory();
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700250 if (!mDestroyed) {
251 onLowMemoryNative(mNativeHandle);
252 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700253 }
254
255 @Override
Dianne Hackborn69969e42010-05-04 11:40:40 -0700256 public void onWindowFocusChanged(boolean hasFocus) {
257 super.onWindowFocusChanged(hasFocus);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700258 if (!mDestroyed) {
259 onWindowFocusChangedNative(mNativeHandle, hasFocus);
260 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700261 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700262
263 public void surfaceCreated(SurfaceHolder holder) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700264 if (!mDestroyed) {
265 mCurSurfaceHolder = holder;
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700266 onSurfaceCreatedNative(mNativeHandle, holder.getSurface());
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700267 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700268 }
269
270 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700271 if (!mDestroyed) {
272 mCurSurfaceHolder = holder;
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700273 onSurfaceChangedNative(mNativeHandle, holder.getSurface(), format, width, height);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700274 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700275 }
276
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700277 public void surfaceRedrawNeeded(SurfaceHolder holder) {
278 if (!mDestroyed) {
279 mCurSurfaceHolder = holder;
280 onSurfaceRedrawNeededNative(mNativeHandle, holder.getSurface());
281 }
282 }
283
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700284 public void surfaceDestroyed(SurfaceHolder holder) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700285 mCurSurfaceHolder = null;
286 if (!mDestroyed) {
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700287 onSurfaceDestroyedNative(mNativeHandle);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700288 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700289 }
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700290
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700291 public void onInputQueueCreated(InputQueue queue) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700292 if (!mDestroyed) {
293 mCurInputQueue = queue;
Michael Wrighta44dd262013-04-10 21:12:00 -0700294 onInputQueueCreatedNative(mNativeHandle, queue.getNativePtr());
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700295 }
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700296 }
297
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700298 public void onInputQueueDestroyed(InputQueue queue) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700299 if (!mDestroyed) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700300 onInputQueueDestroyedNative(mNativeHandle, queue.getNativePtr());
301 mCurInputQueue = null;
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700302 }
303 }
304
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700305 public void onGlobalLayout() {
306 mNativeContentView.getLocationInWindow(mLocation);
307 int w = mNativeContentView.getWidth();
308 int h = mNativeContentView.getHeight();
309 if (mLocation[0] != mLastContentX || mLocation[1] != mLastContentY
310 || w != mLastContentWidth || h != mLastContentHeight) {
311 mLastContentX = mLocation[0];
312 mLastContentY = mLocation[1];
313 mLastContentWidth = w;
314 mLastContentHeight = h;
315 if (!mDestroyed) {
316 onContentRectChangedNative(mNativeHandle, mLastContentX,
317 mLastContentY, mLastContentWidth, mLastContentHeight);
318 }
319 }
320 }
321
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700322 void setWindowFlags(int flags, int mask) {
323 getWindow().setFlags(flags, mask);
324 }
325
326 void setWindowFormat(int format) {
327 getWindow().setFormat(format);
328 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700329
330 void showIme(int mode) {
331 mIMM.showSoftInput(mNativeContentView, mode);
332 }
333
334 void hideIme(int mode) {
335 mIMM.hideSoftInputFromWindow(mNativeContentView.getWindowToken(), mode);
336 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700337}