applock: adapt for API changes

Signed-off-by: jhonboy121 <alfredmathew05@gmail.com>
Signed-off-by: Hưng Phan <phandinhhungvp2001@gmail.com>
This commit is contained in:
jhonboy121
2022-09-16 20:26:16 +05:30
committed by Hưng Phan
parent 21d34ff4e4
commit 7e7aa11228
14 changed files with 445 additions and 206 deletions

View File

@@ -623,8 +623,6 @@
<!-- App lock --> <!-- App lock -->
<string name="app_lock_title">App lock</string> <string name="app_lock_title">App lock</string>
<string name="app_lock_summary_singular">1 application is protected</string>
<string name="app_lock_summary_plural"><xliff:g example="2" id="Number of applications">%1$d</xliff:g> applications are protected</string>
<string name="app_lock_authentication_dialog_title">Unlock</string> <string name="app_lock_authentication_dialog_title">Unlock</string>
<string name="enable_debugging">Enable debugging</string> <string name="enable_debugging">Enable debugging</string>
<string name="disable_debugging">Disable debugging</string> <string name="disable_debugging">Disable debugging</string>
@@ -632,7 +630,7 @@
<string name="app_lock_packages_summary">Select the apps to protect with biometrics or device credentials</string> <string name="app_lock_packages_summary">Select the apps to protect with biometrics or device credentials</string>
<string name="app_lock_timeout_title">Auto lock timeout</string> <string name="app_lock_timeout_title">Auto lock timeout</string>
<string name="app_lock_timeout_summary">Duration of time after which an unlocked app in background should be locked</string> <string name="app_lock_timeout_summary">Duration of time after which an unlocked app in background should be locked</string>
<string name="app_lock_notifications_title">Collapse notifications</string> <string name="app_lock_notifications_title">Redact notifications</string>
<string name="app_lock_notifications_summary"> <string name="app_lock_notifications_summary">
Notification content will be hidden and collapsed for selected apps when they are locked. Notification content will be hidden and collapsed for selected apps when they are locked.
Heads up notifications will be automatically disabled. Heads up notifications will be automatically disabled.
@@ -640,6 +638,7 @@
<string name="app_lock_notifications_disabled_summary">Protect an application first</string> <string name="app_lock_notifications_disabled_summary">Protect an application first</string>
<string name="app_lock_biometrics_allowed_title">Enable biometrics for unlocking</string> <string name="app_lock_biometrics_allowed_title">Enable biometrics for unlocking</string>
<string name="app_lock_footer_text">Bubbles will be automatically dismissed after timeout</string> <string name="app_lock_footer_text">Bubbles will be automatically dismissed after timeout</string>
<string name="enable_protection">Enable protection</string>
<!-- Quick settings styling --> <!-- Quick settings styling -->
<string name="qs_styling">Quick Settings UI</string> <string name="qs_styling">Quick Settings UI</string>

24
res/values/plurals.xml Normal file
View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2022 FlamingoOS Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<!-- App Lock -->
<plurals name="app_lock_summary">
<item quantity="one"><xliff:g example="1" id="Number of applications">%1$d</xliff:g> application is protected</item>
<item quantity="other"><xliff:g example="10" id="Number of applications">%1$d</xliff:g> applications are protected</item>
</plurals>
</resources>

View File

@@ -1,12 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2022 CherishOS Project <!-- Copyright (C) 2022 CherishOS Project
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
You may obtain a copy of the License at You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0 http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -33,20 +30,10 @@
android:persistent="false" android:persistent="false"
settings:controller="com.cherish.settings.security.applock.AppLockTimeoutPreferenceController" /> settings:controller="com.cherish.settings.security.applock.AppLockTimeoutPreferenceController" />
<Preference
android:key="app_lock_notifications"
android:title="@string/app_lock_notifications_title"
android:fragment="com.cherish.settings.security.applock.AppLockNotificationListFragment"
settings:controller="com.cherish.settings.security.applock.AppLockNotificationPreferenceController" />
<SwitchPreference <SwitchPreference
android:key="app_lock_biometrics_allowed" android:key="app_lock_biometrics_allowed"
android:title="@string/app_lock_biometrics_allowed_title" android:title="@string/app_lock_biometrics_allowed_title"
android:persistent="false" android:persistent="false"
settings:controller="com.cherish.settings.security.applock.AppLockBiometricPreferenceController" /> settings:controller="com.cherish.settings.security.applock.AppLockBiometricPreferenceController" />
<com.android.settingslib.widget.FooterPreference
android:title="@string/app_lock_footer_text"
android:selectable="false"
settings:searchable="false" />
</PreferenceScreen> </PreferenceScreen>

