RestrictedSettingsFragment.java revision 1a0297c544cb15ed4b56e70575b169b3889543b7
1/*
2 * Copyright (C) 2013 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
17package com.android.settings;
18
19import java.util.HashSet;
20
21import android.app.Activity;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
26import android.content.RestrictionsManager;
27import android.os.Bundle;
28import android.os.PersistableBundle;
29import android.os.UserManager;
30import android.preference.CheckBoxPreference;
31import android.preference.Preference;
32
33/**
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.
39 *
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.
43 */
44public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
45
46    protected static final String RESTRICT_IF_OVERRIDABLE = "restrict_if_overridable";
47
48    // No RestrictedSettingsFragment screens should use this number in startActivityForResult.
49    private static final int REQUEST_PIN_CHALLENGE = 12309;
50
51    private static final String KEY_CHALLENGE_SUCCEEDED = "chsc";
52    private static final String KEY_CHALLENGE_REQUESTED = "chrq";
53
54    // If the restriction PIN is entered correctly.
55    private boolean mChallengeSucceeded;
56    private boolean mChallengeRequested;
57    private boolean mScreenToggledOff;
58
59    private UserManager mUserManager;
60    private RestrictionsManager mRestrictionsManager;
61
62    private final String mRestrictionKey;
63
64    // Receiver to clear pin status when the screen is turned off.
65    private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
66        @Override
67        public void onReceive(Context context, Intent intent) {
68            if (!mChallengeRequested) {
69                mChallengeSucceeded = false;
70                mChallengeRequested = false;
71                mScreenToggledOff = true;
72            }
73        }
74    };
75
76    /**
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.
81     */
82    public RestrictedSettingsFragment(String restrictionKey) {
83        mRestrictionKey = restrictionKey;
84    }
85
86    @Override
87    public void onCreate(Bundle icicle) {
88        super.onCreate(icicle);
89
90        mRestrictionsManager = (RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE);
91        mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
92
93        if (icicle != null) {
94            mChallengeSucceeded = icicle.getBoolean(KEY_CHALLENGE_SUCCEEDED, false);
95            mChallengeRequested = icicle.getBoolean(KEY_CHALLENGE_REQUESTED, false);
96        } else {
97            mChallengeSucceeded = false;
98            mChallengeRequested = false;
99        }
100        mScreenToggledOff = false;
101
102        IntentFilter offFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
103        offFilter.addAction(Intent.ACTION_USER_PRESENT);
104        getActivity().registerReceiver(mScreenOffReceiver, offFilter);
105
106        if (shouldBeProviderProtected(mRestrictionKey)) {
107            ensurePin();
108        }
109    }
110
111    @Override
112    public void onSaveInstanceState(Bundle outState) {
113        super.onSaveInstanceState(outState);
114
115        if (getActivity().isChangingConfigurations()) {
116            outState.putBoolean(KEY_CHALLENGE_REQUESTED, mChallengeRequested);
117            outState.putBoolean(KEY_CHALLENGE_SUCCEEDED, mChallengeSucceeded);
118        }
119    }
120
121    @Override
122    public void onResume() {
123        super.onResume();
124        if (mScreenToggledOff) {
125            mScreenToggledOff = false;
126            if(shouldBeProviderProtected(mRestrictionKey)) {
127                ensurePin();
128            }
129        }
130    }
131
132    @Override
133    public void onDestroy() {
134        getActivity().unregisterReceiver(mScreenOffReceiver);
135        super.onDestroy();
136    }
137
138    @Override
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;
144            } else {
145                mChallengeSucceeded = false;
146            }
147            return;
148        }
149
150        super.onActivityResult(requestCode, resultCode, data);
151    }
152
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);
165            }
166        }
167    }
168
169    /**
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.
172     */
173    protected boolean isRestrictedAndNotProviderProtected() {
174        if (mRestrictionKey == null || RESTRICT_IF_OVERRIDABLE.equals(mRestrictionKey)) {
175            return false;
176        }
177        return mUserManager.hasUserRestriction(mRestrictionKey)
178                && !mRestrictionsManager.hasRestrictionsProvider();
179    }
180
181    protected boolean hasChallengeSucceeded() {
182        return (mChallengeRequested && mChallengeSucceeded) || !mChallengeRequested;
183    }
184
185    /**
186     * Returns true if this restrictions key is locked down.
187     */
188    protected boolean shouldBeProviderProtected(String restrictionKey) {
189        if (restrictionKey == null) {
190            return false;
191        }
192        boolean restricted = RESTRICT_IF_OVERRIDABLE.equals(restrictionKey)
193                || mUserManager.hasUserRestriction(mRestrictionKey);
194        return restricted && mRestrictionsManager.hasRestrictionsProvider();
195    }
196
197    /**
198     * Returns whether restricted or actionable UI elements should be removed or disabled.
199     */
200    protected boolean isUiRestricted() {
201        return isRestrictedAndNotProviderProtected() || !hasChallengeSucceeded();
202    }
203}
204