Fix Bluetooth device details crash on screen rotation

We were getting the following exception when you rotated the Bluetooth
device details screen:

java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.android.settings/com.android.settings.SubSettings}:
java.lang.IllegalStateException: This Activity already has an action bar
supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR
and set android:windowActionBar to false in your theme to use a Toolbar
instead.

It turns out that allowing EntityHeaderController to inflate the
settings_entity_header.xml view seems to cause this - if you instead
manually include a LayoutPreference and hand that to
EntityHeaderController, you don't have the problem.

The rotation failure couldn't be tested with Robolectric because our
version doesn't support using FragmentTestUtil.startFragment for
fragments which use PreferenceScreen's ("sorry, not yet
implemented"). So instead this includes an app test.

Bug: 62447414
Test: runtest --path=BluetoothDeviceDetailsRotationTest.java
Change-Id: I8d052d1f4ab6e2b0ca5c0e513ec366bdcc382d99
This commit is contained in:
Antony Sargent
2017-06-29 15:32:12 -07:00
parent 4e34839a1e
commit 1b6e7d76ed
9 changed files with 142 additions and 12 deletions

View File

@@ -18,6 +18,11 @@
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
android:title="@string/bluetooth_device_advanced_title"> android:title="@string/bluetooth_device_advanced_title">
<com.android.settings.applications.LayoutPreference
android:key="bluetooth_device_header"
android:layout="@layout/settings_entity_header"
android:selectable="false"/>
<com.android.settings.applications.LayoutPreference <com.android.settings.applications.LayoutPreference
android:key="action_buttons" android:key="action_buttons"
android:layout="@layout/app_action_buttons" android:layout="@layout/app_action_buttons"

View File

