]> gitweb.stoutner.com Git - PrivacyCell.git/blob - app/src/main/java/com/stoutner/privacycell/activities/SettingsActivity.kt
Release 1.10.
[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 import android.view.MenuItem
28
29 import androidx.appcompat.app.AppCompatActivity
30 import androidx.appcompat.widget.Toolbar
31 import androidx.core.app.ActivityCompat
32 import androidx.preference.PreferenceManager
33
34 import com.stoutner.privacycell.R
35 import com.stoutner.privacycell.dialogs.NotificationPermissionDialog.NotificationPermissionDialogListener
36 import com.stoutner.privacycell.fragments.SettingsFragment
37 import com.stoutner.privacycell.services.RealtimeMonitoringService
38
39 class SettingsActivity : AppCompatActivity(), NotificationPermissionDialogListener {
40     override fun onCreate(savedInstanceState: Bundle?) {
41         // Run the default commands.
42         super.onCreate(savedInstanceState)
43
44         // Get a handle for the shared preferences.
45         val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
46
47         // Get the bottom app bar preference.
48         val bottomAppBar = sharedPreferences.getBoolean(getString(R.string.bottom_app_bar_key), false)
49
50         // Set the content view.
51         if (bottomAppBar) {
52             setContentView(R.layout.settings_bottom_appbar)
53         } else {
54             setContentView(R.layout.settings_top_appbar)
55         }
56
57         // Get a handle for the toolbar.
58         val toolbar = findViewById<Toolbar>(R.id.toolbar)
59
60         // Set the support action bar.
61         setSupportActionBar(toolbar)
62
63         // Get a handle for the action bar.
64         val actionBar = supportActionBar!!
65
66         // Display the home arrow on the action bar.
67         actionBar.setDisplayHomeAsUpEnabled(true)
68
69         // Load the preferences fragment.
70         supportFragmentManager.beginTransaction().replace(R.id.preferences_framelayout, SettingsFragment()).commit()
71     }
72
73     override fun onCloseNotificationPermissionDialog() {
74         // Request the post notifications permission if the API >= 33.
75         if (Build.VERSION.SDK_INT >= 33) {
76             ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.POST_NOTIFICATIONS), PrivacyCellActivity.NOTIFICATION_PERMISSION_REQUEST_CODE)
77         }
78     }
79
80     override fun onOptionsItemSelected(item: MenuItem): Boolean {
81         // As there is only one option, go back.
82         onBackPressed()
83
84         // Consume the event.
85         return true
86     }
87
88     override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
89         // Run the default commands.
90         super.onRequestPermissionsResult(requestCode, permissions, grantResults)
91
92         // 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)
93         // and the result is for the notification permission.
94         if (grantResults.isNotEmpty() && (requestCode == PrivacyCellActivity.NOTIFICATION_PERMISSION_REQUEST_CODE)) {
95             // Check to see if the notification permission was granted.  If the dialog was canceled the grant result will be empty.
96             if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {  // The notification permission was granted.
97                 // Start the realtime monitoring service.
98                 startService(Intent(this, RealtimeMonitoringService::class.java))
99             }
100         }
101     }
102 }