Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f0daf925 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Enable speed labeling based on existing network badges."

parents 52b2f160 271e5de5
Loading
Loading
Loading
Loading
+37 −4
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.settingslib.wifi;

import android.annotation.Nullable;
import android.app.AppGlobals;
import android.content.Context;
import android.content.pm.ApplicationInfo;
@@ -639,6 +640,13 @@ public class AccessPoint implements Comparable<AccessPoint> {
        // Update to new summary
        StringBuilder summary = new StringBuilder();

        // TODO(b/62354743): Standardize and international delimiter usage
        final String concatenator = " / ";

        if (mBadge != NetworkBadging.BADGING_NONE) {
            summary.append(getSpeedLabel() + concatenator);
        }

        if (isActive() && config != null && config.isPasspoint()) {
            // This is the active connection on passpoint
            summary.append(getSummary(mContext, getDetailedState(),
@@ -718,6 +726,14 @@ public class AccessPoint implements Comparable<AccessPoint> {
                }
            }
        }

        // Strip trailing delimiter if applicable
        int concatLength = concatenator.length();
        if (summary.length() >= concatLength && summary.substring(
                summary.length() - concatLength, summary.length()).equals(concatenator)) {
            summary.delete(summary.length() - concatLength, summary.length());
        }

        return summary.toString();
    }

@@ -745,8 +761,12 @@ public class AccessPoint implements Comparable<AccessPoint> {
            visibility.append(" rssi=").append(mInfo.getRssi());
            visibility.append(" ");
            visibility.append(" score=").append(mInfo.score);
            if (mRankingScore != Integer.MIN_VALUE) {
                visibility.append(" rankingScore=").append(getRankingScore());
            visibility.append(" badge=").append(getBadge());
            }
            if (mBadge != NetworkBadging.BADGING_NONE) {
                visibility.append(" speed=").append(getSpeedLabel());
            }
            visibility.append(String.format(" tx=%.1f,", mInfo.txSuccessRate));
            visibility.append(String.format("%.1f,", mInfo.txRetriesRate));
            visibility.append(String.format("%.1f ", mInfo.txBadRate));
@@ -1042,8 +1062,21 @@ public class AccessPoint implements Comparable<AccessPoint> {
        return mRankingScore;
    }

    int getBadge() {
        return mBadge;
    int getBadge() { return mBadge;}

    @Nullable
    String getSpeedLabel() {
        switch (mBadge) {
            case NetworkBadging.BADGING_4K:
                return mContext.getString(R.string.speed_label_very_fast);
            case NetworkBadging.BADGING_HD:
                return mContext.getString(R.string.speed_label_fast);
            case NetworkBadging.BADGING_SD:
                return mContext.getString(R.string.speed_label_okay);
            case NetworkBadging.BADGING_NONE:
            default:
                return null;
        }
    }

    /** Return true if the current RSSI is reachable, and false otherwise. */
+4 −1
Original line number Diff line number Diff line
@@ -162,7 +162,10 @@ public class AccessPointPreference extends Preference {
            return;
        }
        TronUtils.logWifiSettingsBadge(context, mWifiBadge);
        Drawable drawable = NetworkBadging.getWifiIcon(level, mWifiBadge, getContext().getTheme());

        // TODO(b/62355275): Revert this to N code after deleting NetworkBadging API
        Drawable drawable = NetworkBadging.getWifiIcon(
                level, NetworkBadging.BADGING_NONE, getContext().getTheme());
        if (!mForSavedNetworks && drawable != null) {
            drawable.setTint(Utils.getColorAttr(context, android.R.attr.colorControlNormal));
            setIcon(drawable);
+97 −4
Original line number Diff line number Diff line
@@ -19,12 +19,17 @@ import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkBadging;
import android.net.NetworkInfo;
import android.net.NetworkKey;
import android.net.RssiCurve;
import android.net.ScoredNetwork;
import android.net.WifiKey;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
@@ -40,6 +45,8 @@ import android.support.test.runner.AndroidJUnit4;
import android.text.SpannableString;
import android.text.style.TtsSpan;

import com.android.settingslib.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -55,7 +62,8 @@ public class AccessPointTest {

    private static final String TEST_SSID = "test_ssid";
    private Context mContext;
    @Mock private WifiNetworkScoreCache mWifiNetworkScoreCache;
    @Mock private RssiCurve mockBadgeCurve;
    @Mock private WifiNetworkScoreCache mockWifiNetworkScoreCache;

    @Before
    public void setUp() {
@@ -294,13 +302,13 @@ public class AccessPointTest {
    public void testIsMetered_returnTrueWhenScoredNetworkIsMetered() {
        AccessPoint ap = createAccessPointWithScanResultCache();

        when(mWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
                .thenReturn(
                        new ScoredNetwork(
                                null /* NetworkKey */,
                                null /* rssiCurve */,
                                true /* metered */));
        ap.update(mWifiNetworkScoreCache, false /* scoringUiEnabled */);
        ap.update(mockWifiNetworkScoreCache, false /* scoringUiEnabled */);

        assertThat(ap.isMetered()).isTrue();
    }
@@ -321,6 +329,91 @@ public class AccessPointTest {
        assertThat(accessPoint.isMetered()).isFalse();
    }

    @Test
    public void testSpeedLabel_returnsVeryFastWhen4kBadgeIsSet() {
        AccessPoint ap = createAccessPointWithScanResultCache();

        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) NetworkBadging.BADGING_4K);

        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);

        assertThat(ap.getBadge()).isEqualTo(NetworkBadging.BADGING_4K);
        assertThat(ap.getSpeedLabel())
                .isEqualTo(mContext.getString(R.string.speed_label_very_fast));
    }

    @Test
    public void testSpeedLabel_returnsFastWhenHdBadgeIsSet() {
        AccessPoint ap = createAccessPointWithScanResultCache();

        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) NetworkBadging.BADGING_HD);

        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);

        assertThat(ap.getBadge()).isEqualTo(NetworkBadging.BADGING_HD);
        assertThat(ap.getSpeedLabel())
                .isEqualTo(mContext.getString(R.string.speed_label_fast));
    }

    @Test
    public void testSpeedLabel_returnsOkayWhenSdBadgeIsSet() {
        AccessPoint ap = createAccessPointWithScanResultCache();

        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) NetworkBadging.BADGING_SD);

        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);

        assertThat(ap.getBadge()).isEqualTo(NetworkBadging.BADGING_SD);
        assertThat(ap.getSpeedLabel())
                .isEqualTo(mContext.getString(R.string.speed_label_okay));
    }

    @Test
    public void testSummaryString_showsSpeedLabel() {
        AccessPoint ap = createAccessPointWithScanResultCache();

        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) NetworkBadging.BADGING_4K);

        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);

        assertThat(ap.getSummary()).isEqualTo(mContext.getString(R.string.speed_label_very_fast));
    }

    @Test
    public void testSummaryString_concatenatesSpeedLabel() {
        AccessPoint ap = createAccessPointWithScanResultCache();
        ap.update(new WifiConfiguration());

        when(mockWifiNetworkScoreCache.getScoredNetwork(any(ScanResult.class)))
                .thenReturn(buildScoredNetworkWithMockBadgeCurve());
        when(mockBadgeCurve.lookupScore(anyInt())).thenReturn((byte) NetworkBadging.BADGING_4K);

        ap.update(mockWifiNetworkScoreCache, true /* scoringUiEnabled */);

        String expectedString = mContext.getString(R.string.speed_label_very_fast) + " / "
                + mContext.getString(R.string.wifi_remembered);
        assertThat(ap.getSummary()).isEqualTo(expectedString);
    }

    private ScoredNetwork buildScoredNetworkWithMockBadgeCurve() {
        Bundle attr1 = new Bundle();
        attr1.putParcelable(ScoredNetwork.ATTRIBUTES_KEY_BADGING_CURVE, mockBadgeCurve);
        return new ScoredNetwork(
                new NetworkKey(new WifiKey("\"ssid\"", "00:00:00:00:00:00")),
                mockBadgeCurve,
                false /* meteredHint */,
                attr1);

    }

    private AccessPoint createAccessPointWithScanResultCache() {
        Bundle bundle = new Bundle();
        ArrayList<ScanResult> scanResults = new ArrayList<>();
@@ -333,7 +426,7 @@ public class AccessPointTest {
            scanResults.add(scanResult);
        }

        bundle.putParcelableArrayList("key_scanresultcache", scanResults);
        bundle.putParcelableArrayList(AccessPoint.KEY_SCANRESULTCACHE, scanResults);
        return new AccessPoint(mContext, bundle);
    }

+3 −1
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.graphics.drawable.Animatable;
import android.graphics.drawable.AnimatedVectorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.net.NetworkBadging;
import android.telephony.SubscriptionInfo;
import android.util.ArraySet;
import android.util.AttributeSet;
@@ -581,10 +582,11 @@ public class SignalClusterView extends LinearLayout implements NetworkController
     */
    private void setBadgedWifiIconForView(ImageView imageView, @DrawableRes int wifiPieId,
            @DrawableRes int badgeId) {
        // TODO(sghuman): Delete this method and revert to N badging logic
        // Using the imageView's context to retrieve the Drawable so that theme is preserved.;
        LayerDrawable icon = new LayerDrawable(new Drawable[] {
                imageView.getContext().getDrawable(wifiPieId),
                imageView.getContext().getDrawable(badgeId)});
                imageView.getContext().getDrawable(NetworkBadging.BADGING_NONE)});

        // The LayerDrawable shares an underlying state so we must mutate the object to change the
        // color between the light and dark themes.