Merge "Fix not displaying "Allow restricted settings"" into udc-dev
This commit is contained in:
@@ -437,7 +437,8 @@ public class AppInfoDashboardFragment extends DashboardFragment
|
||||
}
|
||||
}
|
||||
|
||||
private static void showLockScreen(Context context, Runnable successRunnable) {
|
||||
/** Shows the lock screen if the keyguard is secured. */
|
||||
public static void showLockScreen(Context context, Runnable successRunnable) {
|
||||
final KeyguardManager keyguardManager = context.getSystemService(
|
||||
KeyguardManager.class);
|
||||
|
||||
|
||||
@@ -16,17 +16,25 @@
|
||||
|
||||
package com.android.settings.spa.app.appinfo
|
||||
|
||||
import android.app.AppOpsManager
|
||||
import android.content.Context
|
||||
import android.content.pm.ApplicationInfo
|
||||
import android.os.UserManager
|
||||
import android.widget.Toast
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.produceState
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.android.settings.R
|
||||
import com.android.settings.Utils
|
||||
import com.android.settings.applications.appinfo.AppInfoDashboardFragment
|
||||
import com.android.settingslib.spa.widget.scaffold.MoreOptionsAction
|
||||
import com.android.settingslib.spaprivileged.framework.common.appOpsManager
|
||||
import com.android.settingslib.spaprivileged.framework.common.devicePolicyManager
|
||||
import com.android.settingslib.spaprivileged.framework.common.userManager
|
||||
import com.android.settingslib.spaprivileged.model.app.IPackageManagers
|
||||
@@ -35,6 +43,8 @@ import com.android.settingslib.spaprivileged.model.app.userId
|
||||
import com.android.settingslib.spaprivileged.model.enterprise.Restrictions
|
||||
import com.android.settingslib.spaprivileged.template.scaffold.RestrictedMenuItem
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
@Composable
|
||||
@@ -44,13 +54,11 @@ fun AppInfoSettingsMoreOptions(
|
||||
packageManagers: IPackageManagers = PackageManagers,
|
||||
) {
|
||||
val state = app.produceState(packageManagers).value ?: return
|
||||
when {
|
||||
// We don't allow uninstalling update for DO/PO if it's a system app, because it will clear
|
||||
// data on all users. We also don't allow uninstalling for all users if it's DO/PO for any
|
||||
// user.
|
||||
state.isProfileOrDeviceOwner -> return
|
||||
!state.shownUninstallUpdates && !state.shownUninstallForAllUsers -> return
|
||||
}
|
||||
var restrictedSettingsAllowed by rememberSaveable { mutableStateOf(false) }
|
||||
if (!state.shownUninstallUpdates &&
|
||||
!state.shownUninstallForAllUsers &&
|
||||
!(state.shouldShowAccessRestrictedSettings && !restrictedSettingsAllowed)
|
||||
) return
|
||||
MoreOptionsAction {
|
||||
val restrictions =
|
||||
Restrictions(userId = app.userId, keys = listOf(UserManager.DISALLOW_APPS_CONTROL))
|
||||
@@ -70,13 +78,37 @@ fun AppInfoSettingsMoreOptions(
|
||||
packageInfoPresenter.startUninstallActivity(forAllUsers = true)
|
||||
}
|
||||
}
|
||||
if (state.shouldShowAccessRestrictedSettings && !restrictedSettingsAllowed) {
|
||||
MenuItem(text = stringResource(R.string.app_restricted_settings_lockscreen_title)) {
|
||||
app.allowRestrictedSettings(packageInfoPresenter.context) {
|
||||
restrictedSettingsAllowed = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun ApplicationInfo.allowRestrictedSettings(context: Context, onSuccess: () -> Unit) {
|
||||
AppInfoDashboardFragment.showLockScreen(context) {
|
||||
context.appOpsManager.setMode(
|
||||
AppOpsManager.OP_ACCESS_RESTRICTED_SETTINGS,
|
||||
uid,
|
||||
packageName,
|
||||
AppOpsManager.MODE_ALLOWED,
|
||||
)
|
||||
onSuccess()
|
||||
val toastString = context.getString(
|
||||
R.string.toast_allows_restricted_settings_successfully,
|
||||
loadLabel(context.packageManager),
|
||||
)
|
||||
Toast.makeText(context, toastString, Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}
|
||||
|
||||
private data class AppInfoSettingsMoreOptionsState(
|
||||
val isProfileOrDeviceOwner: Boolean,
|
||||
val shownUninstallUpdates: Boolean,
|
||||
val shownUninstallForAllUsers: Boolean,
|
||||
val shouldShowAccessRestrictedSettings: Boolean,
|
||||
)
|
||||
|
||||
@Composable
|
||||
@@ -86,20 +118,40 @@ private fun ApplicationInfo.produceState(
|
||||
val context = LocalContext.current
|
||||
return produceState<AppInfoSettingsMoreOptionsState?>(initialValue = null, this) {
|
||||
withContext(Dispatchers.IO) {
|
||||
value = AppInfoSettingsMoreOptionsState(
|
||||
isProfileOrDeviceOwner = Utils.isProfileOrDeviceOwner(
|
||||
context.userManager, context.devicePolicyManager, packageName
|
||||
),
|
||||
shownUninstallUpdates = isShowUninstallUpdates(context),
|
||||
shownUninstallForAllUsers = isShowUninstallForAllUsers(
|
||||
userManager = context.userManager,
|
||||
packageManagers = packageManagers,
|
||||
),
|
||||
)
|
||||
value = getMoreOptionsState(context, packageManagers)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun ApplicationInfo.getMoreOptionsState(
|
||||
context: Context,
|
||||
packageManagers: IPackageManagers,
|
||||
) = coroutineScope {
|
||||
val shownUninstallUpdatesDeferred = async {
|
||||
isShowUninstallUpdates(context)
|
||||
}
|
||||
val shownUninstallForAllUsersDeferred = async {
|
||||
isShowUninstallForAllUsers(
|
||||
userManager = context.userManager,
|
||||
packageManagers = packageManagers,
|
||||
)
|
||||
}
|
||||
val shouldShowAccessRestrictedSettingsDeferred = async {
|
||||
shouldShowAccessRestrictedSettings(context.appOpsManager)
|
||||
}
|
||||
val isProfileOrDeviceOwner =
|
||||
Utils.isProfileOrDeviceOwner(context.userManager, context.devicePolicyManager, packageName)
|
||||
AppInfoSettingsMoreOptionsState(
|
||||
// We don't allow uninstalling update for DO/PO if it's a system app, because it will clear
|
||||
// data on all users.
|
||||
shownUninstallUpdates = !isProfileOrDeviceOwner && shownUninstallUpdatesDeferred.await(),
|
||||
// We also don't allow uninstalling for all users if it's DO/PO for any user.
|
||||
shownUninstallForAllUsers =
|
||||
!isProfileOrDeviceOwner && shownUninstallForAllUsersDeferred.await(),
|
||||
shouldShowAccessRestrictedSettings = shouldShowAccessRestrictedSettingsDeferred.await(),
|
||||
)
|
||||
}
|
||||
|
||||
private fun ApplicationInfo.isShowUninstallUpdates(context: Context): Boolean =
|
||||
isUpdatedSystemApp && context.userManager.isUserAdmin(userId) &&
|
||||
!context.resources.getBoolean(R.bool.config_disable_uninstall_update)
|
||||
@@ -116,3 +168,8 @@ private fun ApplicationInfo.isOtherUserHasInstallPackage(
|
||||
): Boolean = userManager.aliveUsers
|
||||
.filter { it.id != userId }
|
||||
.any { packageManagers.isPackageInstalledAsUser(packageName, it.id) }
|
||||
|
||||
private fun ApplicationInfo.shouldShowAccessRestrictedSettings(appOpsManager: AppOpsManager) =
|
||||
appOpsManager.noteOpNoThrow(
|
||||
AppOpsManager.OP_ACCESS_RESTRICTED_SETTINGS, uid, packageName, null, null
|
||||
) == AppOpsManager.MODE_IGNORED
|
||||
|
||||
Reference in New Issue
Block a user