Skip to content

Commit

Permalink
Merge Main branch
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebKL committed Nov 11, 2024
2 parents a71bc12 + 38e4b87 commit af58544
Show file tree
Hide file tree
Showing 65 changed files with 291 additions and 386 deletions.
3 changes: 1 addition & 2 deletions app/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
see more information here https://github.com/kiwix/kiwix-android/issues/3172
-->
<application
android:forceQueryable="true"
android:usesCleartextTraffic="true"/>
android:forceQueryable="true" />
<uses-sdk tools:overrideLibrary="android_libs.ub_uiautomator" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.R.string
import org.kiwix.kiwixmobile.core.R.id
import org.kiwix.kiwixmobile.core.utils.files.Log
import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.testutils.TestUtils.testFlakyView
Expand Down Expand Up @@ -98,7 +99,7 @@ class InitialDownloadRobot : BaseRobot() {
}

fun assertStorageConfigureDialogDisplayed() {
testFlakyView({ isVisible(Text("Download book to internal storage?")) })
testFlakyView({ onView(withText(string.choose_storage_to_download_book)) })
}

fun assertStopDownloadDialogDisplayed() {
Expand All @@ -109,6 +110,17 @@ class InitialDownloadRobot : BaseRobot() {
testFlakyView({ onView(withText("YES")).perform(click()) })
}

fun clickOnInternalStorage() {
pauseForBetterTestPerformance()
testFlakyView({
onView(
RecyclerViewMatcher(id.device_list).atPosition(
0
)
).perform(click())
})
}

fun assertDownloadStart() {
testFlakyView({ isVisible(ViewId(R.id.stop)) }, 10)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class InitialDownloadTest : BaseActivityTest() {
stopDownloadIfAlreadyStarted()
downloadZimFile()
assertStorageConfigureDialogDisplayed()
clickOnYesToConfirm()
clickOnInternalStorage()
assertDownloadStart()
stopDownload()
assertStopDownloadDialogDisplayed()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
android:configChanges="orientation|keyboardHidden|screenSize"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTop"
android:launchMode="singleInstance"
android:theme="@style/KiwixTheme.Launcher"
android:windowSoftInputMode="adjustPan">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ class KiwixMainActivity : CoreMainActivity() {
super.onCreate(savedInstanceState)
activityKiwixMainBinding = ActivityKiwixMainBinding.inflate(layoutInflater)
setContentView(activityKiwixMainBinding.root)
if (intent.action == "GET_CONTENT") {
navigate(R.id.downloadsFragment)
}

navController.addOnDestinationChangedListener(finishActionModeOnDestinationChange)
activityKiwixMainBinding.drawerNavView.setupWithNavController(navController)
Expand All @@ -130,6 +127,7 @@ class KiwixMainActivity : CoreMainActivity() {
activityKiwixMainBinding.bottomNavView.setupWithNavController(navController)
migrateInternalToPublicAppDirectory()
handleZimFileIntent(intent)
handleGetContentIntent(intent)
}

private fun migrateInternalToPublicAppDirectory() {
Expand Down Expand Up @@ -220,11 +218,18 @@ class KiwixMainActivity : CoreMainActivity() {
super.onNewIntent(intent)
handleNotificationIntent(intent)
handleZimFileIntent(intent)
handleGetContentIntent(intent)
supportFragmentManager.fragments.filterIsInstance<FragmentActivityExtensions>().forEach {
it.onNewIntent(intent, this)
}
}

private fun handleGetContentIntent(intent: Intent?) {
if (intent?.action == ACTION_GET_CONTENT) {
navigate(R.id.downloadsFragment)
}
}

private fun handleZimFileIntent(intent: Intent?) {
intent?.data?.let {
when (it.scheme) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomnavigation.BottomNavigationView
import eu.mhutti1.utils.storage.Bytes
import eu.mhutti1.utils.storage.StorageDevice
import eu.mhutti1.utils.storage.StorageDeviceUtils
import eu.mhutti1.utils.storage.StorageSelectDialog
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
Expand Down Expand Up @@ -133,6 +134,9 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
private val zimManageViewModel by lazy {
requireActivity().viewModel<ZimManageViewModel>(viewModelFactory)
}
private val storageDeviceList by lazy {
StorageDeviceUtils.getWritableStorage(requireActivity())
}

private var storagePermissionLauncher: ActivityResultLauncher<Array<String>>? =
registerForActivityResult(
Expand Down Expand Up @@ -664,6 +668,7 @@ class LocalLibraryFragment : BaseFragment(), CopyMoveFileHandler.FileCopyMoveCal
private fun showStorageSelectDialog() = StorageSelectDialog()
.apply {
onSelectAction = ::storeDeviceInPreferences
setStorageDeviceList(storageDeviceList)
}
.show(parentFragmentManager, getString(string.pref_storage))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import eu.mhutti1.utils.storage.STORAGE_SELECT_STORAGE_TITLE_TEXTVIEW_SIZE
import eu.mhutti1.utils.storage.StorageDevice
import eu.mhutti1.utils.storage.StorageDeviceUtils
import eu.mhutti1.utils.storage.StorageSelectDialog
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.cachedComponent
Expand Down Expand Up @@ -111,6 +113,9 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
private val zimManageViewModel by lazy {
requireActivity().viewModel<ZimManageViewModel>(viewModelFactory)
}
private val storageDeviceList by lazy {
StorageDeviceUtils.getWritableStorage(requireActivity())
}

@VisibleForTesting
fun getOnlineLibraryList() = libraryAdapter.items
Expand Down Expand Up @@ -419,6 +424,7 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
private fun storeDeviceInPreferences(
storageDevice: StorageDevice
) {
sharedPreferenceUtil.showStorageOption = false
sharedPreferenceUtil.putPrefStorage(
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name)
)
Expand Down Expand Up @@ -528,7 +534,15 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
}

else -> if (sharedPreferenceUtil.showStorageOption) {
showStorageConfigureDialog()
// Show the storage selection dialog for configuration if there is an SD card available.
if (storageDeviceList.size > 1) {
showStorageSelectDialog()
} else {
// If only internal storage is available, proceed with the ZIM file download directly.
// Displaying a configuration dialog is unnecessary in this case.
sharedPreferenceUtil.showStorageOption = false
onBookItemClick(item)
}
} else if (!requireActivity().isManageExternalStoragePermissionGranted(
sharedPreferenceUtil
)
Expand Down Expand Up @@ -561,22 +575,10 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
private fun showStorageSelectDialog() = StorageSelectDialog()
.apply {
onSelectAction = ::storeDeviceInPreferences
titleSize = STORAGE_SELECT_STORAGE_TITLE_TEXTVIEW_SIZE
setStorageDeviceList(storageDeviceList)
}
.show(parentFragmentManager, getString(string.pref_storage))

private fun showStorageConfigureDialog() {
alertDialogShower.show(
KiwixDialog.StorageConfigure,
{
showStorageSelectDialog()
sharedPreferenceUtil.showStorageOption = false
},
{
sharedPreferenceUtil.showStorageOption = false
clickOnBookItem()
}
)
}
.show(parentFragmentManager, getString(string.choose_storage_to_download_book))

private fun clickOnBookItem() {
if (!requireActivity().isManageExternalStoragePermissionGranted(sharedPreferenceUtil)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.launch
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.extensions.getFreeSpace
import org.kiwix.kiwixmobile.core.extensions.getUsedSpace
import org.kiwix.kiwixmobile.core.extensions.storagePathAndTitle
import org.kiwix.kiwixmobile.core.extensions.usedPercentage
import org.kiwix.kiwixmobile.core.navigateToSettings
import org.kiwix.kiwixmobile.core.settings.CorePrefsFragment
import org.kiwix.kiwixmobile.core.settings.StorageRadioButtonPreference
Expand Down Expand Up @@ -83,70 +87,19 @@ class KiwixPrefsFragment : CorePrefsFragment() {
onStorageDeviceSelected(storageDevice)
true
}
setPathAndTitleForStorage(
buildStoragePathAndTitle(
storageDevice,
index,
selectedStoragePosition,
sharedPreferenceUtil
storageCalculator?.let {
setPathAndTitleForStorage(
storageDevice.storagePathAndTitle(context, index, sharedPreferenceUtil, it)
)
)
setFreeSpace(getFreeSpaceText(storageDevice))
setUsedSpace(getUsedSpaceText(storageDevice))
setProgress(calculateUsedPercentage(storageDevice))
setFreeSpace(storageDevice.getFreeSpace(context, it))
setUsedSpace(storageDevice.getUsedSpace(context, it))
setProgress(storageDevice.usedPercentage(it))
}
}
}
}
}

private fun getFreeSpaceText(storageDevice: StorageDevice): String {
val freeSpace = storageCalculator?.calculateAvailableSpace(storageDevice.file)
return getString(R.string.pref_free_storage, freeSpace)
}

private suspend fun getUsedSpaceText(storageDevice: StorageDevice): String {
val usedSpace = storageCalculator?.calculateUsedSpace(storageDevice.file)
return getString(R.string.pref_storage_used, usedSpace)
}

private suspend fun buildStoragePathAndTitle(
storageDevice: StorageDevice,
index: Int,
selectedStoragePosition: Int,
sharedPreferenceUtil: SharedPreferenceUtil
): String {
val storageName = if (storageDevice.isInternal) {
getString(R.string.internal_storage)
} else {
getString(R.string.external_storage)
}
val storagePath = if (index == selectedStoragePosition) {
sharedPreferenceUtil.prefStorage
} else {
getStoragePathForNonSelectedStorage(storageDevice, sharedPreferenceUtil)
}
val totalSpace = storageCalculator?.calculateTotalSpace(storageDevice.file)
return "$storageName - $totalSpace\n$storagePath/Kiwix"
}

private fun getStoragePathForNonSelectedStorage(
storageDevice: StorageDevice,
sharedPreferenceUtil: SharedPreferenceUtil
): String =
if (storageDevice.isInternal) {
sharedPreferenceUtil.getPublicDirectoryPath(storageDevice.name)
} else {
storageDevice.name
}

@Suppress("MagicNumber")
private suspend fun calculateUsedPercentage(storageDevice: StorageDevice): Int {
val totalSpace = storageCalculator?.totalBytes(storageDevice.file) ?: 1
val availableSpace = storageCalculator?.availableBytes(storageDevice.file) ?: 0
val usedSpace = totalSpace - availableSpace
return (usedSpace.toDouble() / totalSpace * 100).toInt()
}

private fun showExternalPreferenceIfAvailable() {
findPreference<StorageRadioButtonPreference>(PREF_EXTERNAL_STORAGE)?.isVisible =
storageDeviceList.size > 1
Expand Down
1 change: 0 additions & 1 deletion core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
android:resizeableActivity="true"
android:supportsRtl="true"
android:theme="@style/KiwixTheme"
android:usesCleartextTraffic="true"
tools:targetApi="tiramisu">

<!-- Version < 3.0. DeX Mode and Screen Mirroring support -->
Expand Down
Loading

0 comments on commit af58544

Please sign in to comment.