From dc9cd511a8c0b043280fe214fbf52cd5fcbe9f5c Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 29 Aug 2022 10:48:19 +0200 Subject: [PATCH] add handling for the new Post Notifications permission --- app/build.gradle | 2 +- app/src/main/AndroidManifest.xml | 1 + .../activities/BackgroundRecordActivity.kt | 23 +++++++++++++------ .../fragments/RecorderFragment.kt | 20 ++++++++++++++-- .../helpers/MyWidgetRecordDisplayProvider.kt | 3 ++- .../voicerecorder/services/RecorderService.kt | 4 +++- .../layout/widget_record_display_config.xml | 2 ++ 7 files changed, 43 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c3e2a68..eaf96d0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -63,7 +63,7 @@ android { } dependencies { - implementation 'com.github.SimpleMobileTools:Simple-Commons:b2a2c40a27' + implementation 'com.github.SimpleMobileTools:Simple-Commons:7cfc56e855' implementation 'org.greenrobot:eventbus:3.3.1' implementation 'com.github.Armen101:AudioRecordView:1.0.4' implementation 'androidx.documentfile:documentfile:1.0.1' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6162ec4..0dbd3d5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ + diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/BackgroundRecordActivity.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/BackgroundRecordActivity.kt index 76bfdd4..329ce34 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/BackgroundRecordActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/BackgroundRecordActivity.kt @@ -1,6 +1,9 @@ package com.simplemobiletools.voicerecorder.activities import android.content.Intent +import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.commons.helpers.PERMISSION_POST_NOTIFICATIONS +import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.services.RecorderService class BackgroundRecordActivity : SimpleActivity() { @@ -11,14 +14,20 @@ class BackgroundRecordActivity : SimpleActivity() { override fun onResume() { super.onResume() if (intent.action == RECORD_INTENT_ACTION) { - Intent(this@BackgroundRecordActivity, RecorderService::class.java).apply { - try { - if (RecorderService.isRunning) { - stopService(this) - } else { - startService(this) + handlePermission(PERMISSION_POST_NOTIFICATIONS) { + if (it) { + Intent(this@BackgroundRecordActivity, RecorderService::class.java).apply { + try { + if (RecorderService.isRunning) { + stopService(this) + } else { + startService(this) + } + } catch (ignored: Exception) { + } } - } catch (ignored: Exception) { + } else { + toast(R.string.no_post_notifications_permissions) } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt index 9bf0f90..2e7eba3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/fragments/RecorderFragment.kt @@ -6,8 +6,11 @@ import android.graphics.drawable.Drawable import android.os.Handler import android.os.Looper import android.util.AttributeSet +import com.simplemobiletools.commons.activities.BaseSimpleActivity import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.helpers.PERMISSION_POST_NOTIFICATIONS import com.simplemobiletools.commons.helpers.isNougatPlus +import com.simplemobiletools.commons.helpers.isTiramisuPlus import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.helpers.* import com.simplemobiletools.voicerecorder.models.Events @@ -25,7 +28,10 @@ class RecorderFragment(context: Context, attributeSet: AttributeSet) : MyViewPag override fun onResume() { setupColors() - if (!RecorderService.isRunning) status = RECORDING_STOPPED + if (!RecorderService.isRunning) { + status = RECORDING_STOPPED + } + refreshView() } @@ -43,7 +49,17 @@ class RecorderFragment(context: Context, attributeSet: AttributeSet) : MyViewPag updateRecordingDuration(0) toggle_recording_button.setOnClickListener { - toggleRecording() + if (isTiramisuPlus()) { + (context as? BaseSimpleActivity)?.handlePermission(PERMISSION_POST_NOTIFICATIONS) { + if (it) { + toggleRecording() + } else { + context.toast(R.string.no_post_notifications_permissions) + } + } + } else { + toggleRecording() + } } toggle_pause_button.setOnClickListener { diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/MyWidgetRecordDisplayProvider.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/MyWidgetRecordDisplayProvider.kt index dc101d4..0ee3ee0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/MyWidgetRecordDisplayProvider.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/helpers/MyWidgetRecordDisplayProvider.kt @@ -50,7 +50,8 @@ class MyWidgetRecordDisplayProvider : AppWidgetProvider() { private fun setupAppOpenIntent(context: Context, views: RemoteViews) { Intent(context, BackgroundRecordActivity::class.java).apply { action = BackgroundRecordActivity.RECORD_INTENT_ACTION - val pendingIntent = PendingIntent.getActivity(context, OPEN_APP_INTENT_ID, this, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) + val pendingIntent = + PendingIntent.getActivity(context, OPEN_APP_INTENT_ID, this, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) views.setOnClickPendingIntent(R.id.record_display_btn, pendingIntent) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt index 5454de2..53fb1a0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/services/RecorderService.kt @@ -13,7 +13,9 @@ import android.provider.MediaStore import android.provider.MediaStore.Audio.Media import androidx.core.app.NotificationCompat import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.helpers.* +import com.simplemobiletools.commons.helpers.ensureBackgroundThread +import com.simplemobiletools.commons.helpers.isOreoPlus +import com.simplemobiletools.commons.helpers.isRPlus import com.simplemobiletools.voicerecorder.R import com.simplemobiletools.voicerecorder.activities.SplashActivity import com.simplemobiletools.voicerecorder.extensions.config diff --git a/app/src/main/res/layout/widget_record_display_config.xml b/app/src/main/res/layout/widget_record_display_config.xml index 1b85214..a119049 100644 --- a/app/src/main/res/layout/widget_record_display_config.xml +++ b/app/src/main/res/layout/widget_record_display_config.xml @@ -66,6 +66,7 @@ android:layout_centerVertical="true" android:paddingStart="@dimen/activity_margin" android:paddingEnd="@dimen/activity_margin" /> +