Skip to content

Commit 1e8a795

Browse files
committed
- Fixed Denied permission issue on Android 13 and later
- Added FileBrowserHelpers.IsPathDescendantOfAnother function
1 parent bc96d85 commit 1e8a795

File tree

8 files changed

+43
-6
lines changed

8 files changed

+43
-6
lines changed

.github/AAR Source (Android)/java/com/yasirkula/unity/FileBrowser.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public static int CheckPermission( Context context )
152152
if( Build.VERSION.SDK_INT < Build.VERSION_CODES.M )
153153
return 1;
154154

155-
if( context.checkSelfPermission( Manifest.permission.READ_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED )
155+
if( ( Build.VERSION.SDK_INT < 33 || context.getApplicationInfo().targetSdkVersion < 33 ) && context.checkSelfPermission( Manifest.permission.READ_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED )
156156
return 0;
157157

158158
if( Build.VERSION.SDK_INT < 30 && context.checkSelfPermission( Manifest.permission.WRITE_EXTERNAL_STORAGE ) != PackageManager.PERMISSION_GRANTED )
@@ -582,6 +582,20 @@ public static String GetParentDirectory( Context context, String rawUri )
582582
return "";
583583
}
584584

585+
@TargetApi( Build.VERSION_CODES.Q )
586+
public static boolean IsSAFEntryChildOfAnother( Context context, String rawUri, String parentRawUri )
587+
{
588+
try
589+
{
590+
return DocumentsContract.isChildDocument( context.getContentResolver(), Uri.parse( parentRawUri ), Uri.parse( rawUri ) );
591+
}
592+
catch( Exception e )
593+
{
594+
Log.e( "Unity", "Exception:", e );
595+
return false;
596+
}
597+
}
598+
585599
//// BEGIN UTILITY FUNCTIONS
586600
@TargetApi( Build.VERSION_CODES.Q )
587601
private static void CopyRawFile( String sourcePath, String destinationPath )

.github/AAR Source (Android)/java/com/yasirkula/unity/FileBrowserDirectoryPickerFragment.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.os.Build;
1010
import android.os.Bundle;
1111
import android.provider.DocumentsContract;
12+
import android.util.Log;
1213
import android.widget.Toast;
1314

1415
@TargetApi( Build.VERSION_CODES.Q )
@@ -34,7 +35,7 @@ public void onCreate( Bundle savedInstanceState )
3435
super.onCreate( savedInstanceState );
3536

3637
if( directoryReceiver == null )
37-
getFragmentManager().beginTransaction().remove( this ).commit();
38+
onActivityResult( DIRECTORY_PICK_REQUEST_CODE, Activity.RESULT_CANCELED, null );
3839
else
3940
{
4041
Intent intent = new Intent( Intent.ACTION_OPEN_DOCUMENT_TREE );

.github/AAR Source (Android)/java/com/yasirkula/unity/FileBrowserPermissionFragment.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ public void onCreate( Bundle savedInstanceState )
5252
{
5353
super.onCreate( savedInstanceState );
5454
if( permissionReceiver == null )
55-
getFragmentManager().beginTransaction().remove( this ).commit();
55+
onRequestPermissionsResult( PERMISSIONS_REQUEST_CODE, new String[0], new int[0] );
5656
else
5757
{
5858
if( Build.VERSION.SDK_INT < 30 )
5959
requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE }, PERMISSIONS_REQUEST_CODE );
60-
else
60+
else if( Build.VERSION.SDK_INT < 33 || getActivity().getApplicationInfo().targetSdkVersion < 33 )
6161
requestPermissions( new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, PERMISSIONS_REQUEST_CODE );
62+
else
63+
onRequestPermissionsResult( PERMISSIONS_REQUEST_CODE, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, new int[] { PackageManager.PERMISSION_GRANTED } );
6264
}
6365
}
6466

