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.BroadcastReceiver;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.IntentFilter;
26 import android.content.RestrictionsManager;
27 import android.os.Bundle;
28 import android.os.PersistableBundle;
29 import android.os.UserManager;
30 import android.preference.CheckBoxPreference;
31 import android.preference.Preference;
34 * Base class for settings screens that should be pin protected when in restricted mode.
35 * The constructor for this class will take the restriction key that this screen should be
36 * locked by. If {@link RestrictionsManager.hasRestrictionsProvider()} and
37 * {@link UserManager.hasUserRestriction()}, then the user will have to enter the restrictions
38 * pin before seeing the Settings screen.
40 * If this settings screen should be pin protected whenever
41 * {@link RestrictionsManager.hasRestrictionsProvider()} returns true, pass in
42 * {@link RESTRICT_IF_OVERRIDABLE} to the constructor instead of a restrictions key.
44 public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
46 protected static final String RESTRICT_IF_OVERRIDABLE = "restrict_if_overridable";
48 // No RestrictedSettingsFragment screens should use this number in startActivityForResult.
49 private static final int REQUEST_PIN_CHALLENGE = 12309;
51 private static final String KEY_CHALLENGE_SUCCEEDED = "chsc";
52 private static final String KEY_CHALLENGE_REQUESTED = "chrq";
54 // If the restriction PIN is entered correctly.
55 private boolean mChallengeSucceeded;
56 private boolean mChallengeRequested;
58 private UserManager mUserManager;
59 private RestrictionsManager mRestrictionsManager;
61 private final String mRestrictionKey;
63 // Receiver to clear pin status when the screen is turned off.
64 private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
66 public void onReceive(Context context, Intent intent) {
67 if (!mChallengeRequested) {
68 mChallengeSucceeded = false;
69 mChallengeRequested = false;
75 * @param restrictionKey The restriction key to check before pin protecting
76 * this settings page. Pass in {@link RESTRICT_IF_OVERRIDABLE} if it should
77 * be protected whenever a restrictions provider is set. Pass in
78 * null if it should never be protected.
80 public RestrictedSettingsFragment(String restrictionKey) {
81 mRestrictionKey = restrictionKey;
85 public void onCreate(Bundle icicle) {
86 super.onCreate(icicle);
88 mRestrictionsManager = (RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE);
89 mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
92 mChallengeSucceeded = icicle.getBoolean(KEY_CHALLENGE_SUCCEEDED, false);
93 mChallengeRequested = icicle.getBoolean(KEY_CHALLENGE_REQUESTED, false);
96 IntentFilter offFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
97 offFilter.addAction(Intent.ACTION_USER_PRESENT);
98 getActivity().registerReceiver(mScreenOffReceiver, offFilter);
102 public void onSaveInstanceState(Bundle outState) {
103 super.onSaveInstanceState(outState);
105 if (getActivity().isChangingConfigurations()) {
106 outState.putBoolean(KEY_CHALLENGE_REQUESTED, mChallengeRequested);
107 outState.putBoolean(KEY_CHALLENGE_SUCCEEDED, mChallengeSucceeded);
112 public void onResume() {
115 if (shouldBeProviderProtected(mRestrictionKey)) {
121 public void onDestroy() {
122 getActivity().unregisterReceiver(mScreenOffReceiver);
127 public void onActivityResult(int requestCode, int resultCode, Intent data) {
128 if (requestCode == REQUEST_PIN_CHALLENGE) {
129 if (resultCode == Activity.RESULT_OK) {
130 mChallengeSucceeded = true;
131 mChallengeRequested = false;
133 mChallengeSucceeded = false;
138 super.onActivityResult(requestCode, resultCode, data);
141 private void ensurePin() {
142 if (!mChallengeSucceeded && !mChallengeRequested
143 && mRestrictionsManager.hasRestrictionsProvider()) {
144 Intent intent = mRestrictionsManager.createLocalApprovalIntent();
145 if (intent != null) {
146 mChallengeRequested = true;
147 mChallengeSucceeded = false;
148 PersistableBundle request = new PersistableBundle();
149 request.putString(RestrictionsManager.REQUEST_KEY_MESSAGE,
150 getResources().getString(R.string.restr_pin_enter_admin_pin));
151 intent.putExtra(RestrictionsManager.EXTRA_REQUEST_BUNDLE, request);
152 startActivityForResult(intent, REQUEST_PIN_CHALLENGE);
158 * Returns true if this activity is restricted, but no restrictions provider has been set.
159 * Used to determine if the settings UI should disable UI.
161 protected boolean isRestrictedAndNotProviderProtected() {
162 if (mRestrictionKey == null || RESTRICT_IF_OVERRIDABLE.equals(mRestrictionKey)) {
165 return mUserManager.hasUserRestriction(mRestrictionKey)
166 && !mRestrictionsManager.hasRestrictionsProvider();
169 protected boolean hasChallengeSucceeded() {
170 return (mChallengeRequested && mChallengeSucceeded) || !mChallengeRequested;
174 * Returns true if this restrictions key is locked down.
176 protected boolean shouldBeProviderProtected(String restrictionKey) {
177 if (restrictionKey == null) {
180 boolean restricted = RESTRICT_IF_OVERRIDABLE.equals(restrictionKey)
181 || mUserManager.hasUserRestriction(mRestrictionKey);
182 return restricted && mRestrictionsManager.hasRestrictionsProvider();
186 * Returns whether restricted or actionable UI elements should be removed or disabled.
188 protected boolean isUiRestricted() {
189 return isRestrictedAndNotProviderProtected() || !hasChallengeSucceeded();