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