Move packages to com.moez.QKSMS

This commit is contained in:
Moez Bhatti
2018-06-20 14:29:07 -04:00
parent d8141737b3
commit a5f518042e
268 changed files with 1101 additions and 1116 deletions
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.mapper
import android.content.Context
import android.database.Cursor
import android.provider.ContactsContract.CommonDataKinds.Phone.*
import com.moez.QKSMS.manager.PermissionManager
import com.moez.QKSMS.model.Contact
import com.moez.QKSMS.model.PhoneNumber
import javax.inject.Inject
class CursorToContactImpl @Inject constructor(
private val context: Context,
private val permissionManager: PermissionManager
) : CursorToContact {
companion object {
val URI = CONTENT_URI
val PROJECTION = arrayOf(LOOKUP_KEY, NUMBER, TYPE, DISPLAY_NAME)
const val COLUMN_LOOKUP_KEY = 0
const val COLUMN_NUMBER = 1
const val COLUMN_TYPE = 2
const val COLUMN_DISPLAY_NAME = 3
}
override fun map(from: Cursor) = Contact().apply {
lookupKey = from.getString(COLUMN_LOOKUP_KEY)
name = from.getString(COLUMN_DISPLAY_NAME) ?: ""
numbers.add(PhoneNumber().apply {
address = from.getString(COLUMN_NUMBER) ?: ""
type = context.getString(getTypeLabelResource(from.getInt(COLUMN_TYPE)))
})
lastUpdate = System.currentTimeMillis()
}
override fun getContactsCursor(): Cursor? {
return when (permissionManager.hasContacts()) {
true -> context.contentResolver.query(URI, PROJECTION, null, null, null)
false -> null
}
}
}
@@ -0,0 +1,83 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.mapper
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.Telephony.Threads
import com.moez.QKSMS.manager.PermissionManager
import com.moez.QKSMS.model.Conversation
import com.moez.QKSMS.model.Recipient
import javax.inject.Inject
class CursorToConversationImpl @Inject constructor(
private val context: Context,
private val permissionManager: PermissionManager
) : CursorToConversation {
companion object {
val URI: Uri = Uri.parse("content://mms-sms/conversations?simple=true")
val PROJECTION = arrayOf(
Threads._ID,
Threads.DATE,
Threads.RECIPIENT_IDS,
Threads.MESSAGE_COUNT,
Threads.READ,
Threads.SNIPPET)
const val ID = 0
const val DATE = 1
const val RECIPIENT_IDS = 2
const val MESSAGE_COUNT = 3
const val READ = 4
const val SNIPPET = 5
}
override fun map(from: Cursor): Conversation {
return Conversation().apply {
id = from.getLong(ID)
date = from.getLong(DATE)
recipients.addAll(from.getString(RECIPIENT_IDS)
.split(" ")
.filter { it.isNotBlank() }
.map { recipientId -> recipientId.toLong() }
.map { recipientId -> Recipient().apply { id = recipientId } })
count = from.getInt(MESSAGE_COUNT)
read = from.getInt(READ) == 1
snippet = from.getString(SNIPPET) ?: ""
}
}
override fun getConversationsCursor(lastSync: Long): Cursor? {
return when (permissionManager.hasSms()) {
true -> context.contentResolver.query(URI, PROJECTION,
"date > $lastSync", null,
"date desc")
false -> null
}
}
override fun getConversationCursor(threadId: Long): Cursor? {
return context.contentResolver.query(URI, PROJECTION,
"_id = ?", arrayOf(threadId.toString()), null)
}
}
@@ -0,0 +1,156 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.mapper
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.Telephony.*
import com.google.android.mms.pdu_alt.PduHeaders
import com.moez.QKSMS.extensions.map
import com.moez.QKSMS.manager.KeyManager
import com.moez.QKSMS.manager.PermissionManager
import com.moez.QKSMS.model.Message
import javax.inject.Inject
class CursorToMessageImpl @Inject constructor(
private val context: Context,
private val cursorToPart: CursorToPart,
private val keys: KeyManager,
private val permissionManager: PermissionManager
) : CursorToMessage {
companion object {
val URI = Uri.parse("content://mms-sms/complete-conversations")
val PROJECTION = arrayOf(
MmsSms.TYPE_DISCRIMINATOR_COLUMN,
MmsSms._ID,
Mms.DATE,
Mms.DATE_SENT,
Mms.READ,
Mms.THREAD_ID,
Mms.LOCKED,
Mms.SUBSCRIPTION_ID,
Sms.ADDRESS,
Sms.BODY,
Sms.SEEN,
Sms.TYPE,
Sms.STATUS,
Sms.ERROR_CODE,
Mms.SUBJECT,
Mms.SUBJECT_CHARSET,
Mms.SEEN,
Mms.MESSAGE_TYPE,
Mms.MESSAGE_BOX,
Mms.DELIVERY_REPORT,
Mms.READ_REPORT,
MmsSms.PendingMessages.ERROR_TYPE,
Mms.STATUS)
}
override fun map(from: Pair<Cursor, CursorToMessage.MessageColumns>): Message {
val cursor = from.first
val columnsMap = from.second
return Message().apply {
type = when {
cursor.getColumnIndex(MmsSms.TYPE_DISCRIMINATOR_COLUMN) != -1 -> cursor.getString(columnsMap.msgType)
cursor.getColumnIndex(Mms.SUBJECT) != -1 -> "mms"
cursor.getColumnIndex(Sms.ADDRESS) != -1 -> "sms"
else -> "unknown"
}
id = keys.newId()
threadId = cursor.getLong(columnsMap.threadId)
contentId = cursor.getLong(columnsMap.msgId)
date = cursor.getLong(columnsMap.date)
dateSent = cursor.getLong(columnsMap.dateSent)
read = cursor.getInt(columnsMap.read) != 0
locked = cursor.getInt(columnsMap.locked) != 0
subId = if (columnsMap.subId != -1) cursor.getInt(columnsMap.subId) else -1
when (type) {
"sms" -> {
address = cursor.getString(columnsMap.smsAddress) ?: ""
boxId = cursor.getInt(columnsMap.smsType)
seen = cursor.getInt(columnsMap.smsSeen) != 0
body = columnsMap.smsBody
.takeIf { column -> column != -1 } // The column may not be set
?.let { column -> cursor.getString(column) } ?: "" // cursor.getString() may return null
errorCode = cursor.getInt(columnsMap.smsErrorCode)
deliveryStatus = cursor.getInt(columnsMap.smsStatus)
}
"mms" -> {
address = getMmsAddress(contentId)
boxId = cursor.getInt(columnsMap.mmsMessageBox)
date *= 1000L
dateSent *= 1000L
seen = cursor.getInt(columnsMap.mmsSeen) != 0
mmsDeliveryStatusString = cursor.getString(columnsMap.mmsDeliveryReport) ?: ""
errorType = if (columnsMap.mmsErrorType != -1) cursor.getInt(columnsMap.mmsErrorType) else 0
messageSize = 0
readReportString = cursor.getString(columnsMap.mmsReadReport) ?: ""
messageType = cursor.getInt(columnsMap.mmsMessageType)
mmsStatus = cursor.getInt(columnsMap.mmsStatus)
subject = cursor.getString(columnsMap.mmsSubject) ?: ""
textContentType = ""
attachmentType = Message.AttachmentType.NOT_LOADED
parts.addAll(cursorToPart.getPartsCursor(contentId)?.map { cursorToPart.map(it) } ?: listOf())
}
}
}
}
override fun getMessagesCursor(): Cursor? {
return when (permissionManager.hasSms()) {
true -> context.contentResolver.query(URI, PROJECTION, null, null, "normalized_date desc")
false -> null
}
}
override fun getMessageCursor(id: Long): Cursor? {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
private fun getMmsAddress(messageId: Long): String {
val uri = Mms.CONTENT_URI.buildUpon()
.appendPath(messageId.toString())
.appendPath("addr").build()
//TODO: Use Charset to ensure address is decoded correctly
val projection = arrayOf(Mms.Addr.ADDRESS, Mms.Addr.CHARSET)
val selection = "${Mms.Addr.TYPE} = ${PduHeaders.FROM}"
val cursor = context.contentResolver.query(uri, projection, selection, null, null)
cursor?.use {
if (cursor.moveToFirst()) {
return cursor.getString(0) ?: ""
}
}
return ""
}
}
@@ -0,0 +1,83 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.mapper
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.Telephony
import com.moez.QKSMS.extensions.isImage
import com.moez.QKSMS.extensions.isSmil
import com.moez.QKSMS.extensions.isText
import com.moez.QKSMS.extensions.isVideo
import com.moez.QKSMS.model.MmsPart
import timber.log.Timber
import java.io.BufferedReader
import java.io.InputStreamReader
import javax.inject.Inject
class CursorToPartImpl @Inject constructor(private val context: Context) : CursorToPart {
companion object {
val CONTENT_URI: Uri = Uri.parse("content://mms/part")
}
override fun map(from: Cursor) = MmsPart().apply {
id = from.getLong(from.getColumnIndexOrThrow(Telephony.Mms.Part._ID))
// Type will sometimes return null, resulting in a crash if we don't default to an empty string
type = from.getString(from.getColumnIndexOrThrow(Telephony.Mms.Part.CONTENT_TYPE)) ?: ""
val data = from.getString(from.getColumnIndexOrThrow(Telephony.Mms.Part._DATA))
when {
isSmil() || isImage() || isVideo() -> {
// Do nothing special
}
isText() -> {
text = if (data == null) {
from.getString(from.getColumnIndexOrThrow("text"))
} else {
val uri = ContentUris.withAppendedId(CONTENT_URI, id)
val sb = StringBuilder()
context.contentResolver.openInputStream(uri)?.use { inputStream ->
val isr = InputStreamReader(inputStream, "UTF-8")
val reader = BufferedReader(isr)
var temp = reader.readLine()
while (temp != null) {
sb.append(temp)
temp = reader.readLine()
}
}
sb.toString()
}
}
else -> Timber.v("Unhandled type: $type")
}
}
override fun getPartsCursor(messageId: Long): Cursor? {
return context.contentResolver.query(CONTENT_URI, null,
"${Telephony.Mms.Part.MSG_ID} = ?", arrayOf(messageId.toString()), null)
}
}
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.mapper
import android.content.Context
import android.database.Cursor
import android.net.Uri
import com.moez.QKSMS.manager.PermissionManager
import com.moez.QKSMS.model.Recipient
import javax.inject.Inject
class CursorToRecipientImpl @Inject constructor(
private val context: Context,
private val permissionManager: PermissionManager
) : CursorToRecipient {
companion object {
val URI = Uri.parse("content://mms-sms/canonical-addresses")
const val COLUMN_ID = 0
const val COLUMN_ADDRESS = 1
}
override fun map(from: Cursor) = Recipient().apply {
id = from.getLong(COLUMN_ID)
address = from.getString(COLUMN_ADDRESS)
lastUpdate = System.currentTimeMillis()
}
override fun getRecipientCursor(): Cursor? {
return when (permissionManager.hasSms()) {
true -> context.contentResolver.query(URI, null, null, null, null)
false -> null
}
}
override fun getRecipientCursor(id: Long): Cursor? {
return context.contentResolver.query(URI, null, "_id = ?", arrayOf(id.toString()), null)
}
}
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.mapper
import com.f2prateek.rx.preferences2.RxSharedPreferences
import com.moez.QKSMS.manager.AnalyticsManager
import com.moez.QKSMS.manager.RatingManager
import io.reactivex.rxkotlin.Observables
import javax.inject.Inject
class RatingManagerImpl @Inject constructor(
rxPrefs: RxSharedPreferences,
private val analyticsManager: AnalyticsManager
) : RatingManager {
companion object {
private const val SESSION_THRESHOLD = 100
}
private val sessions = rxPrefs.getInteger("sessions", 0)
private val rated = rxPrefs.getBoolean("rated", false)
private val dismissed = rxPrefs.getBoolean("dismissed", false)
override val shouldShowRating = Observables.combineLatest(
sessions.asObservable(),
rated.asObservable(),
dismissed.asObservable(), { sessions, rated, dismissed ->
sessions > SESSION_THRESHOLD && !rated && !dismissed
})
override fun addSession() {
sessions.set(sessions.get() + 1)
}
override fun rate() {
analyticsManager.track("Clicked Rate")
rated.set(true)
}
override fun dismiss() {
analyticsManager.track("Clicked Rate (Dismiss)")
dismissed.set(true)
}
}