From f35afa13da08a058bc28ca0e535e044093148e3a Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Tue, 20 Jun 2017 15:25:30 +0200 Subject: Improve file selection dialog --- .../openvpn/fragments/FileSelectionFragment.java | 44 +++++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) (limited to 'main') diff --git a/main/src/main/java/de/blinkt/openvpn/fragments/FileSelectionFragment.java b/main/src/main/java/de/blinkt/openvpn/fragments/FileSelectionFragment.java index be33072d..74e76cc4 100644 --- a/main/src/main/java/de/blinkt/openvpn/fragments/FileSelectionFragment.java +++ b/main/src/main/java/de/blinkt/openvpn/fragments/FileSelectionFragment.java @@ -6,6 +6,7 @@ package de.blinkt.openvpn.fragments; import android.app.ListFragment; +import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.view.LayoutInflater; @@ -22,10 +23,12 @@ import android.widget.Toast; import java.io.File; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.TreeMap; +import java.util.Vector; import de.blinkt.openvpn.R; import de.blinkt.openvpn.activities.FileSelect; @@ -45,13 +48,13 @@ public class FileSelectionFragment extends ListFragment { private String parentPath; - private String currentPath = ROOT; + private String currentPath = Environment.getExternalStorageDirectory().getAbsolutePath(); private String[] formatFilter = null; private File selectedFile; - private HashMap lastPositions = new HashMap(); + private HashMap lastPositions = new HashMap<>(); private String mStartPath; private CheckBox mInlineImport; private Button mClearButton; @@ -174,7 +177,7 @@ public class FileSelectionFragment extends ListFragment { files = f.listFiles(); if (files == null) - files = new File [] {}; + files = new File[]{}; } myPath.setText(getText(R.string.location) + ": " + currentPath); @@ -192,10 +195,18 @@ public class FileSelectionFragment extends ListFragment { } + TreeMap dirsMap = new TreeMap(); TreeMap dirsPathMap = new TreeMap(); TreeMap filesMap = new TreeMap(); TreeMap filesPathMap = new TreeMap(); + + // add default locations + for (String dir: getExternalStorages()) { + dirsMap.put(dir, dir); + dirsPathMap.put(dir, dir); + } + for (File file : files) { if (file.isDirectory()) { String dirName = file.getName(); @@ -204,7 +215,7 @@ public class FileSelectionFragment extends ListFragment { } else { final String fileName = file.getName(); final String fileNameLwr = fileName.toLowerCase(Locale.getDefault()); - // se ha um filtro de formatos, utiliza-o + if (formatFilter != null) { boolean contains = false; for (String aFormatFilter : formatFilter) { @@ -218,7 +229,6 @@ public class FileSelectionFragment extends ListFragment { filesMap.put(fileName, fileName); filesPathMap.put(fileName, file.getPath()); } - // senao, adiciona todos os arquivos } else { filesMap.put(fileName, fileName); filesPathMap.put(fileName, file.getPath()); @@ -254,6 +264,30 @@ public class FileSelectionFragment extends ListFragment { mList.add(item); } + private Collection getExternalStorages() { + Vector dirs = new Vector<>(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + for (File d : getActivity().getExternalFilesDirs(null)) + dirs.add(getRootOfInnerSdCardFolder(d)); + } else { + dirs.add(Environment.getExternalStorageDirectory().getAbsolutePath()); + } + return dirs; + } + + private static String getRootOfInnerSdCardFolder(File file) { + if (file == null) + return null; + final long totalSpace = file.getTotalSpace(); + while (true) { + final File parentFile = file.getParentFile(); + if (parentFile == null || parentFile.getTotalSpace() != totalSpace + || file.equals(Environment.getExternalStorageDirectory())) + return file.getAbsolutePath(); + file = parentFile; + } + } @Override public void onListItemClick(ListView l, View v, int position, long id) { -- cgit v1.2.3