From dde8d24e0c49151e6ad521be7cd3c1716459755c Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Mon, 11 Mar 2019 09:43:50 +0100 Subject: [PATCH] Fix selecting parent folder after "Sharing to Syncthing" subfolder (fixes #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) --- .../activities/FolderPickerActivity.java | 62 +++++++++++++----- .../activities/ShareActivity.java | 8 ++- .../ic_arrow_upward_white_24.png | Bin 0 -> 205 bytes .../ic_arrow_upward_white_24.png | Bin 0 -> 185 bytes .../ic_arrow_upward_white_24.png | Bin 0 -> 246 bytes .../ic_arrow_upward_white_24.png | Bin 0 -> 301 bytes .../ic_arrow_upward_white_24.png | Bin 0 -> 392 bytes app/src/main/res/menu/folder_picker.xml | 7 ++ app/src/main/res/values/strings.xml | 3 + 9 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/ic_arrow_upward_white_24.png create mode 100644 app/src/main/res/drawable-mdpi/ic_arrow_upward_white_24.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_arrow_upward_white_24.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_arrow_upward_white_24.png create mode 100644 app/src/main/res/drawable-xxxhdpi/ic_arrow_upward_white_24.png diff --git a/app/src/main/java/com/nutomic/syncthingandroid/activities/FolderPickerActivity.java b/app/src/main/java/com/nutomic/syncthingandroid/activities/FolderPickerActivity.java index 31c2ca78..9056515e 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/FolderPickerActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/FolderPickerActivity.java @@ -12,6 +12,7 @@ import android.support.annotation.NonNull; 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; @@ -41,9 +42,16 @@ import java.util.Iterator; 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"; @@ -89,12 +97,12 @@ public class FolderPickerActivity extends SyncthingActivity 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(); } /** @@ -105,16 +113,15 @@ public class FolderPickerActivity extends SyncthingActivity @SuppressLint("NewApi") private void populateRoots() { ArrayList 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)); @@ -167,6 +174,11 @@ public class FolderPickerActivity extends SyncthingActivity .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())); @@ -181,6 +193,26 @@ public class FolderPickerActivity extends SyncthingActivity } } + 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. */ @@ -287,14 +319,8 @@ public class FolderPickerActivity extends SyncthingActivity */ @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(); } /** diff --git a/app/src/main/java/com/nutomic/syncthingandroid/activities/ShareActivity.java b/app/src/main/java/com/nutomic/syncthingandroid/activities/ShareActivity.java index 60754a41..fa9e2686 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/activities/ShareActivity.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/activities/ShareActivity.java @@ -185,9 +185,11 @@ public class ShareActivity extends SyncthingActivity 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()); diff --git a/app/src/main/res/drawable-hdpi/ic_arrow_upward_white_24.png b/app/src/main/res/drawable-hdpi/ic_arrow_upward_white_24.png new file mode 100644 index 0000000000000000000000000000000000000000..de60be40434f4fdef5a4543737518c1ab9c1fb16 GIT binary patch literal 205 zcmeAS@N?(olHy`uVBq!ia0vp^Dj>|k0wldT1B8Lp6i*k&kP61PXBgR90z_CZ%+A`# zD8R&Z>-Yf<4whRtcnUsOoKW3=T>aB|CcdunM<)#3FJJx=DYbr+N!-(nEnMdHv*w3g zOndHQT+APODJRGBqEF6FR;^5_206x@ys%4auFX+iyE`)=x`0 zdT=(EoAU8fs~NA~_?UdQ^vTufvPmXE=iODc0zV3|howi&i2Zgb8|Xd;Pgg&ebxsLQ E0O0*j%m4rY literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-mdpi/ic_arrow_upward_white_24.png b/app/src/main/res/drawable-mdpi/ic_arrow_upward_white_24.png new file mode 100644 index 0000000000000000000000000000000000000000..b4f0e281bdaee3937e25e460669047b09d121465 GIT binary patch literal 185 zcmeAS@N?(olHy`uVBq!ia0vp^5+KaM1|%Pp+x`Gj4W2HJAr*{o`y1IB9C#dlZ)Rd) zVq~m6>G11`#@-!e28S0--obut+4q>>pW+P+K|%8-PWsfdNB2I%8J+x-A#3EsB_#_uZHBzI&aJ z_c|f(bwb|jgtiw5VZYLP2!yatv=UdDaP|umz;^%>7m;xGJ@5?x>;=Nv&t?A$h=jB6 zfC?~Tt_X2I*}eu!>?%Usx3Fr!i0MOU`?2gj0AMQ-yxE=&-~vXBe07B%FC_1__kj|d wjX2}KIPe50z(x#^696lK00Ic$m_QUoPnnoF8Kn1VJpcdz07*qoM6N<$f_x%kegFUf literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxhdpi/ic_arrow_upward_white_24.png b/app/src/main/res/drawable-xxhdpi/ic_arrow_upward_white_24.png new file mode 100644 index 0000000000000000000000000000000000000000..30c51b706bd3a3e5d76f8209d0228cfa7fda7ed3 GIT binary patch literal 301 zcmeAS@N?(olHy`uVBq!ia0vp^9w5xY1|&n@ZgvM!Up!qLLn;{GUSecpa*%L&XzRqt zVU^M#aKY4@{ic~fM6-L(z2pO%e*8K7&)OISHd{V_yE##MPQKFHLYGq(O0F^!nfx4t z)D=1>Pdswc<57xAp+%7WIpsZ!OX}u4zm@sUdi`Dopg_b~-(=Q_zY2u}oL(J2>AmjM}xIm$=6u_~^L!Qa Y2Q`684o&?6p#K>>UHx3vIVCg!06mImX8-^I literal 0 HcmV?d00001 diff --git a/app/src/main/res/drawable-xxxhdpi/ic_arrow_upward_white_24.png b/app/src/main/res/drawable-xxxhdpi/ic_arrow_upward_white_24.png new file mode 100644 index 0000000000000000000000000000000000000000..ae1f6d09cd4f077505604d3bcd720c86022f2bd1 GIT binary patch literal 392 zcmeAS@N?(olHy`uVBq!ia0vp^2_VeD0wg^q?%&M7!07Mk;uuoF`1X8bTN9&z>qABr z!HxO5XNZXjT-dC`(&WJLtuXB^!_tRA`wizX?|4wR&Bp}{9zNXs{{QpE@6+vm)jcDE z3VzSs^6$6(p+!Pc@8&0o_OE@kMEKVG*+-qz77G7*Z_oUq<=pS*d*T@;6{!n@ zsUWsT;m-cB@2U=Bw~ulh`nz@}UxxCD@2n2Yj{Ef)vkv7dI@P=c$_BofC-m)GRbj{P zysx#g3WEE5{nhRyrGK+>P>^|3do*x|J_F04{M!GiPPOq29Y@`T`o8^3(!V3l!1VC) oEMbPfjKI)C1t!1%6$F8c^5SO8y;8GYp8+ZGboFyt=akR{0Cv%(!vFvP literal 0 HcmV?d00001 diff --git a/app/src/main/res/menu/folder_picker.xml b/app/src/main/res/menu/folder_picker.xml index e0331849..a0c0e039 100644 --- a/app/src/main/res/menu/folder_picker.xml +++ b/app/src/main/res/menu/folder_picker.xml @@ -6,6 +6,13 @@ + + Failed to create folder + + Go up +