From 400888d55fcf963e7b8ddbba382f8e4f3c3e14b5 Mon Sep 17 00:00:00 2001 From: alexiscatnip Date: Thu, 20 Jan 2022 23:44:16 +0800 Subject: [PATCH] move useful method to DirectoryHelpers class --- Assets/Raindrop/Disk/DirectoryHelpers.cs | 51 +++++++++++++++++++ Assets/Raindrop/Disk/DirectoryHelpers.cs.meta | 3 ++ Assets/Tests/ImagingTests/ImagingTests.cs | 41 ++------------- 3 files changed, 57 insertions(+), 38 deletions(-) create mode 100644 Assets/Raindrop/Disk/DirectoryHelpers.cs create mode 100644 Assets/Raindrop/Disk/DirectoryHelpers.cs.meta diff --git a/Assets/Raindrop/Disk/DirectoryHelpers.cs b/Assets/Raindrop/Disk/DirectoryHelpers.cs new file mode 100644 index 0000000..fd2e0a8 --- /dev/null +++ b/Assets/Raindrop/Disk/DirectoryHelpers.cs @@ -0,0 +1,51 @@ +using UnityEngine; + +namespace Raindrop.Disk +{ + public class DirectoryHelpers + { + + // returns SD card if the bool is true OR the sd card is not available. + // otherwise i will return internal-but-shared storage + public static string GetAndroidExternalFilesDir(bool preferSDcard) + { + using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) + { + using (AndroidJavaObject context = unityPlayer.GetStatic("currentActivity")) + { + // Get all available external file directories (emulated and sdCards) + AndroidJavaObject[] externalFilesDirectories = context.Call("getExternalFilesDirs", null); + AndroidJavaObject emulated = null; + AndroidJavaObject sdCard = null; + + for (int i = 0; i < externalFilesDirectories.Length; i++) + { + AndroidJavaObject directory = externalFilesDirectories[i]; + using (AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment")) + { + // Check which one is the emulated and which the sdCard. + bool isRemovable = environment.CallStatic ("isExternalStorageRemovable", directory); + bool isEmulated = environment.CallStatic ("isExternalStorageEmulated", directory); + if (isEmulated) + emulated = directory; + else if (isRemovable && isEmulated == false) + sdCard = directory; + } + } + // Return the sdCard if available + if (sdCard != null && preferSDcard) + return sdCard.Call("getAbsolutePath"); + else + return emulated.Call("getAbsolutePath"); + } + } + } + + //gives us the base directory where we should be storing the cache files + public static string GetCacheDir() + { + return Application.persistentDataPath; //todo : correctly implement this. + + } + } +} \ No newline at end of file diff --git a/Assets/Raindrop/Disk/DirectoryHelpers.cs.meta b/Assets/Raindrop/Disk/DirectoryHelpers.cs.meta new file mode 100644 index 0000000..ec68e37 --- /dev/null +++ b/Assets/Raindrop/Disk/DirectoryHelpers.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d04c57fafed647e0bacdc153c5b4a849 +timeCreated: 1642606984 \ No newline at end of file diff --git a/Assets/Tests/ImagingTests/ImagingTests.cs b/Assets/Tests/ImagingTests/ImagingTests.cs index e0853fb..8e35816 100644 --- a/Assets/Tests/ImagingTests/ImagingTests.cs +++ b/Assets/Tests/ImagingTests/ImagingTests.cs @@ -76,7 +76,7 @@ namespace Raindrop.Tests.ImagingTests var outbytes = texture.EncodeToJPG(100); #if UNITY_EDITOR - WriteToFile(outbytes, outPath_persistentDataPath); + Helper.WriteToFile(outbytes, outPath_persistentDataPath); #elif UNITY_ANDROID //warn, this is true if we are in editor... string outPath_GetAndroidExternalFilesDir_internal = GetAndroidExternalFilesDir(false) + "/Pictures/menhara_out_GetAndroidExternalFilesDir_internal.jpg"; @@ -121,49 +121,14 @@ namespace Raindrop.Tests.ImagingTests // should be /storage/emulated/0/Android/data/com.UnityTestRunner.UnityTestRunner/files/Pictures/ Debug.Log("Application.sataPath" + Application.dataPath); - Debug.Log("GetAndroidExternalFilesDir internal"+ GetAndroidExternalFilesDir(true)); + Debug.Log("GetAndroidExternalFilesDir internal"+ Disk.DirectoryHelpers.GetAndroidExternalFilesDir(true)); // should be /storage/6106-8710/Android/data/com.UnityTestRunner.UnityTestRunner/files - Debug.Log("GetAndroidExternalFilesDir prefersdcard"+ GetAndroidExternalFilesDir(false)); + Debug.Log("GetAndroidExternalFilesDir prefersdcard"+ Disk.DirectoryHelpers.GetAndroidExternalFilesDir(false)); //should be /storage/emulated/0/Android/data/com.UnityTestRunner.UnityTestRunner/files/Pictures/ } - // returns SD card if the bool is true OR the sd card is not available. - // otherwise i will return internal-but-shared storage - private static string GetAndroidExternalFilesDir(bool preferSDcard) - { - using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) - { - using (AndroidJavaObject context = unityPlayer.GetStatic("currentActivity")) - { - // Get all available external file directories (emulated and sdCards) - AndroidJavaObject[] externalFilesDirectories = context.Call("getExternalFilesDirs", null); - AndroidJavaObject emulated = null; - AndroidJavaObject sdCard = null; - - for (int i = 0; i < externalFilesDirectories.Length; i++) - { - AndroidJavaObject directory = externalFilesDirectories[i]; - using (AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment")) - { - // Check which one is the emulated and which the sdCard. - bool isRemovable = environment.CallStatic ("isExternalStorageRemovable", directory); - bool isEmulated = environment.CallStatic ("isExternalStorageEmulated", directory); - if (isEmulated) - emulated = directory; - else if (isRemovable && isEmulated == false) - sdCard = directory; - } - } - // Return the sdCard if available - if (sdCard != null && preferSDcard) - return sdCard.Call("getAbsolutePath"); - else - return emulated.Call("getAbsolutePath"); - } - } - } } } } \ No newline at end of file