Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 41544c1f authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Move RestrictedModes to separate file" into main

parents 23d83493 676aa53d
Loading
Loading
Loading
Loading
+8 −6
Original line number Diff line number Diff line
@@ -16,20 +16,22 @@

package com.android.settingslib.spaprivileged.model.enterprise

import android.app.admin.DevicePolicyManager
import android.app.admin.DevicePolicyResources.Strings.Settings.PERSONAL_CATEGORY_HEADER
import android.app.admin.DevicePolicyResources.Strings.Settings.PRIVATE_CATEGORY_HEADER
import android.app.admin.DevicePolicyResources.Strings.Settings.WORK_CATEGORY_HEADER
import android.content.Context
import android.content.pm.UserInfo
import com.android.settingslib.R
import com.android.settingslib.spaprivileged.framework.common.devicePolicyManager

class EnterpriseRepository(private val context: Context) {
    private val resources by lazy {
        checkNotNull(context.getSystemService(DevicePolicyManager::class.java)).resources
interface IEnterpriseRepository {
    fun getEnterpriseString(updatableStringId: String, resId: Int): String
}

    fun getEnterpriseString(updatableStringId: String, resId: Int): String =
class EnterpriseRepository(private val context: Context) : IEnterpriseRepository {
    private val resources by lazy { context.devicePolicyManager.resources }

    override fun getEnterpriseString(updatableStringId: String, resId: Int): String =
        checkNotNull(resources.getString(updatableStringId) { context.getString(resId) })

    fun getProfileTitle(userInfo: UserInfo): String = if (userInfo.isManagedProfile) {
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source 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.android.settingslib.spaprivileged.model.enterprise

import android.app.admin.DevicePolicyResources.Strings.Settings
import android.content.Context
import com.android.settingslib.RestrictedLockUtils
import com.android.settingslib.widget.restricted.R

sealed interface RestrictedMode

data object NoRestricted : RestrictedMode

data object BaseUserRestricted : RestrictedMode

interface BlockedByAdmin : RestrictedMode {
    fun getSummary(checked: Boolean?): String
    fun sendShowAdminSupportDetailsIntent()
}

internal data class BlockedByAdminImpl(
    private val context: Context,
    private val enforcedAdmin: RestrictedLockUtils.EnforcedAdmin,
    private val enterpriseRepository: IEnterpriseRepository = EnterpriseRepository(context),
) : BlockedByAdmin {
    override fun getSummary(checked: Boolean?) = when (checked) {
        true -> enterpriseRepository.getEnterpriseString(
            updatableStringId = Settings.ENABLED_BY_ADMIN_SWITCH_SUMMARY,
            resId = R.string.enabled_by_admin,
        )

        false -> enterpriseRepository.getEnterpriseString(
            updatableStringId = Settings.DISABLED_BY_ADMIN_SWITCH_SUMMARY,
            resId = R.string.disabled_by_admin,
        )

        else -> ""
    }

    override fun sendShowAdminSupportDetailsIntent() {
        RestrictedLockUtils.sendShowAdminSupportDetailsIntent(context, enforcedAdmin)
    }
}
+0 −40
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.settingslib.spaprivileged.model.enterprise

import android.app.admin.DevicePolicyResources.Strings.Settings
import android.content.Context
import android.os.UserHandle
import android.os.UserManager
@@ -25,55 +24,16 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.settingslib.RestrictedLockUtils
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin
import com.android.settingslib.RestrictedLockUtilsInternal
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import com.android.settingslib.widget.restricted.R

data class Restrictions(
    val userId: Int = UserHandle.myUserId(),
    val keys: List<String>,
)

sealed interface RestrictedMode

data object NoRestricted : RestrictedMode

data object BaseUserRestricted : RestrictedMode

interface BlockedByAdmin : RestrictedMode {
    fun getSummary(checked: Boolean?): String
    fun sendShowAdminSupportDetailsIntent()
}

private data class BlockedByAdminImpl(
    private val context: Context,
    private val enforcedAdmin: EnforcedAdmin,
) : BlockedByAdmin {
    private val enterpriseRepository by lazy { EnterpriseRepository(context) }

    override fun getSummary(checked: Boolean?) = when (checked) {
        true -> enterpriseRepository.getEnterpriseString(
            updatableStringId = Settings.ENABLED_BY_ADMIN_SWITCH_SUMMARY,
            resId = R.string.enabled_by_admin,
        )

        false -> enterpriseRepository.getEnterpriseString(
            updatableStringId = Settings.DISABLED_BY_ADMIN_SWITCH_SUMMARY,
            resId = R.string.disabled_by_admin,
        )

        else -> ""
    }

    override fun sendShowAdminSupportDetailsIntent() {
        RestrictedLockUtils.sendShowAdminSupportDetailsIntent(context, enforcedAdmin)
    }
}

interface RestrictionsProvider {
    @Composable
    fun restrictedModeState(): State<RestrictedMode?>
+67 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source 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.android.settingslib.spaprivileged.model.enterprise

import android.app.admin.DevicePolicyResources.Strings.Settings
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.settingslib.RestrictedLockUtils
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class RestrictedModeTest {
    private val context: Context = ApplicationProvider.getApplicationContext()

    private val fakeEnterpriseRepository = object : IEnterpriseRepository {
        override fun getEnterpriseString(updatableStringId: String, resId: Int): String =
            when (updatableStringId) {
                Settings.ENABLED_BY_ADMIN_SWITCH_SUMMARY -> ENABLED_BY_ADMIN
                Settings.DISABLED_BY_ADMIN_SWITCH_SUMMARY -> DISABLED_BY_ADMIN
                else -> ""
            }
    }

    @Test
    fun blockedByAdmin_getSummaryWhenChecked() {
        val blockedByAdmin = BlockedByAdminImpl(context, ENFORCED_ADMIN, fakeEnterpriseRepository)

        val summary = blockedByAdmin.getSummary(true)

        assertThat(summary).isEqualTo(ENABLED_BY_ADMIN)
    }

    @Test
    fun blockedByAdmin_getSummaryNotWhenChecked() {
        val blockedByAdmin = BlockedByAdminImpl(context, ENFORCED_ADMIN, fakeEnterpriseRepository)

        val summary = blockedByAdmin.getSummary(false)

        assertThat(summary).isEqualTo(DISABLED_BY_ADMIN)
    }

    private companion object {
        const val RESTRICTION = "restriction"
        val ENFORCED_ADMIN: RestrictedLockUtils.EnforcedAdmin =
            RestrictedLockUtils.EnforcedAdmin.createDefaultEnforcedAdminWithRestriction(RESTRICTION)

        const val ENABLED_BY_ADMIN = "Enabled by admin"
        const val DISABLED_BY_ADMIN = "Disabled by admin"
    }
}