require Contacts permission at third party intents

This commit is contained in:
tibbi 2018-04-06 21:28:09 +02:00
parent bd83bd5643
commit 6695bf4f77
2 changed files with 42 additions and 5 deletions

View File

@ -16,6 +16,8 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.dialogs.SelectGroupsDialog import com.simplemobiletools.contacts.dialogs.SelectGroupsDialog
@ -51,12 +53,34 @@ class EditContactActivity : ContactActivity() {
private var wasActivityInitialized = false private var wasActivityInitialized = false
private var lastPhotoIntentUri: Uri? = null private var lastPhotoIntentUri: Uri? = null
private var isSaving = false private var isSaving = false
private var isThirdPartyIntent = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_edit_contact) setContentView(R.layout.activity_edit_contact)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_cross) supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_cross)
val action = intent.action
isThirdPartyIntent = action == Intent.ACTION_EDIT || action == Intent.ACTION_INSERT_OR_EDIT || action == Intent.ACTION_INSERT
if (isThirdPartyIntent) {
handlePermission(PERMISSION_READ_CONTACTS) {
if (it) {
handlePermission(PERMISSION_WRITE_CONTACTS) {
if (it) {
initContact() initContact()
} else {
toast(R.string.no_contacts_permission)
finish()
}
}
} else {
toast(R.string.no_contacts_permission)
finish()
}
}
} else {
initContact()
}
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -121,7 +145,7 @@ class EditContactActivity : ContactActivity() {
setupEditContact() setupEditContact()
} }
if (contact!!.id == 0 && intent.extras?.containsKey(KEY_PHONE) == true && (intent.action == Intent.ACTION_INSERT_OR_EDIT || intent.action == Intent.ACTION_INSERT)) { if (contact!!.id == 0 && intent.extras?.containsKey(KEY_PHONE) == true && (action == Intent.ACTION_INSERT_OR_EDIT || action == Intent.ACTION_INSERT)) {
val phoneNumber = intent.extras.get(KEY_PHONE).toString() val phoneNumber = intent.extras.get(KEY_PHONE).toString()
contact!!.phoneNumbers.add(PhoneNumber(phoneNumber, DEFAULT_PHONE_NUMBER_TYPE)) contact!!.phoneNumbers.add(PhoneNumber(phoneNumber, DEFAULT_PHONE_NUMBER_TYPE))
setupPhoneNumbers() setupPhoneNumbers()

View File

@ -9,6 +9,7 @@ import android.view.MenuItem
import android.view.WindowManager import android.view.WindowManager
import android.widget.RelativeLayout import android.widget.RelativeLayout
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.extensions.* import com.simplemobiletools.contacts.extensions.*
import com.simplemobiletools.contacts.helpers.CONTACT_ID import com.simplemobiletools.contacts.helpers.CONTACT_ID
@ -22,6 +23,7 @@ import kotlinx.android.synthetic.main.item_view_group.view.*
import kotlinx.android.synthetic.main.item_view_phone_number.view.* import kotlinx.android.synthetic.main.item_view_phone_number.view.*
class ViewContactActivity : ContactActivity() { class ViewContactActivity : ContactActivity() {
private var isViewIntent = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_view_contact) setContentView(R.layout.activity_view_contact)
@ -29,7 +31,19 @@ class ViewContactActivity : ContactActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
isViewIntent = intent.action == ContactsContract.QuickContact.ACTION_QUICK_CONTACT || intent.action == Intent.ACTION_VIEW
if (isViewIntent) {
handlePermission(PERMISSION_READ_CONTACTS) {
if (it) {
initContact() initContact()
} else {
toast(R.string.no_contacts_permission)
finish()
}
}
} else {
initContact()
}
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -50,8 +64,7 @@ class ViewContactActivity : ContactActivity() {
private fun initContact() { private fun initContact() {
var wasLookupKeyUsed = false var wasLookupKeyUsed = false
var contactId = intent.getIntExtra(CONTACT_ID, 0) var contactId = intent.getIntExtra(CONTACT_ID, 0)
val action = intent.action if (contactId == 0 && isViewIntent) {
if (contactId == 0 && (action == ContactsContract.QuickContact.ACTION_QUICK_CONTACT || action == Intent.ACTION_VIEW)) {
val data = intent.data val data = intent.data
if (data != null) { if (data != null) {
val rawId = if (data.path.contains("lookup")) { val rawId = if (data.path.contains("lookup")) {