2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import java.util.HashSet;
21 import android.app.Activity;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.os.Bundle;
25 import android.os.UserManager;
26 import android.preference.Preference;
27 import android.preference.PreferenceScreen;
30 * Base class for settings activities that should be pin protected when in restricted mode.
31 * The constructor for this class will take the restriction key that this screen should be
32 * locked by. If {@link UserManager.hasRestrictionsPin()} and
33 * {@link UserManager.hasUserRestriction(String)} returns true for the restriction key, then
34 * the user will have to enter the restrictions pin before seeing the Settings screen.
36 * If this settings screen should be pin protected whenever
37 * {@link UserManager.hasUserRestriction(String)} returns true, pass in
38 * {@link RESTRICTIONS_PIN_SET} to the constructor instead of a restrictions key.
40 public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
41 protected static final String RESTRICTIONS_PIN_SET = "restrictions_pin_set";
43 // Should be unique across all settings screens that use this.
44 private static final int REQUEST_PIN_CHALLENGE = 12309;
46 private static final String KEY_CHALLENGE_SUCCEEDED = "chsc";
47 private static final String KEY_CHALLENGE_REQUESTED = "chrq";
49 // If the restriction PIN is entered correctly.
50 private boolean mChallengeSucceeded;
51 private boolean mChallengeRequested;
53 private UserManager mUserManager;
55 private final String mRestrictionKey;
57 private final HashSet<Preference> mProtectedByRestictionsPrefs = new HashSet<Preference>();
60 * @param restrictionKey The restriction key to check before pin protecting
61 * this settings page. Pass in {@link RESTRICTIONS_PIN_SET} if it should
62 * be PIN protected whenever a restrictions pin is set. Pass in
63 * null if it should never be PIN protected.
65 public RestrictedSettingsFragment(String restrictionKey) {
66 mRestrictionKey = restrictionKey;
70 public void onCreate(Bundle icicle) {
71 super.onCreate(icicle);
73 mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
76 mChallengeSucceeded = icicle.getBoolean(KEY_CHALLENGE_SUCCEEDED, false);
77 mChallengeRequested = icicle.getBoolean(KEY_CHALLENGE_REQUESTED, false);
82 public void onSaveInstanceState(Bundle outState) {
83 super.onSaveInstanceState(outState);
85 outState.putBoolean(KEY_CHALLENGE_REQUESTED, mChallengeRequested);
86 if (getActivity().isChangingConfigurations()) {
87 outState.putBoolean(KEY_CHALLENGE_SUCCEEDED, mChallengeSucceeded);
92 public void onResume() {
94 if (shouldBePinProtected(mRestrictionKey)) {
100 public void onActivityResult(int requestCode, int resultCode, Intent data) {
101 if (requestCode == REQUEST_PIN_CHALLENGE) {
102 mChallengeRequested = false;
103 if (resultCode == Activity.RESULT_OK) {
105 mChallengeSucceeded = true;
106 } else if (!isDetached()) {
112 super.onActivityResult(requestCode, resultCode, data);
115 private void ensurePin() {
116 if (!mChallengeSucceeded) {
117 final UserManager um = UserManager.get(getActivity());
118 if (!mChallengeRequested) {
119 if (um.hasRestrictionsPin()) {
121 new Intent(Intent.ACTION_RESTRICTIONS_PIN_CHALLENGE);
122 startActivityForResult(requestPin, REQUEST_PIN_CHALLENGE);
123 mChallengeRequested = true;
127 mChallengeSucceeded = false;
131 * Returns true if this activity is restricted, but no restriction pin has been set.
132 * Used to determine if the settings UI should disable UI.
134 protected boolean isRestrictedAndNotPinProtected() {
135 if (mRestrictionKey == null || RESTRICTIONS_PIN_SET.equals(mRestrictionKey)) {
138 return mUserManager.hasUserRestriction(mRestrictionKey)
139 && !mUserManager.hasRestrictionsPin();
143 * Called to trigger the pin entry if the given restriction key is locked down.
144 * @param restrictionsKey The restriction key or {@link RESTRICTIONS_PIN_SET} if
145 * pin entry should get triggered if there is a pin set.
147 protected boolean restrictionsPinCheck(String restrictionsKey) {
148 if (shouldBePinProtected(restrictionsKey) && !mChallengeSucceeded) {
156 protected boolean hasChallengeSucceeded() {
157 return mChallengeSucceeded;
161 * Returns true if this restrictions key is locked down.
163 protected boolean shouldBePinProtected(String restrictionKey) {
164 if (restrictionKey == null) {
167 boolean restricted = RESTRICTIONS_PIN_SET.equals(restrictionKey)
168 || mUserManager.hasUserRestriction(restrictionKey);
169 return restricted && mUserManager.hasRestrictionsPin();
173 * If the preference is one that was added by protectByRestrictions(), then it will
174 * prompt the user for the restrictions pin if they haven't entered it already.
175 * Intended to be called at the top of onPreferenceTreeClick. If this function returns
176 * true, then onPreferenceTreeClick should return true.
178 boolean ensurePinRestrictedPreference(Preference preference) {
179 return mProtectedByRestictionsPrefs.contains(preference)
180 && !restrictionsPinCheck(RESTRICTIONS_PIN_SET);
184 * Call this with any preferences that should require the PIN to be entered
185 * before they are accessible.
187 protected void protectByRestrictions(Preference pref) {
189 mProtectedByRestictionsPrefs.add(pref);
193 protected void protectByRestrictions(String key) {
194 Preference pref = findPreference(key);
195 protectByRestrictions(pref);