1 /* 2 * Copyright (C) 2014 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.tv.settings.users; 18 19 import android.app.Fragment; 20 import android.app.admin.DevicePolicyManager; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.preference.PreferenceManager; 24 25 import com.android.tv.settings.dialog.PinDialogFragment; 26 27 public class RestrictedProfilePinDialogFragment extends PinDialogFragment { 28 29 public interface Callback extends ResultListener { 30 /** 31 * Save the PIN password for the profile 32 * @param pin Password to save 33 * @param originalPin Previously saved password, or null if no password was previously set 34 * @param quality Password quality, see {@link DevicePolicyManager} 35 */ saveLockPassword(String pin, String originalPin, int quality)36 void saveLockPassword(String pin, String originalPin, int quality); 37 38 /** 39 * Clear the PIN password 40 * @param oldPin Current PIN password (required) 41 */ clearLockPassword(String oldPin)42 void clearLockPassword(String oldPin); 43 44 /** 45 * Check the PIN password 46 * @param password Password to check 47 * @return {@code True} if password is correct 48 */ checkPassword(String password)49 boolean checkPassword(String password); 50 51 /** 52 * Query if there is a password set 53 * @return {@code True} if password is set 54 */ hasLockscreenSecurity()55 boolean hasLockscreenSecurity(); 56 } 57 58 private static final String PREF_DISABLE_PIN_UNTIL = 59 "RestrictedProfileActivity$RestrictedProfilePinDialogFragment.disable_pin_until"; 60 newInstance(@inDialogType int type)61 public static RestrictedProfilePinDialogFragment newInstance(@PinDialogType int type) { 62 RestrictedProfilePinDialogFragment fragment = new RestrictedProfilePinDialogFragment(); 63 final Bundle b = new Bundle(1); 64 b.putInt(PinDialogFragment.ARG_TYPE, type); 65 fragment.setArguments(b); 66 return fragment; 67 } 68 69 /** 70 * Returns the time until we should disable the PIN dialog (because the user input wrong 71 * PINs repeatedly). 72 */ getDisablePinUntil(Context context)73 public static long getDisablePinUntil(Context context) { 74 return PreferenceManager.getDefaultSharedPreferences(context).getLong( 75 PREF_DISABLE_PIN_UNTIL, 0); 76 } 77 78 /** 79 * Saves the time until we should disable the PIN dialog (because the user input wrong PINs 80 * repeatedly). 81 */ setDisablePinUntil(Context context, long timeMillis)82 public static void setDisablePinUntil(Context context, long timeMillis) { 83 PreferenceManager.getDefaultSharedPreferences(context).edit().putLong( 84 PREF_DISABLE_PIN_UNTIL, timeMillis).apply(); 85 } 86 87 @Override getPinDisabledUntil()88 public long getPinDisabledUntil() { 89 return getDisablePinUntil(getActivity()); 90 } 91 92 @Override setPinDisabledUntil(long retryDisableTimeout)93 public void setPinDisabledUntil(long retryDisableTimeout) { 94 setDisablePinUntil(getActivity(), retryDisableTimeout); 95 } 96 97 @Override setPin(String pin, String originalPin)98 public void setPin(String pin, String originalPin) { 99 Callback callback = null; 100 101 Fragment f = getTargetFragment(); 102 if (f instanceof Callback) { 103 callback = (Callback) f; 104 } 105 106 if (callback == null && getActivity() instanceof Callback) { 107 callback = (Callback) getActivity(); 108 } 109 110 if (callback != null) { 111 callback.saveLockPassword(pin, originalPin, 112 DevicePolicyManager.PASSWORD_QUALITY_NUMERIC); 113 } 114 } 115 116 @Override deletePin(String oldPin)117 public void deletePin(String oldPin) { 118 Callback callback = null; 119 120 Fragment f = getTargetFragment(); 121 if (f instanceof Callback) { 122 callback = (Callback) f; 123 } 124 125 if (callback == null && getActivity() instanceof Callback) { 126 callback = (Callback) getActivity(); 127 } 128 129 if (callback != null) { 130 callback.clearLockPassword(oldPin); 131 } 132 } 133 134 @Override isPinCorrect(String pin)135 public boolean isPinCorrect(String pin) { 136 Callback callback = null; 137 138 Fragment f = getTargetFragment(); 139 if (f instanceof Callback) { 140 callback = (Callback) f; 141 } 142 143 if (callback == null && getActivity() instanceof Callback) { 144 callback = (Callback) getActivity(); 145 } 146 147 if (callback != null) { 148 return callback.checkPassword(pin); 149 } 150 return false; 151 } 152 153 @Override isPinSet()154 public boolean isPinSet() { 155 Callback callback = null; 156 157 Fragment f = getTargetFragment(); 158 if (f instanceof Callback) { 159 callback = (Callback) f; 160 } 161 162 if (callback == null && getActivity() instanceof Callback) { 163 callback = (Callback) getActivity(); 164 } 165 166 if (callback != null) { 167 return callback.hasLockscreenSecurity(); 168 } else { 169 throw new IllegalStateException("Can't call isPinSet when not attached"); 170 } 171 } 172 } 173