PandaActivity.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file PandaActivity.java
  10. * @author rdb
  11. * @date 2013-01-22
  12. */
  13. package org.panda3d.android;
  14. import android.app.NativeActivity;
  15. import android.content.Intent;
  16. import android.content.pm.ActivityInfo;
  17. import android.content.pm.PackageManager;
  18. import android.content.res.AssetFileDescriptor;
  19. import android.net.Uri;
  20. import android.os.ParcelFileDescriptor;
  21. import android.widget.Toast;
  22. import android.graphics.Bitmap;
  23. import android.graphics.BitmapFactory;
  24. import dalvik.system.BaseDexClassLoader;
  25. import org.panda3d.android.NativeIStream;
  26. import org.panda3d.android.NativeOStream;
  27. import android.util.Log;
  28. /**
  29. * The entry point for a Panda-based activity. Loads the Panda libraries and
  30. * also provides some utility functions.
  31. */
  32. public class PandaActivity extends NativeActivity {
  33. private static final Bitmap.Config sConfigs[] = {
  34. null,
  35. Bitmap.Config.ALPHA_8,
  36. null,
  37. Bitmap.Config.RGB_565,
  38. Bitmap.Config.ARGB_4444,
  39. Bitmap.Config.ARGB_8888,
  40. null, //Bitmap.Config.RGBA_F16,
  41. null, //Bitmap.Config.HARDWARE,
  42. };
  43. private static final Bitmap.CompressFormat sFormats[] = {
  44. Bitmap.CompressFormat.JPEG,
  45. Bitmap.CompressFormat.PNG,
  46. Bitmap.CompressFormat.WEBP,
  47. };
  48. protected static BitmapFactory.Options readBitmapSize(long istreamPtr) {
  49. BitmapFactory.Options options = new BitmapFactory.Options();
  50. options.inJustDecodeBounds = true;
  51. options.inScaled = false;
  52. NativeIStream stream = new NativeIStream(istreamPtr);
  53. BitmapFactory.decodeStream(stream, null, options);
  54. return options;
  55. }
  56. protected static Bitmap readBitmap(long istreamPtr, int sampleSize) {
  57. BitmapFactory.Options options = new BitmapFactory.Options();
  58. // options.inPreferredConfig = Bitmap.Config.RGBA_8888;
  59. options.inScaled = false;
  60. options.inSampleSize = sampleSize;
  61. NativeIStream stream = new NativeIStream(istreamPtr);
  62. return BitmapFactory.decodeStream(stream, null, options);
  63. }
  64. protected static Bitmap createBitmap(int width, int height, int config, boolean hasAlpha) {
  65. return Bitmap.createBitmap(width, height, sConfigs[config]);
  66. }
  67. protected static boolean compressBitmap(Bitmap bitmap, int format, int quality, long ostreamPtr) {
  68. NativeOStream stream = new NativeOStream(ostreamPtr);
  69. return bitmap.compress(sFormats[format], quality, stream);
  70. }
  71. protected static String getCurrentThreadName() {
  72. return Thread.currentThread().getName();
  73. }
  74. /**
  75. * Maps the blob to memory and returns the pointer.
  76. */
  77. public long mapBlobFromResource(long offset) {
  78. int resourceId = 0;
  79. try {
  80. ActivityInfo ai = getPackageManager().getActivityInfo(
  81. getIntent().getComponent(), PackageManager.GET_META_DATA);
  82. if (ai.metaData == null) {
  83. Log.e("Panda3D", "Failed to get activity metadata");
  84. return 0;
  85. }
  86. resourceId = ai.metaData.getInt("org.panda3d.android.BLOB_RESOURCE");
  87. if (resourceId == 0) {
  88. return 0;
  89. }
  90. AssetFileDescriptor afd = getResources().openRawResourceFd(resourceId);
  91. ParcelFileDescriptor pfd = afd.getParcelFileDescriptor();
  92. long off = afd.getStartOffset() + offset;
  93. long len = afd.getLength();
  94. return nativeMmap(pfd.getFd(), off, len);
  95. } catch (Exception e) {
  96. Log.e("Panda3D", "Received exception while trying to map blob: " + e);
  97. return 0;
  98. }
  99. }
  100. /**
  101. * Returns the path to the main native library.
  102. */
  103. public String getNativeLibraryPath() {
  104. String libname = "main";
  105. try {
  106. ActivityInfo ai = getPackageManager().getActivityInfo(
  107. getIntent().getComponent(), PackageManager.GET_META_DATA);
  108. if (ai.metaData != null) {
  109. String ln = ai.metaData.getString(META_DATA_LIB_NAME);
  110. if (ln != null) libname = ln;
  111. }
  112. } catch (PackageManager.NameNotFoundException e) {
  113. throw new RuntimeException("Error getting activity info", e);
  114. }
  115. BaseDexClassLoader classLoader = (BaseDexClassLoader) getClassLoader();
  116. return classLoader.findLibrary(libname);
  117. }
  118. /**
  119. * Returns the path to some other native library.
  120. */
  121. public String findLibrary(String libname) {
  122. BaseDexClassLoader classLoader = (BaseDexClassLoader)getClassLoader();
  123. return classLoader.findLibrary(libname);
  124. }
  125. public String getIntentDataPath() {
  126. Intent intent = getIntent();
  127. Uri data = intent.getData();
  128. if (data == null) {
  129. return null;
  130. }
  131. String path = data.getPath();
  132. if (path.startsWith("//")) {
  133. path = path.substring(1);
  134. }
  135. return path;
  136. }
  137. public String getIntentOutputUri() {
  138. Intent intent = getIntent();
  139. return intent.getStringExtra("org.panda3d.OUTPUT_URI");
  140. }
  141. public String getCacheDirString() {
  142. return getCacheDir().toString();
  143. }
  144. /**
  145. * Shows a pop-up notification.
  146. */
  147. public void showToast(final String text, final int duration) {
  148. final PandaActivity activity = this;
  149. runOnUiThread(new Runnable() {
  150. public void run() {
  151. Toast toast = Toast.makeText(activity, text, duration);
  152. toast.show();
  153. }
  154. });
  155. }
  156. static {
  157. // Load this explicitly to initialize the JVM with the thread system.
  158. System.loadLibrary("panda");
  159. // Contains our JNI calls.
  160. System.loadLibrary("p3android");
  161. }
  162. private static native long nativeMmap(int fd, long off, long len);
  163. }