1 /*
2  * Copyright (C) 2021 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.settings.enterprise;
18 
19 import static android.app.admin.DevicePolicyManager.DEVICE_OWNER_TYPE_FINANCED;
20 
21 import android.app.admin.DevicePolicyManager;
22 import android.content.Context;
23 
24 import androidx.preference.Preference;
25 
26 import com.android.settings.R;
27 import com.android.settings.overlay.FeatureFactory;
28 
29 import java.util.Objects;
30 
31 /** Helper class for the privacy preference in Settings for a managed device. */
32 class PrivacyPreferenceControllerHelper {
33 
34     private final Context mContext;
35     private final EnterprisePrivacyFeatureProvider mFeatureProvider;
36     private final DevicePolicyManager mDevicePolicyManager;
37 
PrivacyPreferenceControllerHelper(Context context)38     PrivacyPreferenceControllerHelper(Context context) {
39         mContext = Objects.requireNonNull(context);
40         mFeatureProvider = FeatureFactory.getFactory(context)
41                 .getEnterprisePrivacyFeatureProvider(context);
42         mDevicePolicyManager = context.getSystemService(DevicePolicyManager.class);
43     }
44 
45     /** Updates the privacy preference summary. */
updateState(Preference preference)46     void updateState(Preference preference) {
47         if (preference == null) {
48             return;
49         }
50 
51         final String organizationName = mFeatureProvider.getDeviceOwnerOrganizationName();
52         if (organizationName == null) {
53             preference.setSummary(R.string.enterprise_privacy_settings_summary_generic);
54         } else {
55             preference.setSummary(mContext.getResources().getString(
56                     R.string.enterprise_privacy_settings_summary_with_name, organizationName));
57         }
58     }
59 
60     /** Returns {@code true} if the device has a device owner. */
hasDeviceOwner()61     boolean hasDeviceOwner() {
62         return mFeatureProvider.hasDeviceOwner();
63     }
64 
isFinancedDevice()65     boolean isFinancedDevice() {
66         return mDevicePolicyManager.isDeviceManaged() && mDevicePolicyManager.getDeviceOwnerType(
67                 mDevicePolicyManager.getDeviceOwnerComponentOnAnyUser())
68                 == DEVICE_OWNER_TYPE_FINANCED;
69     }
70 }
71