Merge "Foreground/background network stats pie chart."
diff --git a/src/com/android/settings/bluetooth/BluetoothNameDialogFragment.java b/src/com/android/settings/bluetooth/BluetoothNameDialogFragment.java
index c00aff3..4996858 100644
--- a/src/com/android/settings/bluetooth/BluetoothNameDialogFragment.java
+++ b/src/com/android/settings/bluetooth/BluetoothNameDialogFragment.java
@@ -40,7 +40,7 @@
 /**
  * Dialog fragment for renaming the local Bluetooth device.
  */
-final class BluetoothNameDialogFragment extends DialogFragment implements TextWatcher {
+public final class BluetoothNameDialogFragment extends DialogFragment implements TextWatcher {
     private static final int BLUETOOTH_NAME_MAX_LENGTH_BYTES = 248;
 
     private AlertDialog mAlertDialog;
@@ -54,6 +54,13 @@
     // This flag is set when the name is updated by code, to distinguish from user changes
     private boolean mDeviceNameUpdated;
 
+    // This flag is set when the user edits the name (preserved on rotation)
+    private boolean mDeviceNameEdited;
+
+    // Key to save the edited name and edit status for restoring after rotation
+    private static final String KEY_NAME = "device_name";
+    private static final String KEY_NAME_EDITED = "device_name_edited";
+
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -68,16 +75,22 @@
         }
     };
 
