Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Android SDK Target to v32 (Android 12); request MANAGE_EXTERNAL_STORAGE #563

Merged
merged 4 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Assets/Plugins/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@
<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="false" />
</application>
<uses-permission android:name="android.permission.RECORD_AUDIO" tools:node="remove"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
</manifest>
21 changes: 21 additions & 0 deletions Assets/Scenes/Loading.unity
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,27 @@ MonoBehaviour:
m_VrCamera: {fileID: 1815664868}
m_MaximumLoadingTime: 60
m_SceneLoadRatio: 0.75
m_LoadingText:
m_TableReference:
m_TableCollectionName: GUID:c84355079ab3f3e4f8f3812258805f86
m_TableEntryReference:
m_KeyId: 287897575133184
m_Key:
m_FallbackState: 0
m_WaitForCompletion: 0
m_LocalVariables: []
m_RequestAndroidFolderPermissions:
m_TableReference:
m_TableCollectionName: GUID:c84355079ab3f3e4f8f3812258805f86
m_TableEntryReference:
m_KeyId: 170004956477833216
m_Key:
m_FallbackState: 0
m_WaitForCompletion: 0
m_LocalVariables: []
references:
version: 2
RefIds: []
--- !u!4 &326031496
Transform:
m_ObjectHideFlags: 0
Expand Down
68 changes: 68 additions & 0 deletions Assets/Scripts/LoadingScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Localization;

#if UNITY_ANDROID
using UnityEngine.Android;
#endif

namespace TiltBrush
{
Expand All @@ -27,11 +32,17 @@ public class LoadingScene : MonoBehaviour
// Amount of the progress bar taken up by the scene load
[SerializeField] private float m_SceneLoadRatio;

[SerializeField] private LocalizedString m_LoadingText;
[SerializeField] private LocalizedString m_RequestAndroidFolderPermissions;

// We have a slightly faked loading position that will always increase
// The fake loading rate is the minimum amount it will increase in one second, the reciprocal of
// m_MaximumLoadingTime
private float m_FakeLoadingRate;
private float m_CurrentLoadingPosition;
#if UNITY_ANDROID
private bool m_FolderPermissionOverride = false;
#endif

private IEnumerator Start()
{
Expand All @@ -58,6 +69,23 @@ private IEnumerator Start()

DontDestroyOnLoad(gameObject);

#if UNITY_ANDROID
if (Application.platform == RuntimePlatform.Android)
{
if (!UserHasManageExternalStoragePermission())
{
m_Overlay.MessageStatus = m_RequestAndroidFolderPermissions.GetLocalizedString();
AskForManageStoragePermission();
while (!UserHasManageExternalStoragePermission())
{
yield return new WaitForEndOfFrame();
}

m_Overlay.MessageStatus = m_LoadingText.GetLocalizedString();
}
}
#endif

AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Main");
while (!asyncLoad.isDone)
{
Expand Down Expand Up @@ -91,5 +119,45 @@ private void UpdateProgress(float start, float scale, float progress)
m_CurrentLoadingPosition = Mathf.Max(m_CurrentLoadingPosition, position);
m_Overlay.Progress = m_CurrentLoadingPosition;
}

#if UNITY_ANDROID
private bool UserHasManageExternalStoragePermission()
{
bool isExternalStorageManager = false;
try
{
AndroidJavaClass environmentClass = new AndroidJavaClass("android.os.Environment");
isExternalStorageManager = environmentClass.CallStatic<bool>("isExternalStorageManager");
}
catch (AndroidJavaException e)
{
Debug.LogError("Java Exception caught and ignored: " + e.Message);
Debug.LogError("Assuming this means this device doesn't support isExternalStorageManager.");
}
return m_FolderPermissionOverride || isExternalStorageManager;
}

private void AskForManageStoragePermission()
{
try
{
using var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
using AndroidJavaObject currentActivityObject = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
string packageName = currentActivityObject.Call<string>("getPackageName");
using var uriClass = new AndroidJavaClass("android.net.Uri");
using AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("fromParts", "package", packageName, null);
using var intentObject = new AndroidJavaObject("android.content.Intent", "android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION", uriObject);
intentObject.Call<AndroidJavaObject>("addCategory", "android.intent.category.DEFAULT");
currentActivityObject.Call("startActivity", intentObject);
}
catch (AndroidJavaException e)
{
// TODO: only skip this if it's of type act=android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
m_FolderPermissionOverride = true;
Debug.LogError("Java Exception caught and ignored: " + e.Message);
Debug.LogError("Assuming this means we don't need android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION (e.g., Android SDK < 30)");
}
}
#endif
}
} // namespace TiltBrush
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ MonoBehaviour:
m_Metadata:
m_Items: []
- m_Id: 287897575133184
m_Key: LOADING_SCENE_OVERLAY_MESSAGE
m_Key: LOADING_SCENE_OVERLAY_LOADING
m_Metadata:
m_Items: []
- m_Id: 7892854560759808
Expand Down Expand Up @@ -3271,6 +3271,10 @@ MonoBehaviour:
m_Key: POPUP_RECORD_UNSUPPORTED_TEXT
m_Metadata:
m_Items: []
- m_Id: 170004956477833216
m_Key: LOADING_SCENE_OVERLAY_ANDROIDPERMISSIONS
m_Metadata:
m_Items: []
m_Metadata:
m_Items: []
m_KeyGenerator:
Expand Down
10 changes: 7 additions & 3 deletions Assets/Settings/Localization/Strings/Strings_en.asset
Original file line number Diff line number Diff line change
Expand Up @@ -3101,7 +3101,7 @@ MonoBehaviour:
m_Metadata:
m_Items: []
- m_Id: 106429517453328384
m_Localized: "Background Images"
m_Localized: Background Images
m_Metadata:
m_Items: []
- m_Id: 95221400803737600
Expand Down Expand Up @@ -3457,11 +3457,15 @@ MonoBehaviour:
m_Metadata:
m_Items: []
- m_Id: 151490030713540608
m_Localized: 'You can create a video of your sketch by opening it on a Mac or PC.
Search for "Exporting video" on our docs website: https://docs.openbrush.app
m_Localized: 'You can create a video of your sketch by opening it on a Mac or
PC. Search for "Exporting video" on our docs website: https://docs.openbrush.app
for a step by step guide.'
m_Metadata:
m_Items: []
- m_Id: 170004956477833216
m_Localized: Please give Open Brush file access to save your creations!
m_Metadata:
m_Items: []
references:
version: 2
RefIds: []
4 changes: 2 additions & 2 deletions ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ PlayerSettings:
tvOS: 0
overrideDefaultApplicationIdentifier: 1
AndroidBundleVersionCode: 1
AndroidMinSdkVersion: 27
AndroidTargetSdkVersion: 29
AndroidMinSdkVersion: 29
AndroidTargetSdkVersion: 32
AndroidPreferredInstallLocation: 0
aotOptions:
stripEngineCode: 1
Expand Down
Loading