more improvements related to using the app without the Contacts permission

This commit is contained in:
tibbi 2018-04-06 20:58:22 +02:00
parent e05f48b425
commit bd83bd5643
4 changed files with 44 additions and 65 deletions

View File

@ -16,8 +16,6 @@ import android.widget.ImageView
import android.widget.TextView
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
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.contacts.R
import com.simplemobiletools.contacts.dialogs.SelectGroupsDialog
@ -58,22 +56,7 @@ class EditContactActivity : ContactActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_edit_contact)
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_cross)
handlePermission(PERMISSION_READ_CONTACTS) {
if (it) {
handlePermission(PERMISSION_WRITE_CONTACTS) {
if (it) {
initContact()
} else {
toast(R.string.no_contacts_permission)
finish()
}
}
} else {
toast(R.string.no_contacts_permission)
finish()
}
}
initContact()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -378,7 +361,8 @@ class EditContactActivity : ContactActivity() {
private fun setupNewContact() {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
supportActionBar?.title = resources.getString(R.string.new_contact)
contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), config.lastUsedContactSource, 0, 0, "", null, "", ArrayList())
val contactSource = if (hasContactPermissions()) config.lastUsedContactSource else SMT_PRIVATE
contact = Contact(0, "", "", "", "", ArrayList(), ArrayList(), ArrayList(), ArrayList(), contactSource, 0, 0, "", null, "", ArrayList())
contact_source.text = getPublicContactSource(contact!!.source)
contact_source.setOnClickListener {
showContactSourcePicker(contact!!.source) {

View File

@ -36,9 +36,9 @@ import kotlinx.android.synthetic.main.fragment_groups.*
import java.io.FileOutputStream
class MainActivity : SimpleActivity(), RefreshContactsListener {
private var isFirstResume = true
private var isSearchOpen = false
private var searchMenuItem: MenuItem? = null
private var werePermissionsHandled = false
private var storedTextColor = 0
private var storedBackgroundColor = 0
@ -57,6 +57,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
dbHelper
handlePermission(PERMISSION_READ_CONTACTS) {
werePermissionsHandled = true
if (it) {
handlePermission(PERMISSION_WRITE_CONTACTS) {
storeLocalAccountData()
@ -115,7 +116,7 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
favorites_fragment?.startNameWithSurnameChanged(configStartNameWithSurname)
}
if (!isFirstResume) {
if (werePermissionsHandled) {
if (viewpager.adapter == null) {
initFragments()
}
@ -125,10 +126,6 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
}
refreshContacts(ALL_TABS_MASK)
}
if (hasPermission(PERMISSION_WRITE_CONTACTS)) {
isFirstResume = false
}
}
override fun onPause() {
@ -388,7 +385,10 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
}
private fun launchAbout() {
val faqItems = arrayListOf(FAQItem(R.string.faq_2_title_commons, R.string.faq_2_text_commons))
val faqItems = arrayListOf(
FAQItem(R.string.faq_2_title_commons, R.string.faq_2_text_commons)
)
startAboutActivity(R.string.app_name, LICENSE_MULTISELECT or LICENSE_JODA or LICENSE_GLIDE or LICENSE_GSON or LICENSE_STETHO,
BuildConfig.VERSION_NAME, faqItems)
}

View File

@ -9,7 +9,6 @@ import android.view.MenuItem
import android.view.WindowManager
import android.widget.RelativeLayout
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.contacts.R
import com.simplemobiletools.contacts.extensions.*
import com.simplemobiletools.contacts.helpers.CONTACT_ID
@ -30,7 +29,7 @@ class ViewContactActivity : ContactActivity() {
override fun onResume() {
super.onResume()
tryInitContact()
initContact()
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
@ -48,17 +47,6 @@ class ViewContactActivity : ContactActivity() {
return true
}
private fun tryInitContact() {
handlePermission(PERMISSION_READ_CONTACTS) {
if (it) {
initContact()
} else {
toast(R.string.no_contacts_permission)
finish()
}
}
}
private fun initContact() {
var wasLookupKeyUsed = false
var contactId = intent.getIntExtra(CONTACT_ID, 0)

View File

@ -25,8 +25,8 @@ import com.simplemobiletools.contacts.models.*
class ContactsHelper(val activity: BaseSimpleActivity) {
fun getContacts(callback: (ArrayList<Contact>) -> Unit) {
val contacts = SparseArray<Contact>()
Thread {
val contacts = SparseArray<Contact>()
getDeviceContacts(contacts)
activity.dbHelper.getContacts(activity).forEach {
@ -519,37 +519,44 @@ class ContactsHelper(val activity: BaseSimpleActivity) {
}
fun getContactSources(callback: (ArrayList<ContactSource>) -> Unit) {
val sources = LinkedHashSet<ContactSource>()
Thread {
val uri = ContactsContract.RawContacts.CONTENT_URI
val projection = arrayOf(ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE)
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
val name = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: continue
val type = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_TYPE) ?: continue
val contactSource = ContactSource(name, type)
sources.add(contactSource)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
activity.showErrorToast(e)
} finally {
cursor?.close()
}
if (sources.isEmpty() && activity.config.localAccountName.isEmpty() && activity.config.localAccountType.isEmpty()) {
sources.add(ContactSource("", ""))
}
val sources = LinkedHashSet<ContactSource>()
getDeviceContactSources(sources)
sources.add(ContactSource(activity.getString(R.string.phone_storage_hidden), SMT_PRIVATE))
callback(ArrayList(sources))
}.start()
}
private fun getDeviceContactSources(sources: LinkedHashSet<ContactSource>) {
if (!activity.hasContactPermissions()) {
return
}
val uri = ContactsContract.RawContacts.CONTENT_URI
val projection = arrayOf(ContactsContract.RawContacts.ACCOUNT_NAME, ContactsContract.RawContacts.ACCOUNT_TYPE)
var cursor: Cursor? = null
try {
cursor = activity.contentResolver.query(uri, projection, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
val name = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_NAME) ?: continue
val type = cursor.getStringValue(ContactsContract.RawContacts.ACCOUNT_TYPE) ?: continue
val contactSource = ContactSource(name, type)
sources.add(contactSource)
} while (cursor.moveToNext())
}
} catch (e: Exception) {
activity.showErrorToast(e)
} finally {
cursor?.close()
}
if (sources.isEmpty() && activity.config.localAccountName.isEmpty() && activity.config.localAccountType.isEmpty()) {
sources.add(ContactSource("", ""))
}
}
private fun getContactSourceType(accountName: String): String {
if (accountName.isEmpty()) {
return ""