X-Git-Url: https://gitweb.stoutner.com/?p=PrivacyCell.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Factivities%2FSettingsActivity.kt;fp=app%2Fsrc%2Fmain%2Fjava%2Fcom%2Fstoutner%2Fprivacycell%2Factivities%2FSettingsActivity.kt;h=098f45f8c54d8940429dc739e85a1ccb563aa844;hp=279d836cfd82d7c3fa5fa92b18e24fc643225f5d;hb=ff2f3992b7ecddb804a96322789bd35010529f43;hpb=ab7601a6d91b6035862bb47b1ac259855e3aba3b diff --git a/app/src/main/java/com/stoutner/privacycell/activities/SettingsActivity.kt b/app/src/main/java/com/stoutner/privacycell/activities/SettingsActivity.kt index 279d836..098f45f 100644 --- a/app/src/main/java/com/stoutner/privacycell/activities/SettingsActivity.kt +++ b/app/src/main/java/com/stoutner/privacycell/activities/SettingsActivity.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2021 Soren Stoutner . + * Copyright 2021-2022 Soren Stoutner . * * This file is part of Privacy Cell . * @@ -19,17 +19,24 @@ package com.stoutner.privacycell.activities +import android.Manifest +import android.content.Intent +import android.content.pm.PackageManager +import android.os.Build import android.os.Bundle import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar +import androidx.core.app.ActivityCompat import androidx.preference.PreferenceManager import com.stoutner.privacycell.R +import com.stoutner.privacycell.dialogs.NotificationPermissionDialog.NotificationPermissionDialogListener import com.stoutner.privacycell.fragments.SettingsFragment +import com.stoutner.privacycell.services.RealtimeMonitoringService -class SettingsActivity : AppCompatActivity() { +class SettingsActivity : AppCompatActivity(), NotificationPermissionDialogListener { override fun onCreate(savedInstanceState: Bundle?) { // Run the default commands. super.onCreate(savedInstanceState) @@ -63,6 +70,13 @@ class SettingsActivity : AppCompatActivity() { supportFragmentManager.beginTransaction().replace(R.id.preferences_framelayout, SettingsFragment()).commit() } + override fun onCloseNotificationPermissionDialog() { + // Request the post notifications permission if the API >= 33. + if (Build.VERSION.SDK_INT >= 33) { + ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), PrivacyCellActivity.NOTIFICATION_PERMISSION_REQUEST_CODE) + } + } + override fun onOptionsItemSelected(item: MenuItem): Boolean { // As there is only one option, go back. onBackPressed() @@ -70,4 +84,19 @@ class SettingsActivity : AppCompatActivity() { // Consume the event. return true } -} \ No newline at end of file + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + // Run the default commands. + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + + // Only process the results if they exist (this method is triggered when a dialog is presented the first time for an app, but no grant results are included) + // and the result is for the notification permission. + if (grantResults.isNotEmpty() && (requestCode == PrivacyCellActivity.NOTIFICATION_PERMISSION_REQUEST_CODE)) { + // Check to see if the notification permission was granted. If the dialog was canceled the grant result will be empty. + if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // The notification permission was granted. + // Start the realtime monitoring service. + startService(Intent(this, RealtimeMonitoringService::class.java)) + } + } + } +}