• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 package com.android.settings.security;
17 
18 import android.app.Fragment;
19 import android.content.Context;
20 import android.os.UserHandle;
21 import android.support.annotation.VisibleForTesting;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.Preference.OnPreferenceClickListener;
24 import android.support.v7.preference.PreferenceScreen;
25 
26 import com.android.internal.widget.LockPatternUtils;
27 import com.android.settings.core.PreferenceControllerMixin;
28 import com.android.settings.users.OwnerInfoSettings;
29 import com.android.settingslib.RestrictedLockUtils;
30 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
31 import com.android.settingslib.RestrictedPreference;
32 import com.android.settingslib.core.AbstractPreferenceController;
33 import com.android.settingslib.core.lifecycle.Lifecycle;
34 import com.android.settingslib.core.lifecycle.LifecycleObserver;
35 import com.android.settingslib.core.lifecycle.events.OnResume;
36 
37 public class OwnerInfoPreferenceController extends AbstractPreferenceController
38         implements PreferenceControllerMixin, LifecycleObserver, OnResume {
39 
40     private static final String KEY_OWNER_INFO = "owner_info_settings";
41     private static final int MY_USER_ID = UserHandle.myUserId();
42 
43     private final LockPatternUtils mLockPatternUtils;
44     private final Fragment mParent;
45     private RestrictedPreference mOwnerInfoPref;
46 
47     // Container fragment should implement this in order to show the correct summary
48     public interface OwnerInfoCallback {
onOwnerInfoUpdated()49         void onOwnerInfoUpdated();
50     }
51 
OwnerInfoPreferenceController(Context context, Fragment parent, Lifecycle lifecycle )52     public OwnerInfoPreferenceController(Context context, Fragment parent, Lifecycle lifecycle ) {
53         super(context);
54         mParent = parent;
55         mLockPatternUtils = new LockPatternUtils(context);
56         if (lifecycle != null) {
57             lifecycle.addObserver(this);
58         }
59     }
60 
61     @Override
displayPreference(PreferenceScreen screen)62     public void displayPreference(PreferenceScreen screen) {
63         mOwnerInfoPref  = (RestrictedPreference) screen.findPreference(KEY_OWNER_INFO);
64     }
65 
66     @Override
onResume()67     public void onResume() {
68         updateEnableState();
69         updateSummary();
70     }
71 
72     @Override
isAvailable()73     public boolean isAvailable() {
74         return true;
75     }
76 
77     @Override
getPreferenceKey()78     public String getPreferenceKey() {
79         return KEY_OWNER_INFO;
80     }
81 
updateEnableState()82     public void updateEnableState() {
83         if (mOwnerInfoPref == null) {
84             return;
85         }
86         if (isDeviceOwnerInfoEnabled()) {
87             EnforcedAdmin admin = getDeviceOwner();
88             mOwnerInfoPref.setDisabledByAdmin(admin);
89         } else {
90             mOwnerInfoPref.setDisabledByAdmin(null);
91             mOwnerInfoPref.setEnabled(!mLockPatternUtils.isLockScreenDisabled(MY_USER_ID));
92             if (mOwnerInfoPref.isEnabled()) {
93                 mOwnerInfoPref.setOnPreferenceClickListener(
94                     new OnPreferenceClickListener() {
95                         @Override
96                         public boolean onPreferenceClick(Preference preference) {
97                             OwnerInfoSettings.show(mParent);
98                             return true;
99                         }
100                     });
101             }
102         }
103     }
104 
updateSummary()105     public void updateSummary() {
106         if (mOwnerInfoPref != null) {
107             if (isDeviceOwnerInfoEnabled()) {
108                 mOwnerInfoPref.setSummary(
109                     getDeviceOwnerInfo());
110             } else {
111                 mOwnerInfoPref.setSummary(isOwnerInfoEnabled()
112                     ? getOwnerInfo()
113                     : mContext.getString(
114                         com.android.settings.R.string.owner_info_settings_summary));
115             }
116         }
117     }
118 
119     // Wrapper methods to allow testing
120     @VisibleForTesting
isDeviceOwnerInfoEnabled()121     boolean isDeviceOwnerInfoEnabled() {
122         return mLockPatternUtils.isDeviceOwnerInfoEnabled();
123     }
124 
125     @VisibleForTesting
getDeviceOwnerInfo()126     String getDeviceOwnerInfo() {
127         return mLockPatternUtils.getDeviceOwnerInfo();
128     }
129 
130     @VisibleForTesting
isOwnerInfoEnabled()131     boolean isOwnerInfoEnabled() {
132         return mLockPatternUtils.isOwnerInfoEnabled(MY_USER_ID);
133     }
134 
135     @VisibleForTesting
getOwnerInfo()136     String getOwnerInfo() {
137         return mLockPatternUtils.getOwnerInfo(MY_USER_ID);
138     }
139 
140     @VisibleForTesting
getDeviceOwner()141     EnforcedAdmin getDeviceOwner() {
142         return RestrictedLockUtils.getDeviceOwner(mContext);
143     }
144 }
145