Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fe21d9aa authored by Geoffrey Borggaard's avatar Geoffrey Borggaard
Browse files

Pin protect more screens.

When these screens are locked down with user restrictions,
it should prompt the user for the restrictions pin before allowing
access to the settings screen.

Change-Id: Iadbb087da2d9470b855ea0bea89f2da1ffb9e854
parent d98d2abd
Loading
Loading
Loading
Loading
+29 −7
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ import java.util.List;
/*
 * Displays preferences for application developers.
 */
public class DevelopmentSettings extends PreferenceFragment
public class DevelopmentSettings extends RestrictedSettingsFragment
        implements DialogInterface.OnClickListener, DialogInterface.OnDismissListener,
                OnPreferenceChangeListener, CompoundButton.OnCheckedChangeListener {
    private static final String TAG = "DevelopmentSettings";
@@ -210,6 +210,7 @@ public class DevelopmentSettings extends PreferenceFragment
            = new ArrayList<CheckBoxPreference>();

    private final HashSet<Preference> mDisabledPrefs = new HashSet<Preference>();
    private final HashSet<Preference> mProtectedByRestrictionsPrefs = new HashSet<Preference>();

    // To track whether a confirmation dialog was clicked.
    private boolean mDialogClicked;
@@ -219,6 +220,10 @@ public class DevelopmentSettings extends PreferenceFragment

    private boolean mUnavailable;

    public DevelopmentSettings() {
        super(null /* Don't ask for restrictions pin on creation. */);
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
@@ -268,6 +273,13 @@ public class DevelopmentSettings extends PreferenceFragment
            disableForUser(mPassword);
        }

        if (shouldBePinProtected(RESTRICTIONS_PIN_SET)) {
            protectByRestrictions(mEnableAdb);
            protectByRestrictions(mClearAdbKeys);
            protectByRestrictions(mEnableTerminal);
            protectByRestrictions(mPassword);
        }

        mDebugAppPref = findPreference(DEBUG_APP_KEY);
        mAllPrefs.add(mDebugAppPref);
        mWaitForDebugger = findAndInitCheckboxPref(WAIT_FOR_DEBUGGER_KEY);
@@ -350,6 +362,12 @@ public class DevelopmentSettings extends PreferenceFragment
        }
    }

    private void protectByRestrictions(Preference pref) {
        if (pref != null) {
            mProtectedByRestrictionsPrefs.add(pref);
        }
    }

    private CheckBoxPreference findAndInitCheckboxPref(String key) {
        CheckBoxPreference pref = (CheckBoxPreference) findPreference(key);
        if (pref == null) {
@@ -1160,6 +1178,10 @@ public class DevelopmentSettings extends PreferenceFragment

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        if (mProtectedByRestrictionsPrefs.contains(preference)
                && !restrictionsPinCheck(RESTRICTIONS_PIN_SET)) {
            return false;
        }

        if (Utils.isMonkeyRunning()) {
            return false;
+51 −13
Original line number Diff line number Diff line
@@ -25,11 +25,16 @@ import android.os.UserManager;
/**
 * Base class for settings activities 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 hav
 * 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.
 *
 * 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.
 */
public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
    protected static final String RESTRICTIONS_PIN_SET = "restrictions_pin_set";

    // Should be unique across all settings screens that use this.
    private static final int REQUEST_PIN_CHALLENGE = 12309;
@@ -45,21 +50,22 @@ public class RestrictedSettingsFragment extends SettingsPreferenceFragment {

    private final String mRestrictionKey;

    public RestrictedSettingsFragment(String restrictedFlag) {
        mRestrictionKey = restrictedFlag;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
    /**
     * @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.
     */
    public RestrictedSettingsFragment(String restrictionKey) {
        mRestrictionKey = restrictionKey;
    }

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);

        if (icicle != null) {
            mChallengeSucceeded = icicle.getBoolean(KEY_CHALLENGE_SUCCEEDED, false);
            mChallengeRequested = icicle.getBoolean(KEY_CHALLENGE_REQUESTED, false);
@@ -79,8 +85,7 @@ public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
    @Override
    public void onResume() {
        super.onResume();
        if (mUserManager.hasUserRestriction(mRestrictionKey)
                && mUserManager.hasRestrictionsPin()) {
        if (shouldBePinProtected(mRestrictionKey)) {
            ensurePin();
        }
    }
@@ -121,7 +126,40 @@ public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
     * Used to determine if the settings UI should disable UI.
     */
    protected boolean isRestrictedAndNotPinProtected() {
        if (mRestrictionKey == null || RESTRICTIONS_PIN_SET.equals(mRestrictionKey)) {
            return false;
        }
        return mUserManager.hasUserRestriction(mRestrictionKey)
                && !mUserManager.hasRestrictionsPin();
    }

    /**
     * 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.
     */
   protected boolean restrictionsPinCheck(String restrictionsKey) {
       if (shouldBePinProtected(restrictionsKey) && !mChallengeSucceeded) {
           ensurePin();
           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.hasRestrictionsPin();
   }
}
+23 −2
Original line number Diff line number Diff line
@@ -45,14 +45,14 @@ import android.util.Log;
import com.android.internal.widget.LockPatternUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * Gesture lock pattern settings.
 */
public class SecuritySettings extends SettingsPreferenceFragment
public class SecuritySettings extends RestrictedSettingsFragment
        implements OnPreferenceChangeListener, DialogInterface.OnClickListener {

    static final String TAG = "SecuritySettings";

    // Lock Settings
@@ -82,6 +82,8 @@ public class SecuritySettings extends SettingsPreferenceFragment
    private static final String KEY_NOTIFICATION_ACCESS = "manage_notification_access";
    private static final String PACKAGE_MIME_TYPE = "application/vnd.android.package-archive";

    private final HashSet<Preference> mProtectedByRestictionsPrefs = new HashSet<Preference>();

    private PackageManager mPM;
    DevicePolicyManager mDPM;

@@ -106,6 +108,10 @@ public class SecuritySettings extends SettingsPreferenceFragment

    private boolean mIsPrimary;

    public SecuritySettings() {
        super(null /* Don't ask for restrictions pin on creation. */);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
@@ -303,9 +309,19 @@ public class SecuritySettings extends SettingsPreferenceFragment
            }
        }

        if (shouldBePinProtected(RESTRICTIONS_PIN_SET)) {
            protectByRestrictions(mToggleAppInstallation);
            protectByRestrictions(mToggleVerifyApps);
        }
        return root;
    }

    private void protectByRestrictions(Preference pref) {
        if (pref != null) {
            mProtectedByRestictionsPrefs.add(pref);
        }
    }

    private int getNumEnabledNotificationListeners() {
        final String flat = Settings.Secure.getString(getContentResolver(),
                Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
@@ -471,6 +487,11 @@ public class SecuritySettings extends SettingsPreferenceFragment

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        if (mProtectedByRestictionsPrefs.contains(preference)
                && !restrictionsPinCheck(RESTRICTIONS_PIN_SET)) {
            return false;
        }

        final String key = preference.getKey();

        final LockPatternUtils lockPatternUtils = mChooseLockSettingsHelper.utils();
+26 −2
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package com.android.settings;

import java.util.HashSet;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
@@ -44,7 +46,7 @@ import com.android.internal.telephony.TelephonyProperties;
import com.android.settings.nfc.NfcEnabler;
import com.android.settings.NsdEnabler;

public class WirelessSettings extends SettingsPreferenceFragment {
public class WirelessSettings extends RestrictedSettingsFragment {
    private static final String TAG = "WirelessSettings";

    private static final String KEY_TOGGLE_AIRPLANE = "toggle_airplane";
@@ -73,7 +75,11 @@ public class WirelessSettings extends SettingsPreferenceFragment {

    private static final int MANAGE_MOBILE_PLAN_DIALOG_ID = 1;
    private static final String SAVED_MANAGE_MOBILE_PLAN_MSG = "mManageMobilePlanMessage";
    private final HashSet<Preference> mProtectedByRestictionsPrefs = new HashSet<Preference>();

    public WirelessSettings() {
        super(null);
    }
    /**
     * Invoked on each preference click in this hierarchy, overrides
     * PreferenceActivity's implementation.  Used to make sure we track the
@@ -81,6 +87,10 @@ public class WirelessSettings extends SettingsPreferenceFragment {
     */
    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        if (mProtectedByRestictionsPrefs.contains(preference) && !hasChallengeSucceeded()) {
            restrictionsPinCheck(RESTRICTIONS_PIN_SET);
            return false;
        }
        log("onPreferenceTreeClick: preference=" + preference);
        if (preference == mAirplaneModePreference && Boolean.parseBoolean(
                SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
@@ -145,6 +155,13 @@ public class WirelessSettings extends SettingsPreferenceFragment {
        }
    }

    private void protectByRestrictions(String key) {
        Preference pref = findPreference(key);
        if (pref != null) {
            mProtectedByRestictionsPrefs.add(pref);
        }
    }

    @Override
    public Dialog onCreateDialog(int dialogId) {
        log("onCreateDialog: dialogId=" + dialogId);
@@ -225,6 +242,8 @@ public class WirelessSettings extends SettingsPreferenceFragment {
                ps.setDependency(KEY_TOGGLE_AIRPLANE);
            }
        }
        protectByRestrictions(KEY_WIMAX_SETTINGS);

        // Manually set dependencies for Wifi when not toggleable.
        if (toggleable == null || !toggleable.contains(Settings.Global.RADIO_WIFI)) {
            findPreference(KEY_VPN_SETTINGS).setDependency(KEY_TOGGLE_AIRPLANE);
@@ -232,7 +251,7 @@ public class WirelessSettings extends SettingsPreferenceFragment {
        if (isSecondaryUser) { // Disable VPN
            removePreference(KEY_VPN_SETTINGS);
        }

        protectByRestrictions(KEY_VPN_SETTINGS);
        // Manually set dependencies for Bluetooth when not toggleable.
        if (toggleable == null || !toggleable.contains(Settings.Global.RADIO_BLUETOOTH)) {
            // No bluetooth-dependent items in the list. Code kept in case one is added later.
@@ -257,6 +276,8 @@ public class WirelessSettings extends SettingsPreferenceFragment {
            removePreference(KEY_MOBILE_NETWORK_SETTINGS);
            removePreference(KEY_MANAGE_MOBILE_PLAN);
        }
        protectByRestrictions(KEY_MOBILE_NETWORK_SETTINGS);
        protectByRestrictions(KEY_MANAGE_MOBILE_PLAN);

        // Remove Airplane Mode settings if it's a stationary device such as a TV.
        if (getActivity().getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEVISION)) {
@@ -280,6 +301,7 @@ public class WirelessSettings extends SettingsPreferenceFragment {
            Preference p = findPreference(KEY_TETHER_SETTINGS);
            p.setTitle(Utils.getTetheringLabel(cm));
        }
        protectByRestrictions(KEY_TETHER_SETTINGS);

        // Enable link to CMAS app settings depending on the value in config.xml.
        boolean isCellBroadcastAppLinkEnabled = this.getResources().getBoolean(
@@ -300,6 +322,7 @@ public class WirelessSettings extends SettingsPreferenceFragment {
            Preference ps = findPreference(KEY_CELL_BROADCAST_SETTINGS);
            if (ps != null) root.removePreference(ps);
        }
        protectByRestrictions(KEY_CELL_BROADCAST_SETTINGS);
    }

    @Override
@@ -345,6 +368,7 @@ public class WirelessSettings extends SettingsPreferenceFragment {
            mAirplaneModeEnabler.setAirplaneModeInECM(isChoiceYes,
                    mAirplaneModePreference.isChecked());
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
+6 −8
Original line number Diff line number Diff line
@@ -27,7 +27,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.UserManager;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
@@ -74,7 +73,6 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {

    private final IntentFilter mIntentFilter;

    private UserManager mUserManager;

    // accessed from inner class (not private to avoid thunks)
    Preference mMyDevicePreference;
@@ -96,13 +94,13 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
    };

    public BluetoothSettings() {
        super(DISALLOW_CONFIG_BLUETOOTH);
        mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
        mActivityStarted = (savedInstanceState == null);    // don't auto start scan after rotation

        mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
@@ -171,7 +169,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        if (mLocalAdapter == null) return;
        // If the user is not allowed to configure bluetooth, do not show the menu.
        if (mUserManager.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)) return;
        if (isRestrictedAndNotPinProtected()) return;

        boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
        boolean isDiscovering = mLocalAdapter.isDiscovering();
@@ -219,7 +217,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
    }

    private void startScanning() {
        if (mUserManager.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)) return;
        if (isRestrictedAndNotPinProtected()) return;
        if (!mAvailableDevicesCategoryIsPresent) {
            getPreferenceScreen().addPreference(mAvailableDevicesCategory);
        }
@@ -266,7 +264,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
                mMyDevicePreference.setEnabled(true);
                preferenceScreen.addPreference(mMyDevicePreference);

                if (! mUserManager.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)) {
                if (!isRestrictedAndNotPinProtected()) {
                    if (mDiscoverableEnabler == null) {
                        mDiscoverableEnabler = new BluetoothDiscoverableEnabler(getActivity(),
                                mLocalAdapter, mMyDevicePreference);
@@ -297,7 +295,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
                } else {
                    mAvailableDevicesCategory.removeAll();
                }
                if (! mUserManager.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)) {
                if (!isRestrictedAndNotPinProtected()) {
                    addDeviceCategory(mAvailableDevicesCategory,
                            R.string.bluetooth_preference_found_devices,
                            BluetoothDeviceFilter.UNBONDED_DEVICE_FILTER);
@@ -366,7 +364,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
        public void onClick(View v) {
            // User clicked on advanced options icon for a device in the list
            if (v.getTag() instanceof CachedBluetoothDevice) {
                if (mUserManager.hasUserRestriction(DISALLOW_CONFIG_BLUETOOTH)) return;
                if (isRestrictedAndNotPinProtected()) return;

                CachedBluetoothDevice device = (CachedBluetoothDevice) v.getTag();

Loading