1 /* 2 * Copyright (C) 2018 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 17 package com.android.settings.security; 18 19 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_SOMETHING; 20 21 import android.content.Context; 22 import android.os.UserHandle; 23 import android.os.UserManager; 24 import android.support.v7.preference.Preference; 25 import android.support.v7.preference.PreferenceScreen; 26 import android.util.Log; 27 28 import com.android.internal.widget.LockPatternUtils; 29 import com.android.settings.Utils; 30 import com.android.settings.core.TogglePreferenceController; 31 import com.android.settings.overlay.FeatureFactory; 32 import com.android.settingslib.core.lifecycle.Lifecycle; 33 import com.android.settingslib.core.lifecycle.LifecycleObserver; 34 import com.android.settingslib.core.lifecycle.events.OnResume; 35 36 import java.util.concurrent.ExecutionException; 37 import java.util.concurrent.FutureTask; 38 39 public class VisiblePatternProfilePreferenceController extends TogglePreferenceController 40 implements LifecycleObserver, OnResume { 41 42 private static final String KEY_VISIBLE_PATTERN_PROFILE = "visiblepattern_profile"; 43 private static final String TAG = "VisPtnProfPrefCtrl"; 44 45 private final LockPatternUtils mLockPatternUtils; 46 private final UserManager mUm; 47 private final int mUserId = UserHandle.myUserId(); 48 private final int mProfileChallengeUserId; 49 50 private Preference mPreference; 51 52 public VisiblePatternProfilePreferenceController(Context context) { 53 this(context, null /* lifecycle */); 54 } 55 56 // TODO (b/73074893) Replace this constructor without Lifecycle using setter method instead. 57 public VisiblePatternProfilePreferenceController(Context context, Lifecycle lifecycle) { 58 super(context, KEY_VISIBLE_PATTERN_PROFILE); 59 mUm = (UserManager) context.getSystemService(Context.USER_SERVICE); 60 mLockPatternUtils = FeatureFactory.getFactory(context) 61 .getSecurityFeatureProvider() 62 .getLockPatternUtils(context); 63 mProfileChallengeUserId = Utils.getManagedProfileId(mUm, mUserId); 64 if (lifecycle != null) { 65 lifecycle.addObserver(this); 66 } 67 } 68 69 @Override 70 public int getAvailabilityStatus() { 71 final FutureTask<Integer> futureTask = new FutureTask<>( 72 // Put the API call in a future to avoid StrictMode violation. 73 () -> { 74 final boolean isSecure = mLockPatternUtils.isSecure(mProfileChallengeUserId); 75 final boolean hasPassword = mLockPatternUtils 76 .getKeyguardStoredPasswordQuality(mProfileChallengeUserId) 77 == PASSWORD_QUALITY_SOMETHING; 78 if (isSecure && hasPassword) { 79 return AVAILABLE; 80 } 81 return DISABLED_FOR_USER; 82 }); 83 try { 84 futureTask.run(); 85 return futureTask.get(); 86 } catch (InterruptedException | ExecutionException e) { 87 Log.w(TAG, "Error getting lock pattern state."); 88 return DISABLED_FOR_USER; 89 } 90 } 91 92 @Override 93 public boolean isChecked() { 94 return mLockPatternUtils.isVisiblePatternEnabled( 95 mProfileChallengeUserId); 96 } 97 98 @Override 99 public boolean setChecked(boolean isChecked) { 100 if (Utils.startQuietModeDialogIfNecessary(mContext, mUm, mProfileChallengeUserId)) { 101 return false; 102 } 103 mLockPatternUtils.setVisiblePatternEnabled(isChecked, mProfileChallengeUserId); 104 return true; 105 } 106 107 @Override 108 public void displayPreference(PreferenceScreen screen) { 109 super.displayPreference(screen); 110 mPreference = screen.findPreference(getPreferenceKey()); 111 } 112 113 @Override 114 public void onResume() { 115 mPreference.setVisible(isAvailable()); 116 } 117 } 118