updating the way permissions are handled

This commit is contained in:
tibbi 2018-08-04 23:42:30 +02:00
parent e2807bf85b
commit 56646660c9
4 changed files with 24 additions and 20 deletions

View File

@ -45,7 +45,7 @@ ext {
}
dependencies {
implementation 'com.simplemobiletools:commons:4.5.18'
implementation 'com.simplemobiletools:commons:4.5.19'
implementation 'joda-time:joda-time:2.9.9'
implementation 'com.facebook.stetho:stetho:1.5.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'

View File

@ -1,6 +1,5 @@
package com.simplemobiletools.contacts.activities
import android.Manifest
import android.app.SearchManager
import android.content.Context
import android.content.Intent
@ -8,7 +7,6 @@ import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.support.v4.app.ActivityCompat
import android.support.v4.view.MenuItemCompat
import android.support.v4.view.ViewPager
import android.support.v7.widget.SearchView
@ -41,10 +39,6 @@ import kotlinx.android.synthetic.main.fragment_recents.*
import java.io.FileOutputStream
class MainActivity : SimpleActivity(), RefreshContactsListener {
// just some random constants
private val GET_ACCOUNTS_PERMISSION = 34
private val WRITE_CALL_LOG_PERMISSION = 35
private var isSearchOpen = false
private var searchMenuItem: MenuItem? = null
private var werePermissionsHandled = false
@ -70,29 +64,36 @@ class MainActivity : SimpleActivity(), RefreshContactsListener {
// just get a reference to the database to make sure it is created properly
dbHelper
handlePermission(PERMISSION_READ_CALL_LOG) {
if (it) {
handlePermission(PERMISSION_WRITE_CALL_LOG) {
checkContactPermissions()
}
} else {
checkContactPermissions()
}
}
storeStateVariables()
checkWhatsNewDialog()
}
private fun checkContactPermissions() {
handlePermission(PERMISSION_READ_CONTACTS) {
werePermissionsHandled = true
if (it) {
handlePermission(PERMISSION_WRITE_CONTACTS) {
// workaround for upgrading from version 3.x to 4.x as we added a new permission from an already granted permissions group
if (!hasPermission(PERMISSION_GET_ACCOUNTS)) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.GET_ACCOUNTS), GET_ACCOUNTS_PERMISSION)
handlePermission(PERMISSION_GET_ACCOUNTS) {
storeLocalAccountData()
initFragments()
}
if (!hasPermission(PERMISSION_WRITE_CALL_LOG)) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_CALL_LOG), WRITE_CALL_LOG_PERMISSION)
}
storeLocalAccountData()
initFragments()
}
} else {
storeLocalAccountData()
initFragments()
}
}
storeStateVariables()
checkWhatsNewDialog()
}
override fun onResume() {

View File

@ -6,6 +6,7 @@ import android.util.AttributeSet
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.hasPermission
import com.simplemobiletools.commons.extensions.isActivityDestroyed
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALL_LOG
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CALL_LOG
import com.simplemobiletools.contacts.activities.EditContactActivity
import com.simplemobiletools.contacts.adapters.RecentCallsAdapter
@ -24,7 +25,9 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun placeholderClicked() {
activity!!.handlePermission(PERMISSION_WRITE_CALL_LOG) {
if (it) {
activity?.refreshContacts(RECENTS_TAB_MASK)
activity!!.handlePermission(PERMISSION_READ_CALL_LOG) {
activity?.refreshContacts(RECENTS_TAB_MASK)
}
}
}
}

View File

@ -1318,7 +1318,7 @@ class ContactsHelper(val activity: Activity) {
fun getRecents(callback: (ArrayList<RecentCall>) -> Unit) {
Thread {
val calls = ArrayList<RecentCall>()
if (!activity.hasPermission(PERMISSION_WRITE_CALL_LOG)) {
if (!activity.hasPermission(PERMISSION_WRITE_CALL_LOG) || !activity.hasPermission(PERMISSION_READ_CALL_LOG)) {
callback(calls)
return@Thread
}