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
3.0
iwg..ic 13 years ago
parent 2a3e28bb9d
commit dd46be3688
  1. 66
      engine/src/android/com/jme3/system/android/JmeAndroidSystem.java
  2. 25
      engine/src/core/com/jme3/system/JmeSystem.java
  3. 46
      engine/src/core/com/jme3/system/JmeSystemDelegate.java

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

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

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

Loading…
Cancel
Save