From cb30784b91704da724d5ae4dbf4e3638838f53dd Mon Sep 17 00:00:00 2001 From: sumin93 Date: Tue, 6 Dec 2022 11:25:46 +0300 Subject: [PATCH] ISSUE-372: add MakeCallActivity.kt --- tutorial/src/main/AndroidManifest.xml | 4 ++ .../kaspresso/tutorial/MainActivity.kt | 6 +- .../tutorial/permission/MakeCallActivity.kt | 65 +++++++++++++++++++ .../src/main/res/layout/activity_main.xml | 49 ++++++-------- .../main/res/layout/activity_make_call.xml | 21 ++++++ tutorial/src/main/res/values/strings.xml | 4 ++ 6 files changed, 120 insertions(+), 29 deletions(-) create mode 100644 tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/permission/MakeCallActivity.kt create mode 100644 tutorial/src/main/res/layout/activity_make_call.xml diff --git a/tutorial/src/main/AndroidManifest.xml b/tutorial/src/main/AndroidManifest.xml index 778d9b628..72b9b7cd3 100644 --- a/tutorial/src/main/AndroidManifest.xml +++ b/tutorial/src/main/AndroidManifest.xml @@ -13,6 +13,7 @@ + @@ -52,6 +53,9 @@ + + diff --git a/tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/MainActivity.kt b/tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/MainActivity.kt index 4ed64d4d1..f8fefa740 100644 --- a/tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/MainActivity.kt +++ b/tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/MainActivity.kt @@ -4,8 +4,9 @@ import android.content.Intent import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.kaspersky.kaspresso.tutorial.databinding.ActivityMainBinding -import com.kaspersky.kaspresso.tutorial.notification.NotificationActivity import com.kaspersky.kaspresso.tutorial.login.LoginActivity +import com.kaspersky.kaspresso.tutorial.notification.NotificationActivity +import com.kaspersky.kaspresso.tutorial.permission.MakeCallActivity import com.kaspersky.kaspresso.tutorial.simple.SimpleActivity import com.kaspersky.kaspresso.tutorial.wifi.WiFiActivity @@ -32,5 +33,8 @@ class MainActivity : AppCompatActivity() { binding.loginActivityBtn.setOnClickListener { startActivity(Intent(this, LoginActivity::class.java)) } + binding.makeCallActivityBtn.setOnClickListener { + startActivity(Intent(this, MakeCallActivity::class.java)) + } } } diff --git a/tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/permission/MakeCallActivity.kt b/tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/permission/MakeCallActivity.kt new file mode 100644 index 000000000..7cf3d0b4e --- /dev/null +++ b/tutorial/src/main/kotlin/com/kaspersky/kaspresso/tutorial/permission/MakeCallActivity.kt @@ -0,0 +1,65 @@ +package com.kaspersky.kaspresso.tutorial.permission + +import android.content.Intent +import android.content.pm.PackageManager +import android.net.Uri +import android.os.Build +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import com.kaspersky.kaspresso.tutorial.databinding.ActivityMakeCallBinding + +class MakeCallActivity : AppCompatActivity() { + + private lateinit var binding: ActivityMakeCallBinding + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = ActivityMakeCallBinding.inflate(layoutInflater) + setContentView(binding.root) + + binding.makeCallBtn.setOnClickListener { + if (!isPermissionGranted()) { + requestPermission() + } else { + phoneCall() + } + } + } + + private fun requestPermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + requestPermissions( + arrayOf(CALL_PHONE_PERMISSION), + CALL_PHONE_PERMISSION_RC + ) + } + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + if ( + requestCode == CALL_PHONE_PERMISSION_RC && + permissions[0] == android.Manifest.permission.CALL_PHONE && + grantResults[0] == PackageManager.PERMISSION_GRANTED + ) { + phoneCall() + } + } + + private fun phoneCall() { + val number = binding.inputNumber.text.toString().trim() + val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:$number")) + startActivity(intent) + } + + private fun isPermissionGranted(): Boolean = packageManager.checkPermission( + android.Manifest.permission.CALL_PHONE, + packageName + ) == PackageManager.PERMISSION_GRANTED + + companion object { + + private const val CALL_PHONE_PERMISSION = android.Manifest.permission.CALL_PHONE + private const val CALL_PHONE_PERMISSION_RC = 100 + } +} diff --git a/tutorial/src/main/res/layout/activity_main.xml b/tutorial/src/main/res/layout/activity_main.xml index 4f3560fec..0f38a1f9b 100644 --- a/tutorial/src/main/res/layout/activity_main.xml +++ b/tutorial/src/main/res/layout/activity_main.xml @@ -1,58 +1,51 @@ - + android:textAlignment="center" + android:textSize="30sp" />