From 80afe37050c94f46abc58c1ccd951b706681b916 Mon Sep 17 00:00:00 2001 From: septs Date: Mon, 28 Jul 2025 21:44:18 +0800 Subject: [PATCH 1/4] feat: improve notification permission request --- .../im/angry/openeuicc/ui/MainActivity.kt | 26 ++++++++++++------- app-common/src/main/res/values/strings.xml | 3 +++ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt b/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt index b42f4cf..a9c754a 100644 --- a/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt +++ b/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt @@ -1,6 +1,7 @@ package im.angry.openeuicc.ui import android.annotation.SuppressLint +import android.app.AlertDialog import android.content.BroadcastReceiver import android.content.Context import android.content.Intent @@ -16,6 +17,7 @@ import android.view.MenuItem import android.view.View import android.widget.ProgressBar import androidx.activity.enableEdgeToEdge +import androidx.annotation.RequiresApi import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import androidx.viewpager2.adapter.FragmentStateAdapter @@ -125,16 +127,22 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker { } } + @RequiresApi(Build.VERSION_CODES.TIRAMISU) private fun ensureNotificationPermissions() { - val needsNotificationPerms = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU; - val notificationPermsGranted = - needsNotificationPerms && checkSelfPermission(android.Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED - if (needsNotificationPerms && !notificationPermsGranted) { - requestPermissions( - arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), - PERMISSION_REQUEST_CODE - ) + val permission = android.Manifest.permission.POST_NOTIFICATIONS + if (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) return + if (!shouldShowRequestPermissionRationale(permission)) { + requestPermissions(arrayOf(permission), PERMISSION_REQUEST_CODE) + return } + AlertDialog.Builder(this) + .setTitle(R.string.notification_permission_request_title) + .setMessage(R.string.notification_permission_request_desc) + .setPositiveButton(android.R.string.ok) { _, _ -> + requestPermissions(arrayOf(permission), PERMISSION_REQUEST_CODE) + } + .create() + .show() } private suspend fun init(fromUsbEvent: Boolean = false) { @@ -209,7 +217,7 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker { viewPager.currentItem = 0 } - if (pages.size > 0) { + if (pages.isNotEmpty() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { ensureNotificationPermissions() } diff --git a/app-common/src/main/res/values/strings.xml b/app-common/src/main/res/values/strings.xml index e09da9f..d215f23 100644 --- a/app-common/src/main/res/values/strings.xml +++ b/app-common/src/main/res/values/strings.xml @@ -5,6 +5,9 @@ Help + Notification Permission Needed + During the download process, notification permissions are required so that the app does not need to stay in the foreground + Reload Slots Unknown -- 2.45.3 From 47e396ee71f410cb39af8c54e4cb17b63ab3a42f Mon Sep 17 00:00:00 2001 From: septs Date: Tue, 29 Jul 2025 12:55:05 +0800 Subject: [PATCH 2/4] feat: add request permissions result handling --- .../java/im/angry/openeuicc/ui/MainActivity.kt | 17 +++++++++++++++++ app-common/src/main/res/values/strings.xml | 2 ++ 2 files changed, 19 insertions(+) diff --git a/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt b/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt index a9c754a..95e1baf 100644 --- a/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt +++ b/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt @@ -16,6 +16,7 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.widget.ProgressBar +import android.widget.Toast import androidx.activity.enableEdgeToEdge import androidx.annotation.RequiresApi import androidx.fragment.app.Fragment @@ -239,4 +240,20 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker { init(fromUsbEvent) // will set refreshing = false } } + + override fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, + grantResults: IntArray + ) = when (requestCode) { + PERMISSION_REQUEST_CODE -> { + val resId = + if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) + R.string.notification_permission_request_granted else + R.string.notification_permission_request_denied + Toast.makeText(this, resId, Toast.LENGTH_SHORT).show() + } + + else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults) + } } \ No newline at end of file diff --git a/app-common/src/main/res/values/strings.xml b/app-common/src/main/res/values/strings.xml index d215f23..a1ef9af 100644 --- a/app-common/src/main/res/values/strings.xml +++ b/app-common/src/main/res/values/strings.xml @@ -7,6 +7,8 @@ Notification Permission Needed During the download process, notification permissions are required so that the app does not need to stay in the foreground + Notification permission granted + Notification permission denied Reload Slots Unknown -- 2.45.3 From 948e1256ea876801f749633bfc0448ee191f5b3b Mon Sep 17 00:00:00 2001 From: septs Date: Tue, 29 Jul 2025 14:35:43 +0800 Subject: [PATCH 3/4] chore: rename strings --- .../src/main/java/im/angry/openeuicc/ui/MainActivity.kt | 5 +++-- app-common/src/main/res/values/strings.xml | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt b/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt index 95e1baf..6909587 100644 --- a/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt +++ b/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt @@ -137,8 +137,9 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker { return } AlertDialog.Builder(this) - .setTitle(R.string.notification_permission_request_title) - .setMessage(R.string.notification_permission_request_desc) + .setTitle(R.string.notification_permission_request_rationale_title) + .setMessage(R.string.notification_permission_request_rationale_message) + .setNegativeButton(android.R.string.cancel, null) .setPositiveButton(android.R.string.ok) { _, _ -> requestPermissions(arrayOf(permission), PERMISSION_REQUEST_CODE) } diff --git a/app-common/src/main/res/values/strings.xml b/app-common/src/main/res/values/strings.xml index a1ef9af..3572e8d 100644 --- a/app-common/src/main/res/values/strings.xml +++ b/app-common/src/main/res/values/strings.xml @@ -5,8 +5,8 @@ Help - Notification Permission Needed - During the download process, notification permissions are required so that the app does not need to stay in the foreground + Notification Permission Required + During the download process, notification permissions are required so that the app does not need to stay in the foreground Notification permission granted Notification permission denied -- 2.45.3 From 66a719b6960b39d02633743e2f2a99f78d4fb1cf Mon Sep 17 00:00:00 2001 From: septs Date: Tue, 29 Jul 2025 15:53:39 +0800 Subject: [PATCH 4/4] feat: simplify --- .../java/im/angry/openeuicc/ui/MainActivity.kt | 16 ---------------- app-common/src/main/res/values/strings.xml | 2 -- 2 files changed, 18 deletions(-) diff --git a/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt b/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt index 6909587..f072300 100644 --- a/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt +++ b/app-common/src/main/java/im/angry/openeuicc/ui/MainActivity.kt @@ -241,20 +241,4 @@ open class MainActivity : BaseEuiccAccessActivity(), OpenEuiccContextMarker { init(fromUsbEvent) // will set refreshing = false } } - - override fun onRequestPermissionsResult( - requestCode: Int, - permissions: Array, - grantResults: IntArray - ) = when (requestCode) { - PERMISSION_REQUEST_CODE -> { - val resId = - if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) - R.string.notification_permission_request_granted else - R.string.notification_permission_request_denied - Toast.makeText(this, resId, Toast.LENGTH_SHORT).show() - } - - else -> super.onRequestPermissionsResult(requestCode, permissions, grantResults) - } } \ No newline at end of file diff --git a/app-common/src/main/res/values/strings.xml b/app-common/src/main/res/values/strings.xml index 3572e8d..13e22b1 100644 --- a/app-common/src/main/res/values/strings.xml +++ b/app-common/src/main/res/values/strings.xml @@ -7,8 +7,6 @@ Notification Permission Required During the download process, notification permissions are required so that the app does not need to stay in the foreground - Notification permission granted - Notification permission denied Reload Slots Unknown -- 2.45.3