forked from yasirkula/UnitySimpleFileBrowser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFileBrowser.java
More file actions
130 lines (114 loc) · 4.13 KB
/
FileBrowser.java
File metadata and controls
130 lines (114 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.yasirkula.unity;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Environment;
import java.io.File;
/**
* Created by yasirkula on 30.10.2017.
*/
public class FileBrowser
{
public static String GetExternalDrives()
{
File primary = Environment.getExternalStorageDirectory();
String primaryPath = primary.getAbsolutePath();
String result = primaryPath + ":";
// Try paths saved at system environments
// Credit: https://stackoverflow.com/a/32088396/2373034
String strSDCardPath = System.getenv( "SECONDARY_STORAGE" );
if( strSDCardPath == null || strSDCardPath.length() == 0 )
strSDCardPath = System.getenv( "EXTERNAL_SDCARD_STORAGE" );
if( strSDCardPath != null && strSDCardPath.length() > 0 )
{
String[] externalPaths = strSDCardPath.split( ":" );
for( int i = 0; i < externalPaths.length; i++ )
{
String path = externalPaths[i];
if( path != null && path.length() > 0 )
{
File file = new File( path );
if( file.exists() && file.isDirectory() && file.canRead() && !file.getAbsolutePath().equalsIgnoreCase( primaryPath ) )
{
String absolutePath = file.getAbsolutePath() + File.separator + "Android";
if( new File( absolutePath ).exists() )
{
try
{
// Check if two paths lead to same storage (aliases)
if( !primary.getCanonicalPath().equals( file.getCanonicalPath() ) )
result += file.getAbsolutePath() + ":";
}
catch( Exception e ) { }
}
}
}
}
}
// Try most common possible paths
// Credit: https://gist.github.com/PauloLuan/4bcecc086095bce28e22
String[] possibleRoots = new String[] { "/storage", "/mnt", "/storage/removable",
"/removable", "/data", "/mnt/media_rw", "/mnt/sdcard0" };
for( String root : possibleRoots )
{
try
{
File fileList[] = new File( root ).listFiles();
for( File file : fileList )
{
if( file.exists() && file.isDirectory() && file.canRead() && !file.getAbsolutePath().equalsIgnoreCase( primaryPath ) )
{
String absolutePath = file.getAbsolutePath() + File.separator + "Android";
if( new File( absolutePath ).exists() )
{
try
{
// Check if two paths lead to same storage (aliases)
if( !primary.getCanonicalPath().equals( file.getCanonicalPath() ) )
result += file.getAbsolutePath() + ":";
}
catch( Exception ex ) { }
}
}
}
}
catch( Exception e ) { }
}
return result;
}
public static int CheckPermission( Context context )
{
if( Build.VERSION.SDK_INT < Build.VERSION_CODES.M )
return 1;
return CheckPermissionInternal( context );
}
// Credit: https://github.com/Over17/UnityAndroidPermissions/blob/0dca33e40628f1f279decb67d901fd444b409cd7/src/UnityAndroidPermissions/src/main/java/com/unity3d/plugin/UnityAndroidPermissions.java
@TargetApi( Build.VERSION_CODES.M )
private static int CheckPermissionInternal( Context context )
{
if( context.checkSelfPermission( Manifest.permission.WRITE_EXTERNAL_STORAGE ) == PackageManager.PERMISSION_GRANTED &&
context.checkSelfPermission( Manifest.permission.READ_EXTERNAL_STORAGE ) == PackageManager.PERMISSION_GRANTED )
return 1;
return 0;
}
// Credit: https://github.com/Over17/UnityAndroidPermissions/blob/0dca33e40628f1f279decb67d901fd444b409cd7/src/UnityAndroidPermissions/src/main/java/com/unity3d/plugin/UnityAndroidPermissions.java
public static void RequestPermission( Context context, final FileBrowserPermissionReceiver permissionReceiver, final int lastCheckResult )
{
if( CheckPermission( context ) == 1 )
{
permissionReceiver.OnPermissionResult( 1 );
return;
}
if( lastCheckResult == 0 ) // If user clicked "Don't ask again" before, don't bother asking them again
{
permissionReceiver.OnPermissionResult( 0 );
return;
}
final Fragment request = new FileBrowserPermissionFragment( permissionReceiver );
( (Activity) context ).getFragmentManager().beginTransaction().add( 0, request ).commit();
}
}