Home | History | Annotate | Download | only in users
      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.Activity;
     20 import android.app.Fragment;
     21 import android.app.admin.DevicePolicyManager;
     22 import android.content.Context;
     23 import android.os.Bundle;
     24 import android.os.UserHandle;
     25 import android.preference.PreferenceManager;
     26 
     27 import com.android.tv.settings.dialog.PinDialogFragment;
     28 import com.android.tv.settings.dialog.PinDialogFragment.ResultListener;
     29 
     30 public class RestrictedProfilePinDialogFragment extends PinDialogFragment {
     31 
     32     public interface Callback extends ResultListener {
     33         void saveLockPassword(String pin, int quality);
     34         boolean checkPassword(String password, int userId);
     35         boolean hasLockscreenSecurity();
     36     }
     37 
     38     private static final String PREF_DISABLE_PIN_UNTIL =
     39             "RestrictedProfileActivity$RestrictedProfilePinDialogFragment.disable_pin_until";
     40 
     41     public static RestrictedProfilePinDialogFragment newInstance(int type) {
     42         RestrictedProfilePinDialogFragment fragment = new RestrictedProfilePinDialogFragment();
     43         final Bundle b = new Bundle(1);
     44         b.putInt(PinDialogFragment.ARG_TYPE, type);
     45         fragment.setArguments(b);
     46         return fragment;
     47     }
     48 
     49     /**
     50      * Returns the time until we should disable the PIN dialog (because the user input wrong
     51      * PINs repeatedly).
     52      */
     53     public static long getDisablePinUntil(Context context) {
     54         return PreferenceManager.getDefaultSharedPreferences(context).getLong(
     55                 PREF_DISABLE_PIN_UNTIL, 0);
     56     }
     57 
     58     /**
     59      * Saves the time until we should disable the PIN dialog (because the user input wrong PINs
     60      * repeatedly).
     61      */
     62     public static void setDisablePinUntil(Context context, long timeMillis) {
     63         PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(
     64                 PREF_DISABLE_PIN_UNTIL, timeMillis).apply();
     65     }
     66 
     67     @Override
     68     public long getPinDisabledUntil() {
     69         return getDisablePinUntil(getActivity());
     70     }
     71 
     72     @Override
     73     public void setPinDisabledUntil(long retryDisableTimeout) {
     74         setDisablePinUntil(getActivity(), retryDisableTimeout);
     75     }
     76 
     77     @Override
     78     public void setPin(String pin) {
     79         Callback callback = null;
     80 
     81         Fragment f = getTargetFragment();
     82         if (f instanceof Callback) {
     83             callback = (Callback) f;
     84         }
     85 
     86         if (callback == null && getActivity() instanceof Callback) {
     87             callback = (Callback) getActivity();
     88         }
     89 
     90         if (callback != null) {
     91             callback.saveLockPassword(pin, DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
     92         }
     93     }
     94 
     95     @Override
     96     public boolean isPinCorrect(String pin) {
     97         Callback callback = null;
     98 
     99         Fragment f = getTargetFragment();
    100         if (f instanceof Callback) {
    101             callback = (Callback) f;
    102         }
    103 
    104         if (callback == null && getActivity() instanceof Callback) {
    105             callback = (Callback) getActivity();
    106         }
    107 
    108         if (callback != null) {
    109             return callback.checkPassword(pin, UserHandle.USER_OWNER);
    110         }
    111         return false;
    112     }
    113 
    114     @Override
    115     public boolean isPinSet() {
    116         Callback callback = null;
    117 
    118         Fragment f = getTargetFragment();
    119         if (f instanceof Callback) {
    120             callback = (Callback) f;
    121         }
    122 
    123         if (callback == null && getActivity() instanceof Callback) {
    124             callback = (Callback) getActivity();
    125         }
    126 
    127         if (callback != null) {
    128             return UserHandle.myUserId() != UserHandle.USER_OWNER ||
    129                     callback.hasLockscreenSecurity();
    130         } else {
    131             throw new IllegalStateException("Can't call isPinSet when not attached");
    132         }
    133     }
    134 }
    135