Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow users to pick local audio source location #78

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;
Expand All @@ -11,9 +10,13 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import java.io.File;
import java.util.Arrays;


public class SettingsActivity extends AppCompatActivity {

Expand Down Expand Up @@ -63,7 +66,11 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
if (context == null) {
Toast.makeText(getContext(), "Cannot get local audio folder, as context is null.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getContext(), "Local audio folder: " + context.getExternalFilesDir(null), Toast.LENGTH_LONG).show();
ListPreference folderPreference = findPreference("storage_location");
String folder = folderPreference != null && folderPreference.getValue() != null ?
folderPreference.getValue() : context.getExternalFilesDir(null).getAbsolutePath();
Toast.makeText(getContext(), "Local audio folder: " + folder, Toast.LENGTH_LONG).show();

// TODO snackbar?
// getView() seems to be null...
// Snackbar snackbar = Snackbar.make(getView().findViewById(R.id.settings),
Expand All @@ -73,7 +80,19 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
}
return true;
});
}

preference = findPreference("storage_location");
if (preference != null) {
Context context = getContext();
if (context == null) {
Toast.makeText(getContext(), "Cannot get local audio folder, as context is null.", Toast.LENGTH_LONG).show();
} else {
String[] dirs = Arrays.stream(context.getExternalFilesDirs(null)).map(File::getAbsolutePath).toArray(String[]::new);
preference.setDefaultValue(dirs[0]); // The first value is equivalent to context.getExternalFilesDir(null)
((ListPreference) preference).setEntries(dirs);
((ListPreference) preference).setEntryValues(dirs);
}
}

EditTextPreference corsHostPreference = findPreference("cors_hostname");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.preference.PreferenceManager;
import androidx.room.Room;
import androidx.sqlite.db.SimpleSQLiteQuery;

Expand All @@ -27,6 +29,8 @@
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.URLDecoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -76,7 +80,17 @@ public LocalAudioAPIRouting(Context context) {

private EntriesDatabase getDB() {
// TODO global instance?
File databasePath = new File(context.getExternalFilesDir(null), "android.db");
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
File externalFilesDir = context.getExternalFilesDir(null);
String preferredFilesDirPath = sharedPreferences.getString("storage_location", externalFilesDir.getAbsolutePath());

// If the preferences point to a file store that no longer is available
// Attempt the
if (!Files.isReadable(Paths.get(preferredFilesDirPath))){
preferredFilesDirPath = externalFilesDir.getAbsolutePath();
}

File databasePath = new File(preferredFilesDirPath, "android.db");
EntriesDatabase db = Room.databaseBuilder(context,
EntriesDatabase.class, databasePath.toString()).build();
return db;
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<string name="settings_forvo_language_title">Forvo language</string>
<string name="settings_forvo_language_summary">Sets the language to use for Forvo audio.</string>
<string name="settings_forvo_language_dialog_title">Select Language</string>
<string name="settings_preferred_storage_location_title">Choose Local Audio Directory Location</string>
<string name="settings_preferred_storage_location_summary">Allows you to choose where to fetch local audio data from.</string>
<string name="get_dir_path_title">Print Local Audio Directory</string>
<string name="get_dir_path_title_summary">Prints the expected directory path where the local audio is searched in.</string>
<string name="dialog_notif_perm_info">This app uses a persistent notification to inform you that the server is running. Please enable notifications to see this.</string>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
android:entries="@array/forvo_language_entries"
android:entryValues="@array/forvo_language_values" />

<ListPreference
app:key="storage_location"
app:title="@string/settings_preferred_storage_location_title"
app:summary="@string/settings_preferred_storage_location_summary"/>
<Preference
app:key="get_dir_path"
app:title="@string/get_dir_path_title"
Expand Down