Skip to content

Commit

Permalink
Merge branch 'pr/773-configurable-file-root'
Browse files Browse the repository at this point in the history
  • Loading branch information
nevenz committed Nov 20, 2020
2 parents 761368e + cd9f5d1 commit ea2b78b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
19 changes: 19 additions & 0 deletions app/src/main/java/com/orgzly/android/prefs/AppPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,25 @@ public static int imagesScaleDownToWidthValue(Context context) {
context.getResources().getString(R.string.pref_default_images_scale_down_to_width_value)));
}

/*
* File relative path.
*/

/** Root for file:/xxx links */
public static String fileAbsoluteRoot(Context context) {
return getDefaultSharedPreferences(context).getString(
context.getResources().getString(R.string.pref_key_file_absolute_root),
context.getResources().getString(R.string.pref_default_file_absolute_root));
}

/** Root for file:xxx links */
public static String fileRelativeRoot(Context context) {
return getDefaultSharedPreferences(context).getString(
context.getResources().getString(R.string.pref_key_file_relative_root),
Environment.getExternalStorageDirectory().getPath()
);
}

/*
* Note's metadata visibility
*/
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/com/orgzly/android/ui/ImageLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.bumptech.glide.request.RequestOptions
import com.orgzly.R
import com.orgzly.android.usecase.LinkFindTarget
import com.orgzly.android.usecase.UseCaseRunner
import com.orgzly.android.util.LogUtils


Expand Down Expand Up @@ -52,11 +54,11 @@ object ImageLoader {
// Get the current context
val context = App.getAppContext()

// Get the file
val file = if (path.startsWith('/')) {
File(path)
} else {
File(Environment.getExternalStorageDirectory(), path)
val file = UseCaseRunner.run(LinkFindTarget(path)).userData

if (file !is File) {
if (BuildConfig.LOG_DEBUG) LogUtils.d(TAG, "Did not find a File target for $path, actually found $file")
return
}

if (file.exists()) {
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/orgzly/android/usecase/LinkFindTarget.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.orgzly.android.usecase

import android.os.Environment
import com.orgzly.android.App
import com.orgzly.android.BookName
import com.orgzly.android.data.DataRepository
import com.orgzly.android.prefs.AppPreferences
import java.io.File

class LinkFindTarget(val path: String) : UseCase() {
val context = App.getAppContext();

override fun run(dataRepository: DataRepository): UseCaseResult {
val target = openLink(dataRepository, path)

Expand All @@ -16,16 +19,15 @@ class LinkFindTarget(val path: String) : UseCase() {

private fun openLink(dataRepository: DataRepository, path: String): Any {
return if (isAbsolute(path)) {
File(path)

File(AppPreferences.fileAbsoluteRoot(context), path)
} else {
isMaybeBook(path)?.let { bookName ->
dataRepository.getBook(bookName.name)?.let {
return it
}
}

File(Environment.getExternalStorageDirectory(), path)
File(AppPreferences.fileRelativeRoot(context), path)
}
}

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/prefs_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@
<string name="pref_key_images_scale_down_to_width_value" translatable="false">pref_key_set_images_fixed_width_value</string>
<string name="pref_default_images_scale_down_to_width_value" translatable="false">256</string>

<string name="pref_key_file_absolute_root">pref_key_file_absolute_root</string>
<string name="pref_default_file_absolute_root">/</string>

<string name="pref_key_file_relative_root">pref_key_file_relative_root</string>

<!-- Separate notes with an empty line -->
<string name="pref_key_separate_notes_with_new_line" translatable="false">pref_key_separate_notes_with_new_line</string>
<string name="pref_default_separate_notes_with_new_line" translatable="false">@string/pref_value_separate_notes_with_new_line_multi_line_notes_only</string>
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 @@ -624,6 +624,9 @@
<string name="scale_down_to_width">Scale down to width</string>
<string name="scale_down_to_width_summary">Scale down image to specified width</string>

<string name="file_absolute_root">Relative path for "file:/"</string>
<string name="file_relative_root">Relative path for "file:"</string>

<string name="argument_used">%s used</string>
<string name="argument_detected">%s detected</string>
<string name="argument_selected">%s selected</string>
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/xml/prefs_screen_notebooks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,23 @@
android:defaultValue="@string/pref_default_images_scale_down_to_width_value"
app:min="1"
android:dependency="@string/pref_key_images_scale_down_to_width"/>

<EditTextPreference
android:key="@string/pref_key_file_absolute_root"
android:title="@string/file_absolute_root"
android:selectAllOnFocus="false"
android:inputType="text"
android:maxLines="1"
android:defaultValue="@string/pref_default_file_absolute_root"
app:useSimpleSummaryProvider="true"/>

<EditTextPreference
android:key="@string/pref_key_file_relative_root"
android:title="@string/file_relative_root"
android:selectAllOnFocus="false"
android:inputType="text"
android:maxLines="1"
app:useSimpleSummaryProvider="true"/>

</PreferenceCategory>
</androidx.preference.PreferenceScreen>

0 comments on commit ea2b78b

Please sign in to comment.