Kaynağa Gözat

Add support for multiple folder storage types for getStorageFolder
See forum post http://jmonkeyengine.org/groups/android/forum/topic/jmesystem-getstoragefolder-updates/

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@10020 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

iwg..ic 12 yıl önce
ebeveyn
işleme
dd46be3688

+ 50 - 16
engine/src/android/com/jme3/system/android/JmeAndroidSystem.java

@@ -2,6 +2,7 @@ package com.jme3.system.android;
 
 import android.app.Activity;
 import android.app.AlertDialog;
+import android.content.Context;
 import android.graphics.Bitmap;
 import android.os.Environment;
 import com.jme3.asset.AndroidAssetManager;
@@ -164,24 +165,57 @@ public class JmeAndroidSystem extends JmeSystemDelegate {
     }
 
     @Override
-    public synchronized File getStorageFolder() {
-        //http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir
-        //http://developer.android.com/guide/topics/data/data-storage.html
-
-        String state = Environment.getExternalStorageState();
-        if (Environment.MEDIA_MOUNTED.equals(state)) {
-            // getExternalFilesDir automatically creates the directory if necessary.
-            // directory structure should be: /mnt/sdcard/Android/data/<packagename>/files
-            // when created this way, the directory is automatically removed by the Android
-            //   system when the app is uninstalled
-            storageFolder = activity.getApplicationContext().getExternalFilesDir(null);
-            logger.log(Level.INFO, "Storage Folder Path: {0}", storageFolder.getAbsolutePath());
-
-            return storageFolder;
+    public synchronized File getStorageFolder(JmeSystem.StorageFolderType type) {
+        File storageFolder = null;
+
+        switch (type) {
+            case Internal:
+                // http://developer.android.com/guide/topics/data/data-storage.html
+                // http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
+                // http://developer.android.com/reference/android/content/Context.html#getFilesDir()
+                // http://developer.android.com/reference/android/content/Context.html#getDir(java.lang.String, int)
+
+                // getDir automatically creates the directory if necessary.
+                // Directory structure should be: /data/data/<packagename>/app_
+                // When created this way, the directory is automatically removed by the Android
+                //   system when the app is uninstalled.
+                // The directory is NOT accessible by a PC connected to the device
+                // The files can only be accessed by this application
+                storageFolder = storageFolders.get(type);
+                if (storageFolder == null) {
+                    storageFolder = activity.getApplicationContext().getDir("", Context.MODE_PRIVATE);
+                    storageFolders.put(type, storageFolder);
+                }
+                break;
+            case External:
+                //http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir
+                //http://developer.android.com/guide/topics/data/data-storage.html
+
+                // getExternalFilesDir automatically creates the directory if necessary.
+                // Directory structure should be: /mnt/sdcard/Android/data/<packagename>/files
+                // When created this way, the directory is automatically removed by the Android
+                //   system when the app is uninstalled.
+                // The directory is also accessible by a PC connected to the device
+                //   so the files can be copied to the PC (ie. screenshots)
+                storageFolder = storageFolders.get(type);
+                if (storageFolder == null) {
+                    String state = Environment.getExternalStorageState();
+                    logger.log(Level.INFO, "ExternalStorageState: {0}", state);
+                    if (state.equals(Environment.MEDIA_MOUNTED)) {
+                        storageFolder = activity.getApplicationContext().getExternalFilesDir(null);
+                        storageFolders.put(type, storageFolder);
+                    }
+                }
+                break;
+            default:
+                break;
+        }
+        if (storageFolder != null) {
+            logger.log(Level.INFO, "Base Storage Folder Path: {0}", storageFolder.getAbsolutePath());
         } else {
-            return null;
+            logger.log(Level.INFO, "Base Storage Folder not found!");
         }
-
+        return storageFolder;
     }
 
     public static void setActivity(Activity activity) {

+ 17 - 8
engine/src/core/com/jme3/system/JmeSystem.java

@@ -47,15 +47,24 @@ import java.util.logging.Logger;
 
 public class JmeSystem {
 
+    public static enum StorageFolderType {
+        Internal,
+        External,
+    }
+
     private static JmeSystemDelegate systemDelegate;
 
     public static void setSystemDelegate(JmeSystemDelegate systemDelegate) {
         JmeSystem.systemDelegate = systemDelegate;
     }
-    
+
     public static synchronized File getStorageFolder() {
+        return getStorageFolder(StorageFolderType.External);
+    }
+
+    public static synchronized File getStorageFolder(StorageFolderType type) {
         checkDelegate();
-        return systemDelegate.getStorageFolder();
+        return systemDelegate.getStorageFolder(type);
     }
 
     public static String getFullName() {
@@ -97,7 +106,7 @@ public class JmeSystem {
         checkDelegate();
         return systemDelegate.getSoftTextDialogInput();
     }
-    
+
     public static void writeImageFile(OutputStream outStream, String format, ByteBuffer imageData, int width, int height) throws IOException {
         checkDelegate();
         systemDelegate.writeImageFile(outStream, format, imageData, width, height);
@@ -132,7 +141,7 @@ public class JmeSystem {
         checkDelegate();
         return systemDelegate.newAudioRenderer(settings);
     }
-    
+
     public static ImageRaster createImageRaster(Image image, int slice) {
         checkDelegate();
         return systemDelegate.createImageRaster(image, slice);
@@ -141,8 +150,8 @@ public class JmeSystem {
     /**
      * Displays an error message to the user in whichever way the context
      * feels is appropriate. If this is a headless or an offscreen surface
-     * context, this method should do nothing. 
-     * 
+     * context, this method should do nothing.
+     *
      * @param message The error message to display. May contain new line
      * characters.
      */
@@ -150,7 +159,7 @@ public class JmeSystem {
         checkDelegate();
         systemDelegate.showErrorDialog(message);
     }
-    
+
     public static void initialize(AppSettings settings) {
         checkDelegate();
         systemDelegate.initialize(settings);
@@ -163,7 +172,7 @@ public class JmeSystem {
             return null;
         }
     }
-    
+
     @SuppressWarnings("unchecked")
     private static void checkDelegate() {
         if (systemDelegate == null) {

+ 33 - 13
engine/src/core/com/jme3/system/JmeSystemDelegate.java

@@ -42,6 +42,9 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
 import java.nio.ByteBuffer;
+import java.util.EnumMap;
+import java.util.Map;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 /**
@@ -53,23 +56,40 @@ public abstract class JmeSystemDelegate {
     protected final Logger logger = Logger.getLogger(JmeSystem.class.getName());
     protected boolean initialized = false;
     protected boolean lowPermissions = false;
-    protected File storageFolder = null;
+    protected Map<JmeSystem.StorageFolderType, File> storageFolders = new EnumMap<JmeSystem.StorageFolderType, File>(JmeSystem.StorageFolderType.class);
     protected SoftTextDialogInput softTextDialogInput = null;
 
-    public synchronized File getStorageFolder() {
-        if (lowPermissions) {
-            throw new UnsupportedOperationException("File system access restricted");
+    public synchronized File getStorageFolder(JmeSystem.StorageFolderType type) {
+        File storageFolder = null;
+
+        switch (type) {
+            // Internal and External are currently the same folder
+            case Internal:
+            case External:
+                if (lowPermissions) {
+                    throw new UnsupportedOperationException("File system access restricted");
+                }
+                storageFolder = storageFolders.get(type);
+                if (storageFolder == null) {
+                    // Initialize storage folder
+                    storageFolder = new File(System.getProperty("user.home"), ".jme3");
+                    if (!storageFolder.exists()) {
+                        storageFolder.mkdir();
+                    }
+                    storageFolders.put(type, storageFolder);
+                }
+                break;
+            default:
+                break;
         }
-        if (storageFolder == null) {
-            // Initialize storage folder
-            storageFolder = new File(System.getProperty("user.home"), ".jme3");
-            if (!storageFolder.exists()) {
-                storageFolder.mkdir();
-            }
+        if (storageFolder != null) {
+            logger.log(Level.INFO, "Storage Folder Path: {0}", storageFolder.getAbsolutePath());
+        } else {
+            logger.log(Level.INFO, "Storage Folder not found!");
         }
         return storageFolder;
     }
-    
+
     public String getFullName() {
         return JmeVersion.FULL_NAME;
     }
@@ -100,7 +120,7 @@ public abstract class JmeSystemDelegate {
     public SoftTextDialogInput getSoftTextDialogInput() {
         return softTextDialogInput;
     }
-    
+
     public abstract void writeImageFile(OutputStream outStream, String format, ByteBuffer imageData, int width, int height) throws IOException;
 
     public abstract AssetManager newAssetManager(URL configFile);
@@ -108,7 +128,7 @@ public abstract class JmeSystemDelegate {
     public abstract AssetManager newAssetManager();
 
     public abstract void showErrorDialog(String message);
-    
+
     public abstract boolean showSettingsDialog(AppSettings sourceSettings, boolean loadFromRegistry);
 
     private boolean is64Bit(String arch) {