blob: b6e0467154c067b037245075990aedb13334ed4f [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
Dmitriy Ivanov048a0db2015-11-15 14:58:36 -080038import dalvik.system.BaseDexClassLoader;
39
Dianne Hackborn69969e42010-05-04 11:40:40 -070040import java.io.File;
41
42/**
43 * Convenience for implementing an activity that will be implemented
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -070044 * purely in native code. That is, a game (or game-like thing). There
45 * is no need to derive from this class; you can simply declare it in your
46 * manifest, and use the NDK APIs from there.
47 *
48 * <p>A typical manifest would look like:
49 *
50 * {@sample development/ndk/platforms/android-9/samples/native-activity/AndroidManifest.xml
51 * manifest}
52 *
53 * <p>A very simple example of native code that is run by NativeActivity
54 * follows. This reads input events from the user and uses OpenGLES to
55 * draw into the native activity's window.
56 *
57 * {@sample development/ndk/platforms/android-9/samples/native-activity/jni/main.c all}
Dianne Hackborn69969e42010-05-04 11:40:40 -070058 */
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070059public class NativeActivity extends Activity implements SurfaceHolder.Callback2,
60 InputQueue.Callback, OnGlobalLayoutListener {
Dianne Hackborne21d91c62010-10-24 14:56:38 -070061 /**
62 * Optional meta-that can be in the manifest for this component, specifying
63 * the name of the native shared library to load. If not specified,
64 * "main" is used.
65 */
Dianne Hackborn69969e42010-05-04 11:40:40 -070066 public static final String META_DATA_LIB_NAME = "android.app.lib_name";
67
Dianne Hackborne21d91c62010-10-24 14:56:38 -070068 /**
69 * Optional meta-that can be in the manifest for this component, specifying
70 * the name of the main entry point for this native activity in the
71 * {@link #META_DATA_LIB_NAME} native code. If not specified,
72 * "ANativeActivity_onCreate" is used.
73 */
74 public static final String META_DATA_FUNC_NAME = "android.app.func_name";
75
76 private static final String KEY_NATIVE_SAVED_STATE = "android:native_state";
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -070077
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070078 private NativeContentView mNativeContentView;
79 private InputMethodManager mIMM;
80
Ashok Bhat58b8b242014-01-02 16:52:41 +000081 private long mNativeHandle;
Dianne Hackborn69969e42010-05-04 11:40:40 -070082
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070083 private InputQueue mCurInputQueue;
84 private SurfaceHolder mCurSurfaceHolder;
85
Dianne Hackbornd76b67c2010-07-13 17:48:30 -070086 final int[] mLocation = new int[2];
87 int mLastContentX;
88 int mLastContentY;
89 int mLastContentWidth;
90 int mLastContentHeight;
91
92 private boolean mDispatchingUnhandledKey;
93
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -070094 private boolean mDestroyed;
95
Ashok Bhat58b8b242014-01-02 16:52:41 +000096 private native long loadNativeCode(String path, String funcname, MessageQueue queue,
Dianne Hackborn805fd7e2011-01-16 18:30:29 -080097 String internalDataPath, String obbPath, String externalDataPath, int sdkVersion,
Dimitry Ivanova55c7f12016-02-23 14:25:50 -080098 AssetManager assetMgr, byte[] savedState, ClassLoader classLoader, String libraryPath);
Dmitriy Ivanov048a0db2015-11-15 14:58:36 -080099 private native String getDlError();
Ashok Bhat58b8b242014-01-02 16:52:41 +0000100 private native void unloadNativeCode(long handle);
101 private native void onStartNative(long handle);
102 private native void onResumeNative(long handle);
103 private native byte[] onSaveInstanceStateNative(long handle);
104 private native void onPauseNative(long handle);
105 private native void onStopNative(long handle);
106 private native void onConfigurationChangedNative(long handle);
107 private native void onLowMemoryNative(long handle);
108 private native void onWindowFocusChangedNative(long handle, boolean focused);
109 private native void onSurfaceCreatedNative(long handle, Surface surface);
110 private native void onSurfaceChangedNative(long handle, Surface surface,
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700111 int format, int width, int height);
Ashok Bhat58b8b242014-01-02 16:52:41 +0000112 private native void onSurfaceRedrawNeededNative(long handle, Surface surface);
113 private native void onSurfaceDestroyedNative(long handle);
114 private native void onInputQueueCreatedNative(long handle, long queuePtr);
115 private native void onInputQueueDestroyedNative(long handle, long queuePtr);
116 private native void onContentRectChangedNative(long handle, int x, int y, int w, int h);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700117
118 static class NativeContentView extends View {
119 NativeActivity mActivity;
120
121 public NativeContentView(Context context) {
122 super(context);
123 }
124
125 public NativeContentView(Context context, AttributeSet attrs) {
126 super(context, attrs);
127 }
128 }
Dianne Hackborn2c6081c2010-07-15 17:44:53 -0700129
Dianne Hackborn69969e42010-05-04 11:40:40 -0700130 @Override
131 protected void onCreate(Bundle savedInstanceState) {
132 String libname = "main";
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700133 String funcname = "ANativeActivity_onCreate";
Dianne Hackborn69969e42010-05-04 11:40:40 -0700134 ActivityInfo ai;
Yohei Yukawa777ef952015-11-25 20:32:24 -0800135
136 mIMM = getSystemService(InputMethodManager.class);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700137
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700138 getWindow().takeSurface(this);
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700139 getWindow().takeInputQueue(this);
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700140 getWindow().setFormat(PixelFormat.RGB_565);
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700141 getWindow().setSoftInputMode(
142 WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED
143 | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
144
145 mNativeContentView = new NativeContentView(this);
146 mNativeContentView.mActivity = this;
147 setContentView(mNativeContentView);
148 mNativeContentView.requestFocus();
149 mNativeContentView.getViewTreeObserver().addOnGlobalLayoutListener(this);
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700150
Dianne Hackborn69969e42010-05-04 11:40:40 -0700151 try {
152 ai = getPackageManager().getActivityInfo(
153 getIntent().getComponent(), PackageManager.GET_META_DATA);
154 if (ai.metaData != null) {
155 String ln = ai.metaData.getString(META_DATA_LIB_NAME);
156 if (ln != null) libname = ln;
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700157 ln = ai.metaData.getString(META_DATA_FUNC_NAME);
158 if (ln != null) funcname = ln;
Dianne Hackborn69969e42010-05-04 11:40:40 -0700159 }
160 } catch (PackageManager.NameNotFoundException e) {
161 throw new RuntimeException("Error getting activity info", e);
162 }
Dmitriy Ivanov048a0db2015-11-15 14:58:36 -0800163
164 BaseDexClassLoader classLoader = (BaseDexClassLoader) getClassLoader();
165 String path = classLoader.findLibrary(libname);
166
Dianne Hackborn69969e42010-05-04 11:40:40 -0700167 if (path == null) {
Dimitry Ivanovb9c90262016-02-19 14:09:20 -0800168 throw new IllegalArgumentException("Unable to find native library " + libname +
169 " using classloader: " + classLoader.toString());
Dianne Hackborn69969e42010-05-04 11:40:40 -0700170 }
171
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700172 byte[] nativeSavedState = savedInstanceState != null
173 ? savedInstanceState.getByteArray(KEY_NATIVE_SAVED_STATE) : null;
174
Dianne Hackborne21d91c62010-10-24 14:56:38 -0700175 mNativeHandle = loadNativeCode(path, funcname, Looper.myQueue(),
Jeff Sharkeye0475c82013-08-15 11:50:02 -0700176 getAbsolutePath(getFilesDir()), getAbsolutePath(getObbDir()),
177 getAbsolutePath(getExternalFilesDir(null)),
Dmitriy Ivanov048a0db2015-11-15 14:58:36 -0800178 Build.VERSION.SDK_INT, getAssets(), nativeSavedState,
Dimitry Ivanova55c7f12016-02-23 14:25:50 -0800179 classLoader, classLoader.getLdLibraryPath());
Jeff Sharkeye0475c82013-08-15 11:50:02 -0700180
Dianne Hackborn69969e42010-05-04 11:40:40 -0700181 if (mNativeHandle == 0) {
Dmitriy Ivanov048a0db2015-11-15 14:58:36 -0800182 throw new UnsatisfiedLinkError(
183 "Unable to load native library \"" + path + "\": " + getDlError());
Dianne Hackborn69969e42010-05-04 11:40:40 -0700184 }
185 super.onCreate(savedInstanceState);
186 }
187
Jeff Sharkeye0475c82013-08-15 11:50:02 -0700188 private static String getAbsolutePath(File file) {
189 return (file != null) ? file.getAbsolutePath() : null;
190 }
191
Dianne Hackborn69969e42010-05-04 11:40:40 -0700192 @Override
193 protected void onDestroy() {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700194 mDestroyed = true;
195 if (mCurSurfaceHolder != null) {
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700196 onSurfaceDestroyedNative(mNativeHandle);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700197 mCurSurfaceHolder = null;
198 }
199 if (mCurInputQueue != null) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700200 onInputQueueDestroyedNative(mNativeHandle, mCurInputQueue.getNativePtr());
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700201 mCurInputQueue = null;
202 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700203 unloadNativeCode(mNativeHandle);
204 super.onDestroy();
205 }
206
207 @Override
Dianne Hackborn69969e42010-05-04 11:40:40 -0700208 protected void onPause() {
209 super.onPause();
210 onPauseNative(mNativeHandle);
211 }
212
213 @Override
214 protected void onResume() {
215 super.onResume();
216 onResumeNative(mNativeHandle);
217 }
218
219 @Override
220 protected void onSaveInstanceState(Bundle outState) {
221 super.onSaveInstanceState(outState);
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700222 byte[] state = onSaveInstanceStateNative(mNativeHandle);
223 if (state != null) {
224 outState.putByteArray(KEY_NATIVE_SAVED_STATE, state);
225 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700226 }
227
228 @Override
229 protected void onStart() {
230 super.onStart();
231 onStartNative(mNativeHandle);
232 }
233
234 @Override
235 protected void onStop() {
236 super.onStop();
237 onStopNative(mNativeHandle);
238 }
239
240 @Override
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700241 public void onConfigurationChanged(Configuration newConfig) {
242 super.onConfigurationChanged(newConfig);
243 if (!mDestroyed) {
244 onConfigurationChangedNative(mNativeHandle);
245 }
246 }
247
248 @Override
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700249 public void onLowMemory() {
250 super.onLowMemory();
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700251 if (!mDestroyed) {
252 onLowMemoryNative(mNativeHandle);
253 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700254 }
255
256 @Override
Dianne Hackborn69969e42010-05-04 11:40:40 -0700257 public void onWindowFocusChanged(boolean hasFocus) {
258 super.onWindowFocusChanged(hasFocus);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700259 if (!mDestroyed) {
260 onWindowFocusChangedNative(mNativeHandle, hasFocus);
261 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700262 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700263
264 public void surfaceCreated(SurfaceHolder holder) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700265 if (!mDestroyed) {
266 mCurSurfaceHolder = holder;
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700267 onSurfaceCreatedNative(mNativeHandle, holder.getSurface());
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700268 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700269 }
270
271 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700272 if (!mDestroyed) {
273 mCurSurfaceHolder = holder;
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700274 onSurfaceChangedNative(mNativeHandle, holder.getSurface(), format, width, height);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700275 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700276 }
277
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700278 public void surfaceRedrawNeeded(SurfaceHolder holder) {
279 if (!mDestroyed) {
280 mCurSurfaceHolder = holder;
281 onSurfaceRedrawNeededNative(mNativeHandle, holder.getSurface());
282 }
283 }
284
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700285 public void surfaceDestroyed(SurfaceHolder holder) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700286 mCurSurfaceHolder = null;
287 if (!mDestroyed) {
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700288 onSurfaceDestroyedNative(mNativeHandle);
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700289 }
Dianne Hackborn74323fd2010-05-18 17:56:23 -0700290 }
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700291
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700292 public void onInputQueueCreated(InputQueue queue) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700293 if (!mDestroyed) {
294 mCurInputQueue = queue;
Michael Wrighta44dd262013-04-10 21:12:00 -0700295 onInputQueueCreatedNative(mNativeHandle, queue.getNativePtr());
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700296 }
Dianne Hackborna95e4cb2010-06-18 18:09:33 -0700297 }
298
Dianne Hackborn1e4b9f32010-06-23 14:10:57 -0700299 public void onInputQueueDestroyed(InputQueue queue) {
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700300 if (!mDestroyed) {
Michael Wrighta44dd262013-04-10 21:12:00 -0700301 onInputQueueDestroyedNative(mNativeHandle, queue.getNativePtr());
302 mCurInputQueue = null;
Dianne Hackborn3c80a4a2010-06-29 19:20:40 -0700303 }
304 }
305
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700306 public void onGlobalLayout() {
307 mNativeContentView.getLocationInWindow(mLocation);
308 int w = mNativeContentView.getWidth();
309 int h = mNativeContentView.getHeight();
310 if (mLocation[0] != mLastContentX || mLocation[1] != mLastContentY
311 || w != mLastContentWidth || h != mLastContentHeight) {
312 mLastContentX = mLocation[0];
313 mLastContentY = mLocation[1];
314 mLastContentWidth = w;
315 mLastContentHeight = h;
316 if (!mDestroyed) {
317 onContentRectChangedNative(mNativeHandle, mLastContentX,
318 mLastContentY, mLastContentWidth, mLastContentHeight);
319 }
320 }
321 }
322
Dianne Hackborn54a181b2010-06-30 18:35:14 -0700323 void setWindowFlags(int flags, int mask) {
324 getWindow().setFlags(flags, mask);
325 }
326
327 void setWindowFormat(int format) {
328 getWindow().setFormat(format);
329 }
Dianne Hackbornd76b67c2010-07-13 17:48:30 -0700330
331 void showIme(int mode) {
332 mIMM.showSoftInput(mNativeContentView, mode);
333 }
334
335 void hideIme(int mode) {
336 mIMM.hideSoftInputFromWindow(mNativeContentView.getWindowToken(), mode);
337 }
Dianne Hackborn69969e42010-05-04 11:40:40 -0700338}