packages/apps/Settings
リビジョン | 2b52c56eace6728cb1f89692ede6d21963e203e5 (tree) |
---|---|
日時 | 2020-09-16 12:09:30 |
作者 | TreeHugger Robot <treehugger-gerrit@goog...> |
コミッター | Android (Google) Code Review |
Merge "Show just disconnected device on previously connected" into rvc-qpr-dev
@@ -16,11 +16,13 @@ | ||
16 | 16 | package com.android.settings.connecteddevice; |
17 | 17 | |
18 | 18 | import android.bluetooth.BluetoothAdapter; |
19 | +import android.bluetooth.BluetoothDevice; | |
19 | 20 | import android.content.BroadcastReceiver; |
20 | 21 | import android.content.Context; |
21 | 22 | import android.content.Intent; |
22 | 23 | import android.content.IntentFilter; |
23 | 24 | import android.content.pm.PackageManager; |
25 | +import android.util.Log; | |
24 | 26 | |
25 | 27 | import androidx.annotation.VisibleForTesting; |
26 | 28 | import androidx.preference.Preference; |
@@ -28,6 +30,7 @@ import androidx.preference.PreferenceGroup; | ||
28 | 30 | import androidx.preference.PreferenceScreen; |
29 | 31 | |
30 | 32 | import com.android.settings.R; |
33 | +import com.android.settings.bluetooth.BluetoothDevicePreference; | |
31 | 34 | import com.android.settings.bluetooth.BluetoothDeviceUpdater; |
32 | 35 | import com.android.settings.bluetooth.SavedBluetoothDeviceUpdater; |
33 | 36 | import com.android.settings.connecteddevice.dock.DockUpdater; |
@@ -38,12 +41,20 @@ import com.android.settingslib.core.lifecycle.LifecycleObserver; | ||
38 | 41 | import com.android.settingslib.core.lifecycle.events.OnStart; |
39 | 42 | import com.android.settingslib.core.lifecycle.events.OnStop; |
40 | 43 | |
44 | +import java.util.ArrayList; | |
45 | +import java.util.List; | |
46 | + | |
41 | 47 | public class PreviouslyConnectedDevicePreferenceController extends BasePreferenceController |
42 | 48 | implements LifecycleObserver, OnStart, OnStop, DevicePreferenceCallback { |
43 | 49 | |
50 | + private static final String TAG = "PreviouslyDevicePreController"; | |
51 | + private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); | |
52 | + | |
44 | 53 | private static final int MAX_DEVICE_NUM = 3; |
45 | 54 | private static final String KEY_SEE_ALL = "previously_connected_devices_see_all"; |
46 | 55 | |
56 | + private final List<Preference> mDevicesList = new ArrayList<>(); | |
57 | + | |
47 | 58 | private PreferenceGroup mPreferenceGroup; |
48 | 59 | private BluetoothDeviceUpdater mBluetoothDeviceUpdater; |
49 | 60 | private DockUpdater mSavedDockUpdater; |
@@ -116,16 +127,56 @@ public class PreviouslyConnectedDevicePreferenceController extends BasePreferenc | ||
116 | 127 | @Override |
117 | 128 | public void onDeviceAdded(Preference preference) { |
118 | 129 | 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 | + } | |
119 | 140 | if (mPreferenceSize <= MAX_DEVICE_NUM) { |
120 | - mPreferenceGroup.addPreference(preference); | |
141 | + addPreference(mPreferenceSize, index, preference); | |
142 | + } else { | |
143 | + addPreference(MAX_DEVICE_NUM, index, preference); | |
121 | 144 | } |
122 | 145 | updatePreferenceVisibility(); |
123 | 146 | } |
124 | 147 | |
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 | + | |
125 | 165 | @Override |
126 | 166 | public void onDeviceRemoved(Preference preference) { |
127 | 167 | 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 | + } | |
129 | 180 | updatePreferenceVisibility(); |
130 | 181 | } |
131 | 182 |
@@ -23,8 +23,10 @@ import static com.google.common.truth.Truth.assertThat; | ||
23 | 23 | import static org.mockito.Mockito.doReturn; |
24 | 24 | import static org.mockito.Mockito.spy; |
25 | 25 | import static org.mockito.Mockito.verify; |
26 | +import static org.mockito.Mockito.when; | |
26 | 27 | |
27 | 28 | import android.bluetooth.BluetoothAdapter; |
29 | +import android.bluetooth.BluetoothDevice; | |
28 | 30 | import android.content.Context; |
29 | 31 | import android.content.pm.PackageManager; |
30 | 32 |
@@ -34,10 +36,12 @@ import androidx.preference.PreferenceGroup; | ||
34 | 36 | import androidx.preference.PreferenceManager; |
35 | 37 | |
36 | 38 | import com.android.settings.R; |
39 | +import com.android.settings.bluetooth.BluetoothDevicePreference; | |
37 | 40 | import com.android.settings.bluetooth.BluetoothDeviceUpdater; |
38 | 41 | import com.android.settings.connecteddevice.dock.DockUpdater; |
39 | 42 | import com.android.settings.dashboard.DashboardFragment; |
40 | 43 | import com.android.settings.testutils.shadow.ShadowBluetoothAdapter; |
44 | +import com.android.settingslib.bluetooth.CachedBluetoothDevice; | |
41 | 45 | |
42 | 46 | import org.junit.Before; |
43 | 47 | import org.junit.Test; |
@@ -49,11 +53,18 @@ import org.robolectric.RuntimeEnvironment; | ||
49 | 53 | import org.robolectric.annotation.Config; |
50 | 54 | import org.robolectric.shadow.api.Shadow; |
51 | 55 | |
56 | +import java.util.ArrayList; | |
57 | +import java.util.List; | |
58 | + | |
52 | 59 | @RunWith(RobolectricTestRunner.class) |
53 | 60 | @Config(shadows = ShadowBluetoothAdapter.class) |
54 | 61 | public class PreviouslyConnectedDevicePreferenceControllerTest { |
55 | 62 | |
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"; | |
57 | 68 | |
58 | 69 | @Mock |
59 | 70 | private DashboardFragment mDashboardFragment; |
@@ -67,6 +78,22 @@ public class PreviouslyConnectedDevicePreferenceControllerTest { | ||
67 | 78 | private PreferenceManager mPreferenceManager; |
68 | 79 | @Mock |
69 | 80 | 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; | |
70 | 97 | |
71 | 98 | private Context mContext; |
72 | 99 | private PreviouslyConnectedDevicePreferenceController mPreConnectedDeviceController; |
@@ -85,6 +112,22 @@ public class PreviouslyConnectedDevicePreferenceControllerTest { | ||
85 | 112 | mPreConnectedDeviceController.setSavedDockUpdater(mDockUpdater); |
86 | 113 | mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter()); |
87 | 114 | |
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 | + | |
88 | 131 | mPreferenceGroup = spy(new PreferenceCategory(mContext)); |
89 | 132 | doReturn(mPreferenceManager).when(mPreferenceGroup).getPreferenceManager(); |
90 | 133 | mPreferenceGroup.setVisible(false); |
@@ -136,29 +179,43 @@ public class PreviouslyConnectedDevicePreferenceControllerTest { | ||
136 | 179 | |
137 | 180 | @Test |
138 | 181 | 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); | |
140 | 184 | |
141 | - assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(1); | |
185 | + mPreConnectedDeviceController.onDeviceAdded(preference1); | |
186 | + | |
187 | + assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(2); | |
142 | 188 | } |
143 | 189 | |
144 | 190 | @Test |
145 | 191 | 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); | |
152 | 208 | } |
153 | 209 | |
154 | 210 | @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); | |
158 | 215 | |
159 | - mPreConnectedDeviceController.onDeviceRemoved(preference); | |
216 | + mPreConnectedDeviceController.onDeviceRemoved(preference1); | |
160 | 217 | |
161 | - assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(0); | |
218 | + assertThat(mPreferenceGroup.getPreferenceCount()).isEqualTo(1); | |
162 | 219 | } |
163 | 220 | |
164 | 221 | @Test |
@@ -17,6 +17,7 @@ | ||
17 | 17 | package com.android.settings.testutils.shadow; |
18 | 18 | |
19 | 19 | import android.bluetooth.BluetoothAdapter; |
20 | +import android.bluetooth.BluetoothDevice; | |
20 | 21 | |
21 | 22 | import org.robolectric.annotation.Implementation; |
22 | 23 | import org.robolectric.annotation.Implements; |
@@ -29,6 +30,7 @@ public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBlueto | ||
29 | 30 | |
30 | 31 | private int mState; |
31 | 32 | private List<Integer> mSupportedProfiles = new ArrayList<>(); |
33 | + private List<BluetoothDevice> mMostRecentlyConnectedDevices = new ArrayList<>(); | |
32 | 34 | |
33 | 35 | @Implementation |
34 | 36 | protected List<Integer> getSupportedProfiles() { |
@@ -56,4 +58,13 @@ public class ShadowBluetoothAdapter extends org.robolectric.shadows.ShadowBlueto | ||
56 | 58 | protected boolean factoryReset() { |
57 | 59 | return true; |
58 | 60 | } |
61 | + | |
62 | + @Implementation | |
63 | + protected List<BluetoothDevice> getMostRecentlyConnectedDevices() { | |
64 | + return mMostRecentlyConnectedDevices; | |
65 | + } | |
66 | + | |
67 | + public void setMostRecentlyConnectedDevices(List<BluetoothDevice> list) { | |
68 | + mMostRecentlyConnectedDevices = list; | |
69 | + } | |
59 | 70 | } |