View File

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2022 FlamingoOS Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/app_lock_packages_title">
</PreferenceScreen>

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 2022 FlamingoOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cherish.settings
import android.content.Context
import android.widget.Switch
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import com.android.settings.R
import com.android.settings.core.TogglePreferenceController
import com.android.settingslib.widget.MainSwitchPreference
import com.android.settingslib.widget.OnMainSwitchChangeListener
abstract class CherishTogglePreferenceController(
context: Context,
key: String,
) : TogglePreferenceController(context, key),
OnMainSwitchChangeListener {
override fun displayPreference(screen: PreferenceScreen) {
super.displayPreference(screen)
val preference = screen.findPreference<Preference>(preferenceKey) ?: return
if (preference is MainSwitchPreference) {
preference.addOnSwitchChangeListener(this)
}
}
override fun onSwitchChanged(switchView: Switch, isChecked: Boolean) {
setChecked(isChecked)
}
override fun getSliceHighlightMenuRes() = R.string.menu_key_cherish
}

View File

@@ -1,57 +0,0 @@
/*
* Copyright (C) 2022 FlamingoOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cherish.settings.security.applock
import android.app.AppLockManager
import android.os.Bundle
import android.view.View
import com.android.settings.R
import com.cherish.settings.fragment.AppListFragment
class AppLockNotificationListFragment : AppListFragment() {
private lateinit var appLockManager: AppLockManager
private val lockedPackages = mutableListOf<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
appLockManager = requireContext().getSystemService(AppLockManager::class.java)
lockedPackages.addAll(appLockManager.packages)
}
override protected fun getTitle(): Int = R.string.app_lock_notifications_title
override protected fun getInitialCheckedList(): List<String> =
appLockManager.packagesWithSecureNotifications
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
setDisplayCategory(CATEGORY_BOTH)
setCustomFilter {
lockedPackages.contains(it.packageName)
}
super.onViewCreated(view, savedInstanceState)
}
override protected fun onAppSelected(packageName: String) {
appLockManager.setSecureNotification(packageName, true)
}
override protected fun onAppDeselected(packageName: String) {
appLockManager.setSecureNotification(packageName, false)
}
}

View File

@@ -1,77 +0,0 @@
/*
* Copyright (C) 2022 FlamingoOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cherish.settings.security.applock
import android.app.AppLockManager
import android.content.Context
import android.os.UserHandle
import androidx.lifecycle.Lifecycle.Event
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import com.android.internal.widget.LockPatternUtils
import com.android.settings.R
import com.android.settingslib.core.lifecycle.Lifecycle
import com.android.settings.core.BasePreferenceController
class AppLockNotificationPreferenceController(
private val context: Context,
lifecycle: Lifecycle?,
) : BasePreferenceController(context, KEY),
LifecycleEventObserver {
private val appLockManager = context.getSystemService(AppLockManager::class.java)
private var preference: Preference? = null
init {
lifecycle?.addObserver(this)
}
override fun getAvailabilityStatus() =
if (appLockManager.packages.isNotEmpty()) AVAILABLE else DISABLED_DEPENDENT_SETTING
override fun onStateChanged(owner: LifecycleOwner, event: Event) {
if (event == Event.ON_START) {
preference?.let {
updateState(it)
}
}
}
override fun displayPreference(screen: PreferenceScreen) {
super.displayPreference(screen)
preference = screen.findPreference(preferenceKey)
}
override fun updateState(preference: Preference) {
if (getAvailabilityStatus() == AVAILABLE) {
preference.setEnabled(true)
preference.summary = context.getString(R.string.app_lock_notifications_summary)
} else {
preference.setEnabled(false)
preference.summary = context.getString(R.string.app_lock_notifications_disabled_summary)
}
}
companion object {
private const val KEY = "app_lock_notifications"
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2022 FlamingoOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cherish.settings.security.applock
import android.app.AppLockManager
import android.content.Context
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import com.cherish.settings.CherishTogglePreferenceController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
private const val KEY = "redact_notifications"
class AppLockNotificationRedactionPC(
context: Context,
private val packageName: String,
private val coroutineScope: CoroutineScope
) : CherishTogglePreferenceController(context, KEY) {
private val appLockManager = context.getSystemService(AppLockManager::class.java)
private var shouldRedactNotification = false
private var preference: Preference? = null
init {
coroutineScope.launch {
shouldRedactNotification = withContext(Dispatchers.Default) {
appLockManager.packageData.find {
it.packageName == packageName
}?.shouldRedactNotification == true
}
preference?.let {
updateState(it)
}
}
}
override fun getAvailabilityStatus() = AVAILABLE
override fun isChecked() = shouldRedactNotification
override fun setChecked(checked: Boolean): Boolean {
shouldRedactNotification = checked
coroutineScope.launch(Dispatchers.Default) {
appLockManager.setShouldRedactNotification(packageName, checked)
}
return true
}
override fun displayPreference(screen: PreferenceScreen) {
super.displayPreference(screen);
preference = screen.findPreference(preferenceKey)
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2022 FlamingoOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cherish.settings.security.applock
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.android.settings.R
import com.android.settings.widget.EntityHeaderController
import com.android.settingslib.core.AbstractPreferenceController
import com.android.settingslib.widget.LayoutPreference
import com.cherish.settings.fragments.CherishDashboardFragment
private val TAG = AppLockPackageConfigFragment::class.simpleName
private const val KEY_HEADER = "header_view"
class AppLockPackageConfigFragment : CherishDashboardFragment() {
private lateinit var pm: PackageManager
private lateinit var packageInfo: PackageInfo
override fun onAttach(context: Context) {
pm = context.packageManager
packageInfo = arguments?.getParcelable(PACKAGE_INFO, PackageInfo::class.java)!!
super.onAttach(context)
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
super.onCreatePreferences(savedInstanceState, rootKey)
val header = preferenceScreen.findPreference<LayoutPreference>(KEY_HEADER)
EntityHeaderController.newInstance(
requireActivity(),
this,
header?.findViewById(R.id.entity_header)
).setRecyclerView(listView, settingsLifecycle)
.setPackageName(packageInfo.packageName)
.setButtonActions(
EntityHeaderController.ActionType.ACTION_NONE,
EntityHeaderController.ActionType.ACTION_NONE
)
.bindHeaderButtons()
.setLabel(getLabel(packageInfo))
.setIcon(getIcon(packageInfo))
.done(requireActivity(), false /* rebindActions */)
}
private fun getLabel(packageInfo: PackageInfo) =
packageInfo.applicationInfo.loadLabel(pm).toString()
private fun getIcon(packageInfo: PackageInfo) =
packageInfo.applicationInfo.loadIcon(pm)
override protected fun createPreferenceControllers(
context: Context
) : List<AbstractPreferenceController> = listOf(
AppLockPackageProtectionPC(context, packageInfo.packageName, lifecycleScope),
AppLockNotificationRedactionPC(context, packageInfo.packageName, lifecycleScope),
)
override protected fun getPreferenceScreenResId() = R.xml.app_lock_package_config_settings
override protected fun getLogTag() = TAG
}

