Merge pull request #50359 from m4gr3d/address_external_dir_access

[3.x] Add support for Android scoped storage
This commit is contained in:
Rémi Verschelde 2021-08-16 09:48:34 +02:00 committed by GitHub
commit 028a2a7205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 99 additions and 263 deletions

View File

@ -1081,10 +1081,6 @@ String _OS::get_user_data_dir() const {
return OS::get_singleton()->get_user_data_dir();
};
String _OS::get_external_data_dir() const {
return OS::get_singleton()->get_external_data_dir();
}
Error _OS::native_video_play(String p_path, float p_volume, String p_audio_track, String p_subtitle_track) {
return OS::get_singleton()->native_video_play(p_path, p_volume, p_audio_track, p_subtitle_track);
};
@ -1160,8 +1156,8 @@ bool _OS::is_keep_screen_on() const {
return OS::get_singleton()->is_keep_screen_on();
}
String _OS::get_system_dir(SystemDir p_dir) const {
return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir));
String _OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
return OS::get_singleton()->get_system_dir(OS::SystemDir(p_dir), p_shared_storage);
}
String _OS::get_scancode_string(uint32_t p_code) const {
@ -1370,8 +1366,7 @@ void _OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_dynamic_memory_usage"), &_OS::get_dynamic_memory_usage);
ClassDB::bind_method(D_METHOD("get_user_data_dir"), &_OS::get_user_data_dir);
ClassDB::bind_method(D_METHOD("get_external_data_dir"), &_OS::get_external_data_dir);
ClassDB::bind_method(D_METHOD("get_system_dir", "dir"), &_OS::get_system_dir);
ClassDB::bind_method(D_METHOD("get_system_dir", "dir", "shared_storage"), &_OS::get_system_dir, DEFVAL(true));
ClassDB::bind_method(D_METHOD("get_config_dir"), &_OS::get_config_dir);
ClassDB::bind_method(D_METHOD("get_data_dir"), &_OS::get_data_dir);
ClassDB::bind_method(D_METHOD("get_cache_dir"), &_OS::get_cache_dir);

View File

@ -347,10 +347,9 @@ public:
SCREEN_ORIENTATION_SENSOR,
};
String get_system_dir(SystemDir p_dir) const;
String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
String get_user_data_dir() const;
String get_external_data_dir() const;
String get_config_dir() const;
String get_data_dir() const;
String get_cache_dir() const;

View File

@ -334,18 +334,13 @@ String OS::get_user_data_dir() const {
return ".";
};
// Android OS path to app's external data storage
String OS::get_external_data_dir() const {
return get_user_data_dir();
};
// Absolute path to res://
String OS::get_resource_dir() const {
return ProjectSettings::get_singleton()->get_resource_path();
}
// Access system-specific dirs like Documents, Downloads, etc.
String OS::get_system_dir(SystemDir p_dir) const {
String OS::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
return ".";
}

View File

@ -437,7 +437,6 @@ public:
virtual String get_bundle_resource_dir() const;
virtual String get_user_data_dir() const;
virtual String get_external_data_dir() const;
virtual String get_resource_dir() const;
enum SystemDir {
@ -451,7 +450,7 @@ public:
SYSTEM_DIR_RINGTONES,
};
virtual String get_system_dir(SystemDir p_dir) const;
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
virtual Error move_to_trash(const String &p_path) { return FAILED; }

View File

@ -218,12 +218,6 @@
Returns the path to the current engine executable.
</description>
</method>
<method name="get_external_data_dir" qualifiers="const">
<return type="String" />
<description>
On Android, returns the absolute directory path where user data can be written to external storage if available. On all other platforms, this will return the same location as [method get_user_data_dir].
</description>
</method>
<method name="get_granted_permissions" qualifiers="const">
<return type="PoolStringArray" />
<description>
@ -406,9 +400,11 @@
<method name="get_system_dir" qualifiers="const">
<return type="String" />
<argument index="0" name="dir" type="int" enum="OS.SystemDir" />
<argument index="1" name="shared_storage" type="bool" default="true" />
<description>
Returns the actual path to commonly used folders across different platforms. Available locations are specified in [enum SystemDir].
[b]Note:[/b] This method is implemented on Android, Linux, macOS and Windows.
[b]Note:[/b] Shared storage is implemented on Android and allows to differentiate between app specific and shared directories. Shared directories have additional restrictions on Android.
</description>
</method>
<method name="get_system_time_msecs" qualifiers="const">

View File

