Cherish: use a new task stack for app lock fragments

* fixes fragments staying in recents on going home
* also made other preferences do binder calls asynchronously

Signed-off-by: jhonboy121 <alfredmathew05@gmail.com>
Signed-off-by: Hưng Phan <phandinhhungvp2001@gmail.com>
This commit is contained in:
jhonboy121
2022-09-27 23:09:10 +05:30
committed by Hưng Phan
parent fc7a0c985e
commit da941652b3
8 changed files with 141 additions and 42 deletions

View File

@@ -19,37 +19,59 @@ package com.cherish.settings.security.applock
import android.app.AppLockManager
import android.content.Context
import android.hardware.biometrics.BiometricManager
import android.hardware.biometrics.BiometricManager.Authenticators
import android.hardware.biometrics.BiometricManager.Authenticators.BIOMETRIC_STRONG
import androidx.preference.Preference
import androidx.preference.SwitchPreference
import androidx.preference.PreferenceScreen
import com.android.settings.core.BasePreferenceController
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 = "app_lock_biometrics_allowed"
class AppLockBiometricPreferenceController(
context: Context,
key: String,
) : BasePreferenceController(context, key),
Preference.OnPreferenceChangeListener {
private val coroutineScope: CoroutineScope
) : CherishTogglePreferenceController(context, KEY) {
private val appLockManager = context.getSystemService(AppLockManager::class.java)
private val biometricManager = context.getSystemService(BiometricManager::class.java)
private var preference: Preference? = null
private var isBiometricsAllowed = false
init {
coroutineScope.launch {
isBiometricsAllowed = withContext(Dispatchers.Default) {
appLockManager.isBiometricsAllowed()
}
preference?.let {
updateState(it)
}
}
}
override fun getAvailabilityStatus(): Int {
val biometricsAllowed = biometricManager.canAuthenticate(
Authenticators.BIOMETRIC_STRONG) == BiometricManager.BIOMETRIC_SUCCESS
return if (biometricsAllowed)
AVAILABLE
else
UNSUPPORTED_ON_DEVICE
val result = biometricManager.canAuthenticate(BIOMETRIC_STRONG)
return if (result == BiometricManager.BIOMETRIC_SUCCESS) AVAILABLE else CONDITIONALLY_UNAVAILABLE
}
override fun updateState(preference: Preference) {
(preference as SwitchPreference).setChecked(appLockManager.isBiometricsAllowed())
}
override fun isChecked() = isBiometricsAllowed
override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean {
appLockManager.setBiometricsAllowed(newValue as Boolean)
override fun setChecked(checked: Boolean): Boolean {
isBiometricsAllowed = checked
coroutineScope.launch(Dispatchers.Default) {
appLockManager.setBiometricsAllowed(isBiometricsAllowed)
}
return true
}
override fun displayPreference(screen: PreferenceScreen) {
super.displayPreference(screen)
preference = screen.findPreference(preferenceKey)
}
}

View File

@@ -67,7 +67,7 @@ class AppLockNotificationRedactionPC(
}
override fun displayPreference(screen: PreferenceScreen) {
super.displayPreference(screen);
super.displayPreference(screen)
preference = screen.findPreference(preferenceKey)
}
}

View File

