Merge branch 'master' into feat/export-sms

# Conflicts:
#	app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt
#	app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Config.kt
#	app/src/main/kotlin/com/simplemobiletools/smsmessenger/helpers/Constants.kt
This commit is contained in:
darthpaul 2021-09-18 22:16:12 +01:00
commit a5109da19f
44 changed files with 565 additions and 54 deletions

View file

@ -0,0 +1,9 @@
package com.simplemobiletools.smsmessenger.extensions
import android.graphics.Bitmap
fun Bitmap.CompressFormat.extension() = when (this) {
Bitmap.CompressFormat.PNG -> "png"
Bitmap.CompressFormat.WEBP -> "webp"
else -> "jpg"
}

View file

@ -5,6 +5,7 @@ import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ContentResolver
import android.content.ContentValues
import android.content.Context
import android.content.Intent
@ -17,6 +18,7 @@ import android.net.Uri
import android.os.Handler
import android.os.Looper
import android.provider.ContactsContract.PhoneLookup
import android.provider.OpenableColumns
import android.provider.Telephony.*
import android.text.TextUtils
import androidx.core.app.NotificationCompat
@ -35,8 +37,10 @@ import com.simplemobiletools.smsmessenger.interfaces.MessagesDao
import com.simplemobiletools.smsmessenger.models.*
import com.simplemobiletools.smsmessenger.receivers.DirectReplyReceiver
import com.simplemobiletools.smsmessenger.receivers.MarkAsReadReceiver
import java.io.FileNotFoundException
import java.util.*
import kotlin.collections.ArrayList
import java.text.Normalizer
import me.leolin.shortcutbadger.ShortcutBadger
val Context.config: Config get() = Config.newInstance(applicationContext)
@ -73,11 +77,7 @@ fun Context.getMessages(threadId: Long): ArrayList<Message> {
val blockedNumbers = getBlockedNumbers()
var messages = ArrayList<Message>()
queryCursor(uri, projection, selection, selectionArgs, sortOrder, showErrors = true) { cursor ->
val senderNumber = cursor.getStringValue(Sms.ADDRESS)
if(senderNumber == null){
return@queryCursor
}
val senderNumber = cursor.getStringValue(Sms.ADDRESS) ?: return@queryCursor
val isNumberBlocked = if (blockStatus.containsKey(senderNumber)) {
blockStatus[senderNumber]!!
@ -783,6 +783,10 @@ fun Context.getLockScreenVisibilityText(type: Int) = getString(
}
)
fun Context.removeDiacriticsIfNeeded(text: String): String {
return if (config.useSimpleCharacters) text.normalizeString() else text
}
fun Context.getSmsDraft(threadId: Long): String? {
val uri = Sms.Draft.CONTENT_URI
val projection = arrayOf(Sms.BODY)
@ -835,6 +839,18 @@ fun Context.deleteSmsDraft(threadId: Long) {
}
}
fun Context.getMMSFileLimitText(size: Long) = getString(
when (size) {
FILE_SIZE_100_KB -> R.string.mms_file_size_limit_100kb
FILE_SIZE_200_KB -> R.string.mms_file_size_limit_200kb
FILE_SIZE_300_KB -> R.string.mms_file_size_limit_300kb
FILE_SIZE_600_KB -> R.string.mms_file_size_limit_600kb
FILE_SIZE_1_MB -> R.string.mms_file_size_limit_1mb
FILE_SIZE_2_MB -> R.string.mms_file_size_limit_2mb
else -> R.string.mms_file_size_limit_none
}
)
fun Context.updateLastConversationMessage(threadId: Long) {
val uri = Threads.CONTENT_URI
val selection = "${Threads._ID} = ?"
@ -846,3 +862,38 @@ fun Context.updateLastConversationMessage(threadId: Long) {
} catch (e: Exception) {
}
}
fun Context.getFileSizeFromUri(uri: Uri): Long {
val assetFileDescriptor = try {
contentResolver.openAssetFileDescriptor(uri, "r")
} catch (e: FileNotFoundException) {
null
}
// uses ParcelFileDescriptor#getStatSize underneath if failed
val length = assetFileDescriptor?.use { it.length } ?: FILE_SIZE_NONE
if (length != -1L) {
return length
}
// if "content://" uri scheme, try contentResolver table
if (uri.scheme.equals(ContentResolver.SCHEME_CONTENT)) {
return contentResolver.query(uri, arrayOf(OpenableColumns.SIZE), null, null, null)
?.use { cursor ->
// maybe shouldn't trust ContentResolver for size:
// https://stackoverflow.com/questions/48302972/content-resolver-returns-wrong-size
val sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE)
if (sizeIndex == -1) {
return@use FILE_SIZE_NONE
}
cursor.moveToFirst()
return try {
cursor.getLong(sizeIndex)
} catch (_: Throwable) {
FILE_SIZE_NONE
}
} ?: FILE_SIZE_NONE
} else {
return FILE_SIZE_NONE
}
}

View file

@ -0,0 +1,16 @@
package com.simplemobiletools.smsmessenger.extensions
fun String.getExtensionFromMimeType(): String {
return when (lowercase()) {
"image/png" -> ".png"
"image/apng" -> ".apng"
"image/webp" -> ".webp"
"image/svg+xml" -> ".svg"
"image/gif" -> ".gif"
else -> ".jpg"
}
}
fun String.isImageMimeType(): Boolean {
return lowercase().startsWith("image")
}