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
This commit is contained in:
parent
2a3e28bb9d
commit
dd46be3688
engine/src
android/com/jme3/system/android
core/com/jme3/system
@ -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) {
|
||||||
|
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/reference/android/content/Context.html#getExternalFilesDir
|
||||||
//http://developer.android.com/guide/topics/data/data-storage.html
|
//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.
|
// getExternalFilesDir automatically creates the directory if necessary.
|
||||||
// directory structure should be: /mnt/sdcard/Android/data/<packagename>/files
|
// Directory structure should be: /mnt/sdcard/Android/data/<packagename>/files
|
||||||
// when created this way, the directory is automatically removed by the Android
|
// When created this way, the directory is automatically removed by the Android
|
||||||
// system when the app is uninstalled
|
// 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);
|
storageFolder = activity.getApplicationContext().getExternalFilesDir(null);
|
||||||
logger.log(Level.INFO, "Storage Folder Path: {0}", storageFolder.getAbsolutePath());
|
storageFolders.put(type, storageFolder);
|
||||||
|
|
||||||
return storageFolder;
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (storageFolder != null) {
|
||||||
|
logger.log(Level.INFO, "Base Storage Folder Path: {0}", storageFolder.getAbsolutePath());
|
||||||
|
} else {
|
||||||
|
logger.log(Level.INFO, "Base Storage Folder not found!");
|
||||||
|
}
|
||||||
|
return storageFolder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setActivity(Activity activity) {
|
public static void setActivity(Activity activity) {
|
||||||
|
@ -47,6 +47,11 @@ 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) {
|
||||||
@ -54,8 +59,12 @@ public class JmeSystem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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() {
|
||||||
|
@ -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,19 +56,36 @@ 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) {
|
||||||
|
File storageFolder = null;
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
// Internal and External are currently the same folder
|
||||||
|
case Internal:
|
||||||
|
case External:
|
||||||
if (lowPermissions) {
|
if (lowPermissions) {
|
||||||
throw new UnsupportedOperationException("File system access restricted");
|
throw new UnsupportedOperationException("File system access restricted");
|
||||||
}
|
}
|
||||||
|
storageFolder = storageFolders.get(type);
|
||||||
if (storageFolder == null) {
|
if (storageFolder == null) {
|
||||||
// Initialize storage folder
|
// Initialize storage folder
|
||||||
storageFolder = new File(System.getProperty("user.home"), ".jme3");
|
storageFolder = new File(System.getProperty("user.home"), ".jme3");
|
||||||
if (!storageFolder.exists()) {
|
if (!storageFolder.exists()) {
|
||||||
storageFolder.mkdir();
|
storageFolder.mkdir();
|
||||||
}
|
}
|
||||||
|
storageFolders.put(type, storageFolder);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (storageFolder != null) {
|
||||||
|
logger.log(Level.INFO, "Storage Folder Path: {0}", storageFolder.getAbsolutePath());
|
||||||
|
} else {
|
||||||
|
logger.log(Level.INFO, "Storage Folder not found!");
|
||||||
}
|
}
|
||||||
return storageFolder;
|
return storageFolder;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user