Skip to content

Commit

Permalink
Fix selecting parent folder after "Sharing to Syncthing" subfolder (f…
Browse files Browse the repository at this point in the history
…ixes #365) (#366)

* folder_picker: Add icon on action bar to select

* Add drawable: ic_arrow_upward_white_24

* layout/folder_picker: Add folder_go_up (fixes #365)

* Reindent code

* Fix selecting parent folder after "Sharing to Syncthing" subfolder (fixes #365)
  • Loading branch information
Catfriend1 authored Mar 11, 2019
1 parent 055a72c commit dde8d24
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand Down Expand Up @@ -41,9 +42,16 @@
public class FolderPickerActivity extends SyncthingActivity
implements AdapterView.OnItemClickListener {

private static final String TAG = "FolderPickerActivity";

private static final String EXTRA_INITIAL_DIRECTORY =
"com.github.catfriend1.syncthingandroid.activities.FolderPickerActivity.INITIAL_DIRECTORY";

/**
* If requested by {@link #createIntent}, we'll only use one root dir and enforce
* the user stays within that. {@link #populateRoots} will respect this extra.
* See issue #366.
*/
private static final String EXTRA_ROOT_DIRECTORY =
"com.github.catfriend1.syncthingandroid.activities.FolderPickerActivity.ROOT_DIRECTORY";

Expand Down Expand Up @@ -89,12 +97,12 @@ protected void onCreate(Bundle savedInstanceState) {
mListView.setAdapter(mFilesAdapter);

populateRoots();

if (getIntent().hasExtra(EXTRA_INITIAL_DIRECTORY)) {
displayFolder(new File(getIntent().getStringExtra(EXTRA_INITIAL_DIRECTORY)));
} else {
displayRoot();
String initialDirectory = getIntent().getStringExtra(EXTRA_INITIAL_DIRECTORY);
displayFolder(new File(initialDirectory));
return;
}
displayRoot();
}

/**
Expand All @@ -105,16 +113,15 @@ protected void onCreate(Bundle savedInstanceState) {
@SuppressLint("NewApi")
private void populateRoots() {
ArrayList<File> roots = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
roots.addAll(Arrays.asList(getExternalFilesDirs(null)));
roots.remove(getExternalFilesDir(null));
roots.remove(null); // getExternalFilesDirs may return null for an ejected SDcard.
}

String rootDir = getIntent().getStringExtra(EXTRA_ROOT_DIRECTORY);
if (getIntent().hasExtra(EXTRA_ROOT_DIRECTORY) && !TextUtils.isEmpty(rootDir)) {
roots.add(new File(rootDir));
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
roots.addAll(Arrays.asList(getExternalFilesDirs(null)));
roots.remove(getExternalFilesDir(null));
roots.remove(null); // getExternalFilesDirs may return null for an ejected SDcard.
}
roots.add(Environment.getExternalStorageDirectory());
roots.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC));
roots.add(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
Expand Down Expand Up @@ -167,6 +174,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT));
dialog.show();
return true;
case R.id.folder_go_up:
if (canGoUpToSubDir() || canGoUpToRootDir()) {
goUpToParentDir();
}
return true;
case R.id.select:
Intent intent = new Intent()
.putExtra(EXTRA_RESULT_DIRECTORY, Util.formatPath(mLocation.getAbsolutePath()));
Expand All @@ -181,6 +193,26 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
}

public Boolean canGoUpToSubDir() {
return mLocation != null && !mRootsAdapter.contains(mLocation);
}

public Boolean canGoUpToRootDir() {
return mRootsAdapter.contains(mLocation) && mRootsAdapter.getCount() > 1;
}

public void goUpToParentDir() {
if (canGoUpToSubDir()) {
displayFolder(mLocation.getParentFile());
return;
}
if (canGoUpToRootDir()) {
displayRoot();
return;
}
Log.e(TAG, "goUpToParentDir: Cannot go up.");
}

/**
* Creates a new folder with the given name and enters it.
*/
Expand Down Expand Up @@ -287,14 +319,8 @@ public boolean contains(File file) {
*/
@Override
public void onBackPressed() {
if (!mRootsAdapter.contains(mLocation) && mLocation != null) {
displayFolder(mLocation.getParentFile());
} else if (mRootsAdapter.contains(mLocation) && mRootsAdapter.getCount() > 1) {
displayRoot();
} else {
setResult(Activity.RESULT_CANCELED);
finish();
}
setResult(Activity.RESULT_CANCELED);
finish();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ public void onNothingSelected(AdapterView<?> parent) {
browseButton.setOnClickListener(view -> {
Folder folder = (Folder) mFoldersSpinner.getSelectedItem();
File initialDirectory = new File(folder.path, getSavedSubDirectory());
startActivityForResult(FolderPickerActivity.createIntent(getApplicationContext(),
initialDirectory.getAbsolutePath(), folder.path),
FolderPickerActivity.DIRECTORY_REQUEST_CODE);
Intent intent = FolderPickerActivity.createIntent(
getApplicationContext(),
initialDirectory.getAbsolutePath(), folder.path
);
startActivityForResult(intent, FolderPickerActivity.DIRECTORY_REQUEST_CODE);
});

mCancelButton.setOnClickListener(view -> finish());
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions app/src/main/res/menu/folder_picker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
<item
android:id="@+id/select"
android:title="@string/select_folder"
android:icon="@drawable/ic_done_white_24dp"
app:showAsAction="ifRoom" />

<item
android:id="@+id/folder_go_up"
android:title="@string/folder_go_up"
android:icon="@drawable/ic_arrow_upward_white_24"
app:showAsAction="ifRoom" />

<item
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,9 @@ Please report any problems you encounter via Github.</string>

<string name="create_folder_failed">Failed to create folder</string>

<!-- Menu item to go up to parent folder -->
<string name="folder_go_up">Go up</string>

<!-- LogActivity -->


Expand Down

0 comments on commit dde8d24

Please sign in to comment.