11package com .spse .javamodsoptimiser ;
22
3+ import android .content .Context ;
4+ import android .content .Intent ;
5+ import android .database .Cursor ;
6+ import android .net .Uri ;
7+ import android .provider .OpenableColumns ;
38import android .util .Log ;
9+ import android .webkit .MimeTypeMap ;
410
511import java .io .File ;
612import java .io .IOException ;
713
8- import static com .spse .javamodsoptimiser .MainActivity .TEMP_PATH ;
9-
14+ /**
15+ * Class for everything related to interacting with files
16+ */
1017public class FileManager {
11- //Class related to everything related to files
12-
1318
1419 public static boolean removeFile (String inputPath , String inputFile ){
1520 return removeFile (inputPath + inputFile );
@@ -50,17 +55,11 @@ public static boolean fileExists(String absolutePath){
5055 public static boolean createFolder (String absolutePathToFolder ){
5156 File file = new File (absolutePathToFolder );
5257 if (!file .exists ()){
53- return file .mkdir ();
58+ return file .mkdirs ();
5459 }
5560 return true ;
5661 }
5762
58- public static void removeLeftOvers (){
59- //Remove anything within the TEMP_PATH
60-
61- walkAndRemove (TEMP_PATH );
62- }
63-
6463 public static boolean compareFileSize (String fileOne , String fileTwo ) throws IOException {
6564 //return true if file1 > file2
6665 File file1 = new File (fileOne );
@@ -75,23 +74,71 @@ public static boolean compareFileSize(String fileOne, String fileTwo) throws IOE
7574 return file1 .length () > file2 .length ();
7675 }
7776
78- private static void walkAndRemove (String path ) {
79-
77+ /**
78+ * Remove everything inside a folder. Works recursively.
79+ * @param path The path to start from.
80+ */
81+ public static void removeEverything (String path ) {
8082 File root = new File (path );
8183 File [] list = root .listFiles ();
8284
8385 if (list == null ) return ;
8486
8587 for (File f : list ) {
8688 if (f .isDirectory ()) {
87- walkAndRemove (f .getAbsolutePath ());
88- }else {
89- removeFile (f .getAbsolutePath ());
90- return ;
89+ removeEverything (f .getAbsolutePath ());
9190 }
92- removeFile ( f . getAbsolutePath () );
91+ f . delete ( );
9392 }
9493 }
9594
95+ /**
96+ * Tries to get an Uri from the various sources
97+ */
98+ public static Uri [] getUriData (Intent intent ){
99+ Uri [] mUriData = new Uri []{intent .getData ()};
100+ if (mUriData [0 ] != null ) return mUriData ;
101+ try {
102+ mUriData = new Uri [intent .getClipData ().getItemCount ()];
103+ for (int i =0 ; i < mUriData .length ; ++i ){
104+ mUriData [i ] = intent .getClipData ().getItemAt (i ).getUri ();
105+ }
106+ }catch (Exception ignored ){}
107+ return mUriData ;
108+ }
109+
110+ /**
111+ * Extract the file name from an Uri
112+ * @param ctx Context
113+ * @param uri The Uri to extract from
114+ * @return The file name
115+ */
116+ public static String getFileName (Context ctx , Uri uri ){
117+ Cursor returnCursor =
118+ ctx .getContentResolver ().query (uri , null , null , null , null );
119+
120+ int nameIndex = returnCursor .getColumnIndex (OpenableColumns .DISPLAY_NAME );
121+ returnCursor .moveToFirst ();
122+ String fileName = returnCursor .getString (nameIndex );
123+ returnCursor .close ();
124+ return fileName ;
125+ }
126+
127+ /**
128+ * Extract the file size from an Uri
129+ * @param ctx Context
130+ * @param uri The Uri to extract from
131+ * @return The file name
132+ */
133+ public static long getFileSize (Context ctx , Uri uri ){
134+ Cursor returnCursor =
135+ ctx .getContentResolver ().query (uri , null , null , null , null );
136+
137+ int sizeIndex = returnCursor .getColumnIndex (OpenableColumns .SIZE );
138+ returnCursor .moveToFirst ();
139+ long fileSize = returnCursor .getLong (sizeIndex );
140+ returnCursor .close ();
141+ return fileSize ;
142+ }
96143
97144}
0 commit comments