Merge changes Ie1f934b4,Ic3b045a6
* changes: Create BluetoothSwitchPreference Add ConnectedUsbDeviceUpdater
This commit is contained in:
@@ -393,6 +393,8 @@
|
|||||||
<string name="bluetooth_paired_device_title">Your devices</string>
|
<string name="bluetooth_paired_device_title">Your devices</string>
|
||||||
<!-- Title for pairing bluetooth device page [CHAR LIMIT=none] -->
|
<!-- Title for pairing bluetooth device page [CHAR LIMIT=none] -->
|
||||||
<string name="bluetooth_pairing_page_title">Pair new device</string>
|
<string name="bluetooth_pairing_page_title">Pair new device</string>
|
||||||
|
<!-- Summary for bluetooth item in connection detail page -->
|
||||||
|
<string name="bluetooth_pref_summary">Allow device to pair and connect to bluetooth devices</string>
|
||||||
|
|
||||||
<!-- Title for connected device group [CHAR LIMIT=none]-->
|
<!-- Title for connected device group [CHAR LIMIT=none]-->
|
||||||
<string name="connected_device_connected_title">Currently connected</string>
|
<string name="connected_device_connected_title">Currently connected</string>
|
||||||
|
@@ -19,10 +19,11 @@
|
|||||||
android:key="connected_devices_screen"
|
android:key="connected_devices_screen"
|
||||||
android:title="@string/connected_devices_dashboard_title">
|
android:title="@string/connected_devices_dashboard_title">
|
||||||
|
|
||||||
<com.android.settings.widget.MasterSwitchPreference
|
<SwitchPreference
|
||||||
android:key="toggle_bluetooth"
|
android:key="toggle_bluetooth_switch"
|
||||||
android:title="@string/bluetooth_settings_title"
|
android:title="@string/bluetooth_settings_title"
|
||||||
android:icon="@drawable/ic_settings_bluetooth"
|
android:icon="@drawable/ic_settings_bluetooth"
|
||||||
|
android:summary="@string/bluetooth_pref_summary"
|
||||||
android:order="-7"/>
|
android:order="-7"/>
|
||||||
|
|
||||||
<SwitchPreference
|
<SwitchPreference
|
||||||
|
@@ -38,6 +38,7 @@ import com.android.settingslib.core.lifecycle.events.OnResume;
|
|||||||
import com.android.settingslib.core.lifecycle.events.OnStart;
|
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||||
import com.android.settingslib.core.lifecycle.events.OnStop;
|
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||||
|
|
||||||
|
//TODO(b/69926683): remove this controller in Android P.
|
||||||
public class BluetoothMasterSwitchPreferenceController extends AbstractPreferenceController
|
public class BluetoothMasterSwitchPreferenceController extends AbstractPreferenceController
|
||||||
implements PreferenceControllerMixin, OnSummaryChangeListener, LifecycleObserver, OnResume,
|
implements PreferenceControllerMixin, OnSummaryChangeListener, LifecycleObserver, OnResume,
|
||||||
OnPause, OnStart, OnStop {
|
OnPause, OnStart, OnStop {
|
||||||
|
@@ -0,0 +1,161 @@
|
|||||||
|
/*
|
||||||
|
* 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 android.content.Context;
|
||||||
|
import android.support.v14.preference.SwitchPreference;
|
||||||
|
import android.support.v7.preference.Preference;
|
||||||
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
|
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
|
||||||
|
import com.android.settings.core.TogglePreferenceController;
|
||||||
|
import com.android.settings.overlay.FeatureFactory;
|
||||||
|
import com.android.settings.widget.SwitchWidgetController;
|
||||||
|
import com.android.settingslib.RestrictedLockUtils;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothManager;
|
||||||
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||||
|
import com.android.settingslib.core.lifecycle.events.OnStart;
|
||||||
|
import com.android.settingslib.core.lifecycle.events.OnStop;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PreferenceController to update of bluetooth {@link SwitchPreference}. It will
|
||||||
|
*
|
||||||
|
* 1. Invoke the user toggle
|
||||||
|
* 2. Listen to the update from {@link LocalBluetoothManager}
|
||||||
|
*/
|
||||||
|
public class BluetoothSwitchPreferenceController extends TogglePreferenceController
|
||||||
|
implements LifecycleObserver, OnStart, OnStop {
|
||||||
|
|
||||||
|
public static final String KEY_TOGGLE_BLUETOOTH = "toggle_bluetooth_switch";
|
||||||
|
|
||||||
|
private LocalBluetoothManager mBluetoothManager;
|
||||||
|
private SwitchPreference mBtPreference;
|
||||||
|
private BluetoothEnabler mBluetoothEnabler;
|
||||||
|
private RestrictionUtils mRestrictionUtils;
|
||||||
|
@VisibleForTesting
|
||||||
|
LocalBluetoothAdapter mBluetoothAdapter;
|
||||||
|
|
||||||
|
public BluetoothSwitchPreferenceController(Context context) {
|
||||||
|
this(context, Utils.getLocalBtManager(context), new RestrictionUtils());
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
public BluetoothSwitchPreferenceController(Context context,
|
||||||
|
LocalBluetoothManager bluetoothManager, RestrictionUtils restrictionUtils) {
|
||||||
|
super(context, KEY_TOGGLE_BLUETOOTH);
|
||||||
|
mBluetoothManager = bluetoothManager;
|
||||||
|
mRestrictionUtils = restrictionUtils;
|
||||||
|
|
||||||
|
if (mBluetoothManager != null) {
|
||||||
|
mBluetoothAdapter = mBluetoothManager.getBluetoothAdapter();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
|
super.displayPreference(screen);
|
||||||
|
mBtPreference = (SwitchPreference) screen.findPreference(KEY_TOGGLE_BLUETOOTH);
|
||||||
|
mBluetoothEnabler = new BluetoothEnabler(mContext,
|
||||||
|
new SwitchController(mBtPreference),
|
||||||
|
FeatureFactory.getFactory(mContext).getMetricsFeatureProvider(), mBluetoothManager,
|
||||||
|
MetricsEvent.ACTION_SETTINGS_MASTER_SWITCH_BLUETOOTH_TOGGLE,
|
||||||
|
mRestrictionUtils);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getAvailabilityStatus() {
|
||||||
|
return mBluetoothAdapter != null ? AVAILABLE : DISABLED_UNSUPPORTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
mBluetoothEnabler.resume(mContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStop() {
|
||||||
|
mBluetoothEnabler.pause();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isChecked() {
|
||||||
|
return mBluetoothAdapter != null ? mBluetoothAdapter.isEnabled() : false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setChecked(boolean isChecked) {
|
||||||
|
if (mBluetoothAdapter != null) {
|
||||||
|
mBluetoothAdapter.setBluetoothEnabled(isChecked);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Control the switch inside {@link SwitchPreference}
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
class SwitchController extends SwitchWidgetController implements
|
||||||
|
Preference.OnPreferenceChangeListener {
|
||||||
|
private SwitchPreference mSwitchPreference;
|
||||||
|
|
||||||
|
public SwitchController(SwitchPreference switchPreference) {
|
||||||
|
mSwitchPreference = switchPreference;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void updateTitle(boolean isChecked) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startListening() {
|
||||||
|
mSwitchPreference.setOnPreferenceChangeListener(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stopListening() {
|
||||||
|
mSwitchPreference.setOnPreferenceChangeListener(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setChecked(boolean checked) {
|
||||||
|
mSwitchPreference.setChecked(checked);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isChecked() {
|
||||||
|
return mSwitchPreference.isChecked();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
mSwitchPreference.setEnabled(enabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onPreferenceChange(Preference preference, Object newValue) {
|
||||||
|
if (mListener != null) {
|
||||||
|
return mListener.onSwitchToggled((Boolean) newValue);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setDisabledByAdmin(RestrictedLockUtils.EnforcedAdmin admin) {
|
||||||
|
mBtPreference.setEnabled(admin == null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -24,6 +24,7 @@ import com.android.settings.R;
|
|||||||
import com.android.settings.SettingsActivity;
|
import com.android.settings.SettingsActivity;
|
||||||
import com.android.settings.bluetooth.BluetoothFilesPreferenceController;
|
import com.android.settings.bluetooth.BluetoothFilesPreferenceController;
|
||||||
import com.android.settings.bluetooth.BluetoothMasterSwitchPreferenceController;
|
import com.android.settings.bluetooth.BluetoothMasterSwitchPreferenceController;
|
||||||
|
import com.android.settings.bluetooth.BluetoothSwitchPreferenceController;
|
||||||
import com.android.settings.bluetooth.Utils;
|
import com.android.settings.bluetooth.Utils;
|
||||||
import com.android.settings.dashboard.DashboardFragment;
|
import com.android.settings.dashboard.DashboardFragment;
|
||||||
import com.android.settings.deviceinfo.UsbBackend;
|
import com.android.settings.deviceinfo.UsbBackend;
|
||||||
@@ -83,10 +84,8 @@ public class AdvancedConnectedDeviceDashboardFragment extends DashboardFragment
|
|||||||
mUsbPrefController = new UsbModePreferenceController(context, new UsbBackend(context));
|
mUsbPrefController = new UsbModePreferenceController(context, new UsbBackend(context));
|
||||||
lifecycle.addObserver(mUsbPrefController);
|
lifecycle.addObserver(mUsbPrefController);
|
||||||
controllers.add(mUsbPrefController);
|
controllers.add(mUsbPrefController);
|
||||||
final BluetoothMasterSwitchPreferenceController bluetoothPreferenceController =
|
final BluetoothSwitchPreferenceController bluetoothPreferenceController =
|
||||||
new BluetoothMasterSwitchPreferenceController(
|
new BluetoothSwitchPreferenceController(context);
|
||||||
context, Utils.getLocalBtManager(context), this,
|
|
||||||
(SettingsActivity) getActivity());
|
|
||||||
lifecycle.addObserver(bluetoothPreferenceController);
|
lifecycle.addObserver(bluetoothPreferenceController);
|
||||||
controllers.add(bluetoothPreferenceController);
|
controllers.add(bluetoothPreferenceController);
|
||||||
|
|
||||||
|
@@ -19,6 +19,7 @@ import android.support.annotation.VisibleForTesting;
|
|||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceGroup;
|
import android.support.v7.preference.PreferenceGroup;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
|
import com.android.settings.bluetooth.BluetoothDeviceUpdater;
|
||||||
import com.android.settings.bluetooth.ConnectedBluetoothDeviceUpdater;
|
import com.android.settings.bluetooth.ConnectedBluetoothDeviceUpdater;
|
||||||
@@ -42,26 +43,31 @@ public class ConnectedDeviceGroupController extends AbstractPreferenceController
|
|||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
PreferenceGroup mPreferenceGroup;
|
PreferenceGroup mPreferenceGroup;
|
||||||
private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
|
private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
|
||||||
|
private ConnectedUsbDeviceUpdater mConnectedUsbDeviceUpdater;
|
||||||
|
|
||||||
public ConnectedDeviceGroupController(DashboardFragment fragment, Lifecycle lifecycle) {
|
public ConnectedDeviceGroupController(DashboardFragment fragment, Lifecycle lifecycle) {
|
||||||
super(fragment.getContext());
|
super(fragment.getContext());
|
||||||
init(lifecycle, new ConnectedBluetoothDeviceUpdater(fragment, this));
|
init(lifecycle, new ConnectedBluetoothDeviceUpdater(fragment, this),
|
||||||
|
new ConnectedUsbDeviceUpdater(fragment.getContext(), this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
ConnectedDeviceGroupController(DashboardFragment fragment, Lifecycle lifecycle,
|
ConnectedDeviceGroupController(DashboardFragment fragment, Lifecycle lifecycle,
|
||||||
BluetoothDeviceUpdater bluetoothDeviceUpdater) {
|
BluetoothDeviceUpdater bluetoothDeviceUpdater,
|
||||||
|
ConnectedUsbDeviceUpdater connectedUsbDeviceUpdater) {
|
||||||
super(fragment.getContext());
|
super(fragment.getContext());
|
||||||
init(lifecycle, bluetoothDeviceUpdater);
|
init(lifecycle, bluetoothDeviceUpdater, connectedUsbDeviceUpdater);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStart() {
|
public void onStart() {
|
||||||
mBluetoothDeviceUpdater.registerCallback();
|
mBluetoothDeviceUpdater.registerCallback();
|
||||||
|
mConnectedUsbDeviceUpdater.registerCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStop() {
|
public void onStop() {
|
||||||
|
mConnectedUsbDeviceUpdater.unregisterCallback();
|
||||||
mBluetoothDeviceUpdater.unregisterCallback();
|
mBluetoothDeviceUpdater.unregisterCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,8 +76,10 @@ public class ConnectedDeviceGroupController extends AbstractPreferenceController
|
|||||||
super.displayPreference(screen);
|
super.displayPreference(screen);
|
||||||
mPreferenceGroup = (PreferenceGroup) screen.findPreference(KEY);
|
mPreferenceGroup = (PreferenceGroup) screen.findPreference(KEY);
|
||||||
mPreferenceGroup.setVisible(false);
|
mPreferenceGroup.setVisible(false);
|
||||||
|
|
||||||
mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
|
mBluetoothDeviceUpdater.setPrefContext(screen.getContext());
|
||||||
mBluetoothDeviceUpdater.forceUpdate();
|
mBluetoothDeviceUpdater.forceUpdate();
|
||||||
|
mConnectedUsbDeviceUpdater.initUsbPreference(screen.getContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -100,10 +108,12 @@ public class ConnectedDeviceGroupController extends AbstractPreferenceController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void init(Lifecycle lifecycle, BluetoothDeviceUpdater bluetoothDeviceUpdater) {
|
private void init(Lifecycle lifecycle, BluetoothDeviceUpdater bluetoothDeviceUpdater,
|
||||||
|
ConnectedUsbDeviceUpdater connectedUsbDeviceUpdater) {
|
||||||
if (lifecycle != null) {
|
if (lifecycle != null) {
|
||||||
lifecycle.addObserver(this);
|
lifecycle.addObserver(this);
|
||||||
}
|
}
|
||||||
mBluetoothDeviceUpdater = bluetoothDeviceUpdater;
|
mBluetoothDeviceUpdater = bluetoothDeviceUpdater;
|
||||||
|
mConnectedUsbDeviceUpdater = connectedUsbDeviceUpdater;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
* 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.connecteddevice;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.deviceinfo.UsbBackend;
|
||||||
|
import com.android.settings.deviceinfo.UsbModeChooserActivity;
|
||||||
|
import com.android.settings.widget.GearPreference;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller to maintain connected usb device
|
||||||
|
*/
|
||||||
|
public class ConnectedUsbDeviceUpdater {
|
||||||
|
private Context mContext;
|
||||||
|
private UsbBackend mUsbBackend;
|
||||||
|
private DevicePreferenceCallback mDevicePreferenceCallback;
|
||||||
|
@VisibleForTesting
|
||||||
|
GearPreference mUsbPreference;
|
||||||
|
@VisibleForTesting
|
||||||
|
UsbConnectionBroadcastReceiver mUsbReceiver;
|
||||||
|
|
||||||
|
private UsbConnectionBroadcastReceiver.UsbConnectionListener mUsbConnectionListener =
|
||||||
|
(connected) -> {
|
||||||
|
if (connected) {
|
||||||
|
mUsbPreference.setSummary(
|
||||||
|
UsbModePreferenceController.getSummary(mUsbBackend.getCurrentMode()));
|
||||||
|
mDevicePreferenceCallback.onDeviceAdded(mUsbPreference);
|
||||||
|
} else {
|
||||||
|
mDevicePreferenceCallback.onDeviceRemoved(mUsbPreference);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public ConnectedUsbDeviceUpdater(Context context,
|
||||||
|
DevicePreferenceCallback devicePreferenceCallback) {
|
||||||
|
this(context, devicePreferenceCallback, new UsbBackend(context));
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
ConnectedUsbDeviceUpdater(Context context, DevicePreferenceCallback devicePreferenceCallback,
|
||||||
|
UsbBackend usbBackend) {
|
||||||
|
mContext = context;
|
||||||
|
mDevicePreferenceCallback = devicePreferenceCallback;
|
||||||
|
mUsbBackend = usbBackend;
|
||||||
|
mUsbReceiver = new UsbConnectionBroadcastReceiver(context, mUsbConnectionListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void registerCallback() {
|
||||||
|
// This method could handle multiple register
|
||||||
|
mUsbReceiver.register();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unregisterCallback() {
|
||||||
|
mUsbReceiver.unregister();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initUsbPreference(Context context) {
|
||||||
|
mUsbPreference = new GearPreference(context, null /* AttributeSet */);
|
||||||
|
mUsbPreference.setTitle(R.string.usb_pref);
|
||||||
|
mUsbPreference.setIcon(R.drawable.ic_usb);
|
||||||
|
mUsbPreference.setSelectable(false);
|
||||||
|
mUsbPreference.setOnGearClickListener((GearPreference p) -> {
|
||||||
|
final Intent intent = new Intent(mContext, UsbModeChooserActivity.class);
|
||||||
|
mContext.startActivity(intent);
|
||||||
|
});
|
||||||
|
|
||||||
|
forceUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void forceUpdate() {
|
||||||
|
// Register so we can get the connection state from sticky intent.
|
||||||
|
//TODO(b/70336520): Use an API to get data instead of sticky intent
|
||||||
|
mUsbReceiver.register();
|
||||||
|
mUsbConnectionListener.onUsbConnectionChanged(mUsbReceiver.isConnected());
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,76 @@
|
|||||||
|
/*
|
||||||
|
* 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.connecteddevice;
|
||||||
|
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.IntentFilter;
|
||||||
|
import android.hardware.usb.UsbManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receiver to receive usb update and use {@link UsbConnectionListener} to invoke callback
|
||||||
|
*/
|
||||||
|
public class UsbConnectionBroadcastReceiver extends BroadcastReceiver {
|
||||||
|
private Context mContext;
|
||||||
|
private UsbConnectionListener mUsbConnectionListener;
|
||||||
|
private boolean mListeningToUsbEvents;
|
||||||
|
private boolean mConnected;
|
||||||
|
|
||||||
|
public UsbConnectionBroadcastReceiver(Context context,
|
||||||
|
UsbConnectionListener usbConnectionListener) {
|
||||||
|
mContext = context;
|
||||||
|
mUsbConnectionListener = usbConnectionListener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onReceive(Context context, Intent intent) {
|
||||||
|
mConnected = intent != null
|
||||||
|
&& intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
|
||||||
|
if (mUsbConnectionListener != null) {
|
||||||
|
mUsbConnectionListener.onUsbConnectionChanged(mConnected);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void register() {
|
||||||
|
if (!mListeningToUsbEvents) {
|
||||||
|
final IntentFilter intentFilter = new IntentFilter(UsbManager.ACTION_USB_STATE);
|
||||||
|
final Intent intent = mContext.registerReceiver(this, intentFilter);
|
||||||
|
mConnected = intent != null
|
||||||
|
&& intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
|
||||||
|
mListeningToUsbEvents = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void unregister() {
|
||||||
|
if (mListeningToUsbEvents) {
|
||||||
|
mContext.unregisterReceiver(this);
|
||||||
|
mListeningToUsbEvents = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isConnected() {
|
||||||
|
return mConnected;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface definition for a callback to be invoked when usb connection is changed.
|
||||||
|
*/
|
||||||
|
interface UsbConnectionListener {
|
||||||
|
void onUsbConnectionChanged(boolean connected);
|
||||||
|
}
|
||||||
|
}
|
@@ -15,17 +15,12 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.settings.connecteddevice;
|
package com.android.settings.connecteddevice;
|
||||||
|
|
||||||
import android.content.BroadcastReceiver;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.IntentFilter;
|
|
||||||
import android.hardware.usb.UsbManager;
|
|
||||||
import android.support.annotation.VisibleForTesting;
|
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceScreen;
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
|
|
||||||
import com.android.settings.core.PreferenceControllerMixin;
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.core.PreferenceControllerMixin;
|
||||||
import com.android.settings.deviceinfo.UsbBackend;
|
import com.android.settings.deviceinfo.UsbBackend;
|
||||||
import com.android.settingslib.core.AbstractPreferenceController;
|
import com.android.settingslib.core.AbstractPreferenceController;
|
||||||
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
import com.android.settingslib.core.lifecycle.LifecycleObserver;
|
||||||
@@ -44,19 +39,21 @@ public class UsbModePreferenceController extends AbstractPreferenceController
|
|||||||
public UsbModePreferenceController(Context context, UsbBackend usbBackend) {
|
public UsbModePreferenceController(Context context, UsbBackend usbBackend) {
|
||||||
super(context);
|
super(context);
|
||||||
mUsbBackend = usbBackend;
|
mUsbBackend = usbBackend;
|
||||||
mUsbReceiver = new UsbConnectionBroadcastReceiver();
|
mUsbReceiver = new UsbConnectionBroadcastReceiver(mContext, (connected) -> {
|
||||||
|
updateSummary(mUsbPreference);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void displayPreference(PreferenceScreen screen) {
|
public void displayPreference(PreferenceScreen screen) {
|
||||||
super.displayPreference(screen);
|
super.displayPreference(screen);
|
||||||
mUsbPreference = screen.findPreference(KEY_USB_MODE);
|
mUsbPreference = screen.findPreference(KEY_USB_MODE);
|
||||||
updataSummary(mUsbPreference);
|
updateSummary(mUsbPreference);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateState(Preference preference) {
|
public void updateState(Preference preference) {
|
||||||
updataSummary(preference);
|
updateSummary(preference);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -79,8 +76,7 @@ public class UsbModePreferenceController extends AbstractPreferenceController
|
|||||||
mUsbReceiver.register();
|
mUsbReceiver.register();
|
||||||
}
|
}
|
||||||
|
|
||||||
@VisibleForTesting
|
public static int getSummary(int mode) {
|
||||||
int getSummary(int mode) {
|
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_NONE:
|
case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_NONE:
|
||||||
return R.string.usb_summary_charging_only;
|
return R.string.usb_summary_charging_only;
|
||||||
@@ -96,11 +92,11 @@ public class UsbModePreferenceController extends AbstractPreferenceController
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updataSummary(Preference preference) {
|
private void updateSummary(Preference preference) {
|
||||||
updataSummary(preference, mUsbBackend.getCurrentMode());
|
updateSummary(preference, mUsbBackend.getCurrentMode());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updataSummary(Preference preference, int mode) {
|
private void updateSummary(Preference preference, int mode) {
|
||||||
if (preference != null) {
|
if (preference != null) {
|
||||||
if (mUsbReceiver.isConnected()) {
|
if (mUsbReceiver.isConnected()) {
|
||||||
preference.setEnabled(true);
|
preference.setEnabled(true);
|
||||||
@@ -112,40 +108,4 @@ public class UsbModePreferenceController extends AbstractPreferenceController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class UsbConnectionBroadcastReceiver extends BroadcastReceiver {
|
|
||||||
private boolean mListeningToUsbEvents;
|
|
||||||
private boolean mConnected;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReceive(Context context, Intent intent) {
|
|
||||||
boolean connected = intent != null
|
|
||||||
&& intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
|
|
||||||
if (connected != mConnected) {
|
|
||||||
mConnected = connected;
|
|
||||||
updataSummary(mUsbPreference);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void register() {
|
|
||||||
if (!mListeningToUsbEvents) {
|
|
||||||
IntentFilter intentFilter = new IntentFilter(UsbManager.ACTION_USB_STATE);
|
|
||||||
Intent intent = mContext.registerReceiver(this, intentFilter);
|
|
||||||
mConnected = intent != null
|
|
||||||
&& intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
|
|
||||||
mListeningToUsbEvents = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void unregister() {
|
|
||||||
if (mListeningToUsbEvents) {
|
|
||||||
mContext.unregisterReceiver(this);
|
|
||||||
mListeningToUsbEvents = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isConnected() {
|
|
||||||
return mConnected;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
* 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 com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.v14.preference.SwitchPreference;
|
||||||
|
import android.support.v7.preference.PreferenceScreen;
|
||||||
|
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.core.BasePreferenceController;
|
||||||
|
import com.android.settings.testutils.FakeFeatureFactory;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
|
||||||
|
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;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class BluetoothSwitchPreferenceControllerTest {
|
||||||
|
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
|
private LocalBluetoothManager mBluetoothManager;
|
||||||
|
@Mock
|
||||||
|
private PreferenceScreen mScreen;
|
||||||
|
@Mock
|
||||||
|
private SwitchPreference mPreference;
|
||||||
|
@Mock
|
||||||
|
private RestrictionUtils mRestrictionUtils;
|
||||||
|
@Mock
|
||||||
|
private LocalBluetoothAdapter mLocalBluetoothAdapter;
|
||||||
|
|
||||||
|
private Context mContext;
|
||||||
|
private BluetoothSwitchPreferenceController mController;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
mContext = spy(RuntimeEnvironment.application.getApplicationContext());
|
||||||
|
FakeFeatureFactory.setupForTest();
|
||||||
|
|
||||||
|
mController = new BluetoothSwitchPreferenceController(
|
||||||
|
mContext, mBluetoothManager, mRestrictionUtils);
|
||||||
|
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
|
||||||
|
when(mPreference.getKey()).thenReturn(mController.getPreferenceKey());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAvailabilityStatus_adapterNull_returnDisabled() {
|
||||||
|
mController.mBluetoothAdapter = null;
|
||||||
|
|
||||||
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(
|
||||||
|
BasePreferenceController.DISABLED_UNSUPPORTED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAvailabilityStatus_adapterExisted_returnAvailable() {
|
||||||
|
mController.mBluetoothAdapter = mLocalBluetoothAdapter;
|
||||||
|
|
||||||
|
assertThat(mController.getAvailabilityStatus()).isEqualTo(
|
||||||
|
BasePreferenceController.AVAILABLE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnStart_shouldRegisterPreferenceChangeListener() {
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
mController.onStart();
|
||||||
|
|
||||||
|
verify(mPreference).setOnPreferenceChangeListener(
|
||||||
|
any(BluetoothSwitchPreferenceController.SwitchController.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnStop_shouldRegisterPreferenceChangeListener() {
|
||||||
|
mController.displayPreference(mScreen);
|
||||||
|
mController.onStart();
|
||||||
|
|
||||||
|
mController.onStop();
|
||||||
|
|
||||||
|
verify(mPreference).setOnPreferenceChangeListener(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsChecked_adapterNull_returnFalse() {
|
||||||
|
mController.mBluetoothAdapter = null;
|
||||||
|
|
||||||
|
assertThat(mController.isChecked()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsChecked_adapterExisted_returnFromAdapter() {
|
||||||
|
mController.mBluetoothAdapter = mLocalBluetoothAdapter;
|
||||||
|
doReturn(true).when(mLocalBluetoothAdapter).isEnabled();
|
||||||
|
|
||||||
|
assertThat(mController.isChecked()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetChecked_adapterExisted() {
|
||||||
|
mController.mBluetoothAdapter = mLocalBluetoothAdapter;
|
||||||
|
|
||||||
|
mController.setChecked(true);
|
||||||
|
|
||||||
|
verify(mLocalBluetoothAdapter).setBluetoothEnabled(true);
|
||||||
|
}
|
||||||
|
}
|
@@ -37,6 +37,7 @@ import com.android.settingslib.core.lifecycle.Lifecycle;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Answers;
|
||||||
import org.mockito.Mock;
|
import org.mockito.Mock;
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
@@ -45,13 +46,17 @@ import org.robolectric.annotation.Config;
|
|||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
public class ConnectedDeviceGroupControllerTest {
|
public class ConnectedDeviceGroupControllerTest {
|
||||||
|
private static final String PREFERENCE_KEY_1 = "pref_key_1";
|
||||||
|
|
||||||
@Mock
|
@Mock
|
||||||
private DashboardFragment mDashboardFragment;
|
private DashboardFragment mDashboardFragment;
|
||||||
@Mock
|
@Mock
|
||||||
private ConnectedBluetoothDeviceUpdater mConnectedBluetoothDeviceUpdater;
|
private ConnectedBluetoothDeviceUpdater mConnectedBluetoothDeviceUpdater;
|
||||||
@Mock
|
@Mock
|
||||||
private PreferenceScreen mPreferenceScreen;
|
private ConnectedUsbDeviceUpdater mConnectedUsbDeviceUpdater;
|
||||||
@Mock
|
@Mock
|
||||||
|
private PreferenceScreen mPreferenceScreen;
|
||||||
|
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||||
private PreferenceManager mPreferenceManager;
|
private PreferenceManager mPreferenceManager;
|
||||||
|
|
||||||
private PreferenceGroup mPreferenceGroup;
|
private PreferenceGroup mPreferenceGroup;
|
||||||
@@ -66,30 +71,33 @@ public class ConnectedDeviceGroupControllerTest {
|
|||||||
|
|
||||||
mContext = RuntimeEnvironment.application;
|
mContext = RuntimeEnvironment.application;
|
||||||
mPreference = new Preference(mContext);
|
mPreference = new Preference(mContext);
|
||||||
|
mPreference.setKey(PREFERENCE_KEY_1);
|
||||||
mLifecycle = new Lifecycle(() -> mLifecycle);
|
mLifecycle = new Lifecycle(() -> mLifecycle);
|
||||||
mPreferenceGroup = spy(new PreferenceScreen(mContext, null));
|
mPreferenceGroup = spy(new PreferenceScreen(mContext, null));
|
||||||
doReturn(mPreferenceManager).when(mPreferenceGroup).getPreferenceManager();
|
doReturn(mPreferenceManager).when(mPreferenceGroup).getPreferenceManager();
|
||||||
doReturn(mContext).when(mDashboardFragment).getContext();
|
doReturn(mContext).when(mDashboardFragment).getContext();
|
||||||
|
|
||||||
mConnectedDeviceGroupController = new ConnectedDeviceGroupController(mDashboardFragment,
|
mConnectedDeviceGroupController = new ConnectedDeviceGroupController(mDashboardFragment,
|
||||||
mLifecycle, mConnectedBluetoothDeviceUpdater);
|
mLifecycle, mConnectedBluetoothDeviceUpdater, mConnectedUsbDeviceUpdater);
|
||||||
mConnectedDeviceGroupController.mPreferenceGroup = mPreferenceGroup;
|
mConnectedDeviceGroupController.mPreferenceGroup = mPreferenceGroup;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnDeviceAdded_firstAdd_becomeVisible() {
|
public void testOnDeviceAdded_firstAdd_becomeVisibleAndPreferenceAdded() {
|
||||||
mConnectedDeviceGroupController.onDeviceAdded(mPreference);
|
mConnectedDeviceGroupController.onDeviceAdded(mPreference);
|
||||||
|
|
||||||
assertThat(mPreferenceGroup.isVisible()).isTrue();
|
assertThat(mPreferenceGroup.isVisible()).isTrue();
|
||||||
|
assertThat(mPreferenceGroup.findPreference(PREFERENCE_KEY_1)).isEqualTo(mPreference);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOnDeviceRemoved_lastRemove_becomeInvisible() {
|
public void testOnDeviceRemoved_lastRemove_becomeInvisibleAndPreferenceRemoved() {
|
||||||
mPreferenceGroup.addPreference(mPreference);
|
mPreferenceGroup.addPreference(mPreference);
|
||||||
|
|
||||||
mConnectedDeviceGroupController.onDeviceRemoved(mPreference);
|
mConnectedDeviceGroupController.onDeviceRemoved(mPreference);
|
||||||
|
|
||||||
assertThat(mPreferenceGroup.isVisible()).isFalse();
|
assertThat(mPreferenceGroup.isVisible()).isFalse();
|
||||||
|
assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -117,9 +125,11 @@ public class ConnectedDeviceGroupControllerTest {
|
|||||||
// register the callback in onStart()
|
// register the callback in onStart()
|
||||||
mLifecycle.handleLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_START);
|
mLifecycle.handleLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_START);
|
||||||
verify(mConnectedBluetoothDeviceUpdater).registerCallback();
|
verify(mConnectedBluetoothDeviceUpdater).registerCallback();
|
||||||
|
verify(mConnectedUsbDeviceUpdater).registerCallback();
|
||||||
|
|
||||||
// unregister the callback in onStop()
|
// unregister the callback in onStop()
|
||||||
mLifecycle.handleLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_STOP);
|
mLifecycle.handleLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_STOP);
|
||||||
verify(mConnectedBluetoothDeviceUpdater).unregisterCallback();
|
verify(mConnectedBluetoothDeviceUpdater).unregisterCallback();
|
||||||
|
verify(mConnectedUsbDeviceUpdater).unregisterCallback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* 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.connecteddevice;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
|
||||||
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.deviceinfo.UsbBackend;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class ConnectedUsbDeviceUpdaterTest {
|
||||||
|
private Context mContext;
|
||||||
|
private ConnectedUsbDeviceUpdater mDeviceUpdater;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UsbConnectionBroadcastReceiver mUsbReceiver;
|
||||||
|
@Mock
|
||||||
|
private DevicePreferenceCallback mDevicePreferenceCallback;
|
||||||
|
@Mock
|
||||||
|
private UsbBackend mUsbBackend;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mDeviceUpdater = new ConnectedUsbDeviceUpdater(mContext, mDevicePreferenceCallback,
|
||||||
|
mUsbBackend);
|
||||||
|
mDeviceUpdater.mUsbReceiver = mUsbReceiver;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInitUsbPreference_preferenceInit() {
|
||||||
|
mDeviceUpdater.initUsbPreference(mContext);
|
||||||
|
|
||||||
|
assertThat(mDeviceUpdater.mUsbPreference.getTitle()).isEqualTo("USB");
|
||||||
|
assertThat(mDeviceUpdater.mUsbPreference.getIcon()).isEqualTo(mContext.getDrawable(
|
||||||
|
R.drawable.ic_usb));
|
||||||
|
assertThat(mDeviceUpdater.mUsbPreference.isSelectable()).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInitUsbPreference_usbConnected_preferenceAdded() {
|
||||||
|
doReturn(true).when(mUsbReceiver).isConnected();
|
||||||
|
|
||||||
|
mDeviceUpdater.initUsbPreference(mContext);
|
||||||
|
|
||||||
|
verify(mDevicePreferenceCallback).onDeviceAdded(mDeviceUpdater.mUsbPreference);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInitUsbPreference_usbDisconnected_preferenceRemoved() {
|
||||||
|
doReturn(false).when(mUsbReceiver).isConnected();
|
||||||
|
|
||||||
|
mDeviceUpdater.initUsbPreference(mContext);
|
||||||
|
|
||||||
|
verify(mDevicePreferenceCallback).onDeviceRemoved(mDeviceUpdater.mUsbPreference);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,126 @@
|
|||||||
|
/*
|
||||||
|
* 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.connecteddevice;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static com.google.common.truth.Truth.assertWithMessage;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.any;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
import android.content.BroadcastReceiver;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.hardware.usb.UsbManager;
|
||||||
|
|
||||||
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
import org.robolectric.annotation.Config;
|
||||||
|
import org.robolectric.shadows.ShadowApplication;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
|
public class UsbConnectionBroadcastReceiverTest {
|
||||||
|
private Context mContext;
|
||||||
|
private UsbConnectionBroadcastReceiver mReceiver;
|
||||||
|
private ShadowApplication mShadowApplication;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private UsbConnectionBroadcastReceiver.UsbConnectionListener mListener;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mShadowApplication = ShadowApplication.getInstance();
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mReceiver = new UsbConnectionBroadcastReceiver(mContext, mListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnReceive_usbConnected_invokeCallback() {
|
||||||
|
final Intent intent = new Intent();
|
||||||
|
intent.putExtra(UsbManager.USB_CONNECTED, true);
|
||||||
|
|
||||||
|
mReceiver.onReceive(mContext, intent);
|
||||||
|
|
||||||
|
verify(mListener).onUsbConnectionChanged(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnReceive_usbDisconnected_invokeCallback() {
|
||||||
|
final Intent intent = new Intent();
|
||||||
|
intent.putExtra(UsbManager.USB_CONNECTED, false);
|
||||||
|
|
||||||
|
mReceiver.onReceive(mContext, intent);
|
||||||
|
|
||||||
|
verify(mListener).onUsbConnectionChanged(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testRegister_invokeMethodTwice_registerOnce() {
|
||||||
|
mReceiver.register();
|
||||||
|
mReceiver.register();
|
||||||
|
|
||||||
|
final List<BroadcastReceiver> receivers = mShadowApplication.getReceiversForIntent(
|
||||||
|
new Intent(UsbManager.ACTION_USB_STATE));
|
||||||
|
assertHasOneUsbConnectionBroadcastReceiver(receivers);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUnregister_invokeMethodTwice_unregisterOnce() {
|
||||||
|
mReceiver.register();
|
||||||
|
mReceiver.unregister();
|
||||||
|
mReceiver.unregister();
|
||||||
|
|
||||||
|
final List<BroadcastReceiver> receivers = mShadowApplication.getReceiversForIntent(
|
||||||
|
new Intent(UsbManager.ACTION_USB_STATE));
|
||||||
|
assertHasNoUsbConnectionBroadcastReceiver(receivers);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertHasOneUsbConnectionBroadcastReceiver(List<BroadcastReceiver> receivers) {
|
||||||
|
boolean hasReceiver = false;
|
||||||
|
for (final BroadcastReceiver receiver : receivers) {
|
||||||
|
if (receiver instanceof UsbConnectionBroadcastReceiver) {
|
||||||
|
// If hasReceiver is true, then we're at the second copy of it so fail.
|
||||||
|
assertWithMessage(
|
||||||
|
"Only one instance of UsbConnectionBroadcastReceiver should be "
|
||||||
|
+ "registered").that(
|
||||||
|
hasReceiver).isFalse();
|
||||||
|
hasReceiver = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertThat(hasReceiver).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertHasNoUsbConnectionBroadcastReceiver(List<BroadcastReceiver> receivers) {
|
||||||
|
for (final BroadcastReceiver receiver : receivers) {
|
||||||
|
assertThat(receiver instanceof UsbConnectionBroadcastReceiver).isFalse();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user