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