OSDN Git Service

am 439b1543: (-s ours) am 17d0af91: (-s ours) Import translations. DO NOT MERGE
[android-x86/packages-apps-Settings.git] / src / com / android / settings / RestrictedSettingsFragment.java
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
17 package com.android.settings;
18
19 import java.util.HashSet;
20
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;
28
29 /**
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.
35  *
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.
39  */
40 public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
41     protected static final String RESTRICTIONS_PIN_SET = "restrictions_pin_set";
42
43     // Should be unique across all settings screens that use this.
44     private static final int REQUEST_PIN_CHALLENGE = 12309;
45
46     private static final String KEY_CHALLENGE_SUCCEEDED = "chsc";
47     private static final String KEY_CHALLENGE_REQUESTED = "chrq";
48
49     // If the restriction PIN is entered correctly.
50     private boolean mChallengeSucceeded;
51     private boolean mChallengeRequested;
52
53     private UserManager mUserManager;
54
55     private final String mRestrictionKey;
56
57     private final HashSet<Preference> mProtectedByRestictionsPrefs = new HashSet<Preference>();
58
59     /**
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.
64      */
65     public RestrictedSettingsFragment(String restrictionKey) {
66         mRestrictionKey = restrictionKey;
67     }
68
69     @Override
70     public void onCreate(Bundle icicle) {
71         super.onCreate(icicle);
72
73         mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
74
75         if (icicle != null) {
76             mChallengeSucceeded = icicle.getBoolean(KEY_CHALLENGE_SUCCEEDED, false);
77             mChallengeRequested = icicle.getBoolean(KEY_CHALLENGE_REQUESTED, false);
78         }
79     }
80
81     @Override
82     public void onSaveInstanceState(Bundle outState) {
83         super.onSaveInstanceState(outState);
84
85         outState.putBoolean(KEY_CHALLENGE_REQUESTED, mChallengeRequested);
86         if (getActivity().isChangingConfigurations()) {
87             outState.putBoolean(KEY_CHALLENGE_SUCCEEDED, mChallengeSucceeded);
88         }
89     }
90
91     @Override
92     public void onResume() {
93         super.onResume();
94         if (shouldBePinProtected(mRestrictionKey)) {
95             ensurePin();
96         }
97     }
98
99     @Override
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) {
104
105                 mChallengeSucceeded = true;
106             } else if (!isDetached()) {
107                 finishFragment();
108             }
109             return;
110         }
111
112         super.onActivityResult(requestCode, resultCode, data);
113     }
114
115     private void ensurePin() {
116         if (!mChallengeSucceeded) {
117             final UserManager um = UserManager.get(getActivity());
118             if (!mChallengeRequested) {
119                 if (um.hasRestrictionsPin()) {
120                     Intent requestPin =
121                             new Intent(Intent.ACTION_RESTRICTIONS_PIN_CHALLENGE);
122                     startActivityForResult(requestPin, REQUEST_PIN_CHALLENGE);
123                     mChallengeRequested = true;
124                 }
125             }
126         }
127         mChallengeSucceeded = false;
128     }
129
130     /**
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.
133      */
134     protected boolean isRestrictedAndNotPinProtected() {
135         if (mRestrictionKey == null || RESTRICTIONS_PIN_SET.equals(mRestrictionKey)) {
136             return false;
137         }
138         return mUserManager.hasUserRestriction(mRestrictionKey)
139                 && !mUserManager.hasRestrictionsPin();
140     }
141
142     /**
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.
146      */
147    protected boolean restrictionsPinCheck(String restrictionsKey) {
148        if (shouldBePinProtected(restrictionsKey) && !mChallengeSucceeded) {
149            ensurePin();
150            return false;
151        } else {
152            return true;
153        }
154    }
155
156    protected boolean hasChallengeSucceeded() {
157        return mChallengeSucceeded;
158    }
159
160    /**
161     * Returns true if this restrictions key is locked down.
162     */
163    protected boolean shouldBePinProtected(String restrictionKey) {
164        if (restrictionKey == null) {
165            return false;
166        }
167        boolean restricted = RESTRICTIONS_PIN_SET.equals(restrictionKey)
168                || mUserManager.hasUserRestriction(restrictionKey);
169        return restricted && mUserManager.hasRestrictionsPin();
170    }
171
172    /**
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.
177     */
178    boolean ensurePinRestrictedPreference(Preference preference) {
179        return mProtectedByRestictionsPrefs.contains(preference)
180                && !restrictionsPinCheck(RESTRICTIONS_PIN_SET);
181    }
182
183     /**
184      * Call this with any preferences that should require the PIN to be entered
185      * before they are accessible.
186      */
187    protected void protectByRestrictions(Preference pref) {
188        if (pref != null) {
189            mProtectedByRestictionsPrefs.add(pref);
190        }
191    }
192
193    protected void protectByRestrictions(String key) {
194        Preference pref = findPreference(key);
195        protectByRestrictions(pref);
196    }
197 }