diff options
author | Torsten Grote <t@grobox.de> | 2020-06-26 14:15:34 -0300 |
---|---|---|
committer | Torsten Grote <t@grobox.de> | 2020-06-26 14:15:34 -0300 |
commit | 97dac704bcc5f4dc441f0d31c4a680235ad50e2e (patch) | |
tree | cf49442c470d8650a8bb23aa1e02f9e64d68fa87 /anastasis-ui | |
parent | 9cbb35ccc65db7b5d8ce1afe4b3c5feae30be1eb (diff) | |
download | taler-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')
3 files changed, 54 insertions, 0 deletions
diff --git a/anastasis-ui/src/main/AndroidManifest.xml b/anastasis-ui/src/main/AndroidManifest.xml index 3f2402b..9340508 100644 --- a/anastasis-ui/src/main/AndroidManifest.xml +++ b/anastasis-ui/src/main/AndroidManifest.xml @@ -17,6 +17,8 @@ <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.gnu.anastasis.ui"> + <uses-permission android:name="android.permission.READ_PHONE_STATE" /> + <application> <activity android:name=".MainActivity" 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) + } + } + } |