@ -845,7 +845,12 @@ class EditorExportPlatformAndroid : public EditorExportPlatform {
Vector<String> perms;
_get_permissions(p_preset, p_give_internet, perms);
for (int i = 0; i < perms.size(); i++) {
manifest_text += vformat(" <uses-permission android:name=\"%s\" />\n", perms.get(i));
String permission = perms.get(i);
if (permission == "android.permission.WRITE_EXTERNAL_STORAGE" || permission == "android.permission.READ_EXTERNAL_STORAGE") {
manifest_text += vformat(" <uses-permission android:name=\"%s\" android:maxSdkVersion=\"29\" />\n", permission);
} else {
manifest_text += vformat(" <uses-permission android:name=\"%s\" />\n", permission);
}
}
manifest_text += _get_xr_features_tag(p_preset);

View File

@ -1,8 +1,8 @@
ext.versions = [
androidGradlePlugin: '4.2.1',
compileSdk : 29,
minSdk : 18,
targetSdk : 29,
androidGradlePlugin: '4.2.2',
compileSdk : 30,
minSdk : 19,
targetSdk : 30,
buildTools : '30.0.3',
supportCoreUtils : '1.0.0',
kotlinVersion : '1.5.10',

View File

@ -49,7 +49,6 @@ import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
@ -70,15 +69,12 @@ import android.os.Looper;
import android.os.Messenger;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.provider.Settings.Secure;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
@ -573,7 +569,6 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
final Activity activity = getActivity();
io = new GodotIO(activity);
io.unique_id = Secure.getString(activity.getContentResolver(), Secure.ANDROID_ID);
GodotLib.io = io;
netUtils = new GodotNetUtils(activity);
mSensorManager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);

View File

@ -30,16 +30,18 @@
package org.godotengine.godot;
import org.godotengine.godot.input.*;
import org.godotengine.godot.input.GodotEditText;
import android.app.Activity;
import android.content.*;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.graphics.Point;
import android.media.*;
import android.net.Uri;
import android.os.*;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
@ -48,17 +50,17 @@ import android.view.DisplayCutout;
import android.view.WindowInsets;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
// Wrapper for native library
public class GodotIO {
AssetManager am;
final Activity activity;
GodotEditText edit;
private static final String TAG = GodotIO.class.getSimpleName();
MediaPlayer mediaPlayer;
private final AssetManager am;
private final Activity activity;
private final String uniqueId;
GodotEditText edit;
final int SCREEN_LANDSCAPE = 0;
final int SCREEN_PORTRAIT = 1;
@ -68,167 +70,6 @@ public class GodotIO {
final int SCREEN_SENSOR_PORTRAIT = 5;
final int SCREEN_SENSOR = 6;
/////////////////////////
/// FILES
/////////////////////////
public int last_file_id = 1;
static class AssetData {
public boolean eof = false;
public String path;
public InputStream is;
public int len;
public int pos;
}
SparseArray<AssetData> streams;
public int file_open(String path, boolean write) {
//System.out.printf("file_open: Attempt to Open %s\n",path);
//Log.v("MyApp", "TRYING TO OPEN FILE: " + path);
if (write)
return -1;
AssetData ad = new AssetData();
try {
ad.is = am.open(path);
} catch (Exception e) {
//System.out.printf("Exception on file_open: %s\n",path);
return -1;
}
try {
ad.len = ad.is.available();
} catch (Exception e) {
System.out.printf("Exception availabling on file_open: %s\n", path);
return -1;
}
ad.path = path;
ad.pos = 0;
++last_file_id;
streams.put(last_file_id, ad);
return last_file_id;
}
public int file_get_size(int id) {
if (streams.get(id) == null) {
System.out.printf("file_get_size: Invalid file id: %d\n", id);
return -1;
}
return streams.get(id).len;
}
public void file_seek(int id, int bytes) {
if (streams.get(id) == null) {
System.out.printf("file_get_size: Invalid file id: %d\n", id);
return;
}
//seek sucks
AssetData ad = streams.get(id);
if (bytes > ad.len)
bytes = ad.len;
if (bytes < 0)
bytes = 0;
try {
if (bytes > (int)ad.pos) {
int todo = bytes - (int)ad.pos;
while (todo > 0) {
todo -= ad.is.skip(todo);
}
ad.pos = bytes;
} else if (bytes < (int)ad.pos) {
ad.is = am.open(ad.path);
ad.pos = bytes;
int todo = bytes;
while (todo > 0) {
todo -= ad.is.skip(todo);
}
}
ad.eof = false;
} catch (IOException e) {
System.out.printf("Exception on file_seek: %s\n", e);
return;
}
}
public int file_tell(int id) {
if (streams.get(id) == null) {
System.out.printf("file_read: Can't tell eof for invalid file id: %d\n", id);
return 0;
}
AssetData ad = streams.get(id);
return ad.pos;
}
public boolean file_eof(int id) {
if (streams.get(id) == null) {
System.out.printf("file_read: Can't check eof for invalid file id: %d\n", id);
return false;
}
AssetData ad = streams.get(id);
return ad.eof;
}
public byte[] file_read(int id, int bytes) {
if (streams.get(id) == null) {
System.out.printf("file_read: Can't read invalid file id: %d\n", id);
return new byte[0];
}
AssetData ad = streams.get(id);
if (ad.pos + bytes > ad.len) {
bytes = ad.len - ad.pos;
ad.eof = true;
}
if (bytes == 0) {
return new byte[0];
}
byte[] buf1 = new byte[bytes];
int r = 0;
try {
r = ad.is.read(buf1);
} catch (IOException e) {
System.out.printf("Exception on file_read: %s\n", e);
return new byte[bytes];
}
if (r == 0) {
return new byte[0];
}
ad.pos += r;
if (r < bytes) {
byte[] buf2 = new byte[r];
for (int i = 0; i < r; i++)
buf2[i] = buf1[i];
return buf2;
} else {
return buf1;
}
}
public void file_close(int id) {
if (streams.get(id) == null) {
System.out.printf("file_close: Can't close invalid file id: %d\n", id);
return;
}
streams.remove(id);
}
/////////////////////////
/// DIRECTORIES
/////////////////////////
@ -239,9 +80,9 @@ public class GodotIO {
public String path;
}
public int last_dir_id = 1;
private int last_dir_id = 1;
SparseArray<AssetDir> dirs;
private final SparseArray<AssetDir> dirs;
public int dir_open(String path) {
AssetDir ad = new AssetDir();
@ -260,7 +101,6 @@ public class GodotIO {
return -1;
}
//System.out.printf("Opened dir: %s\n",path);
++last_dir_id;
dirs.put(last_dir_id, ad);
@ -323,9 +163,14 @@ public class GodotIO {
GodotIO(Activity p_activity) {
am = p_activity.getAssets();
activity = p_activity;
//streams = new HashMap<Integer, AssetData>();
streams = new SparseArray<>();
dirs = new SparseArray<>();
String androidId = Settings.Secure.getString(activity.getContentResolver(),
Settings.Secure.ANDROID_ID);
if (androidId == null) {
androidId = "";
}
uniqueId = androidId;
}
/////////////////////////
@ -334,7 +179,6 @@ public class GodotIO {
public int openURI(String p_uri) {
try {
Log.v("MyApp", "TRYING TO OPEN URI: " + p_uri);
String path = p_uri;
String type = "";
if (path.startsWith("/")) {
@ -360,12 +204,12 @@ public class GodotIO {
}
}
public String getDataDir() {
return activity.getFilesDir().getAbsolutePath();
public String getCacheDir() {
return activity.getCacheDir().getAbsolutePath();
}
public String getExternalDataDir() {
return activity.getExternalFilesDir(null).getAbsolutePath();
public String getDataDir() {
return activity.getFilesDir().getAbsolutePath();
}
public String getLocale() {
@ -459,51 +303,54 @@ public class GodotIO {
public static final int SYSTEM_DIR_PICTURES = 6;
public static final int SYSTEM_DIR_RINGTONES = 7;
public String getSystemDir(int idx) {
String what = "";
public String getSystemDir(int idx, boolean shared_storage) {
String what;
switch (idx) {
case SYSTEM_DIR_DESKTOP: {
//what=Environment.DIRECTORY_DOCUMENTS;
what = Environment.DIRECTORY_DOWNLOADS;
case SYSTEM_DIR_DESKTOP:
default: {
what = null; // This leads to the app specific external root directory.
} break;
case SYSTEM_DIR_DCIM: {
what = Environment.DIRECTORY_DCIM;
} break;
} break;
case SYSTEM_DIR_DOCUMENTS: {
what = Environment.DIRECTORY_DOWNLOADS;
//what=Environment.DIRECTORY_DOCUMENTS;
what = Environment.DIRECTORY_DOCUMENTS;
} break;
case SYSTEM_DIR_DOWNLOADS: {
what = Environment.DIRECTORY_DOWNLOADS;
} break;
case SYSTEM_DIR_MOVIES: {
what = Environment.DIRECTORY_MOVIES;
} break;
case SYSTEM_DIR_MUSIC: {
what = Environment.DIRECTORY_MUSIC;
} break;
case SYSTEM_DIR_PICTURES: {
what = Environment.DIRECTORY_PICTURES;
} break;
case SYSTEM_DIR_RINGTONES: {
what = Environment.DIRECTORY_RINGTONES;
} break;
}
if (what.equals(""))
return "";
return Environment.getExternalStoragePublicDirectory(what).getAbsolutePath();
if (shared_storage) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Log.w(TAG, "Shared storage access is limited on Android 10 and higher.");
}
return Environment.getExternalStoragePublicDirectory(what).getAbsolutePath();
} else {
return activity.getExternalFilesDir(what).getAbsolutePath();
}
}
protected static final String PREFS_FILE = "device_id.xml";
protected static final String PREFS_DEVICE_ID = "device_id";
public static String unique_id = "";
public String getUniqueID() {
return unique_id;
return uniqueId;
}
}

View File

@ -48,8 +48,8 @@ GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instanc
}
_open_URI = p_env->GetMethodID(cls, "openURI", "(Ljava/lang/String;)I");
_get_cache_dir = p_env->GetMethodID(cls, "getCacheDir", "()Ljava/lang/String;");
_get_data_dir = p_env->GetMethodID(cls, "getDataDir", "()Ljava/lang/String;");
_get_external_data_dir = p_env->GetMethodID(cls, "getExternalDataDir", "()Ljava/lang/String;");
_get_locale = p_env->GetMethodID(cls, "getLocale", "()Ljava/lang/String;");
_get_model = p_env->GetMethodID(cls, "getModel", "()Ljava/lang/String;");
_get_screen_DPI = p_env->GetMethodID(cls, "getScreenDPI", "()I");
@ -59,7 +59,7 @@ GodotIOJavaWrapper::GodotIOJavaWrapper(JNIEnv *p_env, jobject p_godot_io_instanc
_hide_keyboard = p_env->GetMethodID(cls, "hideKeyboard", "()V");
_set_screen_orientation = p_env->GetMethodID(cls, "setScreenOrientation", "(I)V");
_get_screen_orientation = p_env->GetMethodID(cls, "getScreenOrientation", "()I");
_get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(I)Ljava/lang/String;");
_get_system_dir = p_env->GetMethodID(cls, "getSystemDir", "(IZ)Ljava/lang/String;");
}
}
@ -82,22 +82,22 @@ Error GodotIOJavaWrapper::open_uri(const String &p_uri) {
}
}
String GodotIOJavaWrapper::get_user_data_dir() {
if (_get_data_dir) {
String GodotIOJavaWrapper::get_cache_dir() {
if (_get_cache_dir) {
JNIEnv *env = get_jni_env();
ERR_FAIL_COND_V(env == nullptr, String());
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_data_dir);
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_cache_dir);
return jstring_to_string(s, env);
} else {
return String();
}
}
String GodotIOJavaWrapper::get_external_data_dir() {
if (_get_external_data_dir) {
String GodotIOJavaWrapper::get_user_data_dir() {
if (_get_data_dir) {
JNIEnv *env = get_jni_env();
ERR_FAIL_COND_V(env == nullptr, String());
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_external_data_dir);
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_data_dir);
return jstring_to_string(s, env);
} else {
return String();
@ -200,11 +200,11 @@ int GodotIOJavaWrapper::get_screen_orientation() const {
}
}
String GodotIOJavaWrapper::get_system_dir(int p_dir) {
String GodotIOJavaWrapper::get_system_dir(int p_dir, bool p_shared_storage) {
if (_get_system_dir) {
JNIEnv *env = get_jni_env();
ERR_FAIL_COND_V(env == nullptr, String("."));
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_system_dir, p_dir);
jstring s = (jstring)env->CallObjectMethod(godot_io_instance, _get_system_dir, p_dir, p_shared_storage);
return jstring_to_string(s, env);
} else {
return String(".");

View File

@ -46,8 +46,8 @@ private:
jclass cls;
jmethodID _open_URI = 0;
jmethodID _get_cache_dir = 0;
jmethodID _get_data_dir = 0;
jmethodID _get_external_data_dir = 0;
jmethodID _get_locale = 0;
jmethodID _get_model = 0;
jmethodID _get_screen_DPI = 0;
@ -66,8 +66,8 @@ public:
jobject get_instance();
Error open_uri(const String &p_uri);
String get_cache_dir();
String get_user_data_dir();
String get_external_data_dir();
String get_locale();
String get_model();
int get_screen_dpi();
@ -80,7 +80,7 @@ public:
void set_vk_height(int p_height);
void set_screen_orientation(int p_orient);
int get_screen_orientation() const;
String get_system_dir(int p_dir);
String get_system_dir(int p_dir, bool p_shared_storage);
};
#endif /* !JAVA_GODOT_IO_WRAPPER_H */

View File

@ -433,6 +433,10 @@ int OS_Android::get_screen_dpi(int p_screen) const {
return godot_io_java->get_screen_dpi();
}
String OS_Android::get_data_path() const {
return get_user_data_dir();
}
String OS_Android::get_user_data_dir() const {
if (data_dir_cache != String())
return data_dir_cache;
@ -445,11 +449,11 @@ String OS_Android::get_user_data_dir() const {
return ".";
}
String OS_Android::get_external_data_dir() const {
String data_dir = godot_io_java->get_external_data_dir();
if (data_dir != "") {
data_dir = _remove_symlink(data_dir);
return data_dir;
String OS_Android::get_cache_path() const {
String cache_dir = godot_io_java->get_cache_dir();
if (cache_dir != "") {
cache_dir = _remove_symlink(cache_dir);
return cache_dir;
}
return ".";
}
@ -471,8 +475,8 @@ String OS_Android::get_unique_id() const {
return OS::get_unique_id();
}
String OS_Android::get_system_dir(SystemDir p_dir) const {
return godot_io_java->get_system_dir(p_dir);
String OS_Android::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
return godot_io_java->get_system_dir(p_dir, p_shared_storage);
}
void OS_Android::set_context_is_16_bits(bool p_is_16) {

View File

@ -141,7 +141,8 @@ public:
virtual Error shell_open(String p_uri);
virtual String get_user_data_dir() const;
virtual String get_external_data_dir() const;
virtual String get_data_path() const;
virtual String get_cache_path() const;
virtual String get_resource_dir() const;
virtual String get_locale() const;
virtual void set_clipboard(const String &p_text);
@ -151,7 +152,7 @@ public:
virtual String get_unique_id() const;
virtual String get_system_dir(SystemDir p_dir) const;
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
void process_accelerometer(const Vector3 &p_accelerometer);
void process_gravity(const Vector3 &p_gravity);

View File

@ -227,7 +227,7 @@ public:
virtual String get_bundle_resource_dir() const;
virtual String get_godot_dir_name() const;
virtual String get_system_dir(SystemDir p_dir) const;
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
virtual bool can_draw() const;

View File

@ -2296,7 +2296,7 @@ String OS_OSX::get_godot_dir_name() const {
return String(VERSION_SHORT_NAME).capitalize();
}
String OS_OSX::get_system_dir(SystemDir p_dir) const {
String OS_OSX::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
NSSearchPathDirectory id;
bool found = true;

View File

@ -236,7 +236,7 @@ String OS_Server::get_cache_path() const {
}
}
String OS_Server::get_system_dir(SystemDir p_dir) const {
String OS_Server::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
String xdgparam;
switch (p_dir) {

View File

@ -119,7 +119,7 @@ public:
virtual String get_data_path() const;
virtual String get_cache_path() const;
virtual String get_system_dir(SystemDir p_dir) const;
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
void disable_crash_handler();
bool is_disable_crash_handler() const;

View File

@ -3362,7 +3362,7 @@ String OS_Windows::get_godot_dir_name() const {
return String(VERSION_SHORT_NAME).capitalize();
}
String OS_Windows::get_system_dir(SystemDir p_dir) const {
String OS_Windows::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
KNOWNFOLDERID id;
switch (p_dir) {

View File

@ -533,7 +533,7 @@ public:
virtual String get_cache_path() const;
virtual String get_godot_dir_name() const;
virtual String get_system_dir(SystemDir p_dir) const;
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
virtual String get_user_data_dir() const;
virtual String get_unique_id() const;

View File

@ -3250,7 +3250,7 @@ String OS_X11::get_cache_path() const {
return get_config_path();
}
String OS_X11::get_system_dir(SystemDir p_dir) const {
String OS_X11::get_system_dir(SystemDir p_dir, bool p_shared_storage) const {
String xdgparam;
switch (p_dir) {

View File

@ -278,7 +278,7 @@ public:
virtual String get_data_path() const;
virtual String get_cache_path() const;
virtual String get_system_dir(SystemDir p_dir) const;
virtual String get_system_dir(SystemDir p_dir, bool p_shared_storage = true) const;
virtual Error shell_open(String p_uri);