-
Notifications
You must be signed in to change notification settings - Fork 21
/
SettingsActivity.kt
76 lines (68 loc) · 2.69 KB
/
SettingsActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Copyright (C) 2022 The LineageOS Project
*
* SPDX-License-Identifier: Apache-2.0
*/
package org.lineageos.selfie
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.SwitchPreference
import org.lineageos.selfie.utils.CameraSoundsUtils
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.settings_activity)
if (savedInstanceState == null) {
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.commit()
}
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
android.R.id.home -> {
finish()
true
}
else -> {
super.onOptionsItemSelected(item)
}
}
class SettingsFragment : PreferenceFragmentCompat() {
private val shutterSound by lazy { findPreference<SwitchPreference>("shutter_sound") }
private val fileOutputLocation by lazy { findPreference<SwitchPreference>("custom_storage_location") }
private val openDocumentTree = registerForActivityResult(
ActivityResultContracts.OpenDocumentTree()
) {
if (it != null) {
context?.contentResolver?.takePersistableUriPermission(
it, Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
}
fileOutputLocation?.isChecked = it != null
fileOutputLocation?.sharedPreferences?.customStorageLocation = it
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
shutterSound?.isEnabled = !CameraSoundsUtils.mustPlaySounds
fileOutputLocation?.apply {
isChecked = sharedPreferences?.customStorageLocation != null
onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _, newValue ->
if (newValue as Boolean) {
openDocumentTree.launch(null)
} else {
sharedPreferences?.customStorageLocation = null
}
true
}
}
}
}
}