#2256 External device mgmt is not robust and gives contradictory information - don't depend on isEmulated

This commit is contained in:
Sean Mac Gillicuddy 2020-09-08 11:59:26 +01:00
parent ce527d3b00
commit c4e03f3183
4 changed files with 12 additions and 30 deletions

View File

@ -19,8 +19,9 @@
package org.kiwix.kiwixmobile.settings package org.kiwix.kiwixmobile.settings
import android.os.Bundle import android.os.Bundle
import android.os.Environment import androidx.core.content.ContextCompat
import androidx.preference.Preference import androidx.preference.Preference
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.settings.CorePrefsFragment import org.kiwix.kiwixmobile.core.settings.CorePrefsFragment
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_STORAGE import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_STORAGE
@ -33,13 +34,13 @@ class KiwixPrefsFragment : CorePrefsFragment() {
} }
override fun setStorage() { override fun setStorage() {
if (Environment.isExternalStorageEmulated()) { findPreference<Preference>(PREF_STORAGE)?.title = getString(
findPreference<Preference>(PREF_STORAGE)?.title = if (sharedPreferenceUtil.prefStorage == internalStorage()) R.string.internal_storage
sharedPreferenceUtil.getPrefStorageTitle("Internal") else R.string.external_storage
} else { )
findPreference<Preference>(PREF_STORAGE)?.title =
sharedPreferenceUtil.getPrefStorageTitle("External")
}
findPreference<Preference>(PREF_STORAGE)?.summary = storageCalculator.calculateAvailableSpace() findPreference<Preference>(PREF_STORAGE)?.summary = storageCalculator.calculateAvailableSpace()
} }
private fun internalStorage(): String? =
ContextCompat.getExternalFilesDirs(requireContext(), null).firstOrNull()?.path
} }

View File

@ -177,12 +177,6 @@ open class LibraryFragment : BaseFragment() {
private fun storeDeviceInPreferences(storageDevice: StorageDevice) { private fun storeDeviceInPreferences(storageDevice: StorageDevice) {
sharedPreferenceUtil.putPrefStorage(storageDevice.name) sharedPreferenceUtil.putPrefStorage(storageDevice.name)
sharedPreferenceUtil.putPrefStorageTitle(
getString(
if (storageDevice.isInternal) R.string.internal_storage
else R.string.external_storage
)
)
} }
private fun onBookItemClick(item: BookItem) { private fun onBookItemClick(item: BookItem) {

View File

@ -268,7 +268,7 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme
public void openFolderSelect() { public void openFolderSelect() {
StorageSelectDialog dialogFragment = new StorageSelectDialog(); StorageSelectDialog dialogFragment = new StorageSelectDialog();
dialogFragment.setOnSelectAction(this::onStorageDeviceSelected); dialogFragment.setOnSelectAction(this::onStorageDeviceSelected);
dialogFragment.show(((AppCompatActivity) getActivity()).getSupportFragmentManager(), dialogFragment.show(getActivity().getSupportFragmentManager(),
getResources().getString(R.string.pref_storage)); getResources().getString(R.string.pref_storage));
} }
@ -278,13 +278,9 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme
); );
sharedPreferenceUtil.putPrefStorage(storageDevice.getName()); sharedPreferenceUtil.putPrefStorage(storageDevice.getName());
if (storageDevice.isInternal()) { if (storageDevice.isInternal()) {
findPreference(PREF_STORAGE).setTitle(getResources().getString(R.string.internal_storage)); findPreference(PREF_STORAGE).setTitle(getString(R.string.internal_storage));
sharedPreferenceUtil.putPrefStorageTitle(
getResources().getString(R.string.internal_storage));
} else { } else {
findPreference(PREF_STORAGE).setTitle(getResources().getString(R.string.external_storage)); findPreference(PREF_STORAGE).setTitle(getString(R.string.external_storage));
sharedPreferenceUtil.putPrefStorageTitle(
getResources().getString(R.string.external_storage));
} }
return Unit.INSTANCE; return Unit.INSTANCE;
} }

View File

@ -50,7 +50,6 @@ public class SharedPreferenceUtil {
private static final String PREF_BACK_TO_TOP = "pref_backtotop"; private static final String PREF_BACK_TO_TOP = "pref_backtotop";
private static final String PREF_FULLSCREEN = "pref_fullscreen"; private static final String PREF_FULLSCREEN = "pref_fullscreen";
private static final String PREF_NEW_TAB_BACKGROUND = "pref_newtab_background"; private static final String PREF_NEW_TAB_BACKGROUND = "pref_newtab_background";
private static final String PREF_STORAGE_TITLE = "pref_selected_title";
private static final String PREF_EXTERNAL_LINK_POPUP = "pref_external_link_popup"; private static final String PREF_EXTERNAL_LINK_POPUP = "pref_external_link_popup";
private static final String PREF_IS_FIRST_RUN = "isFirstRun"; private static final String PREF_IS_FIRST_RUN = "isFirstRun";
private static final String PREF_SHOW_BOOKMARKS_ALL_BOOKS = "show_bookmarks_current_book"; private static final String PREF_SHOW_BOOKMARKS_ALL_BOOKS = "show_bookmarks_current_book";
@ -115,10 +114,6 @@ public class SharedPreferenceUtil {
: CoreApp.getInstance().getFilesDir().getPath(); // workaround for emulators : CoreApp.getInstance().getFilesDir().getPath(); // workaround for emulators
} }
public String getPrefStorageTitle(String defaultTitle) {
return sharedPreferences.getString(PREF_STORAGE_TITLE, defaultTitle);
}
public void putPrefLanguage(String language) { public void putPrefLanguage(String language) {
sharedPreferences.edit().putString(PREF_LANG, language).apply(); sharedPreferences.edit().putString(PREF_LANG, language).apply();
} }
@ -131,10 +126,6 @@ public class SharedPreferenceUtil {
sharedPreferences.edit().putBoolean(PREF_WIFI_ONLY, wifiOnly).apply(); sharedPreferences.edit().putBoolean(PREF_WIFI_ONLY, wifiOnly).apply();
} }
public void putPrefStorageTitle(String storageTitle) {
sharedPreferences.edit().putString(PREF_STORAGE_TITLE, storageTitle).apply();
}
public void putPrefStorage(String storage) { public void putPrefStorage(String storage) {
sharedPreferences.edit().putString(PREF_STORAGE, storage).apply(); sharedPreferences.edit().putString(PREF_STORAGE, storage).apply();
prefStorages.onNext(storage); prefStorages.onNext(storage);