@@ -22,21 +22,22 @@ import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import com.android.internal.logging.nano.MetricsProto
import androidx.lifecycle.lifecycleScope
import com.android.settings.R
import com.android.settings.search.BaseSearchIndexProvider
import com.android.settingslib.core.AbstractPreferenceController
import com.android.settingslib.search.SearchIndexable
import com.android.settings.dashboard.DashboardFragment
import com.cherish.settings.fragments.CherishDashboardFragment
@SearchIndexable
class AppLockSettingsFragment : DashboardFragment(),
class AppLockSettingsFragment : CherishDashboardFragment(),
MenuItem.OnMenuItemClickListener {
private var debugEnabled = SystemProperties.get(DEBUG_PROPERTY, null) == LEVEL_DEBUG
private var handledClick = false
override protected fun getPreferenceScreenResId() = R.xml.app_lock_package_config_settings
override fun getMetricsCategory() = MetricsProto.MetricsEvent.CHERISH_SETTINGS
override protected fun getPreferenceScreenResId() = R.xml.cherish_settings_app_lock
override protected fun getLogTag() = TAG
@@ -51,22 +52,24 @@ class AppLockSettingsFragment : DashboardFragment(),
}
private fun getDebugMenuItemTitle(): Int =
if (debugEnabled)
R.string.disable_debugging
else
R.string.enable_debugging
if (debugEnabled) R.string.disable_debugging else R.string.enable_debugging
override fun onMenuItemClick(item: MenuItem): Boolean {
if (item.itemId == MENU_ITEM_DEBUG_ID) {
debugEnabled = !debugEnabled
SystemProperties.set(DEBUG_PROPERTY,
if (debugEnabled) LEVEL_DEBUG else null)
SystemProperties.set(DEBUG_PROPERTY, if (debugEnabled) LEVEL_DEBUG else null)
item.setTitle(getDebugMenuItemTitle())
return true
}
return false
}
override protected fun createPreferenceControllers(
context: Context
) : List<AbstractPreferenceController> = listOf(
AppLockBiometricPreferenceController(context, lifecycleScope)
)
companion object {
private const val TAG = "AppLockSettingsFragment"
@@ -75,6 +78,6 @@ class AppLockSettingsFragment : DashboardFragment(),
private const val MENU_ITEM_DEBUG_ID = 101
@JvmField
val SEARCH_INDEX_DATA_PROVIDER = BaseSearchIndexProvider(R.xml.app_lock_package_config_settings)
val SEARCH_INDEX_DATA_PROVIDER = BaseSearchIndexProvider(R.xml.cherish_settings_app_lock)
}
}
}

View File

@@ -36,16 +36,20 @@ import com.android.settings.core.SubSettingLauncher
import com.android.settings.password.ConfirmDeviceCredentialActivity
import com.android.settings.security.SecuritySettings
import com.android.settingslib.core.lifecycle.Lifecycle
import com.android.settingslib.transition.SettingsTransitionHelper.TransitionType
import com.android.settingslib.transition.SettingsTransitionHelper.TransitionType.TRANSITION_SLIDE
import com.android.settings.core.BasePreferenceController
import com.android.settings.SettingsActivity
import com.android.settings.core.SettingsBaseActivity
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider
class AppLockSettingsPreferenceController(
context: Context,
preferenceKey: String,
private val host: SecuritySettings?,
lifecycle: Lifecycle?,
) : BasePreferenceController(context, preferenceKey),
LifecycleEventObserver {
LifecycleEventObserver {
private val lockPatternUtils = LockPatternUtils(context)
private val appLockManager = context.getSystemService(AppLockManager::class.java)
@@ -58,15 +62,13 @@ class AppLockSettingsPreferenceController(
StartActivityForResult()
) {
if (it?.resultCode == Activity.RESULT_OK) {
SubSettingLauncher(mContext)
val intent = SubSettingLauncher(mContext)
.setDestination(AppLockSettingsFragment::class.qualifiedName)
.setSourceMetricsCategory(host.metricsCategory)
.setTransitionType(TransitionType.TRANSITION_SLIDE)
.addFlags(
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS or
Intent.FLAG_ACTIVITY_NEW_TASK
)
.launch()
.setTransitionType(TRANSITION_SLIDE)
.toIntent()
intent.setClass(mContext, AppLockSubSettings::class.java)
mContext.startActivity(intent)
}
}
}

View File

@@ -0,0 +1,26 @@
/*
* 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 com.android.settings.SettingsActivity
class AppLockSubSettings : SettingsActivity() {
override protected fun isValidFragment(fragmentName: String): Boolean {
return AppLockSettingsFragment::class.qualifiedName == fragmentName
}
}