From e9c7154a7e9be6be73e2b88378dcca4fa05d9eff Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 12 Jul 2016 22:51:54 +0200 Subject: [PATCH] get a sorted list of directories --- app/src/main/AndroidManifest.xml | 5 +-- .../filemanager/MainActivity.java | 13 ------ .../simplemobiletools/filemanager/Utils.java | 7 ++++ .../filemanager/activities/MainActivity.java | 40 +++++++++++++++++++ .../filemanager/models/Directory.java | 32 +++++++++++++++ 5 files changed, 81 insertions(+), 16 deletions(-) delete mode 100644 app/src/main/java/com/simplemobiletools/filemanager/MainActivity.java create mode 100644 app/src/main/java/com/simplemobiletools/filemanager/Utils.java create mode 100644 app/src/main/java/com/simplemobiletools/filemanager/activities/MainActivity.java create mode 100644 app/src/main/java/com/simplemobiletools/filemanager/models/Directory.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 016ab9db..72eaeabf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,7 +8,7 @@ android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> - + @@ -16,5 +16,4 @@ - - \ No newline at end of file + diff --git a/app/src/main/java/com/simplemobiletools/filemanager/MainActivity.java b/app/src/main/java/com/simplemobiletools/filemanager/MainActivity.java deleted file mode 100644 index 6c095538..00000000 --- a/app/src/main/java/com/simplemobiletools/filemanager/MainActivity.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.simplemobiletools.filemanager; - -import android.support.v7.app.AppCompatActivity; -import android.os.Bundle; - -public class MainActivity extends AppCompatActivity { - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - } -} diff --git a/app/src/main/java/com/simplemobiletools/filemanager/Utils.java b/app/src/main/java/com/simplemobiletools/filemanager/Utils.java new file mode 100644 index 00000000..9addca4b --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/filemanager/Utils.java @@ -0,0 +1,7 @@ +package com.simplemobiletools.filemanager; + +public class Utils { + public static String getFilename(final String path) { + return path.substring(path.lastIndexOf("/") + 1); + } +} diff --git a/app/src/main/java/com/simplemobiletools/filemanager/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/filemanager/activities/MainActivity.java new file mode 100644 index 00000000..ac05f3d7 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/filemanager/activities/MainActivity.java @@ -0,0 +1,40 @@ +package com.simplemobiletools.filemanager.activities; + +import android.os.Bundle; +import android.os.Environment; +import android.support.v7.app.AppCompatActivity; + +import com.simplemobiletools.filemanager.R; +import com.simplemobiletools.filemanager.Utils; +import com.simplemobiletools.filemanager.models.Directory; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + List dirs = getDirectories(); + Collections.sort(dirs); + } + + private List getDirectories() { + final List dirs = 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)); + } + } + return dirs; + } +} diff --git a/app/src/main/java/com/simplemobiletools/filemanager/models/Directory.java b/app/src/main/java/com/simplemobiletools/filemanager/models/Directory.java new file mode 100644 index 00000000..11463d11 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/filemanager/models/Directory.java @@ -0,0 +1,32 @@ +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() + "}"; + } +}