Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
489 changes: 489 additions & 0 deletions app/schemas/com.glooboo.umessage.spam.data.SpamDatabase/8.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ class ConversationDetailsActivity : ComposeSimpleActivity() {
override fun onResume() {
super.onResume()
hiddenGate.check()
// The system notification settings can change both while this page is in the background.
if (threadId != 0L) {
customNotifications = config.customNotifications.contains(threadId.toString())
refreshMute()
}
}

/** CL-23: shows the search page and loads this chat's messages for it (once). */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class MainRoutes(private val activity: BaseSimpleActivity) {
onSelectAll = viewModel::selectAll,
onPin = viewModel::pinSelected,
onMarkRead = viewModel::markSelectedRead,
onMarkAllRead = viewModel::markAllRead,
onUndoMarkAllRead = viewModel::undoMarkAllRead,
onArchiveSelected = viewModel::archiveSelected,
onDeleteSelected = viewModel::deleteSelected,
onArchiveOne = viewModel::archiveOne,
Expand All @@ -78,6 +80,7 @@ class MainRoutes(private val activity: BaseSimpleActivity) {
onUndoDelete = viewModel::undoDelete,
onCommitDelete = viewModel::commitDelete,
onUndoArchive = viewModel::undoArchive,
onCommitArchive = viewModel::commitArchive,
onNewConversation = ::openNewConversation,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import org.fossify.commons.extensions.showErrorToast
import org.fossify.commons.extensions.toast
import org.fossify.commons.helpers.ExportResult
import org.fossify.commons.helpers.ensureBackgroundThread
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream

@Suppress("TooManyFunctions")
class ManageBlockedKeywordsActivity : ComposeSimpleActivity() {

private var keywords by mutableStateOf<List<String>>(emptyList())
Expand Down Expand Up @@ -104,37 +105,53 @@ class ManageBlockedKeywordsActivity : ComposeSimpleActivity() {

private fun tryImportBlockedKeywordsFromFile(uri: Uri) {
when (uri.scheme) {
"file" -> importBlockedKeywords(uri.path!!)
"content" -> {
val tempFile = getTempFile("blocked", "blocked_keywords.txt")
if (tempFile == null) {
toast(org.fossify.commons.R.string.unknown_error_occurred)
return
}
"file" -> uri.path?.let(::importBlockedKeywords)
?: toast(org.fossify.commons.R.string.invalid_file_format)

"content" -> importFromContent(uri)
else -> toast(org.fossify.commons.R.string.invalid_file_format)
}
}

try {
val inputStream = contentResolver.openInputStream(uri)
val out = FileOutputStream(tempFile)
inputStream!!.copyTo(out)
importBlockedKeywords(tempFile.absolutePath)
} catch (e: Exception) {
showErrorToast(e)
/** Copies the picked file to a temp file off the main thread (closing both streams, size capped), then imports. */
private fun importFromContent(uri: Uri) {
val tempFile = getTempFile("blocked", "blocked_keywords.txt")
if (tempFile == null) {
toast(org.fossify.commons.R.string.unknown_error_occurred)
return
}
ensureBackgroundThread {
try {
val input = contentResolver.openInputStream(uri) ?: error("No input stream")
val copied = input.use { source ->
tempFile.outputStream().use { out -> source.copyAtMost(out, MAX_IMPORT_BYTES) }
}
if (copied) {
BlockedKeywordsImporter(this).importBlockedKeywords(tempFile.absolutePath).let(::showImportResult)
updateBlockedKeywords()
} else {
toast(org.fossify.commons.R.string.invalid_file_format)
}
} catch (e: Exception) {
showErrorToast(e)
} finally {
tempFile.delete()
}

else -> toast(org.fossify.commons.R.string.invalid_file_format)
}
}

private fun showImportResult(result: BlockedKeywordsImporter.ImportResult) {
toast(
when (result) {
BlockedKeywordsImporter.ImportResult.IMPORT_OK -> org.fossify.commons.R.string.importing_successful
BlockedKeywordsImporter.ImportResult.IMPORT_FAIL -> org.fossify.commons.R.string.no_items_found
},
)
}

private fun importBlockedKeywords(path: String) {
ensureBackgroundThread {
val result = BlockedKeywordsImporter(this).importBlockedKeywords(path)
toast(
when (result) {
BlockedKeywordsImporter.ImportResult.IMPORT_OK -> org.fossify.commons.R.string.importing_successful
BlockedKeywordsImporter.ImportResult.IMPORT_FAIL -> org.fossify.commons.R.string.no_items_found
},
)
showImportResult(BlockedKeywordsImporter(this).importBlockedKeywords(path))
updateBlockedKeywords()
}
}
Expand Down Expand Up @@ -196,3 +213,20 @@ class ManageBlockedKeywordsActivity : ComposeSimpleActivity() {
updateBlockedKeywords()
}
}

/** A keyword list is small: anything bigger than this is not one. */
private const val MAX_IMPORT_BYTES = 1_000_000L
private const val COPY_BUFFER_BYTES = 8 * 1024

/** Copies at most [limit] bytes; false (with the copy cut short) when the source has more. */
private fun InputStream.copyAtMost(out: OutputStream, limit: Long): Boolean {
val buffer = ByteArray(COPY_BUFFER_BYTES)
var total = 0L
while (true) {
val read = read(buffer)
if (read < 0) return true
total += read
if (total > limit) return false
out.write(buffer, 0, read)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ class NewConversationActivity : ComposeSimpleActivity() {
* Opens the chat and closes this picker, so Back from the chat returns to the list (MC-10). When Android can't make
* a chat for the number (thread id 0) it says so and the picker stays (MC-07); a link from another app closes.
*/
/** Set while a chat is being opened, so a double tap cannot start the thread twice (cleared on failure). */
private var launching = false

private fun launchThreadActivity(phoneNumber: String, name: String, body: String = "", finishAfter: Boolean = false) {
hideKeyboard()
val numbers = phoneNumber.split(";").map { it.trim() }.filter { it.isNotEmpty() }.toSet()
Expand All @@ -202,9 +205,12 @@ class NewConversationActivity : ComposeSimpleActivity() {
}
val number = if (numbers.size == 1) numbers.first() else Gson().toJson(numbers)
// getOrCreateThreadId is a Telephony provider round trip: keep it off the main thread.
if (launching) return
launching = true
lifecycleScope.launch {
val threadId = withContext(Dispatchers.IO) { runCatching { getThreadId(numbers) }.getOrDefault(0L) }
if (threadId <= 0L) {
launching = false
toast(R.string.new_conversation_invalid_number, length = Toast.LENGTH_LONG)
if (finishAfter) finish()
return@launch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.combine
import com.glooboo.umessage.dialogs.showAppLockDialog
import org.fossify.commons.extensions.getBlockedNumbers
Expand Down Expand Up @@ -57,6 +58,7 @@ import com.glooboo.umessage.helpers.w3aPrefs
import com.glooboo.umessage.backup.SettingsBackup
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import android.widget.Toast
import com.glooboo.umessage.spam.engine.SpamEngine
import android.content.ClipData
Expand All @@ -80,6 +82,7 @@ import com.glooboo.umessage.ui.settings.LongMmsSettingDialog
import com.glooboo.umessage.ui.settings.longMmsLabel
import com.glooboo.umessage.ui.settings.SettingsScreen
import com.glooboo.umessage.ui.settings.SettingsUiState
import com.glooboo.umessage.ui.settings.SimCard
import com.glooboo.umessage.ui.settings.SimCards
import com.glooboo.umessage.ui.settings.SimMessagesDialog
import com.glooboo.umessage.ui.settings.SpamSummary
Expand Down Expand Up @@ -110,6 +113,7 @@ import com.glooboo.umessage.ui.settings.RestoreSummary
* Compose-hosted settings. Every option writes the same Config keys as the former View screen.
* Stays a [SimpleActivity] because the commons security dialog needs one.
*/
@Suppress("LargeClass")
class SettingsActivity : ComposeSimpleActivity() {
private var blockedNumbersAtPause = -1
private var recycleBinMessages = 0
Expand Down Expand Up @@ -153,7 +157,12 @@ class SettingsActivity : ComposeSimpleActivity() {
private val saveDocument =
registerForActivityResult(CreateExportDocument()) { uri ->
if (uri != null) {
exportMessagesDialog?.exportMessages(uri)
val dialog = exportMessagesDialog
if (dialog != null) {
dialog.exportMessages(uri)
} else {
toast(org.fossify.commons.R.string.unknown_error_occurred)
}
}
}

Expand Down Expand Up @@ -189,11 +198,15 @@ class SettingsActivity : ComposeSimpleActivity() {
}

private val phoneStatePermission =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { refreshState() }
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
refreshState()
refreshSims()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
refreshState()
refreshSims()
// Bug 7: another screen can open a page directly (the forward editor opens App lock).
val initialPage = (intent?.getStringExtra(EXTRA_PAGE)
?.let { name -> SettingsPage.entries.firstOrNull { it.name == name } } ?: SettingsPage.Main)
Expand All @@ -210,6 +223,7 @@ class SettingsActivity : ComposeSimpleActivity() {
override fun onResume() {
super.onResume()
refreshState()
refreshSims()
loadRecycleBinCount()
loadForwardingAndArchived()
reportInterruptedBackup()
Expand Down Expand Up @@ -244,11 +258,24 @@ class SettingsActivity : ComposeSimpleActivity() {
}
}

/** The SIM list, read off the main thread (the subscription service can be slow) and kept for [refreshState]. */
private var sims: List<SimCard> = emptyList()

private fun refreshSims() {
lifecycleScope.launch {
val loaded = withContext(Dispatchers.IO) { SimCards.load(this@SettingsActivity) }
if (loaded != sims) {
sims = loaded
uiState = uiState.copy(sims = loaded)
}
}
}

private fun refreshState() {
uiState = SettingsUiState(
simPermissionGranted = SimCards.hasPermission(this),
showOldBlockedWords = config.blockedKeywords.isNotEmpty(),
sims = SimCards.load(this),
sims = sims,
defaultSimId = config.defaultSimId,
lockScreenLabel = lockScreenLabel(),
forwardingAvailable = ForwardingAvailability.isAvailable(this),
Expand Down Expand Up @@ -483,7 +510,7 @@ class SettingsActivity : ComposeSimpleActivity() {
}

private fun pickRestoreFile() = launchOwnPicker {
val types = arrayOf("application/json", "text/plain")
val types = arrayOf("application/json", "text/plain", "application/octet-stream")
if (restoreViaDrive) openDriveSettingsBackup.launch(types) else openSettingsBackup.launch(types)
restoreViaDrive = false
}
Expand Down
Loading
Loading