Confirm traffic consumption

This commit is contained in:
Koitharu
2020-03-05 19:02:25 +02:00
parent c31ade9ba4
commit cb76c54c42
7 changed files with 146 additions and 6 deletions

View File

@@ -4,6 +4,7 @@
package="org.koitharu.kotatsu">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
@@ -43,7 +44,9 @@
android:name=".ui.reader.SimpleSettingsActivity"
android:label="@string/settings" />
<service android:name=".ui.download.DownloadService" />
<service
android:name=".ui.download.DownloadService"
android:foregroundServiceType="dataSync" />
<provider
android:name=".ui.search.MangaSuggestionsProvider"

View File

@@ -37,6 +37,11 @@ class AppSettings private constructor(resources: Resources, private val prefs: S
setOf(PAGE_SWITCH_TAPS)
)
var isTrafficWarningEnabled by BoolPreferenceDelegate(
resources.getString(R.string.key_traffic_warning),
true
)
private var sourcesOrderStr by NullableStringPreferenceDelegate(resources.getString(R.string.key_sources_order))
var sourcesOrder: List<Int>

View File

@@ -0,0 +1,84 @@
package org.koitharu.kotatsu.ui.common.dialog
import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.view.LayoutInflater
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AlertDialog
import com.google.android.material.checkbox.MaterialCheckBox
import org.koitharu.kotatsu.R
class CheckBoxAlertDialog private constructor(private val delegate: AlertDialog) :
DialogInterface by delegate {
fun show() = delegate.show()
class Builder(context: Context) {
@SuppressLint("InflateParams")
private val view = LayoutInflater.from(context)
.inflate(R.layout.dialog_checkbox, null, false)
private val checkBox = view.findViewById<MaterialCheckBox>(android.R.id.checkbox)
private val delegate = AlertDialog.Builder(context)
.setView(view)
fun setTitle(@StringRes titleResId: Int): Builder {
delegate.setTitle(titleResId)
return this
}
fun setTitle(title: CharSequence): Builder {
delegate.setTitle(title)
return this
}
fun setMessage(@StringRes messageId: Int): Builder {
delegate.setMessage(messageId)
return this
}
fun setMessage(message: CharSequence): Builder {
delegate.setMessage(message)
return this
}
fun setCheckBoxText(@StringRes textId: Int): Builder {
checkBox.setText(textId)
return this
}
fun setCheckBoxChecked(isChecked: Boolean): Builder {
checkBox.isChecked = isChecked
return this
}
fun setIcon(@DrawableRes iconId: Int): Builder {
delegate.setIcon(iconId)
return this
}
fun setPositiveButton(
@StringRes textId: Int,
listener: (DialogInterface, Boolean) -> Unit
): Builder {
delegate.setPositiveButton(textId) { dialog, _ ->
listener(dialog, checkBox.isChecked)
}
return this
}
fun setNegativeButton(
@StringRes textId: Int,
listener: DialogInterface.OnClickListener? = null
): Builder {
delegate.setNegativeButton(textId, listener)
return this
}
fun create() = CheckBoxAlertDialog(delegate.create())
}
}

View File

@@ -2,7 +2,9 @@ package org.koitharu.kotatsu.ui.download
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.webkit.MimeTypeMap
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import coil.Coil
import coil.api.get
@@ -13,11 +15,14 @@ import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import org.koin.core.inject
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.local.PagesCache
import org.koitharu.kotatsu.core.model.Manga
import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.domain.MangaProviderFactory
import org.koitharu.kotatsu.domain.local.MangaZip
import org.koitharu.kotatsu.ui.common.BaseService
import org.koitharu.kotatsu.ui.common.dialog.CheckBoxAlertDialog
import org.koitharu.kotatsu.utils.CacheUtils
import org.koitharu.kotatsu.utils.ext.await
import org.koitharu.kotatsu.utils.ext.retryUntilSuccess
@@ -144,12 +149,34 @@ class DownloadService : BaseService() {
private const val EXTRA_CHAPTERS_IDS = "chapters_ids"
fun start(context: Context, manga: Manga, chaptersIds: Collection<Long>? = null) {
val intent = Intent(context, DownloadService::class.java)
intent.putExtra(EXTRA_MANGA, manga)
if (chaptersIds != null) {
intent.putExtra(EXTRA_CHAPTERS_IDS, chaptersIds.toLongArray())
confirmDataTransfer(context) {
val intent = Intent(context, DownloadService::class.java)
intent.putExtra(EXTRA_MANGA, manga)
if (chaptersIds != null) {
intent.putExtra(EXTRA_CHAPTERS_IDS, chaptersIds.toLongArray())
}
ContextCompat.startForegroundService(context, intent)
}
}
private fun confirmDataTransfer(context: Context, callback: () -> Unit) {
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val settings = AppSettings(context)
if (cm.isActiveNetworkMetered && settings.isTrafficWarningEnabled) {
CheckBoxAlertDialog.Builder(context)
.setTitle(R.string.warning)
.setMessage(R.string.network_consumption_warning)
.setCheckBoxText(R.string.dont_ask_again)
.setCheckBoxChecked(false)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(R.string._continue) { _, doNotAsk ->
settings.isTrafficWarningEnabled = !doNotAsk
callback()
}.create()
.show()
} else {
callback()
}
ContextCompat.startForegroundService(context, intent)
}
}
}

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="?android:listPreferredItemPaddingStart"
android:paddingEnd="?android:listPreferredItemPaddingEnd">
<com.google.android.material.checkbox.MaterialCheckBox
android:id="@android:id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:text="@tools:sample/lorem[3]" />
</FrameLayout>

View File

@@ -3,6 +3,7 @@
<string name="key_list_mode">list_mode</string>
<string name="key_theme">theme</string>
<string name="key_sources_order">sources_order</string>
<string name="key_traffic_warning">traffic_warning</string>
<string name="key_pages_cache_clear">pages_cache_clear</string>
<string name="key_grid_size">grid_size</string>
<string name="key_reader_switchers">reader_switchers</string>

View File

@@ -91,4 +91,8 @@
<string name="switch_pages">Switch pages</string>
<string name="taps_on_edges">Taps on edges</string>
<string name="volume_buttons">Volume buttons</string>
<string name="_continue">Continue</string>
<string name="warning">Warning</string>
<string name="network_consumption_warning">This operation may consume a lot of network traffic</string>
<string name="dont_ask_again">Don`t ask again</string>
</resources>