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;
57 private boolean mScreenToggledOff;
59 private UserManager mUserManager;
60 private RestrictionsManager mRestrictionsManager;
62 private final String mRestrictionKey;
64 // Receiver to clear pin status when the screen is turned off.
65 private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
67 public void onReceive(Context context, Intent intent) {
68 if (!mChallengeRequested) {
69 mChallengeSucceeded = false;
70 mChallengeRequested = false;
71 mScreenToggledOff = true;
77 * @param restrictionKey The restriction key to check before pin protecting
78 * this settings page. Pass in {@link RESTRICT_IF_OVERRIDABLE} if it should
79 * be protected whenever a restrictions provider is set. Pass in
80 * null if it should never be protected.
82 public RestrictedSettingsFragment(String restrictionKey) {
83 mRestrictionKey = restrictionKey;
87 public void onCreate(Bundle icicle) {
88 super.onCreate(icicle);
90 mRestrictionsManager = (RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE);
91 mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
94 mChallengeSucceeded = icicle.getBoolean(KEY_CHALLENGE_SUCCEEDED, false);
95 mChallengeRequested = icicle.getBoolean(KEY_CHALLENGE_REQUESTED, false);
97 mChallengeSucceeded = false;
98 mChallengeRequested = false;
100 mScreenToggledOff = false;
102 IntentFilter offFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
103 offFilter.addAction(Intent.ACTION_USER_PRESENT);
104 getActivity().registerReceiver(mScreenOffReceiver, offFilter);
106 if (shouldBeProviderProtected(mRestrictionKey)) {
112 public void onSaveInstanceState(Bundle outState) {
113 super.onSaveInstanceState(outState);
115 if (getActivity().isChangingConfigurations()) {
116 outState.putBoolean(KEY_CHALLENGE_REQUESTED, mChallengeRequested);
117 outState.putBoolean(KEY_CHALLENGE_SUCCEEDED, mChallengeSucceeded);
122 public void onResume() {
124 if (mScreenToggledOff) {
125 mScreenToggledOff = false;
126 if(shouldBeProviderProtected(mRestrictionKey)) {
133 public void onDestroy() {
134 getActivity().unregisterReceiver(mScreenOffReceiver);
139 public void onActivityResult(int requestCode, int resultCode, Intent data) {
140 if (requestCode == REQUEST_PIN_CHALLENGE) {
141 if (resultCode == Activity.RESULT_OK) {
142 mChallengeSucceeded = true;
143 mChallengeRequested = false;
145 mChallengeSucceeded = false;
150 super.onActivityResult(requestCode, resultCode, data);
153 private void ensurePin() {
154 if (!mChallengeSucceeded && !mChallengeRequested
155 && mRestrictionsManager.hasRestrictionsProvider()) {
156 Intent intent = mRestrictionsManager.getLocalApprovalIntent();
157 if (intent != null) {
158 mChallengeRequested = true;
159 mChallengeSucceeded = false;
160 PersistableBundle request = new PersistableBundle();
161 request.putString(RestrictionsManager.REQUEST_KEY_MESSAGE,
162 getResources().getString(R.string.restr_pin_enter_admin_pin));
163 intent.putExtra(RestrictionsManager.EXTRA_REQUEST_BUNDLE, request);
164 startActivityForResult(intent, REQUEST_PIN_CHALLENGE);
170 * Returns true if this activity is restricted, but no restrictions provider has been set.
171 * Used to determine if the settings UI should disable UI.
173 protected boolean isRestrictedAndNotProviderProtected() {
174 if (mRestrictionKey == null || RESTRICT_IF_OVERRIDABLE.equals(mRestrictionKey)) {
177 return mUserManager.hasUserRestriction(mRestrictionKey)
178 && !mRestrictionsManager.hasRestrictionsProvider();
181 protected boolean hasChallengeSucceeded() {
182 return (mChallengeRequested && mChallengeSucceeded) || !mChallengeRequested;
186 * Returns true if this restrictions key is locked down.
188 protected boolean shouldBeProviderProtected(String restrictionKey) {
189 if (restrictionKey == null) {
192 boolean restricted = RESTRICT_IF_OVERRIDABLE.equals(restrictionKey)
193 || mUserManager.hasUserRestriction(mRestrictionKey);
194 return restricted && mRestrictionsManager.hasRestrictionsProvider();
198 * Returns whether restricted or actionable UI elements should be removed or disabled.
200 protected boolean isUiRestricted() {
201 return isRestrictedAndNotProviderProtected() || !hasChallengeSucceeded();