aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarvin Borner2018-09-09 12:34:59 +0200
committerMarvin Borner2018-09-09 12:34:59 +0200
commit3749208a5332e3618753d770da9296602c82239f (patch)
treef64eca06dfb08150bf201d8d4d44e503792f4e8b
parent7c41a4da70c2aa0ec6ade1c7b3ec0fa74edffbdf (diff)
Fixed login activity permissions :bug:
-rw-r--r--app/src/main/java/me/texx/Texx/LoginActivity.kt45
1 files changed, 43 insertions, 2 deletions
diff --git a/app/src/main/java/me/texx/Texx/LoginActivity.kt b/app/src/main/java/me/texx/Texx/LoginActivity.kt
index ee3889f..2950454 100644
--- a/app/src/main/java/me/texx/Texx/LoginActivity.kt
+++ b/app/src/main/java/me/texx/Texx/LoginActivity.kt
@@ -1,17 +1,20 @@
package me.texx.Texx
+import android.Manifest.permission.READ_CONTACTS
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.annotation.TargetApi
import android.app.LoaderManager.LoaderCallbacks
import android.content.CursorLoader
import android.content.Loader
+import android.content.pm.PackageManager
import android.database.Cursor
import android.net.Uri
import android.os.AsyncTask
import android.os.Build
import android.os.Bundle
import android.provider.ContactsContract
+import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.view.View
@@ -24,7 +27,6 @@ import com.madapps.prefrences.EasyPrefrences
import daio.io.dresscode.dressCodeName
import daio.io.dresscode.matchDressCode
import kotlinx.android.synthetic.main.activity_login.*
-import me.texx.Texx.Util.PermissionUtil.askForPermission
import me.texx.Texx.Util.SecureStorage
import me.texx.Texx.util.ThemeUtil.getThemeName
import org.jetbrains.anko.longToast
@@ -63,10 +65,42 @@ class LoginActivity : AppCompatActivity(), LoaderCallbacks<Cursor> {
}
private fun populateAutoComplete() {
- askForPermission(android.Manifest.permission.READ_CONTACTS, this@LoginActivity)
+ if (!mayRequestContacts()) {
+ return
+ }
+
loaderManager.initLoader(0, null, this)
}
+ private fun mayRequestContacts(): Boolean {
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
+ return true
+ }
+ if (checkSelfPermission(READ_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
+ return true
+ }
+ if (shouldShowRequestPermissionRationale(READ_CONTACTS)) {
+ Snackbar.make(email, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
+ .setAction(android.R.string.ok,
+ { requestPermissions(arrayOf(READ_CONTACTS), REQUEST_READ_CONTACTS) })
+ } else {
+ requestPermissions(arrayOf(READ_CONTACTS), REQUEST_READ_CONTACTS)
+ }
+ return false
+ }
+
+ /**
+ * Callback received when a permissions request has been completed.
+ */
+ override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
+ grantResults: IntArray) {
+ if (requestCode == REQUEST_READ_CONTACTS) {
+ if (grantResults.size == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
+ populateAutoComplete()
+ }
+ }
+ }
+
/**
* Attempts to sign in or register the account specified by the login form.
* If there are form errors (invalid email, missing fields, etc.), the
@@ -283,4 +317,11 @@ class LoginActivity : AppCompatActivity(), LoaderCallbacks<Cursor> {
showProgress(false)
}
}
+
+ companion object {
+ /**
+ * Id to identity READ_CONTACTS permission request.
+ */
+ private val REQUEST_READ_CONTACTS = 0
+ }
}