@@ -22,6 +22,7 @@ import android.support.v14.preference.PreferenceFragment;
import android.support.v7.preference.PreferenceScreen; import android.support.v7.preference.PreferenceScreen;
import android.util.Pair; import android.util.Pair;
import com.android.settings.R;
import com.android.settings.applications.LayoutPreference; import com.android.settings.applications.LayoutPreference;
import com.android.settings.widget.EntityHeaderController; import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.CachedBluetoothDevice;
@@ -31,6 +32,7 @@ import com.android.settingslib.core.lifecycle.Lifecycle;
* This class adds a header with device name and status (connected/disconnected, etc.). * This class adds a header with device name and status (connected/disconnected, etc.).
*/ */
public class BluetoothDetailsHeaderController extends BluetoothDetailsController { public class BluetoothDetailsHeaderController extends BluetoothDetailsController {
private static final String KEY_DEVICE_HEADER = "bluetooth_device_header";
private EntityHeaderController mHeaderController; private EntityHeaderController mHeaderController;
@@ -41,10 +43,11 @@ public class BluetoothDetailsHeaderController extends BluetoothDetailsController
@Override @Override
protected void init(PreferenceScreen screen) { protected void init(PreferenceScreen screen) {
final LayoutPreference headerPreference =
(LayoutPreference) screen.findPreference(KEY_DEVICE_HEADER);
mHeaderController = EntityHeaderController.newInstance(mFragment.getActivity(), mFragment, mHeaderController = EntityHeaderController.newInstance(mFragment.getActivity(), mFragment,
null); headerPreference.findViewById(R.id.entity_header));
LayoutPreference pref = mHeaderController.done(mFragment.getActivity(), mContext); screen.addPreference(headerPreference);
screen.addPreference(pref);
} }
protected void setHeaderProperties() { protected void setHeaderProperties() {
@@ -66,6 +69,6 @@ public class BluetoothDetailsHeaderController extends BluetoothDetailsController
@Override @Override
public String getPreferenceKey() { public String getPreferenceKey() {
return EntityHeaderController.PREF_KEY_APP_HEADER; return KEY_DEVICE_HEADER;
} }
} }

View File

@@ -45,7 +45,7 @@ public class BluetoothDetailsMacAddressController extends BluetoothDetailsContro
protected void init(PreferenceScreen screen) { protected void init(PreferenceScreen screen) {
mFooterPreference = mFooterPreferenceMixin.createFooterPreference(); mFooterPreference = mFooterPreferenceMixin.createFooterPreference();
mFooterPreference.setTitle(mContext.getString( mFooterPreference.setTitle(mContext.getString(
R.string.bluetooth_device_mac_address, mCachedDevice.getDevice().getAddress())); R.string.bluetooth_device_mac_address, mCachedDevice.getAddress()));
} }
@Override @Override

View File

@@ -44,6 +44,22 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
@VisibleForTesting @VisibleForTesting
static int EDIT_DEVICE_NAME_ITEM_ID = Menu.FIRST; static int EDIT_DEVICE_NAME_ITEM_ID = Menu.FIRST;
/**
* An interface to let tests override the normal mechanism for looking up the
* CachedBluetoothDevice and LocalBluetoothManager, and substitute their own mocks instead.
* This is only needed in situations where you instantiate the fragment indirectly (eg via an
* intent) and can't use something like spying on an instance you construct directly via
* newInstance.
*/
@VisibleForTesting
interface TestDataFactory {
CachedBluetoothDevice getDevice(String deviceAddress);
LocalBluetoothManager getManager(Context context);
}
@VisibleForTesting
static TestDataFactory sTestDataFactory;
private String mDeviceAddress; private String mDeviceAddress;
private LocalBluetoothManager mManager; private LocalBluetoothManager mManager;
private CachedBluetoothDevice mCachedDevice; private CachedBluetoothDevice mCachedDevice;
@@ -54,11 +70,17 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
@VisibleForTesting @VisibleForTesting
LocalBluetoothManager getLocalBluetoothManager(Context context) { LocalBluetoothManager getLocalBluetoothManager(Context context) {
if (sTestDataFactory != null) {
return sTestDataFactory.getManager(context);
}
return Utils.getLocalBtManager(context); return Utils.getLocalBtManager(context);
} }
@VisibleForTesting @VisibleForTesting
CachedBluetoothDevice getCachedDevice(String deviceAddress) { CachedBluetoothDevice getCachedDevice(String deviceAddress) {
if (sTestDataFactory != null) {
return sTestDataFactory.getDevice(deviceAddress);
}
BluetoothDevice remoteDevice = BluetoothDevice remoteDevice =
mManager.getBluetoothAdapter().getRemoteDevice(deviceAddress); mManager.getBluetoothAdapter().getRemoteDevice(deviceAddress);
return mManager.getCachedDeviceManager().findDevice(remoteDevice); return mManager.getCachedDeviceManager().findDevice(remoteDevice);

View File

@@ -0,0 +1,99 @@
/*
* Copyright (C) 2017 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.bluetooth;
import static org.mockito.Mockito.when;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;
import com.android.settings.SettingsActivity;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
@SmallTest
public class BluetoothDeviceDetailsRotationTest {
private Context mContext;
private UiDevice mUiDevice;
private Instrumentation mInstrumentation;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private CachedBluetoothDevice mCachedDevice;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private LocalBluetoothManager mBluetoothManager;
private String mDeviceAddress;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mContext = InstrumentationRegistry.getTargetContext();
mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mDeviceAddress = "AA:BB:CC:DD:EE:FF";
when(mCachedDevice.getAddress()).thenReturn(mDeviceAddress);
when(mCachedDevice.getName()).thenReturn("Mock Device");
BluetoothDeviceDetailsFragment.sTestDataFactory =
new BluetoothDeviceDetailsFragment.TestDataFactory() {
@Override
public CachedBluetoothDevice getDevice(String deviceAddress) {
return mCachedDevice;
}
@Override
public LocalBluetoothManager getManager(Context context) {
return mBluetoothManager;
}
};
}
@Test
public void rotation() {
Intent intent = new Intent("android.settings.BLUETOOTH_SETTINGS");
SettingsActivity activity = (SettingsActivity) mInstrumentation.startActivitySync(intent);
Bundle args = new Bundle(1);
args.putString(BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS, mDeviceAddress);
activity.startPreferencePanel(null, BluetoothDeviceDetailsFragment.class.getName(), args,
0, null, null, 0);
try {
mUiDevice.setOrientationLeft();
mUiDevice.setOrientationNatural();
mUiDevice.setOrientationRight();
mUiDevice.setOrientationNatural();
} catch (RemoteException e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -142,8 +142,9 @@ public class BluetoothDetailsControllerTestBase {
when(mCachedDevice.isConnected()).thenReturn(config.isConnected()); when(mCachedDevice.isConnected()).thenReturn(config.isConnected());
when(mCachedDevice.getConnectionSummary()).thenReturn(config.getConnectionSummary()); when(mCachedDevice.getConnectionSummary()).thenReturn(config.getConnectionSummary());
mDevice = mBluetoothAdapter.getRemoteDevice(mDeviceConfig.getAddress()); mDevice = mBluetoothAdapter.getRemoteDevice(config.getAddress());
when(mCachedDevice.getDevice()).thenReturn(mDevice); when(mCachedDevice.getDevice()).thenReturn(mDevice);
when(mCachedDevice.getAddress()).thenReturn(config.getAddress());
} }
/** /**

View File

@@ -25,13 +25,14 @@ import static org.mockito.Mockito.when;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import com.android.settings.R;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.testutils.SettingsRobolectricTestRunner; import com.android.settings.testutils.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig; import com.android.settings.TestConfig;
import com.android.settings.testutils.FakeFeatureFactory; import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.SettingsShadowBluetoothDevice; import com.android.settings.testutils.shadow.SettingsShadowBluetoothDevice;
import com.android.settings.testutils.shadow.ShadowEntityHeaderController; import com.android.settings.testutils.shadow.ShadowEntityHeaderController;
import com.android.settings.widget.EntityHeaderController; import com.android.settings.widget.EntityHeaderController;
import com.android.settingslib.R;
import com.android.settingslib.bluetooth.LocalBluetoothProfile; import com.android.settingslib.bluetooth.LocalBluetoothProfile;
import org.junit.After; import org.junit.After;
@@ -49,7 +50,7 @@ import java.util.ArrayList;
shadows={SettingsShadowBluetoothDevice.class, ShadowEntityHeaderController.class}) shadows={SettingsShadowBluetoothDevice.class, ShadowEntityHeaderController.class})
public class BluetoothDetailsHeaderControllerTest extends BluetoothDetailsControllerTestBase { public class BluetoothDetailsHeaderControllerTest extends BluetoothDetailsControllerTestBase {
private BluetoothDetailsHeaderController mController; private BluetoothDetailsHeaderController mController;
private Preference mPreference; private LayoutPreference mPreference;
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private EntityHeaderController mHeaderController; private EntityHeaderController mHeaderController;
@@ -61,7 +62,7 @@ public class BluetoothDetailsHeaderControllerTest extends BluetoothDetailsContro
ShadowEntityHeaderController.setUseMock(mHeaderController); ShadowEntityHeaderController.setUseMock(mHeaderController);
mController = new BluetoothDetailsHeaderController(mContext, mFragment, mCachedDevice, mController = new BluetoothDetailsHeaderController(mContext, mFragment, mCachedDevice,
mLifecycle); mLifecycle);
mPreference = new Preference(mContext); mPreference = new LayoutPreference(mContext, R.layout.settings_entity_header);
mPreference.setKey(mController.getPreferenceKey()); mPreference.setKey(mController.getPreferenceKey());
mScreen.addPreference(mPreference); mScreen.addPreference(mPreference);
setupDevice(mDeviceConfig); setupDevice(mDeviceConfig);
@@ -80,7 +81,6 @@ public class BluetoothDetailsHeaderControllerTest extends BluetoothDetailsContro
verify(mHeaderController).setIcon(any(Drawable.class)); verify(mHeaderController).setIcon(any(Drawable.class));
verify(mHeaderController).setIconContentDescription(any(String.class)); verify(mHeaderController).setIconContentDescription(any(String.class));
verify(mHeaderController).setSummary(any(String.class)); verify(mHeaderController).setSummary(any(String.class));
verify(mHeaderController).done(mActivity, mContext);
verify(mHeaderController).done(mActivity, false); verify(mHeaderController).done(mActivity, false);
} }

View File

@@ -75,7 +75,7 @@ public class BluetoothDeviceDetailsFragmentTest {
doReturn(mLocalManager).when(mFragment).getLocalBluetoothManager(any()); doReturn(mLocalManager).when(mFragment).getLocalBluetoothManager(any());
doReturn(mCachedDevice).when(mFragment).getCachedDevice(any()); doReturn(mCachedDevice).when(mFragment).getCachedDevice(any());
when(mCachedDevice.getDevice().getAddress()).thenReturn(deviceAddress); when(mCachedDevice.getAddress()).thenReturn(deviceAddress);
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString(BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS, deviceAddress); args.putString(BluetoothDeviceDetailsFragment.KEY_DEVICE_ADDRESS, deviceAddress);
mFragment.setArguments(args); mFragment.setArguments(args);

View File

@@ -66,7 +66,7 @@ public class RemoteDeviceNameDialogFragmentTest {
FakeFeatureFactory.setupForTest(mContext); FakeFeatureFactory.setupForTest(mContext);
String deviceAddress = "55:66:77:88:99:AA"; String deviceAddress = "55:66:77:88:99:AA";
when(mCachedDevice.getDevice().getAddress()).thenReturn(deviceAddress); when(mCachedDevice.getAddress()).thenReturn(deviceAddress);
mFragment = spy(RemoteDeviceNameDialogFragment.newInstance(mCachedDevice)); mFragment = spy(RemoteDeviceNameDialogFragment.newInstance(mCachedDevice));
doReturn(mCachedDevice).when(mFragment).getDevice(any()); doReturn(mCachedDevice).when(mFragment).getDevice(any());
} }