@@ -10,7 +10,6 @@ import android.os.Bundle
1010import android.provider.Settings
1111import android.telecom.TelecomManager
1212import android.widget.Toast
13- import androidx.activity.enableEdgeToEdge
1413import androidx.activity.result.ActivityResultLauncher
1514import androidx.activity.result.contract.ActivityResultContracts
1615import androidx.annotation.RequiresApi
@@ -20,6 +19,12 @@ import androidx.core.content.ContextCompat
2019import androidx.core.view.ViewCompat
2120import androidx.core.view.WindowInsetsCompat
2221
22+ /* *
23+ * MainActivity for handling permissions and requesting call screening role.
24+ * Requires Android P (API level 28) or higher.
25+ *
26+ * Note: For Android Q (API level 29) or higher, MyCallScreeningService should be used instead.
27+ */
2328class MainActivity : AppCompatActivity () {
2429
2530 companion object {
@@ -28,6 +33,10 @@ class MainActivity : AppCompatActivity() {
2833
2934 private lateinit var intentLauncher: ActivityResultLauncher <Intent >
3035
36+ /* *
37+ * Called when the activity is starting.
38+ * @param savedInstanceState If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Otherwise it is null.
39+ */
3140 override fun onCreate (savedInstanceState : Bundle ? ) {
3241 super .onCreate(savedInstanceState)
3342 setContentView(R .layout.activity_main)
@@ -38,6 +47,9 @@ class MainActivity : AppCompatActivity() {
3847 requestCallScreeningRole()
3948 }
4049
50+ /* *
51+ * Sets up the window insets to ensure proper padding for system bars.
52+ */
4153 private fun setupWindowInsets () {
4254 ViewCompat .setOnApplyWindowInsetsListener(findViewById(R .id.main)) { v, insets ->
4355 val systemBars = insets.getInsets(WindowInsetsCompat .Type .systemBars())
@@ -46,6 +58,9 @@ class MainActivity : AppCompatActivity() {
4658 }
4759 }
4860
61+ /* *
62+ * Sets up the ActivityResultLauncher for handling the result of requesting the call screening role.
63+ */
4964 private fun setupIntentLauncher () {
5065 intentLauncher =
5166 registerForActivityResult(ActivityResultContracts .StartActivityForResult ()) {
@@ -57,18 +72,33 @@ class MainActivity : AppCompatActivity() {
5772 }
5873 }
5974
75+ /* *
76+ * Shows a toast message.
77+ * @param context Context for accessing resources.
78+ * @param message The message to display in the toast.
79+ * @param duration The length of time to show the toast. Default is Toast.LENGTH_SHORT.
80+ */
6081 private fun showToast (context : Context , message : String , duration : Int = Toast .LENGTH_SHORT ) {
6182 Toast .makeText(context, message, duration).show()
6283 }
6384
85+ /* *
86+ * Checks and requests the necessary permissions.
87+ */
6488 private fun checkPermissionsAndRequest () {
65- val permissions = arrayOf (
89+ val permissions = mutableListOf (
6690 android.Manifest .permission.READ_CALL_LOG ,
67- android.Manifest .permission.ANSWER_PHONE_CALLS ,
68- android.Manifest .permission.READ_PHONE_STATE ,
69- android.Manifest .permission.POST_NOTIFICATIONS
91+ android.Manifest .permission.READ_PHONE_STATE
7092 )
7193
94+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O ) {
95+ permissions.add(android.Manifest .permission.ANSWER_PHONE_CALLS )
96+ }
97+
98+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
99+ permissions.add(android.Manifest .permission.POST_NOTIFICATIONS )
100+ }
101+
72102 val missingPermissions = permissions.filter {
73103 ContextCompat .checkSelfPermission(this , it) != PackageManager .PERMISSION_GRANTED
74104 }
@@ -82,6 +112,9 @@ class MainActivity : AppCompatActivity() {
82112 }
83113 }
84114
115+ /* *
116+ * Requests the call screening role.
117+ */
85118 private fun requestCallScreeningRole () {
86119 val telecomManager = ContextCompat .getSystemService(this , TelecomManager ::class .java)
87120
@@ -97,6 +130,9 @@ class MainActivity : AppCompatActivity() {
97130 }
98131 }
99132
133+ /* *
134+ * Requests the call screening role for Android Q (API level 29) and above.
135+ */
100136 @RequiresApi(Build .VERSION_CODES .Q )
101137 private fun requestRoleForQAndAbove () {
102138 val roleManager = getSystemService(Context .ROLE_SERVICE ) as RoleManager
@@ -109,6 +145,10 @@ class MainActivity : AppCompatActivity() {
109145 }
110146 }
111147
148+ /* *
149+ * Requests the call screening role for Android P (API level 28) and below.
150+ * @param telecomManager The TelecomManager for managing telecom services.
151+ */
112152 private fun requestRoleForBelowQ (telecomManager : TelecomManager ) {
113153 if (telecomManager.defaultDialerPackage == packageName) {
114154 showToast(this , getString(R .string.call_screening_role_already_granted))
0 commit comments