Skip to content

Commit

Permalink
Merge branch 'main' into weblate-copy-sms-code-otp-helper-app-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
jd1378 authored Jul 9, 2024
2 parents c039f1e + 375d2c6 commit 657df45
Show file tree
Hide file tree
Showing 24 changed files with 300 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.painterResource
Expand All @@ -50,6 +51,7 @@ import io.github.jd1378.otphelper.ui.components.DangerousActionDialog
import io.github.jd1378.otphelper.ui.components.NewSensitivePhraseDialog
import io.github.jd1378.otphelper.ui.components.TitleBar
import io.github.jd1378.otphelper.ui.components.drawVerticalScrollbar
import io.github.jd1378.otphelper.utils.Clipboard

@OptIn(ExperimentalFoundationApi::class)
@Composable
Expand All @@ -59,6 +61,8 @@ fun SensitivePhrases(
viewModel: SensitivePhrasesViewModel
) {
val uriHandler = LocalUriHandler.current
val context = LocalContext.current

val phrases by viewModel.sensitivePhrases.collectAsStateWithLifecycle()
val listState = rememberLazyListState()
val showResetToDefaultDialog = viewModel.showResetToDefaultDialog.collectAsStateWithLifecycle()
Expand Down Expand Up @@ -100,6 +104,39 @@ fun SensitivePhrases(
},
leadingIcon = { Icon(Icons.Default.Delete, contentDescription = null) },
)
HorizontalDivider()
DropdownMenuItem(
text = { Text(stringResource(R.string.copy_general_regex)) },
onClick = {
Clipboard.copyToClipboard(
context,
viewModel.autoUpdatingCodeExtractor.instance?.generalCodeMatcher?.toString()
?: "",
false)
expanded = false
},
leadingIcon = {
Icon(
painterResource(R.drawable.baseline_content_copy_24),
contentDescription = null)
},
)
DropdownMenuItem(
text = { Text(stringResource(R.string.copy_special_regex)) },
onClick = {
Clipboard.copyToClipboard(
context,
viewModel.autoUpdatingCodeExtractor.instance?.specialCodeMatcher?.toString()
?: "",
false)
expanded = false
},
leadingIcon = {
Icon(
painterResource(R.drawable.baseline_content_copy_24),
contentDescription = null)
},
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import io.github.jd1378.otphelper.repository.UserSettingsRepository
import io.github.jd1378.otphelper.utils.AutoUpdatingCodeExtractor
import io.github.jd1378.otphelper.utils.CodeExtractorDefaults
import javax.inject.Inject
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.toPersistentList
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import javax.inject.Inject

@Stable
@HiltViewModel
Expand All @@ -23,6 +24,7 @@ class SensitivePhrasesViewModel
constructor(
private val savedStateHandle: SavedStateHandle,
private val userSettingsRepository: UserSettingsRepository,
val autoUpdatingCodeExtractor: AutoUpdatingCodeExtractor,
) : ViewModel() {
val showResetToDefaultDialog = MutableStateFlow(false)
val showNewSensitivePhraseDialog = MutableStateFlow(false)
Expand Down
36 changes: 22 additions & 14 deletions app/src/main/java/io/github/jd1378/otphelper/utils/Clipboard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,32 @@ import io.github.jd1378.otphelper.R

class Clipboard {
companion object {
/** returns true if successful */
fun copyCodeToClipboard(context: Context, code: String, showToast: Boolean = true): Boolean {
fun copyToClipboard(context: Context, code: String, isSensitive: Boolean = false): Boolean {
val clipboardManager =
context.getSystemService(Activity.CLIPBOARD_SERVICE) as? ClipboardManager

return if (clipboardManager !== null) {
clipboardManager.setPrimaryClip(
ClipData.newPlainText(code, code).apply {
description.extras =
PersistableBundle().apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, isSensitive)
} else {
putBoolean("android.content.extra.IS_SENSITIVE", isSensitive)
}
}
})
true
} else {
false
}
}

/** returns true if successful */
fun copyCodeToClipboard(context: Context, code: String, showToast: Boolean = true): Boolean {
val toastText: Int =
if (clipboardManager !== null) {
clipboardManager.setPrimaryClip(
ClipData.newPlainText(code, code).apply {
description.extras =
PersistableBundle().apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
putBoolean(ClipDescription.EXTRA_IS_SENSITIVE, true)
} else {
putBoolean("android.content.extra.IS_SENSITIVE", true)
}
}
})
if (copyToClipboard(context, code, true)) {
R.string.code_copied_to_clipboard
} else {
R.string.code_failed_to_access_clipboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object CodeExtractorDefaults {
}

class CodeExtractor // this comment is to separate parts
(val sensitivePhrases: List<String>) {
(private val sensitivePhrases: List<String>) {

val generalCodeMatcher: Regex =
"""(${sensitivePhrases.joinToString("|")})(?:\s*(?!${
Expand Down Expand Up @@ -175,6 +175,7 @@ constructor(private val userSettingsRepository: UserSettingsRepository) {
private val scope = CoroutineScope(Dispatchers.IO + exceptionHandler)

var instance: CodeExtractor? = null
private set

init {
scope.launch {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">Allow App</string>
<string name="n_items">%1$d عنصر</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
12 changes: 12 additions & 0 deletions app/src/main/res/values-b+pt+BR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">Allow App</string>
<string name="n_items">%1$d itens</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
14 changes: 13 additions & 1 deletion app/src/main/res/values-b+zh+Hans/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">允许应用</string>
<string name="n_items">%1$d 个项目</string>
<string name="app_label_not_visible_reason">应用程序名不可用,可能因为应用被卸载了。</string>
</resources>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
14 changes: 13 additions & 1 deletion app/src/main/res/values-b+zh+Hant/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">放行應用程式</string>
<string name="n_items">%1$d 個項目</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
</resources>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
12 changes: 12 additions & 0 deletions app/src/main/res/values-bn-rBD/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="no">No</string>
<string name="n_items">%1$d items</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
12 changes: 12 additions & 0 deletions app/src/main/res/values-ca/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">Allow App</string>
<string name="n_items">%1$s elements</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
14 changes: 13 additions & 1 deletion app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">Anwendungen zulassen</string>
<string name="n_items">%1$d Objekte</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
</resources>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
12 changes: 12 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">Permitir aplicación</string>
<string name="n_items">%1$d elemento(s)</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
14 changes: 13 additions & 1 deletion app/src/main/res/values-fa-rIR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">اجازه به برنامه</string>
<string name="n_items">%1$d مورد</string>
<string name="app_label_not_visible_reason">نام برنامه در دسترس نیست، احتمالا بخاطر این که پاک شده است.</string>
</resources>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
14 changes: 13 additions & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">Autoriser</string>
<string name="n_items">%1$s composants</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
</resources>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
12 changes: 12 additions & 0 deletions app/src/main/res/values-hi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">ऐप्स को अनुमति दें</string>
<string name="n_items">%1$sवस्तु</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
12 changes: 12 additions & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">Consenti app</string>
<string name="n_items">%1$d oggetti</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
12 changes: 12 additions & 0 deletions app/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,16 @@
<string name="allow_app">Allow App</string>
<string name="n_items">%1$s アイテム</string>
<string name="app_label_not_visible_reason">The application name is not available, probably because it was uninstalled.</string>
<string name="sensitive_phrases">Sensitive Phrases</string>
<string name="reset_to_default">Reset to default</string>
<string name="add">Add</string>
<string name="sensitive_phrases_desc">Here you can add phrases that trigger code detection.</string>
<string name="sensitive_phrases_desc_regex">Phrases must be valid regex.</string>
<string name="learn_more">Learn more</string>
<string name="new_sensitive_phrase">New Sensitive Phrase</string>
<string name="your_new_phrase">Your new phrase</string>
<string name="cancel">Cancel</string>
<string name="clear_list">Clear list</string>
<string name="copy_general_regex">Copy general regex</string>
<string name="copy_special_regex">Copy special regex</string>
</resources>
Loading

0 comments on commit 657df45

Please sign in to comment.