]> gitweb.stoutner.com Git - PrivacyCell.git/blob - app/src/main/java/com/stoutner/privacycell/activities/SettingsActivity.kt
Remove unneeded `onBackPressed`. https://redmine.stoutner.com/issues/934
[PrivacyCell.git] / app / src / main / java / com / stoutner / privacycell / activities / SettingsActivity.kt
1 /*
2  * Copyright 2021-2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Cell <https://www.stoutner.com/privacy-cell>.
5  *
6  * Privacy Cell is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Cell is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Cell.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacycell.activities
21
22 import android.Manifest
23 import android.content.Intent
24 import android.content.pm.PackageManager
25 import android.os.Build
26 import android.os.Bundle
27
28 import androidx.appcompat.app.AppCompatActivity
29 import androidx.appcompat.widget.Toolbar
30 import androidx.core.app.ActivityCompat
31 import androidx.preference.PreferenceManager
32
33 import com.stoutner.privacycell.R
34 import com.stoutner.privacycell.dialogs.NotificationPermissionDialog.NotificationPermissionDialogListener
35 import com.stoutner.privacycell.fragments.SettingsFragment
36 import com.stoutner.privacycell.services.RealtimeMonitoringService
37
38 class SettingsActivity : AppCompatActivity(), NotificationPermissionDialogListener {
39     // Declare the class variables.
40     private lateinit var settingsFragment: SettingsFragment
41
42     override fun onCreate(savedInstanceState: Bundle?) {
43         // Run the default commands.
44         super.onCreate(savedInstanceState)
45
46         // Get a handle for the shared preferences.
47         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
48
49         // Get the bottom app bar preference.
50         val bottomAppBar = sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)
51
52         // Set the content view.
53         if (bottomAppBar) {
54             setContentView(R.layout.settings_bottom_appbar)
55         } else {
56             setContentView(R.layout.settings_top_appbar)
57         }
58
59         // Get a handle for the toolbar.
60         val toolbar = findViewById<Toolbar>(R.id.toolbar)
61
62         // Set the support action bar.
63         setSupportActionBar(toolbar)
64
65         // Get a handle for the action bar.
66         val actionBar = supportActionBar!!
67
68         // Display the home arrow on the action bar.
69         actionBar.setDisplayHomeAsUpEnabled(true)
70
71         // Instantiate the settings fragment.
72         settingsFragment = SettingsFragment()
73
74         // Load the settings fragment.
75         supportFragmentManager.beginTransaction().replace(R.id.preferences_framelayout, settingsFragment).commitNow()
76     }
77
78     override fun onCloseNotificationPermissionDialog() {
79         // Request the post notifications permission if the API >= 33.
80         if (Build.VERSION.SDK_INT >= 33) {
81             ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), PrivacyCellActivity.NOTIFICATION_PERMISSION_REQUEST_CODE)
82         }
83     }
84
85     override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
86         // Run the default commands.
87         super.onRequestPermissionsResult(requestCode, permissions, grantResults)
88
89         // 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)
90         // and the result is for the notification permission.
91         if (grantResults.isNotEmpty() && (requestCode == PrivacyCellActivity.NOTIFICATION_PERMISSION_REQUEST_CODE)) {
92             // Check to see if the notification permission was granted.  If the dialog was canceled the grant result will be empty.
93             if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  // The notification permission was granted.
94                 // Start the realtime monitoring service.
95                 startService(Intent(this, RealtimeMonitoringService::class.java))
96
97                 // Update the realtime monitoring preference summary.
98                 settingsFragment.updateRealtimeMonitoringSummary()
99             }
100         }
101     }
102 }