Changed folder picker to show sorted, unique roots (fixes #388).

This commit is contained in:
Felix Ableitner 2015-05-17 17:35:37 +02:00
parent 41ad260035
commit f0daeb0cf8
1 changed files with 40 additions and 20 deletions

View File

@ -29,10 +29,10 @@ import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.SyncthingService; import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.TreeSet;
/** /**
* Activity that allows selecting a directory in the local file system. * Activity that allows selecting a directory in the local file system.
@ -58,7 +58,6 @@ public class FolderPickerActivity extends SyncthingActivity
private File mLocation; private File mLocation;
@Override @Override
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -72,24 +71,7 @@ public class FolderPickerActivity extends SyncthingActivity
mRootsAdapter = new RootsAdapter(this); mRootsAdapter = new RootsAdapter(this);
mListView.setAdapter(mFilesAdapter); mListView.setAdapter(mFilesAdapter);
// Populate roots. populateRoots();
ArrayList<File> roots = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
roots.addAll(Arrays.asList(getExternalFilesDirs(null)));
}
roots.add(Environment.getExternalStorageDirectory());
File storage = new File("/storage/");
if (storage.exists() && storage.isDirectory())
Collections.addAll(roots, storage.listFiles());
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
if (sp.getBoolean("advanced_folder_picker", false)) {
roots.add(new File("/"));
}
for (File f : roots) {
if (f != null)
mRootsAdapter.add(f);
}
if (getIntent().hasExtra(EXTRA_INITIAL_DIRECTORY)) { if (getIntent().hasExtra(EXTRA_INITIAL_DIRECTORY)) {
displayFolder(new File(getIntent().getStringExtra(EXTRA_INITIAL_DIRECTORY))); displayFolder(new File(getIntent().getStringExtra(EXTRA_INITIAL_DIRECTORY)));
@ -103,6 +85,44 @@ public class FolderPickerActivity extends SyncthingActivity
} }
} }
/**
* Reads available storage devices/folders from various APIs and inserts them into
* {@link #mRootsAdapter}.
*/
@SuppressLint("NewApi")
private void populateRoots() {
// Use own comparator to handle null values.
TreeSet<File> roots = new TreeSet<>(new Comparator<File>() {
@Override
public int compare(File lhs, File rhs) {
if (lhs == null | rhs == null)
return 0;
return lhs.compareTo(rhs);
}
});
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
roots.addAll(Arrays.asList(getExternalFilesDirs(null)));
}
roots.add(Environment.getExternalStorageDirectory());
// Add all sd cards. These might already be in {@link getExternalFilesDirs}, and might not
// be writable.
File storage = new File("/storage/");
if (storage.exists() && storage.isDirectory())
Collections.addAll(roots, storage.listFiles());
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
if (sp.getBoolean("advanced_folder_picker", false)) {
roots.add(new File("/"));
}
for (File f : roots) {
if (f != null)
mRootsAdapter.add(f);
}
}
@Override @Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) { public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
super.onServiceConnected(componentName, iBinder); super.onServiceConnected(componentName, iBinder);