Skip to content

Commit

Permalink
Merge pull request #449 from KasperskyLab/ISSUE-372-update-tutorial
Browse files Browse the repository at this point in the history
ISSUE-372: add MakeCallActivity.kt
  • Loading branch information
AzamatCherchesov authored Dec 6, 2022
2 parents acfd33a + cb30784 commit 48c6811
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 29 deletions.
4 changes: 4 additions & 0 deletions tutorial/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_CALL_LOG"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" tools:ignore="ProtectedPermissions"/>

Expand Down Expand Up @@ -52,6 +53,9 @@
<activity
android:name=".afterlogin.AfterLoginActivity"
android:exported="true" />

<activity android:name=".permission.MakeCallActivity"
android:exported="true" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
}
}
}
Original file line number Diff line number Diff line change
@@ -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<out String>, 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
}
}
49 changes: 21 additions & 28 deletions tutorial/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,58 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Tutorial"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
android:textAlignment="center"
android:textSize="30sp" />

<Button
android:id="@+id/simple_activity_btn"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Simple test"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title" />
android:text="Simple test" />

<Button
android:id="@+id/wifi_activity_btn"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Internet availability"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/simple_activity_btn" />
android:text="Internet availability" />

<Button
android:id="@+id/notification_activity_btn"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="notification activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/wifi_activity_btn" />
android:text="notification activity" />

<Button
android:id="@+id/login_activity_btn"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Login activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/notification_activity_btn" />
</androidx.constraintlayout.widget.ConstraintLayout>
android:text="Login activity" />

<Button
android:id="@+id/make_call_activity_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Make call activity" />
</LinearLayout>
21 changes: 21 additions & 0 deletions tutorial/src/main/res/layout/activity_make_call.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/input_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/phone_number_hint"
android:textSize="16sp" />

<Button
android:id="@+id/make_call_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/make_call_btn" />
</LinearLayout>
4 changes: 4 additions & 0 deletions tutorial/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@

<!-- After Login screens -->
<string name="screen_after_login">Screen after authorization</string>

<!-- Make call screen-->
<string name="phone_number_hint">Phone number:</string>
<string name="make_call_btn">Make Call</string>
</resources>

0 comments on commit 48c6811

Please sign in to comment.