summaryrefslogtreecommitdiff
path: root/anastasis-ui/src/main/java/org/gnu
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2020-06-26 14:15:34 -0300
committerTorsten Grote <t@grobox.de>2020-06-26 14:15:34 -0300
commit97dac704bcc5f4dc441f0d31c4a680235ad50e2e (patch)
treecf49442c470d8650a8bb23aa1e02f9e64d68fa87 /anastasis-ui/src/main/java/org/gnu
parent9cbb35ccc65db7b5d8ce1afe4b3c5feae30be1eb (diff)
downloadtaler-android-97dac704bcc5f4dc441f0d31c4a680235ad50e2e.tar.gz
taler-android-97dac704bcc5f4dc441f0d31c4a680235ad50e2e.tar.bz2
taler-android-97dac704bcc5f4dc441f0d31c4a680235ad50e2e.zip
[anastasis] ask for permission to fill in phone number automatically
Diffstat (limited to 'anastasis-ui/src/main/java/org/gnu')
-rw-r--r--anastasis-ui/src/main/java/org/gnu/anastasis/ui/MainActivity.kt2
-rw-r--r--anastasis-ui/src/main/java/org/gnu/anastasis/ui/authentication/SmsFragment.kt50
2 files changed, 52 insertions, 0 deletions
diff --git a/anastasis-ui/src/main/java/org/gnu/anastasis/ui/MainActivity.kt b/anastasis-ui/src/main/java/org/gnu/anastasis/ui/MainActivity.kt
index 3ca3224..ac355c1 100644
--- a/anastasis-ui/src/main/java/org/gnu/anastasis/ui/MainActivity.kt
+++ b/anastasis-ui/src/main/java/org/gnu/anastasis/ui/MainActivity.kt
@@ -24,6 +24,8 @@ import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupWithNavController
import kotlinx.android.synthetic.main.activity.*
+internal const val PERMISSION_REQUEST_CODE = 1
+
class MainActivity : AppCompatActivity() {
private lateinit var nav: NavController
diff --git a/anastasis-ui/src/main/java/org/gnu/anastasis/ui/authentication/SmsFragment.kt b/anastasis-ui/src/main/java/org/gnu/anastasis/ui/authentication/SmsFragment.kt
index 2ceb422..f20fba5 100644
--- a/anastasis-ui/src/main/java/org/gnu/anastasis/ui/authentication/SmsFragment.kt
+++ b/anastasis-ui/src/main/java/org/gnu/anastasis/ui/authentication/SmsFragment.kt
@@ -16,10 +16,16 @@
package org.gnu.anastasis.ui.authentication
+import android.Manifest
+import android.annotation.SuppressLint
+import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Bundle
+import android.telephony.TelephonyManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
+import androidx.annotation.RequiresPermission
+import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.findNavController
@@ -27,8 +33,11 @@ import com.google.android.material.transition.MaterialContainerTransform
import com.google.android.material.transition.MaterialContainerTransform.FADE_MODE_CROSS
import kotlinx.android.synthetic.main.fragment_sms.*
import org.gnu.anastasis.ui.MainViewModel
+import org.gnu.anastasis.ui.PERMISSION_REQUEST_CODE
import org.gnu.anastasis.ui.R
+private const val PERMISSION = Manifest.permission.READ_PHONE_STATE
+
class SmsFragment : Fragment() {
private val viewModel: MainViewModel by activityViewModels()
@@ -46,10 +55,51 @@ class SmsFragment : Fragment() {
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+ smsView.editText?.setOnFocusChangeListener { _, hasFocus ->
+ if (hasFocus) checkPerm()
+ }
saveSmsButton.setOnClickListener {
viewModel.smsChecked.value = true
findNavController().popBackStack()
}
}
+ private fun checkPerm() = when {
+ ContextCompat.checkSelfPermission(requireContext(), PERMISSION)
+ == PERMISSION_GRANTED -> {
+ // You can use the API that requires the permission.
+ fillPhoneNumber()
+ }
+ shouldShowRequestPermissionRationale(PERMISSION) -> {
+ // In an educational UI, explain to the user why your app requires this
+ // permission for a specific feature to behave as expected. In this UI,
+ // include a "cancel" or "no thanks" button that allows the user to
+ // continue using your app without granting the permission.
+ }
+ else -> {
+ // You can directly ask for the permission.
+ requestPermissions(arrayOf(PERMISSION), PERMISSION_REQUEST_CODE)
+ }
+ }
+
+ override fun onRequestPermissionsResult(
+ requestCode: Int,
+ permissions: Array<String>,
+ grantResults: IntArray
+ ) {
+ if (requestCode == PERMISSION_REQUEST_CODE && grantResults.isNotEmpty() &&
+ grantResults[0] == PERMISSION_GRANTED
+ ) checkPerm()
+ }
+
+ @SuppressLint("HardwareIds")
+ @RequiresPermission(PERMISSION)
+ private fun fillPhoneNumber() {
+ val telephonyService = requireContext().getSystemService(TelephonyManager::class.java)
+ telephonyService?.line1Number?.let { phoneNumber ->
+ smsView?.editText?.setText(phoneNumber)
+ smsView?.editText?.setSelection(phoneNumber.length)
+ }
+ }
+
}