View File

@@ -17,41 +17,123 @@
package com.cherish.settings.security.applock package com.cherish.settings.security.applock
import android.app.AppLockManager import android.app.AppLockManager
import android.content.Context
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.PackageManager.PackageInfoFlags
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import com.android.settings.R import androidx.lifecycle.lifecycleScope
import com.cherish.settings.fragment.AppListFragment import androidx.preference.Preference
import androidx.preference.forEach
class AppLockPackageListFragment : AppListFragment() { import com.android.settings.R
import com.android.settings.core.SubSettingLauncher
import com.android.settingslib.PrimarySwitchPreference
import com.android.settingslib.widget.TwoTargetPreference.ICON_SIZE_SMALL
import com.cherish.settings.fragments.CherishDashboardFragment
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
private val TAG = AppLockPackageListFragment::class.simpleName
internal const val PACKAGE_INFO = "package_info"
class AppLockPackageListFragment : CherishDashboardFragment() {
private lateinit var appLockManager: AppLockManager private lateinit var appLockManager: AppLockManager
private lateinit var pm: PackageManager
private lateinit var whiteListedPackages: Array<String> private lateinit var whiteListedPackages: Array<String>
override fun onCreate(savedInstanceState: Bundle?) { override fun onAttach(context: Context) {
super.onCreate(savedInstanceState) super.onAttach(context)
appLockManager = requireContext().getSystemService(AppLockManager::class.java) appLockManager = context.getSystemService(AppLockManager::class.java)
whiteListedPackages = requireContext().resources.getStringArray( pm = context.packageManager
whiteListedPackages = resources.getStringArray(
com.android.internal.R.array.config_appLockAllowedSystemApps) com.android.internal.R.array.config_appLockAllowedSystemApps)
} }
override protected fun getTitle(): Int = R.string.app_lock_packages_title override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
super.onCreatePreferences(savedInstanceState, rootKey)
override protected fun getInitialCheckedList(): List<String> = appLockManager.packages lifecycleScope.launch {
val selectedPackages = getSelectedPackages()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { val preferences = withContext(Dispatchers.Default) {
setDisplayCategory(CATEGORY_BOTH) pm.getInstalledPackages(
setCustomFilter { PackageInfoFlags.of(PackageManager.MATCH_ALL.toLong())
!it.applicationInfo.isSystemApp() || whiteListedPackages.contains(it.packageName) ).filter {
!it.applicationInfo.isSystemApp() || whiteListedPackages.contains(it.packageName)
}.sortedWith { first, second ->
getLabel(first).compareTo(getLabel(second))
}
}.map { packageInfo ->
createPreference(packageInfo, selectedPackages.contains(packageInfo.packageName))
}
preferenceScreen?.let {
preferences.forEach { pref ->
it.addPreference(pref)
}
}
} }
super.onViewCreated(view, savedInstanceState)
} }
override protected fun onAppSelected(packageName: String) { override fun onResume() {
appLockManager.addPackage(packageName) super.onResume()
lifecycleScope.launch {
val selectedPackages = getSelectedPackages()
preferenceScreen?.forEach {
if (it is PrimarySwitchPreference) {
it.isChecked = selectedPackages.contains(it.key)
}
}
}
} }
override protected fun onAppDeselected(packageName: String) { private suspend fun getSelectedPackages(): Set<String> {
appLockManager.removePackage(packageName) return withContext(Dispatchers.IO) {
appLockManager.packageData.map { it.packageName }.toSet()
}
} }
private fun getLabel(packageInfo: PackageInfo) =
packageInfo.applicationInfo.loadLabel(pm).toString()
private fun createPreference(packageInfo: PackageInfo, isProtected: Boolean): Preference {
val label = getLabel(packageInfo)
return PrimarySwitchPreference(requireContext()).apply {
key = packageInfo.packageName
title = label
icon = packageInfo.applicationInfo.loadIcon(pm)
setIconSize(ICON_SIZE_SMALL)
isChecked = isProtected
setOnPreferenceChangeListener { _, newValue ->
lifecycleScope.launch(Dispatchers.IO) {
if (newValue as Boolean) {
appLockManager.addPackage(packageInfo.packageName)
} else {
appLockManager.removePackage(packageInfo.packageName)
}
}
return@setOnPreferenceChangeListener true
}
setOnPreferenceClickListener {
SubSettingLauncher(requireContext())
.setDestination(AppLockPackageConfigFragment::class.qualifiedName)
.setSourceMetricsCategory(metricsCategory)
.setTitleText(label)
.setArguments(
Bundle(1).apply {
putParcelable(PACKAGE_INFO, packageInfo)
}
)
.launch()
true
}
}
}
override protected fun getPreferenceScreenResId() = R.xml.app_lock_package_list_settings
override protected fun getLogTag() = TAG
} }

