Add a loading text when waiting for wifitracker results

- When there is no result from background worker, a "scanning" text is
displayed instead of a totally empty card.

- Also cleaned up a few minor coding style issues

Change-Id: I73afdb1dd72e7accb387ad9aa493cfbe00bffe66
Fixes: 120051186
Test: robotests
This commit is contained in:
Fan Zhang
2018-12-06 12:45:43 -08:00
parent c8580f3a36
commit 3e107469aa
2 changed files with 45 additions and 25 deletions

View File

@@ -37,7 +37,6 @@ import androidx.annotation.VisibleForTesting;
import androidx.core.graphics.drawable.IconCompat; import androidx.core.graphics.drawable.IconCompat;
import androidx.slice.Slice; import androidx.slice.Slice;
import androidx.slice.builders.ListBuilder; import androidx.slice.builders.ListBuilder;
import androidx.slice.builders.ListBuilder.RowBuilder;
import androidx.slice.builders.SliceAction; import androidx.slice.builders.SliceAction;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
@@ -64,9 +63,11 @@ public class WifiSlice implements CustomSliceable {
static final int DEFAULT_EXPANDED_ROW_COUNT = 3; static final int DEFAULT_EXPANDED_ROW_COUNT = 3;
private final Context mContext; private final Context mContext;
private final WifiManager mWifiManager;
public WifiSlice(Context context) { public WifiSlice(Context context) {
mContext = context; mContext = context;
mWifiManager = mContext.getSystemService(WifiManager.class);
} }
@Override @Override
@@ -100,7 +101,7 @@ public class WifiSlice implements CustomSliceable {
final ListBuilder listBuilder = new ListBuilder(mContext, WIFI_SLICE_URI, final ListBuilder listBuilder = new ListBuilder(mContext, WIFI_SLICE_URI,
ListBuilder.INFINITY) ListBuilder.INFINITY)
.setAccentColor(color) .setAccentColor(color)
.addRow(new RowBuilder() .addRow(new ListBuilder.RowBuilder()
.setTitle(title) .setTitle(title)
.setSubtitle(summary) .setSubtitle(summary)
.addEndItem(toggleSliceAction) .addEndItem(toggleSliceAction)
@@ -110,18 +111,25 @@ public class WifiSlice implements CustomSliceable {
return listBuilder.build(); return listBuilder.build();
} }
List<AccessPoint> results = SliceBackgroundWorker.getInstance(mContext, this).getResults(); final List<AccessPoint> results =
if (results == null) { SliceBackgroundWorker.getInstance(mContext, this).getResults();
results = new ArrayList<>();
} // Need a loading text when results are not ready.
final int apCount = results.size(); boolean needLoadingRow = results == null;
final int apCount = needLoadingRow ? 0 : results.size();
// Add AP rows // Add AP rows
final CharSequence placeholder = mContext.getText(R.string.summary_placeholder); final CharSequence placeholder = mContext.getText(R.string.summary_placeholder);
for (int i = 0; i < DEFAULT_EXPANDED_ROW_COUNT; i++) { for (int i = 0; i < DEFAULT_EXPANDED_ROW_COUNT; i++) {
if (i < apCount) { if (i < apCount) {
listBuilder.addRow(getAccessPointRow(results.get(i))); listBuilder.addRow(getAccessPointRow(results.get(i)));
} else if (needLoadingRow) {
listBuilder.addRow(new ListBuilder.RowBuilder()
.setTitle(mContext.getText(R.string.wifi_empty_list_wifi_on))
.setSubtitle(placeholder));
needLoadingRow = false;
} else { } else {
listBuilder.addRow(new RowBuilder() listBuilder.addRow(new ListBuilder.RowBuilder()
.setTitle(placeholder) .setTitle(placeholder)
.setSubtitle(placeholder)); .setSubtitle(placeholder));
} }
@@ -129,12 +137,12 @@ public class WifiSlice implements CustomSliceable {
return listBuilder.build(); return listBuilder.build();
} }
private RowBuilder getAccessPointRow(AccessPoint accessPoint) { private ListBuilder.RowBuilder getAccessPointRow(AccessPoint accessPoint) {
final String title = accessPoint.getConfigName(); final String title = accessPoint.getConfigName();
final IconCompat levelIcon = IconCompat.createWithResource(mContext, final IconCompat levelIcon = IconCompat.createWithResource(mContext,
com.android.settingslib.Utils.getWifiIconResource(accessPoint.getLevel())); com.android.settingslib.Utils.getWifiIconResource(accessPoint.getLevel()));
final CharSequence apSummary = accessPoint.getSettingsSummary(); final CharSequence apSummary = accessPoint.getSettingsSummary();
final RowBuilder rowBuilder = new RowBuilder() final ListBuilder.RowBuilder rowBuilder = new ListBuilder.RowBuilder()
.setTitleItem(levelIcon, ListBuilder.ICON_IMAGE) .setTitleItem(levelIcon, ListBuilder.ICON_IMAGE)
.setTitle(title) .setTitle(title)
.setSubtitle(!TextUtils.isEmpty(apSummary) .setSubtitle(!TextUtils.isEmpty(apSummary)
@@ -188,10 +196,9 @@ public class WifiSlice implements CustomSliceable {
*/ */
@Override @Override
public void onNotifyChange(Intent intent) { public void onNotifyChange(Intent intent) {
final WifiManager wifiManager = mContext.getSystemService(WifiManager.class);
final boolean newState = intent.getBooleanExtra(EXTRA_TOGGLE_STATE, final boolean newState = intent.getBooleanExtra(EXTRA_TOGGLE_STATE,
wifiManager.isWifiEnabled()); mWifiManager.isWifiEnabled());
wifiManager.setWifiEnabled(newState); mWifiManager.setWifiEnabled(newState);
// Do not notifyChange on Uri. The service takes longer to update the current value than it // Do not notifyChange on Uri. The service takes longer to update the current value than it
// does for the Slice to check the current value again. Let {@link SliceBroadcastRelay} // does for the Slice to check the current value again. Let {@link SliceBroadcastRelay}
// handle it. // handle it.
@@ -211,26 +218,19 @@ public class WifiSlice implements CustomSliceable {
} }
private boolean isWifiEnabled() { private boolean isWifiEnabled() {
final WifiManager wifiManager = mContext.getSystemService(WifiManager.class); switch (mWifiManager.getWifiState()) {
switch (wifiManager.getWifiState()) {
case WifiManager.WIFI_STATE_ENABLED: case WifiManager.WIFI_STATE_ENABLED:
case WifiManager.WIFI_STATE_ENABLING: case WifiManager.WIFI_STATE_ENABLING:
return true; return true;
case WifiManager.WIFI_STATE_DISABLED:
case WifiManager.WIFI_STATE_DISABLING:
case WifiManager.WIFI_STATE_UNKNOWN:
default: default:
return false; return false;
} }
} }
private CharSequence getSummary() { private CharSequence getSummary() {
final WifiManager wifiManager = mContext.getSystemService(WifiManager.class); switch (mWifiManager.getWifiState()) {
switch (wifiManager.getWifiState()) {
case WifiManager.WIFI_STATE_ENABLED: case WifiManager.WIFI_STATE_ENABLED:
final String ssid = WifiInfo.removeDoubleQuotes(wifiManager.getConnectionInfo() final String ssid = WifiInfo.removeDoubleQuotes(mWifiManager.getConnectionInfo()
.getSSID()); .getSSID());
if (TextUtils.equals(ssid, WifiSsid.NONE)) { if (TextUtils.equals(ssid, WifiSsid.NONE)) {
return mContext.getText(R.string.disconnected); return mContext.getText(R.string.disconnected);

View File

@@ -53,14 +53,17 @@ public class WifiSliceTest {
private Context mContext; private Context mContext;
private WifiManager mWifiManager;
private WifiSlice mWifiSlice; private WifiSlice mWifiSlice;
@Before @Before
public void setUp() { public void setUp() {
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mWifiManager = mContext.getSystemService(WifiManager.class);
// Set-up specs for SliceMetadata. // Set-up specs for SliceMetadata.
SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS); SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
mWifiManager.setWifiEnabled(true);
mWifiSlice = new WifiSlice(mContext); mWifiSlice = new WifiSlice(mContext);
} }
@@ -83,13 +86,30 @@ public class WifiSliceTest {
} }
@Test @Test
public void getWifiSlice_noAp_shouldReturnPlaceholder() { public void getWifiSlice_wifiOff_shouldReturnSingleRow() {
mWifiManager.setWifiEnabled(false);
final Slice wifiSlice = mWifiSlice.getSlice(); final Slice wifiSlice = mWifiSlice.getSlice();
int rows = SliceQuery.findAll(wifiSlice, FORMAT_SLICE, HINT_LIST_ITEM, final int rows = SliceQuery.findAll(wifiSlice, FORMAT_SLICE, HINT_LIST_ITEM,
null /* nonHints */).size(); null /* nonHints */).size();
// Title row
assertThat(rows).isEqualTo(1);
}
@Test
public void getWifiSlice_noAp_shouldReturnLoadingRow() {
final Slice wifiSlice = mWifiSlice.getSlice();
final int rows = SliceQuery.findAll(wifiSlice, FORMAT_SLICE, HINT_LIST_ITEM,
null /* nonHints */).size();
final List<SliceItem> sliceItems = wifiSlice.getItems();
// All AP rows + title row // All AP rows + title row
assertThat(rows).isEqualTo(DEFAULT_EXPANDED_ROW_COUNT + 1); assertThat(rows).isEqualTo(DEFAULT_EXPANDED_ROW_COUNT + 1);
// Has scanning text
SliceTester.assertTitle(sliceItems, mContext.getString(R.string.wifi_empty_list_wifi_on));
} }
@Test @Test