Adds a base class for restricted settings

Creates an easy way to ask for the restriction pin before
allowing access to a settings page.  Does this to the WiFiSettings.

Change-Id: I49734f66e09b6449596412ecf6fc1113bf57ce7f
This commit is contained in:
Geoffrey Borggaard
2013-08-02 15:03:22 -04:00
parent 040ce60582
commit e38ea38002
2 changed files with 140 additions and 18 deletions

View File

@@ -0,0 +1,127 @@
/*
* Copyright (C) 2013 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
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
*
*/
public class RestrictedSettingsFragment extends SettingsPreferenceFragment {
// Should be unique across all settings screens that use this.
private static final int REQUEST_PIN_CHALLENGE = 12309;
private static final String KEY_CHALLENGE_SUCCEEDED = "chsc";
private static final String KEY_CHALLENGE_REQUESTED = "chrq";
// If the restriction PIN is entered correctly.
private boolean mChallengeSucceeded;
private boolean mChallengeRequested;
private UserManager mUserManager;
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);
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (icicle != null) {
mChallengeSucceeded = icicle.getBoolean(KEY_CHALLENGE_SUCCEEDED, false);
mChallengeRequested = icicle.getBoolean(KEY_CHALLENGE_REQUESTED, false);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(KEY_CHALLENGE_REQUESTED, mChallengeRequested);
if (getActivity().isChangingConfigurations()) {
outState.putBoolean(KEY_CHALLENGE_SUCCEEDED, mChallengeSucceeded);
}
}
@Override
public void onResume() {
super.onResume();
if (mUserManager.hasUserRestriction(mRestrictionKey)
&& mUserManager.hasRestrictionsPin()) {
ensurePin();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_PIN_CHALLENGE) {
mChallengeRequested = false;
if (resultCode == Activity.RESULT_OK) {
mChallengeSucceeded = true;
} else if (!isDetached()) {
finishFragment();
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void ensurePin() {
if (!mChallengeSucceeded) {
final UserManager um = UserManager.get(getActivity());
if (!mChallengeRequested) {
if (um.hasRestrictionsPin()) {
Intent requestPin =
new Intent(Intent.ACTION_RESTRICTIONS_PIN_CHALLENGE);
startActivityForResult(requestPin, REQUEST_PIN_CHALLENGE);
mChallengeRequested = true;
}
}
}
mChallengeSucceeded = false;
}
/**
* Returns true if this activity is restricted, but no restriction pin has been set.
* Used to determine if the settings UI should disable UI.
*/
protected boolean isRestrictedAndNotPinProtected() {
return mUserManager.hasUserRestriction(mRestrictionKey)
&& !mUserManager.hasRestrictionsPin();
}
}

View File

@@ -19,6 +19,13 @@ package com.android.settings.wifi;
import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID; import static android.net.wifi.WifiConfiguration.INVALID_NETWORK_ID;
import static android.os.UserManager.DISALLOW_CONFIG_WIFI; import static android.os.UserManager.DISALLOW_CONFIG_WIFI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import android.app.ActionBar; import android.app.ActionBar;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
@@ -43,12 +50,10 @@ import android.net.wifi.WpsInfo;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.os.UserManager;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.provider.Settings; import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.util.Log; import android.util.Log;
import android.view.ContextMenu; import android.view.ContextMenu;
@@ -72,16 +77,9 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment; import com.android.settings.RestrictedSettingsFragment;
import com.android.settings.wifi.p2p.WifiP2pSettings; import com.android.settings.wifi.p2p.WifiP2pSettings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
/** /**
* Two types of UI are provided here. * Two types of UI are provided here.
* *
@@ -90,7 +88,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
* The second is for Setup Wizard, with a simplified interface that hides the action bar * The second is for Setup Wizard, with a simplified interface that hides the action bar
* and menus. * and menus.
*/ */
public class WifiSettings extends SettingsPreferenceFragment public class WifiSettings extends RestrictedSettingsFragment
implements DialogInterface.OnClickListener { implements DialogInterface.OnClickListener {
private static final String TAG = "WifiSettings"; private static final String TAG = "WifiSettings";
private static final int MENU_ID_WPS_PBC = Menu.FIRST; private static final int MENU_ID_WPS_PBC = Menu.FIRST;
@@ -129,9 +127,6 @@ public class WifiSettings extends SettingsPreferenceFragment
private WifiManager.ActionListener mForgetListener; private WifiManager.ActionListener mForgetListener;
private boolean mP2pSupported; private boolean mP2pSupported;
private UserManager mUserManager;
private WifiEnabler mWifiEnabler; private WifiEnabler mWifiEnabler;
// An access point being editted is stored here. // An access point being editted is stored here.
private AccessPoint mSelectedAccessPoint; private AccessPoint mSelectedAccessPoint;
@@ -179,6 +174,7 @@ public class WifiSettings extends SettingsPreferenceFragment
/* End of "used in Wifi Setup context" */ /* End of "used in Wifi Setup context" */
public WifiSettings() { public WifiSettings() {
super(DISALLOW_CONFIG_WIFI);
mFilter = new IntentFilter(); mFilter = new IntentFilter();
mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); mFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); mFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
@@ -292,7 +288,6 @@ public class WifiSettings extends SettingsPreferenceFragment
mP2pSupported = getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT); mP2pSupported = getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI_DIRECT);
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
mConnectListener = new WifiManager.ActionListener() { mConnectListener = new WifiManager.ActionListener() {
@Override @Override
@@ -449,7 +444,7 @@ public class WifiSettings extends SettingsPreferenceFragment
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// If the user is not allowed to configure wifi, do not show the menu. // If the user is not allowed to configure wifi, do not show the menu.
if (mUserManager.hasUserRestriction(DISALLOW_CONFIG_WIFI)) return; if (isRestrictedAndNotPinProtected()) return;
final boolean wifiIsEnabled = mWifiManager.isWifiEnabled(); final boolean wifiIsEnabled = mWifiManager.isWifiEnabled();
if (mSetupWizardMode) { if (mSetupWizardMode) {
@@ -507,7 +502,7 @@ public class WifiSettings extends SettingsPreferenceFragment
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
// If the user is not allowed to configure wifi, do not handle menu selections. // If the user is not allowed to configure wifi, do not handle menu selections.
if (mUserManager.hasUserRestriction(DISALLOW_CONFIG_WIFI)) return false; if (isRestrictedAndNotPinProtected()) return false;
switch (item.getItemId()) { switch (item.getItemId()) {
case MENU_ID_WPS_PBC: case MENU_ID_WPS_PBC:
@@ -709,7 +704,7 @@ public class WifiSettings extends SettingsPreferenceFragment
// Safeguard from some delayed event handling // Safeguard from some delayed event handling
if (getActivity() == null) return; if (getActivity() == null) return;
if (mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI)) { if (isRestrictedAndNotPinProtected()) {
addMessagePreference(R.string.wifi_empty_list_user_restricted); addMessagePreference(R.string.wifi_empty_list_user_restricted);
return; return;
} }