OSDN Git Service

Don't crash if there's no bluetooth functionality (such as in an emulator) do not...
[android-x86/packages-apps-Settings.git] / src / com / android / settings / bluetooth / BluetoothSettings.java
old mode 100644 (file)
new mode 100755 (executable)
index 91bcffd..7c8cb6e
@@ -68,8 +68,8 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
 
     private PreferenceGroup mAvailableDevicesCategory;
     private boolean mAvailableDevicesCategoryIsPresent;
+    private boolean mActivityStarted;
 
-    private View mView;
     private TextView mEmptyView;
 
     private final IntentFilter mIntentFilter;
@@ -98,17 +98,11 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
     }
 
     @Override
-    public View onCreateView(LayoutInflater inflater, ViewGroup container,
-            Bundle savedInstanceState) {
-        mView = inflater.inflate(R.layout.custom_preference_list_fragment, container, false);
-        return mView;
-    }
-
-    @Override
     public void onActivityCreated(Bundle savedInstanceState) {
         super.onActivityCreated(savedInstanceState);
+        mActivityStarted = (savedInstanceState == null);    // don't auto start scan after rotation
 
-        mEmptyView = (TextView) mView.findViewById(R.id.empty);
+        mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
         getListView().setEmptyView(mEmptyView);
     }
 
@@ -144,21 +138,26 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
     public void onResume() {
         // resume BluetoothEnabler before calling super.onResume() so we don't get
         // any onDeviceAdded() callbacks before setting up view in updateContent()
-        mBluetoothEnabler.resume();
+        if (mBluetoothEnabler != null) {
+            mBluetoothEnabler.resume();
+        }
         super.onResume();
 
         if (mDiscoverableEnabler != null) {
             mDiscoverableEnabler.resume();
         }
         getActivity().registerReceiver(mReceiver, mIntentFilter);
-
-        updateContent(mLocalAdapter.getBluetoothState());
+        if (mLocalAdapter != null) {
+            updateContent(mLocalAdapter.getBluetoothState(), mActivityStarted);
+        }
     }
 
     @Override
     public void onPause() {
         super.onPause();
-        mBluetoothEnabler.pause();
+        if (mBluetoothEnabler != null) {
+            mBluetoothEnabler.pause();
+        }
         getActivity().unregisterReceiver(mReceiver);
         if (mDiscoverableEnabler != null) {
             mDiscoverableEnabler.pause();
@@ -167,6 +166,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
 
     @Override
     public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
+        if (mLocalAdapter == null) return;
         boolean bluetoothIsEnabled = mLocalAdapter.getBluetoothState() == BluetoothAdapter.STATE_ON;
         boolean isDiscovering = mLocalAdapter.isDiscovering();
         int textId = isDiscovering ? R.string.bluetooth_searching_for_devices :
@@ -234,7 +234,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
         preferenceGroup.setEnabled(true);
     }
 
-    private void updateContent(int bluetoothState) {
+    private void updateContent(int bluetoothState, boolean scanState) {
         final PreferenceScreen preferenceScreen = getPreferenceScreen();
         int messageId = 0;
 
@@ -242,6 +242,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
             case BluetoothAdapter.STATE_ON:
                 preferenceScreen.removeAll();
                 preferenceScreen.setOrderingAsAdded(true);
+                mDevicePreferenceMap.clear();
 
                 // This device
                 if (mMyDevicePreference == null) {
@@ -297,7 +298,14 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
 
                 if (numberOfPairedDevices == 0) {
                     preferenceScreen.removePreference(mPairedDevicesCategory);
-                    startScanning();
+                    if (scanState == true) {
+                        mActivityStarted = false;
+                        startScanning();
+                    } else {
+                        if (!mAvailableDevicesCategoryIsPresent) {
+                            getPreferenceScreen().addPreference(mAvailableDevicesCategory);
+                        }
+                    }
                 }
                 getActivity().invalidateOptionsMenu();
                 return; // not break
@@ -324,7 +332,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
     @Override
     public void onBluetoothStateChanged(int bluetoothState) {
         super.onBluetoothStateChanged(bluetoothState);
-        updateContent(bluetoothState);
+        updateContent(bluetoothState, true);
     }
 
     @Override
@@ -337,7 +345,7 @@ public final class BluetoothSettings extends DeviceListPreferenceFragment {
     public void onDeviceBondStateChanged(CachedBluetoothDevice cachedDevice, int bondState) {
         setDeviceListGroup(getPreferenceScreen());
         removeAllDevices();
-        updateContent(mLocalAdapter.getBluetoothState());
+        updateContent(mLocalAdapter.getBluetoothState(), false);
     }
 
     private final View.OnClickListener mDeviceProfilesListener = new View.OnClickListener() {