• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

packages/apps/Settings


Commit MetaInfo

Revision2b52c56eace6728cb1f89692ede6d21963e203e5 (tree)
Time2020-09-16 12:09:30
AuthorTreeHugger Robot <treehugger-gerrit@goog...>
CommiterAndroid (Google) Code Review

Log Message

Merge "Show just disconnected device on previously connected" into rvc-qpr-dev

Change Summary

Incremental Difference

--- a/src/com/android/settings/connecteddevice/PreviouslyConnectedDevicePreferenceController.java
+++ b/src/com/android/settings/connecteddevice/PreviouslyConnectedDevicePreferenceController.java
@@ -16,11 +16,13 @@
1616 package com.android.settings.connecteddevice;
1717
1818 import android.bluetooth.BluetoothAdapter;
19+import android.bluetooth.BluetoothDevice;
1920 import android.content.BroadcastReceiver;
2021 import android.content.Context;
2122 import android.content.Intent;
2223 import android.content.IntentFilter;
2324 import android.content.pm.PackageManager;
25+import android.util.Log;
2426
2527 import androidx.annotation.VisibleForTesting;
2628 import androidx.preference.Preference;
@@ -28,6 +30,7 @@ import androidx.preference.PreferenceGroup;
2830 import androidx.preference.PreferenceScreen;
2931
3032 import com.android.settings.R;
33+import com.android.settings.bluetooth.BluetoothDevicePreference;
3134 import com.android.settings.bluetooth.BluetoothDeviceUpdater;
3235 import com.android.settings.bluetooth.SavedBluetoothDeviceUpdater;
3336 import com.android.settings.connecteddevice.dock.DockUpdater;
@@ -38,12 +41,20 @@ import com.android.settingslib.core.lifecycle.LifecycleObserver;
3841 import com.android.settingslib.core.lifecycle.events.OnStart;
3942 import com.android.settingslib.core.lifecycle.events.OnStop;
4043
44+import java.util.ArrayList;
45+import java.util.List;
46+
4147 public class PreviouslyConnectedDevicePreferenceController extends BasePreferenceController
4248 implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback {
4349
50+ private static final String TAG = "PreviouslyDevicePreController";
51+ private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
52+
4453 private static final int MAX_DEVICE_NUM = 3;
4554 private static final String KEY_SEE_ALL = "previously_connected_devices_see_all";
4655
56+ private final List<Preference> mDevicesList = new ArrayList<>();
57+
4758 private PreferenceGroup mPreferenceGroup;
4859 private BluetoothDeviceUpdater mBluetoothDeviceUpdater;
4960 private DockUpdater mSavedDockUpdater;
@@ -116,16 +127,56 @@ public class PreviouslyConnectedDevicePreferenceController extends BasePreferenc
116127 @Override
117128 public void onDeviceAdded(Preference preference) {
118129 mPreferenceSize++;
130+ final List<BluetoothDevice> bluetoothDevices =
131+ mBluetoothAdapter.getMostRecentlyConnectedDevices();
132+ final int index = bluetoothDevices.indexOf(((BluetoothDevicePreference) preference)
133+ .getBluetoothDevice().getDevice());
134+ if (DEBUG) {
135+ Log.d(TAG, "onDeviceAdded() " + preference.getTitle() + ", index of : " + index);
136+ for (BluetoothDevice device : bluetoothDevices) {
137+ Log.d(TAG, "onDeviceAdded() most recently device : " + device.getName());
138+ }
139+ }
119140 if (mPreferenceSize <= MAX_DEVICE_NUM) {
120- mPreferenceGroup.addPreference(preference);
141+ addPreference(mPreferenceSize, index, preference);
142+ } else {
143+ addPreference(MAX_DEVICE_NUM, index, preference);
121144 }
122145 updatePreferenceVisibility();
123146 }
124147
148+ private void addPreference(int size, int index, Preference preference) {
149+ if (mDevicesList.size() >= index) {
150+ mDevicesList.add(index, preference);
151+ } else {
152+ mDevicesList.add(preference);
153+ }
154+ mPreferenceGroup.removeAll();
155+ mPreferenceGroup.addPreference(mSeeAllPreference);
156+ for (int i = 0; i < size; i++) {
157+ if (DEBUG) {
158+ Log.d(TAG, "addPreference() add device : " + mDevicesList.get(i).getTitle());
159+ }
160+ mDevicesList.get(i).setOrder(i);
161+ mPreferenceGroup.addPreference(mDevicesList.get(i));
162+ }
163+ }
164+
125165 @Override
126166 public void onDeviceRemoved(Preference preference) {
127167 mPreferenceSize--;
128- mPreferenceGroup.removePreference(preference);
168+ mDevicesList.remove(preference);
169+ mPreferenceGroup.removeAll();
170+ mPreferenceGroup.addPreference(mSeeAllPreference);
171+ final int size = mDevicesList.size() >= MAX_DEVICE_NUM
172+ ? MAX_DEVICE_NUM : mDevicesList.size();
173+ for (int i = 0; i < size; i++) {
174+ if (DEBUG) {
175+ Log.d(TAG, "onDeviceRemoved() add device : " + mDevicesList.get(i).getTitle());
176+ }
177+ mDevicesList.get(i).setOrder(i);
178+ mPreferenceGroup.addPreference(mDevicesList.get(i));
179+ }
129180 updatePreferenceVisibility();
130181 }
131182
--- a/tests/robotests/src/com/android/settings/connecteddevice/PreviouslyConnectedDevicePreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/connecteddevice/PreviouslyConnectedDevicePreferenceControllerTest.java
@@ -23,8 +23,10 @@ import static com.google.common.truth.Truth.assertThat;
2323 import static org.mockito.Mockito.doReturn;
2424 import static org.mockito.Mockito.spy;
2525 import static org.mockito.Mockito.verify;
26+import static org.mockito.Mockito.when;
2627
2728 import android.bluetooth.BluetoothAdapter;
29+import android.bluetooth.BluetoothDevice;
2830 import android.content.Context;
2931 import android.content.pm.PackageManager;
3032
@@ -34,10 +36,12 @@ import androidx.preference.PreferenceGroup;
3436 import androidx.preference.PreferenceManager;
3537
3638 import com.android.settings.R;
39+import com.android.settings.bluetooth.BluetoothDevicePreference;
3740 import com.android.settings.bluetooth.BluetoothDeviceUpdater;
3841 import com.android.settings.connecteddevice.dock.DockUpdater;
3942 import com.android.settings.dashboard.DashboardFragment;
4043 import com.android.settings.testutils.shadow.ShadowBluetoothAdapter;
44+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
4145
4246 import org.junit.Before;
4347 import org.junit.Test;
@@ -49,11 +53,18 @@ import org.robolectric.RuntimeEnvironment;
4953 import org.robolectric.annotation.Config;
5054 import org.robolectric.shadow.api.Shadow;
5155
56+import java.util.ArrayList;
57+import java.util.List;
58+
5259 @RunWith(RobolectricTestRunner.class)
5360 @Config(shadows = ShadowBluetoothAdapter.class)
5461 public class PreviouslyConnectedDevicePreferenceControllerTest {
5562
56- private final String KEY = "test_key";
63+ private static final String KEY = "test_key";
64+ private static final String FAKE_ADDRESS_1 = "AA:AA:AA:AA:AA:01";
65+ private static final String FAKE_ADDRESS_2 = "AA:AA:AA:AA:AA:02";
66+ private static final String FAKE_ADDRESS_3 = "AA:AA:AA:AA:AA:03";
67+ private static final String FAKE_ADDRESS_4 = "AA:AA:AA:AA:AA:04";
5768
5869 @Mock
5970 private DashboardFragment mDashboardFragment;
@@ -67,6 +78,22 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
6778 private PreferenceManager mPreferenceManager;
6879 @Mock
6980 private Preference mSeeAllPreference;
81+ @Mock
82+ private CachedBluetoothDevice mCachedDevice1;
83+ @Mock
84+ private CachedBluetoothDevice mCachedDevice2;
85+ @Mock
86+ private CachedBluetoothDevice mCachedDevice3;
87+ @Mock
88+ private CachedBluetoothDevice mCachedDevice4;
89+ @Mock
90+ private BluetoothDevice mBluetoothDevice1;
91+ @Mock
92+ private BluetoothDevice mBluetoothDevice2;
93+ @Mock
94+ private BluetoothDevice mBluetoothDevice3;
95+ @Mock
96+ private BluetoothDevice mBluetoothDevice4;
7097
7198 private Context mContext;
7299 private PreviouslyConnectedDevicePreferenceController mPreConnectedDeviceController;
@@ -85,6 +112,22 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
85112 mPreConnectedDeviceController.setSavedDockUpdater(mDockUpdater);
86113 mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
87114
115+ when(mCachedDevice1.getDevice()).thenReturn(mBluetoothDevice1);
116+ when(mCachedDevice1.getAddress()).thenReturn(FAKE_ADDRESS_1);
117+ when(mCachedDevice2.getDevice()).thenReturn(mBluetoothDevice2);
118+ when(mCachedDevice2.getAddress()).thenReturn(FAKE_ADDRESS_2);
119+ when(mCachedDevice3.getDevice()).thenReturn(mBluetoothDevice3);
120+ when(mCachedDevice3.getAddress()).thenReturn(FAKE_ADDRESS_3);
121+ when(mCachedDevice4.getDevice()).thenReturn(mBluetoothDevice4);
122+ when(mCachedDevice4.getAddress()).thenReturn(FAKE_ADDRESS_4);
123+
124+ final List<BluetoothDevice> mMostRecentlyConnectedDevices = new ArrayList<>();
125+ mMostRecentlyConnectedDevices.add(mBluetoothDevice1);
126+ mMostRecentlyConnectedDevices.add(mBluetoothDevice2);
127+ mMostRecentlyConnectedDevices.add(mBluetoothDevice4);
128+ mMostRecentlyConnectedDevices.add(mBluetoothDevice3);
129+ mShadowBluetoothAdapter.setMostRecentlyConnectedDevices(mMostRecentlyConnectedDevices);
130+
88131 mPreferenceGroup = spy(new PreferenceCategory(mContext));
89132 doReturn(mPreferenceManager).when(mPreferenceGroup).getPreferenceManager();
90133 mPreferenceGroup.setVisible(false);
@@ -136,29 +179,43 @@ public class PreviouslyConnectedDevicePreferenceControllerTest {
136179
137180 @Test
138181 public void onDeviceAdded_addDevicePreference_displayIt() {
139- mPreConnectedDeviceController.onDeviceAdded(new Preference(mContext));
182+ final BluetoothDevicePreference preference1 = new BluetoothDevicePreference(
183+ mContext, mCachedDevice1, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT);
140184
141- assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(1);
185+ mPreConnectedDeviceController.onDeviceAdded(preference1);
186+
187+ assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(2);
142188 }
143189
144190 @Test
145191 public void onDeviceAdded_addFourDevicePreference_onlyDisplayThree() {
146- mPreConnectedDeviceController.onDeviceAdded(new Preference(mContext));
147- mPreConnectedDeviceController.onDeviceAdded(new Preference(mContext));
148- mPreConnectedDeviceController.onDeviceAdded(new Preference(mContext));
149- mPreConnectedDeviceController.onDeviceAdded(new Preference(mContext));
150-
151- assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(3);
192+ final BluetoothDevicePreference preference1 = new BluetoothDevicePreference(
193+ mContext, mCachedDevice1, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT);
194+ final BluetoothDevicePreference preference2 = new BluetoothDevicePreference(
195+ mContext, mCachedDevice2, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT);
196+ final BluetoothDevicePreference preference3 = new BluetoothDevicePreference(
197+ mContext, mCachedDevice3, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT);
198+ final BluetoothDevicePreference preference4 = new BluetoothDevicePreference(
199+ mContext, mCachedDevice4, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT);
200+
201+ mPreConnectedDeviceController.onDeviceAdded(preference1);
202+ mPreConnectedDeviceController.onDeviceAdded(preference2);
203+ mPreConnectedDeviceController.onDeviceAdded(preference3);
204+ mPreConnectedDeviceController.onDeviceAdded(preference4);
205+
206+ // 3 BluetoothDevicePreference and 1 see all preference
207+ assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(4);
152208 }
153209
154210 @Test
155- public void onDeviceRemoved_removeLastDevice_setInvisible() {
156- final Preference preference = new Preference(mContext);
157- mPreferenceGroup.addPreference(preference);
211+ public void onDeviceRemoved_removeLastDevice_showSeeAllPreference() {
212+ final BluetoothDevicePreference preference1 = new BluetoothDevicePreference(
213+ mContext, mCachedDevice1, true, BluetoothDevicePreference.SortType.TYPE_NO_SORT);
214+ mPreferenceGroup.addPreference(preference1);
158215
159- mPreConnectedDeviceController.onDeviceRemoved(preference);
216+ mPreConnectedDeviceController.onDeviceRemoved(preference1);
160217
161- assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(0);
218+ assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(1);
162219 }
163220
164221 @Test
--- a/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothAdapter.java
+++ b/tests/robotests/src/com/android/settings/testutils/shadow/ShadowBluetoothAdapter.java
@@ -17,6 +17,7 @@
1717 package com.android.settings.testutils.shadow;
1818
1919 import android.bluetooth.BluetoothAdapter;
20+import android.bluetooth.BluetoothDevice;
2021
2122 import org.robolectric.annotation.Implementation;
2223 import org.robolectric.annotation.Implements;
@@ -29,6 +30,7 @@ public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBlueto
2930
3031 private int mState;
3132 private List<Integer> mSupportedProfiles = new ArrayList<>();
33+ private List<BluetoothDevice> mMostRecentlyConnectedDevices = new ArrayList<>();
3234
3335 @Implementation
3436 protected List<Integer> getSupportedProfiles() {
@@ -56,4 +58,13 @@ public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBlueto
5658 protected boolean factoryReset() {
5759 return true;
5860 }
61+
62+ @Implementation
63+ protected List<BluetoothDevice> getMostRecentlyConnectedDevices() {
64+ return mMostRecentlyConnectedDevices;
65+ }
66+
67+ public void setMostRecentlyConnectedDevices(List<BluetoothDevice> list) {
68+ mMostRecentlyConnectedDevices = list;
69+ }
5970 }