.github/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ The following file manipulation functions work on all platforms (including *Stor
191191
public static bool FileBrowserHelpers.FileExists( string path );
192192
public static bool FileBrowserHelpers.DirectoryExists( string path );
193193
public static bool FileBrowserHelpers.IsDirectory( string path );
194+
public static bool FileBrowserHelpers.IsPathDescendantOfAnother( string path, string parentFolderPath );
194195
public static string FileBrowserHelpers.GetDirectoryName( string path );
195196
public static FileSystemEntry[] FileBrowserHelpers.GetEntriesInDirectory( string path, bool extractOnlyLastSuffixFromExtensions ); // Returns all files and folders in a directory. If you want "File.tar.gz"s extension to be extracted as ".tar.gz" instead of ".gz", set 'extractOnlyLastSuffixFromExtensions' to false
196197
public static string FileBrowserHelpers.CreateFileInDirectory( string directoryPath, string filename ); // Returns the created file's path
Binary file not shown.

Plugins/SimpleFileBrowser/README.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
= Simple File Browser =
1+
= Simple File Browser (v1.5.8) =
22

33
Online documentation & example code available at: https://github.com/yasirkula/UnitySimpleFileBrowser
44
@@ -97,6 +97,7 @@ FileBrowser.Permission RequestPermission();
9797
bool FileBrowserHelpers.FileExists( string path );
9898
bool FileBrowserHelpers.DirectoryExists( string path );
9999
bool FileBrowserHelpers.IsDirectory( string path );
100+
bool FileBrowserHelpers.IsPathDescendantOfAnother( string path, string parentFolderPath );
100101
string FileBrowserHelpers.GetDirectoryName( string path );
101102
FileSystemEntry[] FileBrowserHelpers.GetEntriesInDirectory( string path, bool extractOnlyLastSuffixFromExtensions ); // Returns all files and folders in a directory. If you want "File.tar.gz"s extension to be extracted as ".tar.gz" instead of ".gz", set 'extractOnlyLastSuffixFromExtensions' to false
102103
string FileBrowserHelpers.CreateFileInDirectory( string directoryPath, string filename ); // Returns the created file's path

Plugins/SimpleFileBrowser/Scripts/FileBrowserHelpers.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ public static bool IsDirectory( string path )
140140
return extension == null || extension.Length <= 1; // extension includes '.'
141141
}
142142

143+
public static bool IsPathDescendantOfAnother( string path, string parentFolderPath )
144+
{
145+
#if !UNITY_EDITOR && UNITY_ANDROID
146+
if( ShouldUseSAFForPath( path ) )
147+
return AJC.CallStatic<bool>( "IsSAFEntryChildOfAnother", Context, path, parentFolderPath );
148+
#endif
149+
path = Path.GetFullPath( path ).Replace( '\\', '/' );
150+
parentFolderPath = Path.GetFullPath( parentFolderPath ).Replace( '\\', '/' );
151+
152+
if( path == parentFolderPath )
153+
return false;
154+
155+
if( parentFolderPath[parentFolderPath.Length - 1] != '/' )
156+
parentFolderPath += "/";
157+
158+
return path != parentFolderPath && path.StartsWith( parentFolderPath, System.StringComparison.OrdinalIgnoreCase );
159+
}
160+
143161
public static string GetDirectoryName( string path )
144162
{
145163
#if !UNITY_EDITOR && UNITY_ANDROID

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.yasirkula.simplefilebrowser",
33
"displayName": "Simple File Browser",
4-
"version": "1.5.7",
4+
"version": "1.5.8",
55
"documentationUrl": "https://github.com/yasirkula/UnitySimpleFileBrowser",
66
"changelogUrl": "https://github.com/yasirkula/UnitySimpleFileBrowser/releases",
77
"licensesUrl": "https://github.com/yasirkula/UnitySimpleFileBrowser/blob/master/LICENSE.txt",

0 commit comments

Comments
 (0)