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