From d9c043a7879e010649d86fd01ab01a4f4e09eef6 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 12 Jul 2016 23:17:37 +0200 Subject: [PATCH] put directories and files in the same list + permission handling --- app/src/main/AndroidManifest.xml | 2 + .../simplemobiletools/filemanager/Utils.java | 14 +++++ .../filemanager/activities/MainActivity.java | 55 +++++++++++++++---- .../filemanager/models/Directory.java | 32 ----------- .../filemanager/models/FileDirItem.java | 45 +++++++++++++++ app/src/main/res/values/strings.xml | 1 + 6 files changed, 106 insertions(+), 43 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/filemanager/models/Directory.java create mode 100644 app/src/main/java/com/simplemobiletools/filemanager/models/FileDirItem.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 72eaeabf..3ae46d84 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + dirs = getDirectories(); - Collections.sort(dirs); } - private List getDirectories() { - final List dirs = new ArrayList<>(); + @Override + protected void onResume() { + super.onResume(); + tryInitFileManager(); + } + + private void tryInitFileManager() { + if (Utils.hasStoragePermission(getApplicationContext())) { + initializeFileManager(); + } else { + ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION); + } + } + + private void initializeFileManager() { + List items = getItems(); + Collections.sort(items); + } + + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + if (requestCode == STORAGE_PERMISSION) { + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { + initializeFileManager(); + } else { + Utils.showToast(getApplicationContext(), R.string.no_permissions); + finish(); + } + } + } + + private List getItems() { + final List items = new ArrayList<>(); final String path = Environment.getExternalStorageDirectory().toString(); final File root = new File(path); File[] files = root.listFiles(); for (File file : files) { - if (file.isDirectory()) { - final String dirPath = file.getAbsolutePath(); - final String dirName = Utils.getFilename(dirPath); - dirs.add(new Directory(dirPath, dirName)); - } + final String curPath = file.getAbsolutePath(); + final String curName = Utils.getFilename(curPath); + items.add(new FileDirItem(curPath, curName, file.isDirectory())); } - return dirs; + return items; } } diff --git a/app/src/main/java/com/simplemobiletools/filemanager/models/Directory.java b/app/src/main/java/com/simplemobiletools/filemanager/models/Directory.java deleted file mode 100644 index 11463d11..00000000 --- a/app/src/main/java/com/simplemobiletools/filemanager/models/Directory.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.simplemobiletools.filemanager.models; - -public class Directory implements Comparable { - private final String path; - private final String name; - - public Directory(String path, String name) { - this.path = path; - this.name = name; - } - - public String getPath() { - return path; - } - - public String getName() { - return name; - } - - @Override - public int compareTo(Object object) { - final Directory directory = (Directory) object; - return this.name.compareToIgnoreCase(directory.getName()); - } - - @Override - public String toString() { - return "Directory {" + - "name=" + getName() + - ", path=" + getPath() + "}"; - } -} diff --git a/app/src/main/java/com/simplemobiletools/filemanager/models/FileDirItem.java b/app/src/main/java/com/simplemobiletools/filemanager/models/FileDirItem.java new file mode 100644 index 00000000..a5a8a7ea --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/filemanager/models/FileDirItem.java @@ -0,0 +1,45 @@ +package com.simplemobiletools.filemanager.models; + +public class FileDirItem implements Comparable { + private final String mPath; + private final String mName; + private final boolean mIsDirectory; + + public FileDirItem(String path, String name, boolean isDirectory) { + mPath = path; + mName = name; + mIsDirectory = isDirectory; + } + + public String getPath() { + return mPath; + } + + public String getName() { + return mName; + } + + public boolean getIsDirectory() { + return mIsDirectory; + } + + @Override + public int compareTo(Object object) { + final FileDirItem item = (FileDirItem) object; + if (mIsDirectory && !item.getIsDirectory()) { + return -1; + } else if (!mIsDirectory && item.getIsDirectory()) { + return 1; + } + + return mName.compareToIgnoreCase(item.getName()); + } + + @Override + public String toString() { + return "FileDirItem {" + + "name=" + getName() + + ", isDirectory=" + getIsDirectory() + + ", path=" + getPath() + "}"; + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index efa6057d..1a18523a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ Simple File Manager + We need the permission to access your storage