Commit 9c8bde57 authored by Daisuke Miyakawa's avatar Daisuke Miyakawa

Fragmentize WifiSettings.

- Add button bar feature toward SettingsPreferenceFragment,
  which has existed in PreferenceActivity and has been used
  (probably) only by Settings app.
- super.onActivityCreated() is not called at the beggining of
  WifiSettings#onActivityCreated(), the parent method assumes
  the child should have prepared PreferenceScreen, while
  WifiSettings cannot do until the parent Activity is ready.
- Call SetHasOptionMenu() should be called AFTER the parent
  Activity is ready. It is not documented, so it would be better
  to file another bug.
- Add exception to proguard...

Change-Id: Iebd27f0cb0abdbee9b4b1cc9b00f4bf127f7815d
parent e742d42d
......@@ -83,10 +83,11 @@
<!-- Top-level settings -->
<activity android:name=".wifi.WifiSettings"
<activity-alias android:name=".wifi.WifiSettings"
android:label="@string/wifi_settings"
android:configChanges="orientation|keyboardHidden"
android:clearTaskOnLaunch="true"
android:targetActivity="Settings"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......@@ -96,7 +97,7 @@
<category android:name="android.intent.category.VOICE_LAUNCH" />
<category android:name="com.android.settings.SHORTCUT" />
</intent-filter>
</activity>
</activity-alias>
<activity android:name=".wifi.AdvancedSettings"
android:label="@string/wifi_ip_settings_titlebar"
......
# Keep all Fragments in this package, which are used by reflection.
-keep class com.android.settings.*Fragment
-keep class com.android.settings.*Settings
-keep class com.android.settings.wifi.*Settings
-keep class com.android.settings.deviceinfo.*
......@@ -24,4 +24,44 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!-- button_bar -->
<RelativeLayout android:id="@+id/button_bar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="0"
android:background="@android:drawable/bottom_bar"
android:visibility="gone">
<Button android:id="@+id/back_button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_alignParentLeft="true"
android:drawablePadding="3dip"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true">
<Button android:id="@+id/skip_button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:text="@string/skip_button_label"
android:visibility="gone"
/>
<Button android:id="@+id/next_button"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:drawableRight="@drawable/ic_btn_next"
android:drawablePadding="3dip"
android:text="@string/next_button_label"
/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
......@@ -32,13 +32,10 @@
android:persistent="false" />
<PreferenceScreen
android:fragment="com.android.settings.wifi.WifiSettings"
android:key="wifi_settings"
android:title="@string/wifi_settings"
android:summary="@string/wifi_settings_summary" >
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.android.settings"
android:targetClass="com.android.settings.wifi.WifiSettings" />
</PreferenceScreen>
<CheckBoxPreference
......
......@@ -98,10 +98,11 @@ public class Settings extends Activity
if (showFragment(intent.getComponent().getClassName(), intent.getExtras())) {
mMainPane.setVisibility(View.GONE);
}
}
Fragment topLevel = getFragmentManager().findFragmentById(R.id.top_level);
if (topLevel != null) {
((TopLevelSettings) topLevel).selectFirst();
} else {
Fragment topLevel = getFragmentManager().findFragmentById(R.id.top_level);
if (topLevel != null) {
((TopLevelSettings) topLevel).selectFirst();
}
}
}
}
......@@ -165,13 +166,13 @@ public class Settings extends Activity
}
public void onCreated(SettingsPreferenceFragment fragment) {
Log.d(TAG, "Fragment created " + fragment);
Log.d(TAG, "Fragment created " + fragment + " (name: " + fragment.getClass() + ")");
addToBreadCrumbs(fragment);
}
public void onDestroyed(SettingsPreferenceFragment fragment) {
removeFromBreadCrumbs(fragment);
Log.d(TAG, "Fragment destroyed " + fragment);
Log.d(TAG, "Fragment destroyed " + fragment + " (name: " + fragment.getClass() + ")");
}
public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
......
......@@ -16,14 +16,21 @@
package com.android.settings;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* Base class for Settings fragments, with some helper functions and dialog management.
......@@ -32,10 +39,18 @@ public class SettingsPreferenceFragment extends PreferenceFragment {
private static final String TAG = "SettingsPreferenceFragment";
// Originally from PreferenceActivity.
private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";
private static final String EXTRA_PREFS_SHOW_SKIP = "extra_prefs_show_skip";
private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";
private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";
private SettingsDialogFragment mDialogFragment;
private OnStateListener mOnStateListener;
private Button mNextButton;
interface OnStateListener {
void onCreated(SettingsPreferenceFragment fragment);
......@@ -53,6 +68,8 @@ public class SettingsPreferenceFragment extends PreferenceFragment {
if (mOnStateListener != null) {
mOnStateListener.onCreated(this);
}
setupButtonBar();
}
@Override
......@@ -134,4 +151,101 @@ public class SettingsPreferenceFragment extends PreferenceFragment {
return mDialogId;
}
}
protected boolean hasNextButton() {
return mNextButton != null;
}
protected Button getNextButton() {
return mNextButton;
}
/**
* Sets up Button Bar possibly required in the Fragment. Probably available only in
* phones.
*
* Previously {@link PreferenceActivity} had the capability as hidden functionality.
*/
private void setupButtonBar() {
// Originally from PreferenceActivity, which has had button bar inside its layout.
final Activity activity = getActivity();
final Intent intent = activity.getIntent();
final View buttonBar = activity.findViewById(com.android.internal.R.id.button_bar);
if (!intent.getBooleanExtra(EXTRA_PREFS_SHOW_BUTTON_BAR, false) || buttonBar == null) {
return;
}
buttonBar.setVisibility(View.VISIBLE);
View tmpView = activity.findViewById(com.android.internal.R.id.back_button);
if (tmpView != null) {
// TODO: Assume this is pressed only in single pane, finishing current Activity.
try {
final Button backButton = (Button)tmpView;
backButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
activity.setResult(Activity.RESULT_CANCELED);
activity.finish();
}
});
if (intent.hasExtra(EXTRA_PREFS_SET_BACK_TEXT)) {
String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_BACK_TEXT);
if (TextUtils.isEmpty(buttonText)) {
backButton.setVisibility(View.GONE);
}
else {
backButton.setText(buttonText);
}
}
} catch (ClassCastException e) {
Log.w(TAG, "The view originally for back_button is used not as Button. " +
"Ignored.");
}
}
tmpView = activity.findViewById(com.android.internal.R.id.skip_button);
if (tmpView != null) {
try {
final Button skipButton = (Button)tmpView;
skipButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
activity.setResult(Activity.RESULT_OK);
activity.finish();
}
});
if (intent.getBooleanExtra(EXTRA_PREFS_SHOW_SKIP, false)) {
skipButton.setVisibility(View.VISIBLE);
}
} catch (ClassCastException e) {
Log.w(TAG, "The view originally for skip_button is used not as Button. " +
"Ignored.");
}
}
tmpView = activity.findViewById(com.android.internal.R.id.next_button);
if (tmpView != null) {
try {
mNextButton = (Button)tmpView;
mNextButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
activity.setResult(Activity.RESULT_OK);
activity.finish();
}
});
// set our various button parameters
if (intent.hasExtra(EXTRA_PREFS_SET_NEXT_TEXT)) {
String buttonText = intent.getStringExtra(EXTRA_PREFS_SET_NEXT_TEXT);
if (TextUtils.isEmpty(buttonText)) {
mNextButton.setVisibility(View.GONE);
}
else {
mNextButton.setText(buttonText);
}
}
} catch (ClassCastException e) {
Log.w(TAG, "The view originally for next_button is used not as Button. " +
"Ignored.");
mNextButton = null;
}
}
}
}
......@@ -16,9 +16,7 @@
package com.android.settings.wifi;
import com.android.settings.ProgressCategory;
import com.android.settings.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
......@@ -31,7 +29,6 @@ import android.net.wifi.ScanResult;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.Status;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
......@@ -39,24 +36,29 @@ import android.os.Handler;
import android.os.Message;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.Settings.Secure;
import android.security.Credentials;
import android.security.KeyStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.Toast;
import com.android.settings.ProgressCategory;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import java.util.ArrayList;
import java.util.List;
public class WifiSettings extends PreferenceActivity implements DialogInterface.OnClickListener {
public class WifiSettings extends SettingsPreferenceFragment
implements DialogInterface.OnClickListener {
private static final int MENU_ID_SCAN = Menu.FIRST;
private static final int MENU_ID_ADVANCED = Menu.FIRST + 1;
private static final int MENU_ID_CONNECT = Menu.FIRST + 2;
......@@ -108,28 +110,33 @@ public class WifiSettings extends PreferenceActivity implements DialogInterface.
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
public void onActivityCreated(Bundle savedInstanceState) {
// We don't call super.onActivityCreated() here, since it assumes we already set up
// Preference (probably in onCreate()), while WifiSettings exceptionally set it up in
// this method.
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final Activity activity = getActivity();
final Intent intent = activity.getIntent();
// if we're supposed to enable/disable the Next button based on our current connection
// state, start it off in the right state
mEnableNextOnConnection = getIntent().getBooleanExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, false);
mEnableNextOnConnection = intent.getBooleanExtra(EXTRA_ENABLE_NEXT_ON_CONNECT, false);
if (mEnableNextOnConnection && hasNextButton()) {
ConnectivityManager connectivity = (ConnectivityManager)
getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
getNextButton().setEnabled(info.isConnected());
}
}
if (getIntent().getBooleanExtra("only_access_points", false)) {
if (intent.getBooleanExtra("only_access_points", false)) {
addPreferencesFromResource(R.xml.wifi_access_points);
} else {
addPreferencesFromResource(R.xml.wifi_settings);
mWifiEnabler = new WifiEnabler(this,
mWifiEnabler = new WifiEnabler(activity,
(CheckBoxPreference) findPreference("enable_wifi"));
mNotifyOpenNetworks =
(CheckBoxPreference) findPreference("notify_open_networks");
......@@ -137,20 +144,25 @@ public class WifiSettings extends PreferenceActivity implements DialogInterface.
Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 0) == 1);
}
// After confirming PreferenceScreen is available, we call super.
super.onActivityCreated(savedInstanceState);
mAccessPoints = (ProgressCategory) findPreference("access_points");
mAccessPoints.setOrderingAsAdded(false);
mAddNetwork = findPreference("add_network");
registerForContextMenu(getListView());
setHasOptionsMenu(true);
}
@Override
protected void onResume() {
public void onResume() {
super.onResume();
if (mWifiEnabler != null) {
mWifiEnabler.resume();
}
registerReceiver(mReceiver, mFilter);
getActivity().registerReceiver(mReceiver, mFilter);
if (mKeyStoreNetworkId != -1 && KeyStore.getInstance().test() == KeyStore.NO_ERROR) {
mWifiManager.connectNetwork(mKeyStoreNetworkId);
}
......@@ -159,12 +171,12 @@ public class WifiSettings extends PreferenceActivity implements DialogInterface.
}
@Override
protected void onPause() {
public void onPause() {
super.onPause();
if (mWifiEnabler != null) {
mWifiEnabler.pause();
}
unregisterReceiver(mReceiver);
getActivity().unregisterReceiver(mReceiver);
mScanner.pause();
if (mDialog != null) {
mDialog.dismiss();
......@@ -173,12 +185,12 @@ public class WifiSettings extends PreferenceActivity implements DialogInterface.
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add(Menu.NONE, MENU_ID_SCAN, 0, R.string.wifi_menu_scan)
.setIcon(R.drawable.ic_menu_scan_network);
menu.add(Menu.NONE, MENU_ID_ADVANCED, 0, R.string.wifi_menu_advanced)
.setIcon(android.R.drawable.ic_menu_manage);
return super.onCreateOptionsMenu(menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
......@@ -190,7 +202,7 @@ public class WifiSettings extends PreferenceActivity implements DialogInterface.
}
return true;
case MENU_ID_ADVANCED:
startActivity(new Intent(this, AdvancedSettings.class));
startActivity(new Intent(getActivity(), AdvancedSettings.class));
return true;
}
return super.onOptionsItemSelected(item);
......@@ -295,7 +307,7 @@ public class WifiSettings extends PreferenceActivity implements DialogInterface.
if (mDialog != null) {
mDialog.dismiss();
}
mDialog = new WifiDialog(this, this, accessPoint, edit);
mDialog = new WifiDialog(getActivity(), this, accessPoint, edit);
mDialog.show();
}
......@@ -303,7 +315,7 @@ public class WifiSettings extends PreferenceActivity implements DialogInterface.
if (WifiDialog.requireKeyStore(config) &&
KeyStore.getInstance().test() != KeyStore.NO_ERROR) {
mKeyStoreNetworkId = config.networkId;
Credentials.getInstance().unlock(this);
Credentials.getInstance().unlock(getActivity());
return true;
}
return false;
......@@ -315,7 +327,7 @@ public class WifiSettings extends PreferenceActivity implements DialogInterface.
List<WifiConfiguration> configs = mWifiManager.getConfiguredNetworks();
if (configs != null) {
for (WifiConfiguration config : configs) {
AccessPoint accessPoint = new AccessPoint(this, config);
AccessPoint accessPoint = new AccessPoint(getActivity(), config);
accessPoint.update(mLastInfo, mLastState);
accessPoints.add(accessPoint);
}
......@@ -337,7 +349,7 @@ public class WifiSettings extends PreferenceActivity implements DialogInterface.
}
}
if (!found) {
accessPoints.add(new AccessPoint(this, result));
accessPoints.add(new AccessPoint(getActivity(), result));
}
}
}
......@@ -424,7 +436,7 @@ public class WifiSettings extends PreferenceActivity implements DialogInterface.
mRetry = 0;
} else if (++mRetry >= 3) {
mRetry = 0;
Toast.makeText(WifiSettings.this, R.string.wifi_fail_to_scan,
Toast.makeText(getActivity(), R.string.wifi_fail_to_scan,
Toast.LENGTH_LONG).show();
return;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment