Fragmentize ProxySettings.

Change-Id: I2c962726b0adc8811c18dd6df7ff7bdc93c31a07
This commit is contained in:
Daisuke Miyakawa
2010-09-12 15:42:56 -07:00
parent 2712fb5d79
commit 21c1abcef7
4 changed files with 89 additions and 96 deletions

View File

@@ -749,13 +749,15 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name="ProxySelector" android:label="@string/proxy_settings_label"> <activity-alias android:name="ProxySelector"
android:label="@string/proxy_settings_label"
android:targetActivity="Settings">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.VOICE_LAUNCH" /> <category android:name="android.intent.category.VOICE_LAUNCH" />
</intent-filter> </intent-filter>
</activity> </activity-alias>
<!-- TODO: Is this needed? --> <!-- TODO: Is this needed? -->
<activity android:name="BandMode" <activity android:name="BandMode"

View File

@@ -82,13 +82,10 @@
</PreferenceScreen> </PreferenceScreen>
<PreferenceScreen <PreferenceScreen
android:fragment="com.android.settings.ProxySelector"
android:key="proxy_settings" android:key="proxy_settings"
android:title="@string/proxy_settings_title" android:title="@string/proxy_settings_title"
android:summary="@string/proxy_settings_summary" > android:summary="@string/proxy_settings_summary" >
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.android.settings"
android:targetClass="com.android.settings.ProxySelector" />
</PreferenceScreen> </PreferenceScreen>
</PreferenceScreen> </PreferenceScreen>

View File

@@ -16,10 +16,13 @@
package com.android.settings; package com.android.settings;
import com.android.settings.SettingsPreferenceFragment.SettingsDialogFragment;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.admin.DevicePolicyManager;
import android.app.Dialog; import android.app.Dialog;
import android.app.Fragment;
import android.app.admin.DevicePolicyManager;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@@ -34,6 +37,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener; import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
@@ -41,44 +45,23 @@ import android.widget.TextView;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/** public class ProxySelector extends Fragment implements DialogCreatable {
* To start the Proxy Selector activity, create the following intent. private static final String TAG = "ProxySelector";
*
* <code>
* Intent intent = new Intent();
* intent.setClassName("com.android.browser.ProxySelector");
* startActivity(intent);
* </code>
*
* you can add extra options to the intent by using
*
* <code>
* intent.putExtra(key, value);
* </code>
*
* the extra options are:
*
* button-label: a string label to display for the okay button
* title: the title of the window
* error-text: If not null, will be used as the label of the error message.
*/
public class ProxySelector extends Activity
{
private final static String LOGTAG = "Settings";
private View mInitialView;
private LayoutInflater mInflater;
boolean mUserSetGlobalProxy;
EditText mHostnameField; EditText mHostnameField;
EditText mPortField; EditText mPortField;
EditText mExclusionListField; EditText mExclusionListField;
Button mOKButton; Button mOKButton;
DevicePolicyManager mDPM; Button mClearButton;
Button mDefaultButton;
// Matches blank input, ips, and domain names // Matches blank input, ips, and domain names
private static final String HOSTNAME_REGEXP = "^$|^[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*)*$"; private static final String HOSTNAME_REGEXP =
"^$|^[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*)*$";
private static final Pattern HOSTNAME_PATTERN; private static final Pattern HOSTNAME_PATTERN;
private static final String EXCLLIST_REGEXP = "$|^(.?[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*)*)+(,(.?[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*)*))*$"; private static final String EXCLLIST_REGEXP =
"$|^(.?[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*)*)+" +
"(,(.?[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*(\\.[a-zA-Z0-9]+(\\-[a-zA-Z0-9]+)*)*))*$";
private static final Pattern EXCLLIST_PATTERN; private static final Pattern EXCLLIST_PATTERN;
static { static {
HOSTNAME_PATTERN = Pattern.compile(HOSTNAME_REGEXP); HOSTNAME_PATTERN = Pattern.compile(HOSTNAME_REGEXP);
@@ -87,85 +70,90 @@ public class ProxySelector extends Activity
private static final int ERROR_DIALOG_ID = 0; private static final int ERROR_DIALOG_ID = 0;
private SettingsDialogFragment mDialogFragment;
private View mView;
@Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
if (android.util.Config.LOGV) Log.v(LOGTAG, "[ProxySelector] onStart");
mInflater = LayoutInflater.from(this);
if (mInitialView == null) {
mInitialView = mInflater.inflate(R.layout.proxy, null);
}
mDPM = (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
mUserSetGlobalProxy = (mDPM.getGlobalProxyAdmin() == null);
setContentView(mInitialView);
initView();
// TODO: Populate based on connection status
populateFields(false);
} }
@Override @Override
protected Dialog onCreateDialog(int id) { public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.proxy, container, false);
initView(mView);
// TODO: Populate based on connection status
populateFields(false);
return mView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final DevicePolicyManager dpm =
(DevicePolicyManager)getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
final boolean userSetGlobalProxy = (dpm.getGlobalProxyAdmin() == null);
// Disable UI if the Global Proxy is being controlled by a Device Admin
mHostnameField.setEnabled(userSetGlobalProxy);
mPortField.setEnabled(userSetGlobalProxy);
mExclusionListField.setEnabled(userSetGlobalProxy);
mOKButton.setEnabled(userSetGlobalProxy);
mClearButton.setEnabled(userSetGlobalProxy);
mDefaultButton.setEnabled(userSetGlobalProxy);
}
// Dialog management
@Override
public Dialog onCreateDialog(int id) {
if (id == ERROR_DIALOG_ID) { if (id == ERROR_DIALOG_ID) {
String hostname = mHostnameField.getText().toString().trim(); String hostname = mHostnameField.getText().toString().trim();
String portStr = mPortField.getText().toString().trim(); String portStr = mPortField.getText().toString().trim();
String exclList = mExclusionListField.getText().toString().trim(); String exclList = mExclusionListField.getText().toString().trim();
String msg = getString(validate(hostname, portStr, exclList)); String msg = getActivity().getString(validate(hostname, portStr, exclList));
return new AlertDialog.Builder(this) return new AlertDialog.Builder(getActivity())
.setTitle(R.string.proxy_error) .setTitle(R.string.proxy_error)
.setPositiveButton(R.string.proxy_error_dismiss, null) .setPositiveButton(R.string.proxy_error_dismiss, null)
.setMessage(msg) .setMessage(msg)
.create(); .create();
} }
return super.onCreateDialog(id); return null;
} }
@Override private void showDialog(int dialogId) {
protected void onPrepareDialog(int id, Dialog dialog) { if (mDialogFragment != null) {
super.onPrepareDialog(id, dialog); Log.e(TAG, "Old dialog fragment not null!");
if (id == ERROR_DIALOG_ID) {
String hostname = mHostnameField.getText().toString().trim();
String portStr = mPortField.getText().toString().trim();
String exclList = mExclusionListField.getText().toString().trim();
String msg = getString(validate(hostname, portStr, exclList));
((AlertDialog)dialog).setMessage(msg);
} }
mDialogFragment = new SettingsDialogFragment(this, dialogId);
mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
} }
void initView() { private void initView(View view) {
mHostnameField = (EditText)view.findViewById(R.id.hostname);
mHostnameField = (EditText)findViewById(R.id.hostname);
mHostnameField.setOnFocusChangeListener(mOnFocusChangeHandler); mHostnameField.setOnFocusChangeListener(mOnFocusChangeHandler);
mPortField = (EditText)findViewById(R.id.port); mPortField = (EditText)view.findViewById(R.id.port);
mPortField.setOnClickListener(mOKHandler); mPortField.setOnClickListener(mOKHandler);
mPortField.setOnFocusChangeListener(mOnFocusChangeHandler); mPortField.setOnFocusChangeListener(mOnFocusChangeHandler);
mExclusionListField = (EditText)findViewById(R.id.exclusionlist); mExclusionListField = (EditText)view.findViewById(R.id.exclusionlist);
mExclusionListField.setOnFocusChangeListener(mOnFocusChangeHandler); mExclusionListField.setOnFocusChangeListener(mOnFocusChangeHandler);
mOKButton = (Button)findViewById(R.id.action); mOKButton = (Button)view.findViewById(R.id.action);
mOKButton.setOnClickListener(mOKHandler); mOKButton.setOnClickListener(mOKHandler);
Button clearButton = (Button)findViewById(R.id.clear); mClearButton = (Button)view.findViewById(R.id.clear);
clearButton.setOnClickListener(mClearHandler); mClearButton.setOnClickListener(mClearHandler);
Button defButton = (Button)findViewById(R.id.defaultView); mDefaultButton = (Button)view.findViewById(R.id.defaultView);
defButton.setOnClickListener(mDefaultHandler); mDefaultButton.setOnClickListener(mDefaultHandler);
}
// Disable UI if the Global Proxy is being controlled by a Device Admin
mHostnameField.setEnabled(mUserSetGlobalProxy);
mPortField.setEnabled(mUserSetGlobalProxy);
mExclusionListField.setEnabled(mUserSetGlobalProxy);
mOKButton.setEnabled(mUserSetGlobalProxy);
clearButton.setEnabled(mUserSetGlobalProxy);
defButton.setEnabled(mUserSetGlobalProxy);
}
void populateFields(boolean useDefault) { void populateFields(boolean useDefault) {
final Activity activity = getActivity();
String hostname = null; String hostname = null;
int port = -1; int port = -1;
String exclList = null; String exclList = null;
@@ -175,9 +163,9 @@ public class ProxySelector extends Activity
port = Proxy.getDefaultPort(); port = Proxy.getDefaultPort();
} else { } else {
// Use the last setting given by the user // Use the last setting given by the user
ContentResolver res = getContentResolver(); ContentResolver res = getActivity().getContentResolver();
hostname = Proxy.getHost(this); hostname = Proxy.getHost(activity);
port = Proxy.getPort(this); port = Proxy.getPort(activity);
exclList = Settings.Secure.getString(res, Settings.Secure.HTTP_PROXY_EXCLUSION_LIST); exclList = Settings.Secure.getString(res, Settings.Secure.HTTP_PROXY_EXCLUSION_LIST);
} }
@@ -192,7 +180,7 @@ public class ProxySelector extends Activity
mExclusionListField.setText(exclList); mExclusionListField.setText(exclList);
Intent intent = getIntent(); final Intent intent = activity.getIntent();
String buttonLabel = intent.getStringExtra("button-label"); String buttonLabel = intent.getStringExtra("button-label");
if (!TextUtils.isEmpty(buttonLabel)) { if (!TextUtils.isEmpty(buttonLabel)) {
@@ -201,7 +189,7 @@ public class ProxySelector extends Activity
String title = intent.getStringExtra("title"); String title = intent.getStringExtra("title");
if (!TextUtils.isEmpty(title)) { if (!TextUtils.isEmpty(title)) {
setTitle(title); activity.setTitle(title);
} }
} }
@@ -272,7 +260,7 @@ public class ProxySelector extends Activity
// controls. // controls.
// FIXME: If the user types in a proxy that matches the default, should // FIXME: If the user types in a proxy that matches the default, should
// we keep that setting? Can be fixed with a new UI. // we keep that setting? Can be fixed with a new UI.
ContentResolver res = getContentResolver(); ContentResolver res = getActivity().getContentResolver();
if (hostname.equals(Proxy.getDefaultHost()) if (hostname.equals(Proxy.getDefaultHost())
&& port == Proxy.getDefaultPort()) { && port == Proxy.getDefaultPort()) {
// If the user hit the default button and didn't change any of // If the user hit the default button and didn't change any of
@@ -286,7 +274,7 @@ public class ProxySelector extends Activity
} }
Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY, hostname); Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY, hostname);
Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY_EXCLUSION_LIST, exclList); Settings.Secure.putString(res, Settings.Secure.HTTP_PROXY_EXCLUSION_LIST, exclList);
sendBroadcast(new Intent(Proxy.PROXY_CHANGE_ACTION)); getActivity().sendBroadcast(new Intent(Proxy.PROXY_CHANGE_ACTION));
return true; return true;
} }
@@ -294,7 +282,7 @@ public class ProxySelector extends Activity
OnClickListener mOKHandler = new OnClickListener() { OnClickListener mOKHandler = new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
if (saveToDb()) { if (saveToDb()) {
finish(); getActivity().onBackPressed();
} }
} }
}; };

View File

@@ -32,10 +32,15 @@ import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.Button; import android.widget.Button;
interface DialogCreatable {
public Dialog onCreateDialog(int dialogId);
}
/** /**
* Base class for Settings fragments, with some helper functions and dialog management. * Base class for Settings fragments, with some helper functions and dialog management.
*/ */
public class SettingsPreferenceFragment extends PreferenceFragment { public class SettingsPreferenceFragment extends PreferenceFragment
implements DialogCreatable {
private static final String TAG = "SettingsPreferenceFragment"; private static final String TAG = "SettingsPreferenceFragment";
@@ -121,9 +126,10 @@ public class SettingsPreferenceFragment extends PreferenceFragment {
Log.e(TAG, "Old dialog fragment not null!"); Log.e(TAG, "Old dialog fragment not null!");
} }
mDialogFragment = new SettingsDialogFragment(this, dialogId); mDialogFragment = new SettingsDialogFragment(this, dialogId);
mDialogFragment.show(getActivity(), Integer.toString(dialogId)); mDialogFragment.show(getActivity().getFragmentManager(), Integer.toString(dialogId));
} }
@Override
public Dialog onCreateDialog(int dialogId) { public Dialog onCreateDialog(int dialogId) {
return null; return null;
} }
@@ -139,9 +145,9 @@ public class SettingsPreferenceFragment extends PreferenceFragment {
static class SettingsDialogFragment extends DialogFragment { static class SettingsDialogFragment extends DialogFragment {
private int mDialogId; private int mDialogId;
private SettingsPreferenceFragment mFragment; private DialogCreatable mFragment;
SettingsDialogFragment(SettingsPreferenceFragment fragment, int dialogId) { SettingsDialogFragment(DialogCreatable fragment, int dialogId) {
mDialogId = dialogId; mDialogId = dialogId;
mFragment = fragment; mFragment = fragment;
} }