Skip to content

Commit

Permalink
- Fixed Denied permission issue on Android 13 and later
Browse files Browse the repository at this point in the history
- Added FileBrowserHelpers.IsPathDescendantOfAnother function
  • Loading branch information
yasirkula committed Dec 10, 2022
1 parent bc96d85 commit 1e8a795
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static int CheckPermission( Context context )
if( Build.VERSION.SDK_INT < Build.VERSION_CODES.M )
return 1;

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

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

@TargetApi( Build.VERSION_CODES.Q )
public static boolean IsSAFEntryChildOfAnother( Context context, String rawUri, String parentRawUri )
{
try
{
return DocumentsContract.isChildDocument( context.getContentResolver(), Uri.parse( parentRawUri ), Uri.parse( rawUri ) );
}
catch( Exception e )
{
Log.e( "Unity", "Exception:", e );
return false;
}
}

//// BEGIN UTILITY FUNCTIONS
@TargetApi( Build.VERSION_CODES.Q )
private static void CopyRawFile( String sourcePath, String destinationPath )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.util.Log;
import android.widget.Toast;

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

if( directoryReceiver == null )
getFragmentManager().beginTransaction().remove( this ).commit();
onActivityResult( DIRECTORY_PICK_REQUEST_CODE, Activity.RESULT_CANCELED, null );
else
{
Intent intent = new Intent( Intent.ACTION_OPEN_DOCUMENT_TREE );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
if( permissionReceiver == null )
getFragmentManager().beginTransaction().remove( this ).commit();
onRequestPermissionsResult( PERMISSIONS_REQUEST_CODE, new String[0], new int[0] );
else
{
if( Build.VERSION.SDK_INT < 30 )
requestPermissions( new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE }, PERMISSIONS_REQUEST_CODE );
else
else if( Build.VERSION.SDK_INT < 33 || getActivity().getApplicationInfo().targetSdkVersion < 33 )
requestPermissions( new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, PERMISSIONS_REQUEST_CODE );
else
onRequestPermissionsResult( PERMISSIONS_REQUEST_CODE, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, new int[] { PackageManager.PERMISSION_GRANTED } );
}
}

Expand Down
1 change: 1 addition & 0 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ The following file manipulation functions work on all platforms (including *Stor
public static bool FileBrowserHelpers.FileExists( string path );
public static bool FileBrowserHelpers.DirectoryExists( string path );
public static bool FileBrowserHelpers.IsDirectory( string path );
public static bool FileBrowserHelpers.IsPathDescendantOfAnother( string path, string parentFolderPath );
public static string FileBrowserHelpers.GetDirectoryName( string path );
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
public static string FileBrowserHelpers.CreateFileInDirectory( string directoryPath, string filename ); // Returns the created file's path
Expand Down
Binary file modified Plugins/SimpleFileBrowser/Android/SimpleFileBrowser.aar
Binary file not shown.
3 changes: 2 additions & 1 deletion Plugins/SimpleFileBrowser/README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= Simple File Browser =
= Simple File Browser (v1.5.8) =

Online documentation & example code available at: https://github.com/yasirkula/UnitySimpleFileBrowser
E-mail: [email protected]
Expand Down Expand Up @@ -97,6 +97,7 @@ FileBrowser.Permission RequestPermission();
bool FileBrowserHelpers.FileExists( string path );
bool FileBrowserHelpers.DirectoryExists( string path );
bool FileBrowserHelpers.IsDirectory( string path );
bool FileBrowserHelpers.IsPathDescendantOfAnother( string path, string parentFolderPath );
string FileBrowserHelpers.GetDirectoryName( string path );
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
string FileBrowserHelpers.CreateFileInDirectory( string directoryPath, string filename ); // Returns the created file's path
Expand Down
18 changes: 18 additions & 0 deletions Plugins/SimpleFileBrowser/Scripts/FileBrowserHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,24 @@ public static bool IsDirectory( string path )
return extension == null || extension.Length <= 1; // extension includes '.'
}

public static bool IsPathDescendantOfAnother( string path, string parentFolderPath )
{
#if !UNITY_EDITOR && UNITY_ANDROID
if( ShouldUseSAFForPath( path ) )
return AJC.CallStatic<bool>( "IsSAFEntryChildOfAnother", Context, path, parentFolderPath );
#endif
path = Path.GetFullPath( path ).Replace( '\\', '/' );
parentFolderPath = Path.GetFullPath( parentFolderPath ).Replace( '\\', '/' );

if( path == parentFolderPath )
return false;

if( parentFolderPath[parentFolderPath.Length - 1] != '/' )
parentFolderPath += "/";

return path != parentFolderPath && path.StartsWith( parentFolderPath, System.StringComparison.OrdinalIgnoreCase );
}

public static string GetDirectoryName( string path )
{
#if !UNITY_EDITOR && UNITY_ANDROID
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.simplefilebrowser",
"displayName": "Simple File Browser",
"version": "1.5.7",
"version": "1.5.8",
"documentationUrl": "https://github.com/yasirkula/UnitySimpleFileBrowser",
"changelogUrl": "https://github.com/yasirkula/UnitySimpleFileBrowser/releases",
"licensesUrl": "https://github.com/yasirkula/UnitySimpleFileBrowser/blob/master/LICENSE.txt",
Expand Down

0 comments on commit 1e8a795

Please sign in to comment.