/* * Copyright (C) 2017 Moez Bhatti * * 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 . */ package mapper import android.content.Context import android.database.Cursor import android.net.Uri import android.provider.Telephony import android.provider.Telephony.Threads import model.Conversation import model.Recipient import javax.inject.Inject class CursorToConversationImpl @Inject constructor(private val context: Context) : CursorToConversation { companion object { val URI: Uri = Uri.parse("content://mms-sms/conversations?simple=true") val PROJECTION = arrayOf( Threads._ID, Threads.RECIPIENT_IDS) val ID = 0 val RECIPIENT_IDS = 1 } override fun map(from: Cursor): Conversation { return Conversation().apply { id = from.getLong(ID) recipients.addAll(from.getString(RECIPIENT_IDS) .split(" ") .filter { it.isNotBlank() } .map { recipientId -> recipientId.toLong() } .map { recipientId -> Recipient().apply { id = recipientId } }) } } override fun getConversationsCursor(lastSync: Long): Cursor { return context.contentResolver.query(URI, PROJECTION, "date > $lastSync AND ${Telephony.Threads.MESSAGE_COUNT} > 0", null, "date desc") } override fun getConversationCursor(threadId: Long): Cursor { return context.contentResolver.query(URI, PROJECTION, "_id = ?", arrayOf(threadId.toString()), null) } }