View File

@@ -0,0 +1,78 @@
/*
* Copyright (C) 2022 FlamingoOS Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cherish.settings.security.applock
import android.app.AppLockManager
import android.content.Context
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceScreen
import com.cherish.settings.CherishTogglePreferenceController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
private const val KEY = "main_switch"
class AppLockPackageProtectionPC(
context: Context,
private val packageName: String,
private val coroutineScope: CoroutineScope
) : CherishTogglePreferenceController(context, KEY) {
private val appLockManager = context.getSystemService(AppLockManager::class.java)
private var isProtected = false
private var preference: Preference? = null
init {
coroutineScope.launch {
isProtected = withContext(Dispatchers.Default) {
appLockManager.packageData.any {
it.packageName == packageName
}
}
preference?.let {
updateState(it)
}
}
}
override fun getAvailabilityStatus() = AVAILABLE
override fun isChecked() = isProtected
override fun setChecked(checked: Boolean): Boolean {
isProtected = checked
coroutineScope.launch(Dispatchers.Default) {
if (isProtected) {
appLockManager.addPackage(packageName)
} else {
appLockManager.removePackage(packageName)
}
}
return true
}
override fun displayPreference(screen: PreferenceScreen) {
super.displayPreference(screen)
preference = screen.findPreference(preferenceKey)
}
}

View File

@@ -25,8 +25,6 @@ import android.view.MenuItem
import com.android.internal.logging.nano.MetricsProto import com.android.internal.logging.nano.MetricsProto
import com.android.settings.R import com.android.settings.R
import com.android.settings.search.BaseSearchIndexProvider import com.android.settings.search.BaseSearchIndexProvider
import com.android.settingslib.core.AbstractPreferenceController
import com.android.settingslib.core.lifecycle.Lifecycle
import com.android.settingslib.search.SearchIndexable import com.android.settingslib.search.SearchIndexable
import com.android.settings.dashboard.DashboardFragment import com.android.settings.dashboard.DashboardFragment
@@ -36,15 +34,12 @@ class AppLockSettingsFragment : DashboardFragment(),
private var debugEnabled = SystemProperties.get(DEBUG_PROPERTY, null) == LEVEL_DEBUG private var debugEnabled = SystemProperties.get(DEBUG_PROPERTY, null) == LEVEL_DEBUG
override protected fun getPreferenceScreenResId() = R.xml.app_lock_settings override protected fun getPreferenceScreenResId() = R.xml.app_lock_package_config_settings
override fun getMetricsCategory() = MetricsProto.MetricsEvent.CHERISH_SETTINGS override fun getMetricsCategory() = MetricsProto.MetricsEvent.CHERISH_SETTINGS
override protected fun getLogTag() = TAG override protected fun getLogTag() = TAG
override protected fun createPreferenceControllers(context: Context) =
buildPreferenceControllers(context, settingsLifecycle)
override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) { override fun onCreateOptionsMenu(menu: Menu, menuInflater: MenuInflater) {
super.onCreateOptionsMenu(menu, menuInflater) super.onCreateOptionsMenu(menu, menuInflater)
menu.add( menu.add(
@@ -80,22 +75,6 @@ class AppLockSettingsFragment : DashboardFragment(),
private const val MENU_ITEM_DEBUG_ID = 101 private const val MENU_ITEM_DEBUG_ID = 101
@JvmField @JvmField
val SEARCH_INDEX_DATA_PROVIDER = object : BaseSearchIndexProvider( val SEARCH_INDEX_DATA_PROVIDER = BaseSearchIndexProvider(R.xml.app_lock_package_config_settings)
R.xml.app_lock_settings
) {
override fun createPreferenceControllers(context: Context) =
buildPreferenceControllers(context)
}
fun buildPreferenceControllers(
context: Context,
lifecycle: Lifecycle? = null
): List<AbstractPreferenceController> =
listOf(
AppLockNotificationPreferenceController(
context,
lifecycle,
)
)
} }
} }

View File

@@ -91,20 +91,22 @@ class AppLockSettingsPreferenceController(
} }
override fun updateState(preference: Preference) { override fun updateState(preference: Preference) {
if (getAvailabilityStatus() == AVAILABLE) { preference.apply {
preference.setEnabled(true) if (getAvailabilityStatus() == AVAILABLE) {
preference.summary = getSummaryForListSize(appLockManager.getPackages().size) setEnabled(true)
} else { summary = getSummaryForListSize(appLockManager.packageData.size)
preference.setEnabled(false) } else {
preference.summary = mContext.getString(R.string.disabled_because_no_backup_security) setEnabled(false)
summary = mContext.getString(R.string.disabled_because_no_backup_security)
}
} }
} }
private fun getSummaryForListSize(size: Int): CharSequence? = private fun getSummaryForListSize(size: Int): CharSequence? =
when { if (size == 0) {
size == 0 -> null null
size == 1 -> mContext.getString(R.string.app_lock_summary_singular) } else {
else -> mContext.getString(R.string.app_lock_summary_plural, size) mContext.resources.getQuantityString(R.plurals.app_lock_summary, size, size)
} }
override fun handlePreferenceTreeClick(preference: Preference): Boolean { override fun handlePreferenceTreeClick(preference: Preference): Boolean {

View File

@@ -28,7 +28,7 @@ class AppLockTimeoutPreferenceController(
context: Context, context: Context,
key: String, key: String,
) : BasePreferenceController(context, key), ) : BasePreferenceController(context, key),
Preference.OnPreferenceChangeListener { Preference.OnPreferenceChangeListener {
private val appLockManager = context.getSystemService(AppLockManager::class.java) private val appLockManager = context.getSystemService(AppLockManager::class.java)