put directories and files in the same list + permission handling

This commit is contained in:
tibbi 2016-07-12 23:17:37 +02:00
parent e9c7154a7e
commit d9c043a787
6 changed files with 106 additions and 43 deletions

View File

@ -2,6 +2,8 @@
<manifest package="com.simplemobiletools.filemanager"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"

View File

@ -1,7 +1,21 @@
package com.simplemobiletools.filemanager;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
public class Utils {
public static String getFilename(final String path) {
return path.substring(path.lastIndexOf("/") + 1);
}
public static void showToast(Context context, int resId) {
Toast.makeText(context, context.getResources().getString(resId), Toast.LENGTH_SHORT).show();
}
public static boolean hasStoragePermission(Context cxt) {
return ContextCompat.checkSelfPermission(cxt, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}
}

View File

@ -1,12 +1,15 @@
package com.simplemobiletools.filemanager.activities;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import com.simplemobiletools.filemanager.R;
import com.simplemobiletools.filemanager.Utils;
import com.simplemobiletools.filemanager.models.Directory;
import com.simplemobiletools.filemanager.models.FileDirItem;
import java.io.File;
import java.util.ArrayList;
@ -14,27 +17,57 @@ import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int STORAGE_PERMISSION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<Directory> dirs = getDirectories();
Collections.sort(dirs);
}
private List<Directory> getDirectories() {
final List<Directory> 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<FileDirItem> 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<FileDirItem> getItems() {
final List<FileDirItem> 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;
}
}

View File

@ -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() + "}";
}
}

View File

@ -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() + "}";
}
}

View File

@ -1,3 +1,4 @@
<resources>
<string name="app_name">Simple File Manager</string>
<string name="no_permissions">We need the permission to access your storage</string>
</resources>