diff --git a/app/src/main/java/org/kiwix/kiwixmobile/settings/KiwixPrefsFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/settings/KiwixPrefsFragment.kt index e634c753f..226148190 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/settings/KiwixPrefsFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/settings/KiwixPrefsFragment.kt @@ -23,7 +23,7 @@ import android.os.Environment import androidx.preference.Preference import org.kiwix.kiwixmobile.core.settings.CorePrefsFragment import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil -import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.Companion.PREF_STORAGE +import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_STORAGE class KiwixPrefsFragment : CorePrefsFragment() { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.java b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.java index e69de29bb..894e46f3a 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.java +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.java @@ -0,0 +1,225 @@ +/* + * Kiwix Android + * Copyright (c) 2019 Kiwix + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package org.kiwix.kiwixmobile.core.utils; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import androidx.annotation.NonNull; +import androidx.appcompat.app.AppCompatDelegate; +import androidx.core.content.ContextCompat; +import io.reactivex.Flowable; +import io.reactivex.processors.PublishProcessor; +import java.io.File; +import java.util.HashSet; +import java.util.Locale; +import java.util.Set; +import javax.inject.Inject; +import javax.inject.Singleton; +import org.kiwix.kiwixmobile.core.CoreApp; +import org.kiwix.kiwixmobile.core.NightModeConfig; + +/** + * Manager for the Default Shared Preferences of the application. + */ + +@Singleton +public class SharedPreferenceUtil { + // Prefs + public static final String PREF_LANG = "pref_language_chooser"; + public static final String PREF_STORAGE = "pref_select_folder"; + public static final String PREF_WIFI_ONLY = "pref_wifi_only"; + public static final String PREF_KIWIX_MOBILE = "kiwix-mobile"; + public static final String PREF_SHOW_INTRO = "showIntro"; + private static final String PREF_BACK_TO_TOP = "pref_backtotop"; + private static final String PREF_FULLSCREEN = "pref_fullscreen"; + 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_IS_FIRST_RUN = "isFirstRun"; + private static final String PREF_SHOW_BOOKMARKS_ALL_BOOKS = "show_bookmarks_current_book"; + private static final String PREF_SHOW_HISTORY_ALL_BOOKS = "show_history_current_book"; + private static final String PREF_HOSTED_BOOKS = "hosted_books"; + public static final String PREF_NIGHT_MODE = "pref_night_mode"; + private static final String TEXT_ZOOM = "true_text_zoom"; + private SharedPreferences sharedPreferences; + private final PublishProcessor prefStorages = PublishProcessor.create(); + private final PublishProcessor textZooms = PublishProcessor.create(); + private final PublishProcessor nightModes = PublishProcessor.create(); + + @Inject + public SharedPreferenceUtil(Context context) { + sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); + } + + public boolean getPrefWifiOnly() { + return sharedPreferences.getBoolean(PREF_WIFI_ONLY, true); + } + + public boolean getPrefIsFirstRun() { + return sharedPreferences.getBoolean(PREF_IS_FIRST_RUN, true); + } + + public boolean getPrefFullScreen() { + return sharedPreferences.getBoolean(PREF_FULLSCREEN, false); + } + + public boolean getPrefBackToTop() { + return sharedPreferences.getBoolean(PREF_BACK_TO_TOP, false); + } + + public boolean getPrefNewTabBackground() { + return sharedPreferences.getBoolean(PREF_NEW_TAB_BACKGROUND, false); + } + + public boolean getPrefExternalLinkPopup() { + return sharedPreferences.getBoolean(PREF_EXTERNAL_LINK_POPUP, true); + } + + public String getPrefLanguage() { + return sharedPreferences.getString(PREF_LANG, Locale.ROOT.toString()); + } + + public String getPrefStorage() { + final String storage = sharedPreferences.getString(PREF_STORAGE, null); + if (storage == null) { + final String defaultStorage = defaultStorage(); + putPrefStorage(defaultStorage); + return defaultStorage; + } else if (!new File(storage).exists()) { + return defaultStorage(); + } + return storage; + } + + private String defaultStorage() { + final File externalFilesDir = + ContextCompat.getExternalFilesDirs(CoreApp.getInstance(), null)[0]; + return externalFilesDir != null ? externalFilesDir.getPath() + : CoreApp.getInstance().getFilesDir().getPath(); // workaround for emulators + } + + public String getPrefStorageTitle(String defaultTitle) { + return sharedPreferences.getString(PREF_STORAGE_TITLE, defaultTitle); + } + + public void putPrefLanguage(String language) { + sharedPreferences.edit().putString(PREF_LANG, language).apply(); + } + + public void putPrefIsFirstRun(boolean isFirstRun) { + sharedPreferences.edit().putBoolean(PREF_IS_FIRST_RUN, isFirstRun).apply(); + } + + public void putPrefWifiOnly(boolean wifiOnly) { + 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) { + sharedPreferences.edit().putString(PREF_STORAGE, storage).apply(); + prefStorages.onNext(storage); + } + + public Flowable getPrefStorages() { + return prefStorages.startWith(getPrefStorage()); + } + + public void putPrefFullScreen(boolean fullScreen) { + sharedPreferences.edit().putBoolean(PREF_FULLSCREEN, fullScreen).apply(); + } + + public void putPrefExternalLinkPopup(boolean externalLinkPopup) { + sharedPreferences.edit().putBoolean(PREF_EXTERNAL_LINK_POPUP, externalLinkPopup).apply(); + } + + public boolean showIntro() { + return sharedPreferences.getBoolean(PREF_SHOW_INTRO, true); + } + + public void setIntroShown() { + sharedPreferences.edit().putBoolean(PREF_SHOW_INTRO, false).apply(); + } + + public boolean getShowHistoryAllBooks() { + return sharedPreferences.getBoolean(PREF_SHOW_HISTORY_ALL_BOOKS, true); + } + + public void setShowHistoryAllBooks(boolean prefShowHistoryAllBooks) { + sharedPreferences.edit() + .putBoolean(PREF_SHOW_HISTORY_ALL_BOOKS, prefShowHistoryAllBooks) + .apply(); + } + + public boolean getShowBookmarksAllBooks() { + return sharedPreferences.getBoolean(PREF_SHOW_BOOKMARKS_ALL_BOOKS, true); + } + + public void setShowBookmarksAllBooks(boolean prefShowBookmarksFromCurrentBook) { + sharedPreferences.edit() + .putBoolean(PREF_SHOW_BOOKMARKS_ALL_BOOKS, prefShowBookmarksFromCurrentBook) + .apply(); + } + + @NonNull + public NightModeConfig.Mode getNightMode() { + return NightModeConfig.Mode.from( + Integer.parseInt( + sharedPreferences.getString( + PREF_NIGHT_MODE, + "" + AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM + ) + ) + ); + } + + public Flowable nightModes() { + return nightModes.startWith(getNightMode()); + } + + public void updateNightMode() { + nightModes.offer(getNightMode()); + } + + public Set getHostedBooks() { + return sharedPreferences.getStringSet(PREF_HOSTED_BOOKS, new HashSet<>()); + } + + public void setHostedBooks(Set hostedBooks) { + sharedPreferences.edit() + .putStringSet(PREF_HOSTED_BOOKS, hostedBooks) + .apply(); + } + + public void setTextZoom(int textZoom) { + sharedPreferences.edit().putInt(TEXT_ZOOM, textZoom).apply(); + textZooms.offer(textZoom); + } + + public int getTextZoom() { + return sharedPreferences.getInt(TEXT_ZOOM, 100); + } + + public Flowable getTextZooms() { + return textZooms.startWith(getTextZoom()); + } +} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt deleted file mode 100644 index 775fc5298..000000000 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/SharedPreferenceUtil.kt +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Kiwix Android - * Copyright (c) 2019 Kiwix - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ -package org.kiwix.kiwixmobile.core.utils - -import android.content.Context -import android.content.SharedPreferences -import android.preference.PreferenceManager -import androidx.appcompat.app.AppCompatDelegate -import androidx.core.content.ContextCompat.getExternalFilesDirs -import androidx.core.content.edit -import io.reactivex.Flowable -import io.reactivex.processors.PublishProcessor -import org.kiwix.kiwixmobile.core.NightModeConfig -import org.kiwix.kiwixmobile.core.NightModeConfig.Mode.Companion.from -import java.io.File -import java.util.Locale -import javax.inject.Inject -import javax.inject.Singleton - -/** - * Manager for the Default Shared Preferences of the application. - */ -@Singleton -class SharedPreferenceUtil @Inject constructor(val context: Context) { - private val sharedPreferences: SharedPreferences = - PreferenceManager.getDefaultSharedPreferences(context) - private val _prefStorages = PublishProcessor.create() - val prefStorages - get() = _prefStorages.startWith { prefStorage } - private val _textZooms = PublishProcessor.create() - val textZooms - get() = _textZooms.startWith { textZoom } - private val nightModes = PublishProcessor.create() - - val prefWifiOnly: Boolean - get() = sharedPreferences.getBoolean(PREF_WIFI_ONLY, true) - - val prefIsFirstRun: Boolean - get() = sharedPreferences.getBoolean(PREF_IS_FIRST_RUN, true) - - val prefFullScreen: Boolean - get() = sharedPreferences.getBoolean(PREF_FULLSCREEN, false) - - val prefBackToTop: Boolean - get() = sharedPreferences.getBoolean(PREF_BACK_TO_TOP, false) - - val prefNewTabBackground: Boolean - get() = sharedPreferences.getBoolean(PREF_NEW_TAB_BACKGROUND, false) - - val prefExternalLinkPopup: Boolean - get() = sharedPreferences.getBoolean(PREF_EXTERNAL_LINK_POPUP, true) - - val prefLanguage: String - get() = sharedPreferences.getString(PREF_LANG, "") ?: Locale.ROOT.toString() - - val prefStorage: String - get() { - val storage = sharedPreferences.getString(PREF_STORAGE, null) - return when { - storage == null -> defaultStorage().also(::putPrefStorage) - !File(storage).exists() -> defaultStorage() - else -> storage - } - } - - private fun defaultStorage(): String = - getExternalFilesDirs(context, null)[0]?.path - ?: context.filesDir.path // a workaround for emulators - - fun getPrefStorageTitle(defaultTitle: String): String = - sharedPreferences.getString(PREF_STORAGE_TITLE, defaultTitle) ?: defaultTitle - - fun putPrefLanguage(language: String) = - sharedPreferences.edit { putString(PREF_LANG, language) } - - fun putPrefIsFirstRun(isFirstRun: Boolean) = - sharedPreferences.edit { putBoolean(PREF_IS_FIRST_RUN, isFirstRun) } - - fun putPrefWifiOnly(wifiOnly: Boolean) = - sharedPreferences.edit { putBoolean(PREF_WIFI_ONLY, wifiOnly) } - - fun putPrefStorageTitle(storageTitle: String) = - sharedPreferences.edit { putString(PREF_STORAGE_TITLE, storageTitle) } - - fun putPrefStorage(storage: String) { - sharedPreferences.edit { putString(PREF_STORAGE, storage) } - _prefStorages.onNext(storage) - } - - fun putPrefFullScreen(fullScreen: Boolean) = - sharedPreferences.edit { putBoolean(PREF_FULLSCREEN, fullScreen) } - - fun putPrefExternalLinkPopup(externalLinkPopup: Boolean) = - sharedPreferences.edit { putBoolean(PREF_EXTERNAL_LINK_POPUP, externalLinkPopup) } - - fun showIntro(): Boolean = sharedPreferences.getBoolean(PREF_SHOW_INTRO, true) - - fun setIntroShown() = sharedPreferences.edit { putBoolean(PREF_SHOW_INTRO, false) } - - var showHistoryAllBooks: Boolean - get() = sharedPreferences.getBoolean(PREF_SHOW_HISTORY_ALL_BOOKS, true) - set(prefShowHistoryAllBooks) { - sharedPreferences.edit { putBoolean(PREF_SHOW_HISTORY_ALL_BOOKS, prefShowHistoryAllBooks) } - } - - var showBookmarksAllBooks: Boolean - get() = sharedPreferences.getBoolean(PREF_SHOW_BOOKMARKS_ALL_BOOKS, true) - set(prefShowBookmarksFromCurrentBook) = sharedPreferences.edit { - putBoolean(PREF_SHOW_BOOKMARKS_ALL_BOOKS, prefShowBookmarksFromCurrentBook) - } - - val nightMode: NightModeConfig.Mode - get() = from( - sharedPreferences.getString(PREF_NIGHT_MODE, null)?.toInt() - ?: AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM - ) - - fun nightModes(): Flowable = nightModes.startWith(nightMode) - - fun updateNightMode() = nightModes.offer(nightMode) - - var hostedBooks: Set - get() = sharedPreferences.getStringSet(PREF_HOSTED_BOOKS, null)?.toHashSet() ?: HashSet() - set(hostedBooks) { - sharedPreferences.edit { putStringSet(PREF_HOSTED_BOOKS, hostedBooks) } - } - - var textZoom: Int - get() = sharedPreferences.getInt(TEXT_ZOOM, DEFAULT_ZOOM) - set(textZoom) { - sharedPreferences.edit { putInt(TEXT_ZOOM, textZoom) } - _textZooms.offer(textZoom) - } - - companion object { - // Prefs - const val PREF_LANG = "pref_language_chooser" - const val PREF_STORAGE = "pref_select_folder" - const val PREF_WIFI_ONLY = "pref_wifi_only" - const val PREF_KIWIX_MOBILE = "kiwix-mobile" - const val PREF_SHOW_INTRO = "showIntro" - private const val PREF_BACK_TO_TOP = "pref_backtotop" - private const val PREF_FULLSCREEN = "pref_fullscreen" - private const val PREF_NEW_TAB_BACKGROUND = "pref_newtab_background" - private const val PREF_STORAGE_TITLE = "pref_selected_title" - private const val PREF_EXTERNAL_LINK_POPUP = "pref_external_link_popup" - private const val PREF_IS_FIRST_RUN = "isFirstRun" - private const val PREF_SHOW_BOOKMARKS_ALL_BOOKS = "show_bookmarks_current_book" - private const val PREF_SHOW_HISTORY_ALL_BOOKS = "show_history_current_book" - private const val PREF_HOSTED_BOOKS = "hosted_books" - const val PREF_NIGHT_MODE = "pref_night_mode" - private const val TEXT_ZOOM = "true_text_zoom" - private const val DEFAULT_ZOOM = 100 - } -} diff --git a/core/src/main/java/org/kiwix/kiwixmobile/zim_manager/MountPointProducer.kt b/core/src/main/java/org/kiwix/kiwixmobile/zim_manager/MountPointProducer.kt index 63a2bfbed..6d9be6fd0 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/zim_manager/MountPointProducer.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/zim_manager/MountPointProducer.kt @@ -44,8 +44,8 @@ data class MountInfo(val device: String, val mountPoint: String, val fileSystem: val doesNotSupport4GBFiles = DOES_NOT_SUPPORT_4GB_FILE_SYSTEMS.contains(fileSystem) companion object { - private val VIRTUAL_FILE_SYSTEMS = listOf("fuse", "sdcardfs") + private val VIRTUAL_FILE_SYSTEMS = listOf("fuse", "sdcardfs", "sdfat") private val SUPPORTS_4GB_FILE_SYSTEMS = listOf("ext4", "exfat") - private val DOES_NOT_SUPPORT_4GB_FILE_SYSTEMS = listOf("fat32", "vfat", "sdfat") + private val DOES_NOT_SUPPORT_4GB_FILE_SYSTEMS = listOf("fat32", "vfat") } } diff --git a/core/src/main/res/layout/bottom_toolbar.xml b/core/src/main/res/layout/bottom_toolbar.xml index 09386ee7e..49ed8f7d6 100644 --- a/core/src/main/res/layout/bottom_toolbar.xml +++ b/core/src/main/res/layout/bottom_toolbar.xml @@ -7,6 +7,8 @@ android:layout_height="wrap_content" android:layout_gravity="bottom" android:padding="8dp" + app:fabAlignmentMode="end" + app:fabCradleVerticalOffset="@dimen/fab_vertical_offset" app:hideOnScroll="true" tools:ignore="BottomAppBar"> diff --git a/core/src/main/res/layout/main_fragment_content.xml b/core/src/main/res/layout/main_fragment_content.xml index 195a27732..ca7fc6a50 100644 --- a/core/src/main/res/layout/main_fragment_content.xml +++ b/core/src/main/res/layout/main_fragment_content.xml @@ -29,7 +29,6 @@ android:layout_height="match_parent" android:fitsSystemWindows="true"> - + - -