From ab44c35fac684e853fc87733cee3a8878581f26b Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 27 Oct 2020 17:53:59 +0100 Subject: [PATCH] removing some social action fetching related code duplication --- .../pro/activities/ViewContactActivity.kt | 65 ++----------- .../contacts/pro/extensions/Context.kt | 92 ++----------------- 2 files changed, 17 insertions(+), 140 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt index f3d3bdb2..4d140fbb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/ViewContactActivity.kt @@ -11,7 +11,10 @@ import android.view.WindowManager import android.widget.RelativeLayout import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.* +import com.simplemobiletools.commons.helpers.CONTACT_ID +import com.simplemobiletools.commons.helpers.IS_PRIVATE +import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.contacts.pro.R import com.simplemobiletools.contacts.pro.dialogs.CallConfirmationDialog import com.simplemobiletools.contacts.pro.dialogs.ChooseSocialDialog @@ -536,7 +539,7 @@ class ViewContactActivity : ContactActivity() { contact_source_image.setImageDrawable(getPackageDrawable(WHATSAPP_PACKAGE)) contact_source_image.beVisible() contact_source_image.setOnClickListener { - showWhatsAppActions(key.id) + showSocialActions(key.id) } } @@ -544,7 +547,7 @@ class ViewContactActivity : ContactActivity() { contact_source_image.setImageDrawable(getPackageDrawable(SIGNAL_PACKAGE)) contact_source_image.beVisible() contact_source_image.setOnClickListener { - showSignalActions(key.id) + showSocialActions(key.id) } } @@ -552,7 +555,7 @@ class ViewContactActivity : ContactActivity() { contact_source_image.setImageDrawable(getPackageDrawable(VIBER_PACKAGE)) contact_source_image.beVisible() contact_source_image.setOnClickListener { - showViberActions(key.id) + showSocialActions(key.id) } } @@ -560,7 +563,7 @@ class ViewContactActivity : ContactActivity() { contact_source_image.setImageDrawable(getPackageDrawable(TELEGRAM_PACKAGE)) contact_source_image.beVisible() contact_source_image.setOnClickListener { - showTelegramActions(key.id) + showSocialActions(key.id) } } } @@ -608,57 +611,9 @@ class ViewContactActivity : ContactActivity() { } } - private fun showWhatsAppActions(contactId: Int) { + private fun showSocialActions(contactId: Int) { ensureBackgroundThread { - val actions = getWhatsAppActions(contactId) - runOnUiThread { - ChooseSocialDialog(this@ViewContactActivity, actions) { action -> - Intent(Intent.ACTION_VIEW).apply { - val uri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, action.dataId) - setDataAndType(uri, action.mimetype) - flags = Intent.FLAG_ACTIVITY_CLEAR_TASK - startActivity(this) - } - } - } - } - } - - private fun showSignalActions(contactId: Int) { - ensureBackgroundThread { - val actions = getSignalActions(contactId) - runOnUiThread { - ChooseSocialDialog(this@ViewContactActivity, actions) { action -> - Intent(Intent.ACTION_VIEW).apply { - val uri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, action.dataId) - setDataAndType(uri, action.mimetype) - flags = Intent.FLAG_ACTIVITY_CLEAR_TASK - startActivity(this) - } - } - } - } - } - - private fun showViberActions(contactId: Int) { - ensureBackgroundThread { - val actions = getViberActions(contactId) - runOnUiThread { - ChooseSocialDialog(this@ViewContactActivity, actions) { action -> - Intent(Intent.ACTION_VIEW).apply { - val uri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, action.dataId) - setDataAndType(uri, action.mimetype) - flags = Intent.FLAG_ACTIVITY_CLEAR_TASK - startActivity(this) - } - } - } - } - } - - private fun showTelegramActions(contactId: Int) { - ensureBackgroundThread { - val actions = getTelegramActions(contactId) + val actions = getSocialActions(contactId) runOnUiThread { ChooseSocialDialog(this@ViewContactActivity, actions) { action -> Intent(Intent.ACTION_VIEW).apply { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt index 3c1b8fad..96fd322e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt @@ -329,7 +329,7 @@ fun Context.getAllContactSources(): ArrayList { fun Context.getPrivateContactSource() = ContactSource(SMT_PRIVATE, SMT_PRIVATE, getString(R.string.phone_storage_hidden)) -fun Context.getWhatsAppActions(id: Int): ArrayList { +fun Context.getSocialActions(id: Int): ArrayList { val uri = ContactsContract.Data.CONTENT_URI val projection = arrayOf( ContactsContract.Data._ID, @@ -345,98 +345,21 @@ fun Context.getWhatsAppActions(id: Int): ArrayList { queryCursor(uri, projection, selection, selectionArgs, null, true) { cursor -> val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE) val type = when (mimetype) { + // WhatsApp "vnd.android.cursor.item/vnd.com.whatsapp.profile" -> SOCIAL_MESSAGE "vnd.android.cursor.item/vnd.com.whatsapp.voip.call" -> SOCIAL_VOICE_CALL "vnd.android.cursor.item/vnd.com.whatsapp.video.call" -> SOCIAL_VIDEO_CALL - else -> return@queryCursor - } - val label = cursor.getStringValue(ContactsContract.Data.DATA3) - val realID = cursor.getLongValue(ContactsContract.Data._ID) - val packageName = cursor.getStringValue(ContactsContract.Data.ACCOUNT_TYPE_AND_DATA_SET) - val socialAction = SocialAction(curActionId++, type, label, mimetype, realID, packageName) - socialActions.add(socialAction) - } - return socialActions -} - -fun Context.getSignalActions(id: Int): ArrayList { - val uri = ContactsContract.Data.CONTENT_URI - val projection = arrayOf( - ContactsContract.Data._ID, - ContactsContract.Data.DATA3, - ContactsContract.Data.MIMETYPE, - ContactsContract.Data.ACCOUNT_TYPE_AND_DATA_SET - ) - - val socialActions = ArrayList() - var curActionId = 0 - val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?" - val selectionArgs = arrayOf(id.toString()) - queryCursor(uri, projection, selection, selectionArgs, null, true) { cursor -> - val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE) - val type = when (mimetype) { - "vnd.android.cursor.item/vnd.org.thoughtcrime.securesms.contact" -> SOCIAL_MESSAGE - "vnd.android.cursor.item/vnd.org.thoughtcrime.securesms.call" -> SOCIAL_VOICE_CALL - else -> return@queryCursor - } - - val label = cursor.getStringValue(ContactsContract.Data.DATA3) - val realID = cursor.getLongValue(ContactsContract.Data._ID) - val packageName = cursor.getStringValue(ContactsContract.Data.ACCOUNT_TYPE_AND_DATA_SET) - val socialAction = SocialAction(curActionId++, type, label, mimetype, realID, packageName) - socialActions.add(socialAction) - } - return socialActions -} - -fun Context.getViberActions(id: Int): ArrayList { - val uri = ContactsContract.Data.CONTENT_URI - val projection = arrayOf( - ContactsContract.Data._ID, - ContactsContract.Data.DATA3, - ContactsContract.Data.MIMETYPE, - ContactsContract.Data.ACCOUNT_TYPE_AND_DATA_SET - ) - - val socialActions = ArrayList() - var curActionId = 0 - val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?" - val selectionArgs = arrayOf(id.toString()) - queryCursor(uri, projection, selection, selectionArgs, null, true) { cursor -> - val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE) - val type = when (mimetype) { + // Viber "vnd.android.cursor.item/vnd.com.viber.voip.viber_number_call" -> SOCIAL_VOICE_CALL "vnd.android.cursor.item/vnd.com.viber.voip.viber_out_call_viber" -> SOCIAL_VOICE_CALL "vnd.android.cursor.item/vnd.com.viber.voip.viber_number_message" -> SOCIAL_MESSAGE - else -> return@queryCursor - } - val label = cursor.getStringValue(ContactsContract.Data.DATA3) - val realID = cursor.getLongValue(ContactsContract.Data._ID) - val packageName = cursor.getStringValue(ContactsContract.Data.ACCOUNT_TYPE_AND_DATA_SET) - val socialAction = SocialAction(curActionId++, type, label, mimetype, realID, packageName) - socialActions.add(socialAction) - } - return socialActions -} + // Signal + "vnd.android.cursor.item/vnd.org.thoughtcrime.securesms.contact" -> SOCIAL_MESSAGE + "vnd.android.cursor.item/vnd.org.thoughtcrime.securesms.call" -> SOCIAL_VOICE_CALL -fun Context.getTelegramActions(id: Int): ArrayList { - val uri = ContactsContract.Data.CONTENT_URI - val projection = arrayOf( - ContactsContract.Data._ID, - ContactsContract.Data.DATA3, - ContactsContract.Data.MIMETYPE, - ContactsContract.Data.ACCOUNT_TYPE_AND_DATA_SET - ) - - val socialActions = ArrayList() - var curActionId = 0 - val selection = "${ContactsContract.Data.RAW_CONTACT_ID} = ?" - val selectionArgs = arrayOf(id.toString()) - queryCursor(uri, projection, selection, selectionArgs, null, true) { cursor -> - val mimetype = cursor.getStringValue(ContactsContract.Data.MIMETYPE) - val type = when (mimetype) { + // Telegram "vnd.android.cursor.item/vnd.org.telegram.messenger.android.call" -> SOCIAL_VOICE_CALL "vnd.android.cursor.item/vnd.org.telegram.messenger.android.call.video" -> SOCIAL_VIDEO_CALL "vnd.android.cursor.item/vnd.org.telegram.messenger.android.profile" -> SOCIAL_MESSAGE @@ -449,7 +372,6 @@ fun Context.getTelegramActions(id: Int): ArrayList { val socialAction = SocialAction(curActionId++, type, label, mimetype, realID, packageName) socialActions.add(socialAction) } - return socialActions }