-    public BluetoothNameDialogFragment(LocalBluetoothAdapter adapter) {
-        mLocalAdapter = adapter;
+    public BluetoothNameDialogFragment() {
+        LocalBluetoothManager localManager = LocalBluetoothManager.getInstance(getActivity());
+        mLocalAdapter = localManager.getBluetoothAdapter();
     }
 
     @Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
+        String deviceName = mLocalAdapter.getName();
+        if (savedInstanceState != null) {
+            deviceName = savedInstanceState.getString(KEY_NAME, deviceName);
+            mDeviceNameEdited = savedInstanceState.getBoolean(KEY_NAME_EDITED, false);
+        }
         mAlertDialog = new AlertDialog.Builder(getActivity())
                 .setIcon(android.R.drawable.ic_dialog_info)
                 .setTitle(R.string.bluetooth_rename_device)
-                .setView(createDialogView())
+                .setView(createDialogView(deviceName))
                 .setPositiveButton(R.string.bluetooth_rename_button,
                         new DialogInterface.OnClickListener() {
                             public void onClick(DialogInterface dialog, int which) {
@@ -94,7 +107,13 @@
         return mAlertDialog;
     }
 
-    private View createDialogView() {
+    @Override
+    public void onSaveInstanceState(Bundle outState) {
+        outState.putString(KEY_NAME, mDeviceNameView.getText().toString());
+        outState.putBoolean(KEY_NAME_EDITED, mDeviceNameEdited);
+    }
+
+    private View createDialogView(String deviceName) {
         final LayoutInflater layoutInflater = (LayoutInflater)getActivity()
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         View view = layoutInflater.inflate(R.layout.dialog_edittext, null);
@@ -102,6 +121,7 @@
         mDeviceNameView.setFilters(new InputFilter[] {
                 new Utf8ByteLengthFilter(BLUETOOTH_NAME_MAX_LENGTH_BYTES)
         });
+        mDeviceNameView.setText(deviceName);    // set initial value before adding listener
         mDeviceNameView.addTextChangedListener(this);
         return view;
     }
@@ -119,13 +139,12 @@
         super.onResume();
         if (mOkButton == null) {
             mOkButton = mAlertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
-            mOkButton.setEnabled(false);    // Ok button is enabled when the user edits the name
+            mOkButton.setEnabled(mDeviceNameEdited);    // Ok button enabled after user edits
         }
         IntentFilter filter = new IntentFilter();
         filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
         filter.addAction(BluetoothAdapter.ACTION_LOCAL_NAME_CHANGED);
         getActivity().registerReceiver(mReceiver, filter);
-        updateDeviceName();
     }
 
     @Override
@@ -137,6 +156,7 @@
     void updateDeviceName() {
         if (mLocalAdapter != null && mLocalAdapter.isEnabled()) {
             mDeviceNameUpdated = true;
+            mDeviceNameEdited = false;
             mDeviceNameView.setText(mLocalAdapter.getName());
         }
     }
@@ -147,7 +167,10 @@
             mDeviceNameUpdated = false;
             mOkButton.setEnabled(false);
         } else {
-            mOkButton.setEnabled(s.length() != 0);
+            mDeviceNameEdited = true;
+            if (mOkButton != null) {
+                mOkButton.setEnabled(s.length() != 0);
+            }
         }
     }
 
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index af21149..91bcffd 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -194,12 +194,12 @@
                 return true;
 
             case MENU_ID_RENAME_DEVICE:
-                new BluetoothNameDialogFragment(mLocalAdapter).show(
+                new BluetoothNameDialogFragment().show(
                         getFragmentManager(), "rename device");
                 return true;
 
             case MENU_ID_VISIBILITY_TIMEOUT:
-                new BluetoothVisibilityTimeoutFragment(mDiscoverableEnabler).show(
+                new BluetoothVisibilityTimeoutFragment().show(
                         getFragmentManager(), "visibility timeout");
                 return true;
 
@@ -261,6 +261,8 @@
                     mDiscoverableEnabler = new BluetoothDiscoverableEnabler(getActivity(),
                             mLocalAdapter, mMyDevicePreference);
                     mDiscoverableEnabler.resume();
+                    LocalBluetoothManager.getInstance(getActivity()).setDiscoverableEnabler(
+                            mDiscoverableEnabler);
                 }
 
                 // Paired devices category
diff --git a/src/com/android/settings/bluetooth/BluetoothVisibilityTimeoutFragment.java b/src/com/android/settings/bluetooth/BluetoothVisibilityTimeoutFragment.java
index 7c518fb..a65c6c1 100644
--- a/src/com/android/settings/bluetooth/BluetoothVisibilityTimeoutFragment.java
+++ b/src/com/android/settings/bluetooth/BluetoothVisibilityTimeoutFragment.java
@@ -41,13 +41,14 @@
 /**
  * Dialog fragment for setting the discoverability timeout.
  */
-final class BluetoothVisibilityTimeoutFragment extends DialogFragment
+public final class BluetoothVisibilityTimeoutFragment extends DialogFragment
         implements DialogInterface.OnClickListener {
 
     private final BluetoothDiscoverableEnabler mDiscoverableEnabler;
 
-    public BluetoothVisibilityTimeoutFragment(BluetoothDiscoverableEnabler enabler) {
-        mDiscoverableEnabler = enabler;
+    public BluetoothVisibilityTimeoutFragment() {
+        mDiscoverableEnabler = LocalBluetoothManager.getInstance(getActivity())
+                .getDiscoverableEnabler();
     }
 
     @Override
diff --git a/src/com/android/settings/bluetooth/LocalBluetoothManager.java b/src/com/android/settings/bluetooth/LocalBluetoothManager.java
old mode 100755
new mode 100644
index a1edca1..3357e59
--- a/src/com/android/settings/bluetooth/LocalBluetoothManager.java
+++ b/src/com/android/settings/bluetooth/LocalBluetoothManager.java
@@ -36,6 +36,8 @@
     /** If a BT-related activity is in the foreground, this will be it. */
     private Context mForegroundActivity;
 
+    private BluetoothDiscoverableEnabler mDiscoverableEnabler;
+
     private final LocalBluetoothAdapter mLocalAdapter;
 
     private final CachedBluetoothDeviceManager mCachedDeviceManager;
@@ -60,6 +62,14 @@
         return sInstance;
     }
 
+    public void setDiscoverableEnabler(BluetoothDiscoverableEnabler discoverableEnabler) {
+        mDiscoverableEnabler = discoverableEnabler;
+    }
+
+    public BluetoothDiscoverableEnabler getDiscoverableEnabler() {
+        return mDiscoverableEnabler;
+    }
+
     private LocalBluetoothManager(LocalBluetoothAdapter adapter, Context context) {
         mContext = context;
         mLocalAdapter = adapter;