| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- /**
- * PANDA 3D SOFTWARE
- * Copyright (c) Carnegie Mellon University. All rights reserved.
- *
- * All use of this software is subject to the terms of the revised BSD
- * license. You should have received a copy of this license along
- * with this source code in a file named "LICENSE."
- *
- * @file PandaActivity.java
- * @author rdb
- * @date 2013-01-22
- */
- package org.panda3d.android;
- import android.app.NativeActivity;
- import android.content.Intent;
- import android.content.pm.ActivityInfo;
- import android.content.pm.PackageManager;
- import android.content.res.AssetFileDescriptor;
- import android.net.Uri;
- import android.os.ParcelFileDescriptor;
- import android.widget.Toast;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import dalvik.system.BaseDexClassLoader;
- import org.panda3d.android.NativeIStream;
- import org.panda3d.android.NativeOStream;
- import android.util.Log;
- /**
- * The entry point for a Panda-based activity. Loads the Panda libraries and
- * also provides some utility functions.
- */
- public class PandaActivity extends NativeActivity {
- private static final Bitmap.Config sConfigs[] = {
- null,
- Bitmap.Config.ALPHA_8,
- null,
- Bitmap.Config.RGB_565,
- Bitmap.Config.ARGB_4444,
- Bitmap.Config.ARGB_8888,
- null, //Bitmap.Config.RGBA_F16,
- null, //Bitmap.Config.HARDWARE,
- };
- private static final Bitmap.CompressFormat sFormats[] = {
- Bitmap.CompressFormat.JPEG,
- Bitmap.CompressFormat.PNG,
- Bitmap.CompressFormat.WEBP,
- };
- protected static BitmapFactory.Options readBitmapSize(long istreamPtr) {
- BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- options.inScaled = false;
- NativeIStream stream = new NativeIStream(istreamPtr);
- BitmapFactory.decodeStream(stream, null, options);
- return options;
- }
- protected static Bitmap readBitmap(long istreamPtr, int sampleSize) {
- BitmapFactory.Options options = new BitmapFactory.Options();
- // options.inPreferredConfig = Bitmap.Config.RGBA_8888;
- options.inScaled = false;
- options.inSampleSize = sampleSize;
- NativeIStream stream = new NativeIStream(istreamPtr);
- return BitmapFactory.decodeStream(stream, null, options);
- }
- protected static Bitmap createBitmap(int width, int height, int config, boolean hasAlpha) {
- return Bitmap.createBitmap(width, height, sConfigs[config]);
- }
- protected static boolean compressBitmap(Bitmap bitmap, int format, int quality, long ostreamPtr) {
- NativeOStream stream = new NativeOStream(ostreamPtr);
- return bitmap.compress(sFormats[format], quality, stream);
- }
- protected static String getCurrentThreadName() {
- return Thread.currentThread().getName();
- }
- /**
- * Maps the blob to memory and returns the pointer.
- */
- public long mapBlobFromResource(long offset) {
- int resourceId = 0;
- try {
- ActivityInfo ai = getPackageManager().getActivityInfo(
- getIntent().getComponent(), PackageManager.GET_META_DATA);
- if (ai.metaData == null) {
- Log.e("Panda3D", "Failed to get activity metadata");
- return 0;
- }
- resourceId = ai.metaData.getInt("org.panda3d.android.BLOB_RESOURCE");
- if (resourceId == 0) {
- return 0;
- }
- AssetFileDescriptor afd = getResources().openRawResourceFd(resourceId);
- ParcelFileDescriptor pfd = afd.getParcelFileDescriptor();
- long off = afd.getStartOffset() + offset;
- long len = afd.getLength();
- return nativeMmap(pfd.getFd(), off, len);
- } catch (Exception e) {
- Log.e("Panda3D", "Received exception while trying to map blob: " + e);
- return 0;
- }
- }
- /**
- * Returns the path to the main native library.
- */
- public String getNativeLibraryPath() {
- String libname = "main";
- try {
- ActivityInfo ai = getPackageManager().getActivityInfo(
- getIntent().getComponent(), PackageManager.GET_META_DATA);
- if (ai.metaData != null) {
- String ln = ai.metaData.getString(META_DATA_LIB_NAME);
- if (ln != null) libname = ln;
- }
- } catch (PackageManager.NameNotFoundException e) {
- throw new RuntimeException("Error getting activity info", e);
- }
- BaseDexClassLoader classLoader = (BaseDexClassLoader) getClassLoader();
- return classLoader.findLibrary(libname);
- }
- /**
- * Returns the path to some other native library.
- */
- public String findLibrary(String libname) {
- BaseDexClassLoader classLoader = (BaseDexClassLoader)getClassLoader();
- return classLoader.findLibrary(libname);
- }
- public String getIntentDataPath() {
- Intent intent = getIntent();
- Uri data = intent.getData();
- if (data == null) {
- return null;
- }
- String path = data.getPath();
- if (path.startsWith("//")) {
- path = path.substring(1);
- }
- return path;
- }
- public String getIntentOutputUri() {
- Intent intent = getIntent();
- return intent.getStringExtra("org.panda3d.OUTPUT_URI");
- }
- public String getCacheDirString() {
- return getCacheDir().toString();
- }
- /**
- * Shows a pop-up notification.
- */
- public void showToast(final String text, final int duration) {
- final PandaActivity activity = this;
- runOnUiThread(new Runnable() {
- public void run() {
- Toast toast = Toast.makeText(activity, text, duration);
- toast.show();
- }
- });
- }
- static {
- // Load this explicitly to initialize the JVM with the thread system.
- System.loadLibrary("panda");
- // Contains our JNI calls.
- System.loadLibrary("p3android");
- }
- private static native long nativeMmap(int fd, long off, long len);
- }
|