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 21 import androidx.annotation.VisibleForTesting; 22 import androidx.fragment.app.Fragment; 23 import androidx.preference.Preference; 24 import androidx.preference.Preference.OnPreferenceClickListener; 25 import androidx.preference.PreferenceScreen; 26 27 import com.android.internal.widget.LockPatternUtils; 28 import com.android.settings.core.PreferenceControllerMixin; 29 import com.android.settings.users.OwnerInfoSettings; 30 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 31 import com.android.settingslib.RestrictedLockUtilsInternal; 32 import com.android.settingslib.RestrictedPreference; 33 import com.android.settingslib.core.AbstractPreferenceController; 34 import com.android.settingslib.core.lifecycle.Lifecycle; 35 import com.android.settingslib.core.lifecycle.LifecycleObserver; 36 import com.android.settingslib.core.lifecycle.events.OnResume; 37 38 public class OwnerInfoPreferenceController extends AbstractPreferenceController 39 implements PreferenceControllerMixin, LifecycleObserver, OnResume { 40 41 private 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 Fragment 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, Fragment parent, Lifecycle lifecycle )53 public OwnerInfoPreferenceController(Context context, Fragment parent, Lifecycle lifecycle ) { 54 super(context); 55 mParent = parent; 56 mLockPatternUtils = new LockPatternUtils(context); 57 if (lifecycle != null) { 58 lifecycle.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 updateEnableState()83 public void updateEnableState() { 84 if (mOwnerInfoPref == null) { 85 return; 86 } 87 if (isDeviceOwnerInfoEnabled()) { 88 EnforcedAdmin admin = getDeviceOwner(); 89 mOwnerInfoPref.setDisabledByAdmin(admin); 90 } else { 91 mOwnerInfoPref.setDisabledByAdmin(null); 92 mOwnerInfoPref.setEnabled(!mLockPatternUtils.isLockScreenDisabled(MY_USER_ID)); 93 if (mOwnerInfoPref.isEnabled()) { 94 mOwnerInfoPref.setOnPreferenceClickListener( 95 new OnPreferenceClickListener() { 96 @Override 97 public boolean onPreferenceClick(Preference preference) { 98 OwnerInfoSettings.show(mParent); 99 return true; 100 } 101 }); 102 } 103 } 104 } 105 updateSummary()106 public void updateSummary() { 107 if (mOwnerInfoPref != null) { 108 if (isDeviceOwnerInfoEnabled()) { 109 mOwnerInfoPref.setSummary( 110 getDeviceOwnerInfo()); 111 } else { 112 mOwnerInfoPref.setSummary(isOwnerInfoEnabled() 113 ? getOwnerInfo() 114 : mContext.getString( 115 com.android.settings.R.string.owner_info_settings_summary)); 116 } 117 } 118 } 119 120 // Wrapper methods to allow testing 121 @VisibleForTesting isDeviceOwnerInfoEnabled()122 boolean isDeviceOwnerInfoEnabled() { 123 return mLockPatternUtils.isDeviceOwnerInfoEnabled(); 124 } 125 126 @VisibleForTesting getDeviceOwnerInfo()127 String getDeviceOwnerInfo() { 128 return mLockPatternUtils.getDeviceOwnerInfo(); 129 } 130 131 @VisibleForTesting isOwnerInfoEnabled()132 boolean isOwnerInfoEnabled() { 133 return mLockPatternUtils.isOwnerInfoEnabled(MY_USER_ID); 134 } 135 136 @VisibleForTesting getOwnerInfo()137 String getOwnerInfo() { 138 return mLockPatternUtils.getOwnerInfo(MY_USER_ID); 139 } 140 141 @VisibleForTesting getDeviceOwner()142 EnforcedAdmin getDeviceOwner() { 143 return RestrictedLockUtilsInternal.getDeviceOwner(mContext); 144 } 145 } 146