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.content.pm.UserInfo;
23 import android.os.Bundle;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 import android.preference.PreferenceManager;
27 
28 import com.android.tv.settings.dialog.PinDialogFragment;
29 
30 public class RestrictedProfilePinDialogFragment extends PinDialogFragment {
31 
32     public interface Callback extends ResultListener {
saveLockPassword(String pin, int quality)33         void saveLockPassword(String pin, int quality);
checkPassword(String password, int userId)34         boolean checkPassword(String password, int userId);
hasLockscreenSecurity()35         boolean hasLockscreenSecurity();
36     }
37 
38     private static final String PREF_DISABLE_PIN_UNTIL =
39             "RestrictedProfileActivity$RestrictedProfilePinDialogFragment.disable_pin_until";
40 
newInstance(@inDialogType int type)41     public static RestrictedProfilePinDialogFragment newInstance(@PinDialogType 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      */
getDisablePinUntil(Context context)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      */
setDisablePinUntil(Context context, long timeMillis)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
getPinDisabledUntil()68     public long getPinDisabledUntil() {
69         return getDisablePinUntil(getActivity());
70     }
71 
72     @Override
setPinDisabledUntil(long retryDisableTimeout)73     public void setPinDisabledUntil(long retryDisableTimeout) {
74         setDisablePinUntil(getActivity(), retryDisableTimeout);
75     }
76 
77     @Override
setPin(String pin)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
isPinCorrect(String pin)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             UserInfo myUserInfo = UserManager.get(getActivity()).getUserInfo(UserHandle.myUserId());
110             return myUserInfo != null &&
111                     callback.checkPassword(pin, myUserInfo.restrictedProfileParentId);
112         }
113         return false;
114     }
115 
116     @Override
isPinSet()117     public boolean isPinSet() {
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             UserInfo myUserInfo = UserManager.get(getActivity()).getUserInfo(UserHandle.myUserId());
131             return (myUserInfo != null && myUserInfo.isRestricted()) ||
132                     callback.hasLockscreenSecurity();
133         } else {
134             throw new IllegalStateException("Can't call isPinSet when not attached");
135         }
136     }
137 }
138