• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.security.data.model
18 
19 import android.graphics.drawable.Drawable
20 import androidx.annotation.VisibleForTesting
21 import com.android.systemui.dagger.qualifiers.Background
22 import com.android.systemui.statusbar.policy.SecurityController
23 import kotlinx.coroutines.CoroutineDispatcher
24 import kotlinx.coroutines.withContext
25 
26 /** The security info exposed by [com.android.systemui.statusbar.policy.SecurityController]. */
27 // TODO(b/242040009): Consider splitting this model into smaller submodels.
28 data class SecurityModel(
29     val isDeviceManaged: Boolean,
30     val hasWorkProfile: Boolean,
31     val isWorkProfileOn: Boolean,
32     val isProfileOwnerOfOrganizationOwnedDevice: Boolean,
33     val deviceOwnerOrganizationName: String?,
34     val workProfileOrganizationName: String?,
35     val isNetworkLoggingEnabled: Boolean,
36     val isVpnBranded: Boolean,
37     val primaryVpnName: String?,
38     val workProfileVpnName: String?,
39     val hasCACertInCurrentUser: Boolean,
40     val hasCACertInWorkProfile: Boolean,
41     val isParentalControlsEnabled: Boolean,
42     val deviceAdminIcon: Drawable?,
43 ) {
44     companion object {
45         /** Create a [SecurityModel] from the current [securityController] state. */
createnull46         suspend fun create(
47             securityController: SecurityController,
48             @Background bgDispatcher: CoroutineDispatcher,
49         ): SecurityModel {
50             return withContext(bgDispatcher) { create(securityController) }
51         }
52 
53         /**
54          * Create a [SecurityModel] from the current [securityController] state.
55          *
56          * Important: This method should be called from a background thread as this will do a lot of
57          * binder calls.
58          */
59         @JvmStatic
60         @VisibleForTesting
createnull61         fun create(securityController: SecurityController): SecurityModel {
62             val deviceAdminInfo =
63                 if (securityController.isParentalControlsEnabled) {
64                     securityController.deviceAdminInfo
65                 } else {
66                     null
67                 }
68 
69             return SecurityModel(
70                 isDeviceManaged = securityController.isDeviceManaged,
71                 hasWorkProfile = securityController.hasWorkProfile(),
72                 isWorkProfileOn = securityController.isWorkProfileOn,
73                 isProfileOwnerOfOrganizationOwnedDevice =
74                     securityController.isProfileOwnerOfOrganizationOwnedDevice,
75                 deviceOwnerOrganizationName =
76                     securityController.deviceOwnerOrganizationName?.toString(),
77                 workProfileOrganizationName =
78                     securityController.workProfileOrganizationName?.toString(),
79                 isNetworkLoggingEnabled = securityController.isNetworkLoggingEnabled,
80                 isVpnBranded = securityController.isVpnBranded,
81                 primaryVpnName = securityController.primaryVpnName,
82                 workProfileVpnName = securityController.workProfileVpnName,
83                 hasCACertInCurrentUser = securityController.hasCACertInCurrentUser(),
84                 hasCACertInWorkProfile = securityController.hasCACertInWorkProfile(),
85                 isParentalControlsEnabled = securityController.isParentalControlsEnabled,
86                 deviceAdminIcon = securityController.getIcon(deviceAdminInfo),
87             )
88         }
89     }
90 }
91