Use restrictionsprovider framework in Settings.
Bug: 15305103 Change-Id: Ib0147e366fe90de3512d0cc06e2a47ee16012dff
This commit is contained in:
@@ -23,62 +23,61 @@ import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.RestrictionsManager;
|
||||
import android.os.Bundle;
|
||||
import android.os.PersistableBundle;
|
||||
import android.os.UserManager;
|
||||
import android.preference.CheckBoxPreference;
|
||||
import android.preference.Preference;
|
||||
|
||||
/**
|
||||
* Base class for settings activities that should be pin protected when in restricted mode.
|
||||
* Base class for settings screens that should be pin protected when in restricted mode.
|
||||
* The constructor for this class will take the restriction key that this screen should be
|
||||
* locked by. If {@link UserManager.hasRestrictionsPin()} and
|
||||
* {@link UserManager.hasUserRestriction(String)} returns true for the restriction key, then
|
||||
* the user will have to enter the restrictions pin before seeing the Settings screen.
|
||||
* locked by. If {@link RestrictionsManager.hasRestrictionsProvider()} and
|
||||
* {@link UserManager.hasUserRestriction()}, then the user will have to enter the restrictions
|
||||
* pin before seeing the Settings screen.
|
||||
*
|
||||
* If this settings screen should be pin protected whenever
|
||||
* {@link UserManager.hasUserRestriction(String)} returns true, pass in
|
||||
* {@link RESTRICTIONS_PIN_SET} to the constructor instead of a restrictions key.
|
||||
* {@link RestrictionsManager.hasRestrictionsProvider()} returns true, pass in
|
||||
* {@link RESTRICT_IF_OVERRIDABLE} to the constructor instead of a restrictions key.
|
||||
*/
|
||||
public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
|
||||
|
||||
protected static final String RESTRICTIONS_PIN_SET = "restrictions_pin_set";
|
||||
protected static final String RESTRICT_IF_OVERRIDABLE = "restrict_if_overridable";
|
||||
|
||||
private static final String EXTRA_PREFERENCE = "pref";
|
||||
private static final String EXTRA_CHECKBOX_STATE = "isChecked";
|
||||
// Should be unique across all settings screens that use this.
|
||||
// No RestrictedSettingsFragment screens should use this number in startActivityForResult.
|
||||
private static final int REQUEST_PIN_CHALLENGE = 12309;
|
||||
|
||||
private static final String KEY_CHALLENGE_SUCCEEDED = "chsc";
|
||||
private static final String KEY_CHALLENGE_REQUESTED = "chrq";
|
||||
private static final String KEY_RESUME_ACTION_BUNDLE = "rsmb";
|
||||
|
||||
// If the restriction PIN is entered correctly.
|
||||
private boolean mChallengeSucceeded;
|
||||
private boolean mChallengeRequested;
|
||||
private Bundle mResumeActionBundle;
|
||||
private boolean mScreenToggledOff;
|
||||
|
||||
private UserManager mUserManager;
|
||||
private RestrictionsManager mRestrictionsManager;
|
||||
|
||||
private final String mRestrictionKey;
|
||||
|
||||
private final HashSet<Preference> mProtectedByRestictionsPrefs = new HashSet<Preference>();
|
||||
|
||||
// Receiver to clear pin status when the screen is turned off.
|
||||
private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
mChallengeSucceeded = false;
|
||||
if (shouldBePinProtected(mRestrictionKey)) {
|
||||
ensurePin(null);
|
||||
if (!mChallengeRequested) {
|
||||
mChallengeSucceeded = false;
|
||||
mChallengeRequested = false;
|
||||
mScreenToggledOff = true;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param restrictionKey The restriction key to check before pin protecting
|
||||
* this settings page. Pass in {@link RESTRICTIONS_PIN_SET} if it should
|
||||
* be PIN protected whenever a restrictions pin is set. Pass in
|
||||
* null if it should never be PIN protected.
|
||||
* this settings page. Pass in {@link RESTRICT_IF_OVERRIDABLE} if it should
|
||||
* be protected whenever a restrictions provider is set. Pass in
|
||||
* null if it should never be protected.
|
||||
*/
|
||||
public RestrictedSettingsFragment(String restrictionKey) {
|
||||
mRestrictionKey = restrictionKey;
|
||||
@@ -88,12 +87,24 @@ public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
|
||||
public void onCreate(Bundle icicle) {
|
||||
super.onCreate(icicle);
|
||||
|
||||
mRestrictionsManager = (RestrictionsManager) getSystemService(Context.RESTRICTIONS_SERVICE);
|
||||
mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
|
||||
|
||||
if (icicle != null) {
|
||||
mChallengeSucceeded = icicle.getBoolean(KEY_CHALLENGE_SUCCEEDED, false);
|
||||
mChallengeRequested = icicle.getBoolean(KEY_CHALLENGE_REQUESTED, false);
|
||||
mResumeActionBundle = icicle.getBundle(KEY_RESUME_ACTION_BUNDLE);
|
||||
} else {
|
||||
mChallengeSucceeded = false;
|
||||
mChallengeRequested = false;
|
||||
}
|
||||
mScreenToggledOff = false;
|
||||
|
||||
IntentFilter offFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
|
||||
offFilter.addAction(Intent.ACTION_USER_PRESENT);
|
||||
getActivity().registerReceiver(mScreenOffReceiver, offFilter);
|
||||
|
||||
if (shouldBeProviderProtected(mRestrictionKey)) {
|
||||
ensurePin();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,11 +112,8 @@ public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
|
||||
public void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
|
||||
outState.putBoolean(KEY_CHALLENGE_REQUESTED, mChallengeRequested);
|
||||
if (mResumeActionBundle != null) {
|
||||
outState.putBundle(KEY_RESUME_ACTION_BUNDLE, mResumeActionBundle);
|
||||
}
|
||||
if (getActivity().isChangingConfigurations()) {
|
||||
outState.putBoolean(KEY_CHALLENGE_REQUESTED, mChallengeRequested);
|
||||
outState.putBoolean(KEY_CHALLENGE_SUCCEEDED, mChallengeSucceeded);
|
||||
}
|
||||
}
|
||||
@@ -113,55 +121,28 @@ public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
if (shouldBePinProtected(mRestrictionKey)) {
|
||||
ensurePin(null);
|
||||
} else {
|
||||
// If the whole screen is not pin protected, reset mChallengeSucceeded so next
|
||||
// time user uses a protected preference, they are prompted for pin again.
|
||||
mChallengeSucceeded = false;
|
||||
if (mScreenToggledOff) {
|
||||
mScreenToggledOff = false;
|
||||
if(shouldBeProviderProtected(mRestrictionKey)) {
|
||||
ensurePin();
|
||||
}
|
||||
}
|
||||
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
|
||||
filter.addAction(Intent.ACTION_USER_PRESENT);
|
||||
getActivity().registerReceiver(mScreenOffReceiver, filter);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
public void onDestroy() {
|
||||
getActivity().unregisterReceiver(mScreenOffReceiver);
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == REQUEST_PIN_CHALLENGE) {
|
||||
Bundle resumeActionBundle = mResumeActionBundle;
|
||||
mResumeActionBundle = null;
|
||||
mChallengeRequested = false;
|
||||
if (resultCode == Activity.RESULT_OK) {
|
||||
mChallengeSucceeded = true;
|
||||
String prefKey = resumeActionBundle == null ?
|
||||
null : resumeActionBundle.getString(EXTRA_PREFERENCE);
|
||||
if (prefKey != null) {
|
||||
Preference pref = findPreference(prefKey);
|
||||
if (pref != null) {
|
||||
// Make sure the checkbox state is the same as it was when we launched the
|
||||
// pin challenge.
|
||||
if (pref instanceof CheckBoxPreference
|
||||
&& resumeActionBundle.containsKey(EXTRA_CHECKBOX_STATE)) {
|
||||
boolean isChecked =
|
||||
resumeActionBundle.getBoolean(EXTRA_CHECKBOX_STATE, false);
|
||||
((CheckBoxPreference)pref).setChecked(isChecked);
|
||||
}
|
||||
if (!onPreferenceTreeClick(getPreferenceScreen(), pref)) {
|
||||
Intent prefIntent = pref.getIntent();
|
||||
if (prefIntent != null) {
|
||||
pref.getContext().startActivity(prefIntent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (!isDetached()) {
|
||||
finishFragment();
|
||||
mChallengeRequested = false;
|
||||
} else {
|
||||
mChallengeSucceeded = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -169,93 +150,54 @@ public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
}
|
||||
|
||||
private void ensurePin(Preference preference) {
|
||||
if (!mChallengeSucceeded) {
|
||||
final UserManager um = UserManager.get(getActivity());
|
||||
if (!mChallengeRequested) {
|
||||
if (um.hasRestrictionsChallenge()) {
|
||||
mResumeActionBundle = new Bundle();
|
||||
if (preference != null) {
|
||||
mResumeActionBundle.putString(EXTRA_PREFERENCE, preference.getKey());
|
||||
if (preference instanceof CheckBoxPreference) {
|
||||
mResumeActionBundle.putBoolean(EXTRA_CHECKBOX_STATE,
|
||||
((CheckBoxPreference)preference).isChecked());
|
||||
}
|
||||
}
|
||||
Intent requestPin = new Intent(Intent.ACTION_RESTRICTIONS_CHALLENGE);
|
||||
startActivityForResult(requestPin, REQUEST_PIN_CHALLENGE);
|
||||
mChallengeRequested = true;
|
||||
}
|
||||
private void ensurePin() {
|
||||
if (!mChallengeSucceeded && !mChallengeRequested
|
||||
&& mRestrictionsManager.hasRestrictionsProvider()) {
|
||||
Intent intent = mRestrictionsManager.getLocalApprovalIntent();
|
||||
if (intent != null) {
|
||||
mChallengeRequested = true;
|
||||
mChallengeSucceeded = false;
|
||||
PersistableBundle request = new PersistableBundle();
|
||||
request.putString(RestrictionsManager.REQUEST_KEY_MESSAGE,
|
||||
getResources().getString(R.string.restr_pin_enter_admin_pin));
|
||||
intent.putExtra(RestrictionsManager.EXTRA_REQUEST_BUNDLE, request);
|
||||
startActivityForResult(intent, REQUEST_PIN_CHALLENGE);
|
||||
}
|
||||
}
|
||||
mChallengeSucceeded = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this activity is restricted, but no restriction pin has been set.
|
||||
* Returns true if this activity is restricted, but no restrictions provider has been set.
|
||||
* Used to determine if the settings UI should disable UI.
|
||||
*/
|
||||
protected boolean isRestrictedAndNotPinProtected() {
|
||||
if (mRestrictionKey == null || RESTRICTIONS_PIN_SET.equals(mRestrictionKey)) {
|
||||
protected boolean isRestrictedAndNotProviderProtected() {
|
||||
if (mRestrictionKey == null || RESTRICT_IF_OVERRIDABLE.equals(mRestrictionKey)) {
|
||||
return false;
|
||||
}
|
||||
return mUserManager.hasUserRestriction(mRestrictionKey)
|
||||
&& !mUserManager.hasRestrictionsChallenge();
|
||||
&& !mRestrictionsManager.hasRestrictionsProvider();
|
||||
}
|
||||
|
||||
protected boolean hasChallengeSucceeded() {
|
||||
return (mChallengeRequested && mChallengeSucceeded) || !mChallengeRequested;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called to trigger the pin entry if the given restriction key is locked down.
|
||||
* @param restrictionsKey The restriction key or {@link RESTRICTIONS_PIN_SET} if
|
||||
* pin entry should get triggered if there is a pin set.
|
||||
* Returns true if this restrictions key is locked down.
|
||||
*/
|
||||
protected boolean restrictionsPinCheck(String restrictionsKey, Preference preference) {
|
||||
if (shouldBePinProtected(restrictionsKey) && !mChallengeSucceeded) {
|
||||
ensurePin(preference);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean hasChallengeSucceeded() {
|
||||
return mChallengeSucceeded;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if this restrictions key is locked down.
|
||||
*/
|
||||
protected boolean shouldBePinProtected(String restrictionKey) {
|
||||
if (restrictionKey == null) {
|
||||
return false;
|
||||
}
|
||||
boolean restricted = RESTRICTIONS_PIN_SET.equals(restrictionKey)
|
||||
|| mUserManager.hasUserRestriction(restrictionKey);
|
||||
return restricted && mUserManager.hasRestrictionsChallenge();
|
||||
}
|
||||
|
||||
/**
|
||||
* If the preference is one that was added by protectByRestrictions(), then it will
|
||||
* prompt the user for the restrictions pin if they haven't entered it already.
|
||||
* Intended to be called at the top of onPreferenceTreeClick. If this function returns
|
||||
* true, then onPreferenceTreeClick should return true.
|
||||
*/
|
||||
boolean ensurePinRestrictedPreference(Preference preference) {
|
||||
return mProtectedByRestictionsPrefs.contains(preference)
|
||||
&& !restrictionsPinCheck(RESTRICTIONS_PIN_SET, preference);
|
||||
}
|
||||
protected boolean shouldBeProviderProtected(String restrictionKey) {
|
||||
if (restrictionKey == null) {
|
||||
return false;
|
||||
}
|
||||
boolean restricted = RESTRICT_IF_OVERRIDABLE.equals(restrictionKey)
|
||||
|| mUserManager.hasUserRestriction(mRestrictionKey);
|
||||
return restricted && mRestrictionsManager.hasRestrictionsProvider();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call this with any preferences that should require the PIN to be entered
|
||||
* before they are accessible.
|
||||
* Returns whether restricted or actionable UI elements should be removed or disabled.
|
||||
*/
|
||||
protected void protectByRestrictions(Preference pref) {
|
||||
if (pref != null) {
|
||||
mProtectedByRestictionsPrefs.add(pref);
|
||||
}
|
||||
}
|
||||
|
||||
protected void protectByRestrictions(String key) {
|
||||
Preference pref = findPreference(key);
|
||||
protectByRestrictions(pref);
|
||||
}
|
||||
protected boolean isUiRestricted() {
|
||||
return isRestrictedAndNotProviderProtected() || !hasChallengeSucceeded();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user