1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-23 11:21:29 +00:00

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

View file

@ -29,10 +29,10 @@ import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.syncthing.SyncthingService;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
/**
* Activity that allows selecting a directory in the local file system.
@ -58,7 +58,6 @@ public class FolderPickerActivity extends SyncthingActivity
private File mLocation;
@Override
@SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -72,24 +71,7 @@ public class FolderPickerActivity extends SyncthingActivity
mRootsAdapter = new RootsAdapter(this);
mListView.setAdapter(mFilesAdapter);
// Populate roots.
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);
}
populateRoots();
if (getIntent().hasExtra(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
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
super.onServiceConnected(componentName, iBinder);