mirror of
https://github.com/SimpleMobileTools/Simple-Contacts.git
synced 2025-06-05 21:59:27 +02:00
handle converting local contacts to the generic contacts
This commit is contained in:
@ -4,7 +4,9 @@ import android.content.Context
|
||||
import androidx.room.Database
|
||||
import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
import com.simplemobiletools.contacts.pro.helpers.Converters
|
||||
import com.simplemobiletools.contacts.pro.helpers.getEmptyLocalContact
|
||||
import com.simplemobiletools.contacts.pro.interfaces.ContactsDao
|
||||
import com.simplemobiletools.contacts.pro.models.LocalContact
|
||||
@ -12,12 +14,13 @@ import com.simplemobiletools.contacts.pro.objects.MyExecutor
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
@Database(entities = [(LocalContact::class)], version = 1)
|
||||
@TypeConverters(Converters::class)
|
||||
abstract class ContactsDatabase : RoomDatabase() {
|
||||
|
||||
abstract fun ContactsDao(): ContactsDao
|
||||
|
||||
companion object {
|
||||
private val FIRST_CONTACT_ID = 1000000L
|
||||
private const val FIRST_CONTACT_ID = 1000000
|
||||
|
||||
private var db: ContactsDatabase? = null
|
||||
|
||||
|
@ -121,4 +121,4 @@ const val TELEGRAM_PACKAGE = "org.telegram.messenger"
|
||||
const val SIGNAL_PACKAGE = "org.thoughtcrime.securesms"
|
||||
const val WHATSAPP_PACKAGE = "com.whatsapp"
|
||||
|
||||
fun getEmptyLocalContact() = LocalContact(0, "", "", "", "", "", "", null, "", "", "", false, "", "", "", "", "", "", "")
|
||||
fun getEmptyLocalContact() = LocalContact(0, "", "", "", "", "", "", null, ArrayList(), ArrayList(), ArrayList(), 0, ArrayList(), "", ArrayList(), "", "", ArrayList(), ArrayList())
|
||||
|
@ -1213,7 +1213,7 @@ class ContactsHelper(val activity: Activity) {
|
||||
|
||||
fun insertContact(contact: Contact): Boolean {
|
||||
if (contact.source == SMT_PRIVATE) {
|
||||
return insertLocalContact(contact)
|
||||
return LocalContactsHelper(activity).insertContact(contact)
|
||||
}
|
||||
|
||||
try {
|
||||
@ -1398,8 +1398,6 @@ class ContactsHelper(val activity: Activity) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun insertLocalContact(contact: Contact) = activity.dbHelper.insertContact(contact)
|
||||
|
||||
private fun addFullSizePhoto(contactId: Long, fullSizePhotoData: ByteArray) {
|
||||
val baseUri = ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, contactId)
|
||||
val displayPhotoUri = Uri.withAppendedPath(baseUri, ContactsContract.RawContacts.DisplayPhoto.CONTENT_DIRECTORY)
|
||||
|
@ -0,0 +1,59 @@
|
||||
package com.simplemobiletools.contacts.pro.helpers
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.simplemobiletools.contacts.pro.models.*
|
||||
|
||||
class Converters {
|
||||
private val gson = Gson()
|
||||
private val stringType = object : TypeToken<List<String>>() {}.type
|
||||
private val numberType = object : TypeToken<List<PhoneNumber>>() {}.type
|
||||
private val emailType = object : TypeToken<List<Email>>() {}.type
|
||||
private val addressType = object : TypeToken<List<Address>>() {}.type
|
||||
private val eventType = object : TypeToken<List<Event>>() {}.type
|
||||
private val groupType = object : TypeToken<List<Group>>() {}.type
|
||||
private val imType = object : TypeToken<List<IM>>() {}.type
|
||||
|
||||
@TypeConverter
|
||||
fun jsonToStringList(value: String) = gson.fromJson<ArrayList<String>>(value, stringType)
|
||||
|
||||
@TypeConverter
|
||||
fun stringListToJson(list: ArrayList<String>) = gson.toJson(list)
|
||||
|
||||
@TypeConverter
|
||||
fun jsonToPhoneNumberList(value: String) = gson.fromJson<ArrayList<PhoneNumber>>(value, numberType)
|
||||
|
||||
@TypeConverter
|
||||
fun phoneNumberListToJson(list: ArrayList<PhoneNumber>) = gson.toJson(list)
|
||||
|
||||
@TypeConverter
|
||||
fun jsonToEmailList(value: String) = gson.fromJson<ArrayList<Email>>(value, emailType)
|
||||
|
||||
@TypeConverter
|
||||
fun emailListToJson(list: ArrayList<Email>) = gson.toJson(list)
|
||||
|
||||
@TypeConverter
|
||||
fun jsonToAddressList(value: String) = gson.fromJson<ArrayList<Address>>(value, addressType)
|
||||
|
||||
@TypeConverter
|
||||
fun addressListToJson(list: ArrayList<Address>) = gson.toJson(list)
|
||||
|
||||
@TypeConverter
|
||||
fun jsonToEventList(value: String) = gson.fromJson<ArrayList<Event>>(value, eventType)
|
||||
|
||||
@TypeConverter
|
||||
fun eventListToJson(list: ArrayList<Event>) = gson.toJson(list)
|
||||
|
||||
@TypeConverter
|
||||
fun jsonToGroupList(value: String) = gson.fromJson<ArrayList<Group>>(value, groupType)
|
||||
|
||||
@TypeConverter
|
||||
fun groupListToJson(list: ArrayList<Group>) = gson.toJson(list)
|
||||
|
||||
@TypeConverter
|
||||
fun jsonToIMsList(value: String) = gson.fromJson<ArrayList<IM>>(value, imType)
|
||||
|
||||
@TypeConverter
|
||||
fun IMsListToJson(list: ArrayList<IM>) = gson.toJson(list)
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.simplemobiletools.contacts.pro.helpers
|
||||
|
||||
import android.app.Activity
|
||||
import com.simplemobiletools.contacts.pro.extensions.contactsDB
|
||||
import com.simplemobiletools.contacts.pro.models.Contact
|
||||
import com.simplemobiletools.contacts.pro.models.LocalContact
|
||||
|
||||
class LocalContactsHelper(val activity: Activity) {
|
||||
fun insertContact(contact: Contact): Boolean {
|
||||
val localContact = convertContactToLocalContact(contact)
|
||||
activity.contactsDB.insertOrUpdate(localContact)
|
||||
return true
|
||||
}
|
||||
|
||||
private fun convertContactToLocalContact(contact: Contact): LocalContact {
|
||||
return getEmptyLocalContact().apply {
|
||||
id = if (contact.id == 0) null else contact.id
|
||||
prefix = contact.prefix
|
||||
firstName = contact.firstName
|
||||
middleName = contact.middleName
|
||||
surname = contact.surname
|
||||
suffix = contact.suffix
|
||||
nickname = contact.nickname
|
||||
phoneNumbers = contact.phoneNumbers
|
||||
emails = contact.emails
|
||||
events = contact.events
|
||||
starred = contact.starred
|
||||
addresses = contact.addresses
|
||||
notes = contact.notes
|
||||
groups = contact.groups
|
||||
company = contact.organization.company
|
||||
jobPosition = contact.organization.jobPosition
|
||||
websites = contact.websites
|
||||
ims = contact.IMs
|
||||
}
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@ import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "contacts", indices = [(Index(value = ["id"], unique = true))])
|
||||
data class LocalContact(
|
||||
@PrimaryKey(autoGenerate = true) var id: Long?,
|
||||
@PrimaryKey(autoGenerate = true) var id: Int?,
|
||||
@ColumnInfo(name = "prefix") var prefix: String,
|
||||
@ColumnInfo(name = "first_name") var firstName: String,
|
||||
@ColumnInfo(name = "middle_name") var middleName: String,
|
||||
@ -15,17 +15,17 @@ data class LocalContact(
|
||||
@ColumnInfo(name = "suffix") var suffix: String,
|
||||
@ColumnInfo(name = "nickname") var nickname: String,
|
||||
@ColumnInfo(name = "photo", typeAffinity = ColumnInfo.BLOB) var photo: ByteArray?,
|
||||
@ColumnInfo(name = "phone_numbers") var phoneNumbers: String,
|
||||
@ColumnInfo(name = "emails") var emails: String,
|
||||
@ColumnInfo(name = "events") var events: String,
|
||||
@ColumnInfo(name = "starred") var starred: Boolean,
|
||||
@ColumnInfo(name = "addresses") var addresses: String,
|
||||
@ColumnInfo(name = "phone_numbers") var phoneNumbers: ArrayList<PhoneNumber>,
|
||||
@ColumnInfo(name = "emails") var emails: ArrayList<Email>,
|
||||
@ColumnInfo(name = "events") var events: ArrayList<Event>,
|
||||
@ColumnInfo(name = "starred") var starred: Int,
|
||||
@ColumnInfo(name = "addresses") var addresses: ArrayList<Address>,
|
||||
@ColumnInfo(name = "notes") var notes: String,
|
||||
@ColumnInfo(name = "groups") var groups: String,
|
||||
@ColumnInfo(name = "groups") var groups: ArrayList<Group>,
|
||||
@ColumnInfo(name = "company") var company: String,
|
||||
@ColumnInfo(name = "job_position") var jobPosition: String,
|
||||
@ColumnInfo(name = "websites") var websites: String,
|
||||
@ColumnInfo(name = "ims") var ims: String) {
|
||||
@ColumnInfo(name = "websites") var websites: ArrayList<String>,
|
||||
@ColumnInfo(name = "ims") var ims: ArrayList<IM>) {
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = -655314977575622L
|
||||
|
Reference in New Issue
Block a user