1 /*
2  * Copyright (C) 2018 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.accounts;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.provider.Settings;
22 
23 import androidx.annotation.VisibleForTesting;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settings.enterprise.EnterprisePrivacyFeatureProvider;
29 import com.android.settings.overlay.FeatureFactory;
30 import com.android.settingslib.widget.FooterPreference;
31 
32 public class EnterpriseDisclosurePreferenceController extends BasePreferenceController {
33     private final EnterprisePrivacyFeatureProvider mFeatureProvider;
34 
EnterpriseDisclosurePreferenceController(Context context, String key)35     public EnterpriseDisclosurePreferenceController(Context context, String key) {
36         // Preference key doesn't matter as we are creating the preference in code.
37         super(context, key);
38         mFeatureProvider = FeatureFactory.getFeatureFactory()
39                 .getEnterprisePrivacyFeatureProvider();
40     }
41 
42     @Override
displayPreference(PreferenceScreen screen)43     public void displayPreference(PreferenceScreen screen) {
44         super.displayPreference(screen);
45         final CharSequence disclosure = getDisclosure();
46         if (disclosure == null) {
47             return;
48         }
49         updateFooterPreference(screen, disclosure);
50     }
51 
52     @Override
getAvailabilityStatus()53     public int getAvailabilityStatus() {
54         if (getDisclosure() == null) {
55             return UNSUPPORTED_ON_DEVICE;
56         }
57         return AVAILABLE;
58     }
59 
60     @VisibleForTesting
getDisclosure()61     CharSequence getDisclosure() {
62         return mFeatureProvider.getDeviceOwnerDisclosure();
63     }
64 
updateFooterPreference(PreferenceScreen screen, CharSequence disclosure)65     void updateFooterPreference(PreferenceScreen screen, CharSequence disclosure) {
66         final FooterPreference footerPreference = screen.findPreference(getPreferenceKey());
67         footerPreference.setTitle(disclosure);
68         footerPreference.setLearnMoreAction(view -> {
69             mContext.startActivity(new Intent(Settings.ACTION_ENTERPRISE_PRIVACY_SETTINGS)
70                     .setPackage(mContext.getPackageName()));
71         });
72         final String learnMoreText = mContext.getString(
73                 R.string.footer_learn_more_content_description, getLabelName());
74         footerPreference.setLearnMoreText(learnMoreText);
75     }
76 
getLabelName()77     private String getLabelName() {
78         return mContext.getString(R.string.header_add_an_account);
79     }
80 }
81