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.content.Context; 19 import android.os.UserHandle; 20 import android.text.TextUtils; 21 22 import androidx.annotation.VisibleForTesting; 23 import androidx.preference.Preference; 24 import androidx.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.EnforcedAdmin; 30 import com.android.settingslib.RestrictedLockUtilsInternal; 31 import com.android.settingslib.RestrictedPreference; 32 import com.android.settingslib.core.AbstractPreferenceController; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.ObservablePreferenceFragment; 35 import com.android.settingslib.core.lifecycle.events.OnResume; 36 37 public class OwnerInfoPreferenceController extends AbstractPreferenceController 38 implements PreferenceControllerMixin, LifecycleObserver, OnResume { 39 40 @VisibleForTesting 41 static final String KEY_OWNER_INFO = "owner_info_settings"; 42 private static final int MY_USER_ID = UserHandle.myUserId(); 43 44 private final LockPatternUtils mLockPatternUtils; 45 private final ObservablePreferenceFragment mParent; 46 private RestrictedPreference mOwnerInfoPref; 47 48 // Container fragment should implement this in order to show the correct summary 49 public interface OwnerInfoCallback { onOwnerInfoUpdated()50 void onOwnerInfoUpdated(); 51 } 52 OwnerInfoPreferenceController(Context context, ObservablePreferenceFragment parent)53 public OwnerInfoPreferenceController(Context context, ObservablePreferenceFragment parent) { 54 super(context); 55 mParent = parent; 56 mLockPatternUtils = new LockPatternUtils(context); 57 if (parent != null) { 58 parent.getSettingsLifecycle().addObserver(this); 59 } 60 } 61 62 @Override displayPreference(PreferenceScreen screen)63 public void displayPreference(PreferenceScreen screen) { 64 mOwnerInfoPref = screen.findPreference(KEY_OWNER_INFO); 65 } 66 67 @Override onResume()68 public void onResume() { 69 updateEnableState(); 70 updateSummary(); 71 } 72 73 @Override isAvailable()74 public boolean isAvailable() { 75 return true; 76 } 77 78 @Override getPreferenceKey()79 public String getPreferenceKey() { 80 return KEY_OWNER_INFO; 81 } 82 83 @Override handlePreferenceTreeClick(Preference preference)84 public boolean handlePreferenceTreeClick(Preference preference) { 85 if (TextUtils.equals(getPreferenceKey(), preference.getKey())) { 86 OwnerInfoSettings.show(mParent); 87 return true; 88 } 89 return false; 90 } 91 updateEnableState()92 public void updateEnableState() { 93 if (mOwnerInfoPref == null) { 94 return; 95 } 96 if (isDeviceOwnerInfoEnabled()) { 97 EnforcedAdmin admin = getDeviceOwner(); 98 mOwnerInfoPref.setDisabledByAdmin(admin); 99 } else { 100 mOwnerInfoPref.setDisabledByAdmin(null); 101 mOwnerInfoPref.setEnabled(!mLockPatternUtils.isLockScreenDisabled(MY_USER_ID)); 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 RestrictedLockUtilsInternal.getDeviceOwner(mContext); 143 } 144 } 145