From 15902eef8ba349366498b20bfeabfab29880e931 Mon Sep 17 00:00:00 2001 From: Amin Shaikh Date: Fri, 13 Jan 2017 11:02:16 -0800 Subject: [PATCH] Create an API for retrieving a badged wifi icon. Bug: 34202067 Test: ./gts-tradefed run gts -m GtsNetTestCases -t com.google.android.net.gts.wifi.NetworkBadgingTest Change-Id: Ia3bf9c0a489ae08de8d635aa620cbccca22af1b2 --- api/system-current.txt | 4 + core/java/android/net/NetworkBadging.java | 152 ++++++++++++++++++ .../drawable/ic_signal_wifi_badged_0_bars.xml | 6 +- .../drawable/ic_signal_wifi_badged_1_bar.xml | 6 +- .../drawable/ic_signal_wifi_badged_2_bars.xml | 6 +- .../drawable/ic_signal_wifi_badged_3_bars.xml | 6 +- .../drawable/ic_signal_wifi_badged_4_bars.xml | 6 +- .../res/drawable/ic_signal_wifi_badged_4k.xml | 6 +- .../res/drawable/ic_signal_wifi_badged_hd.xml | 6 +- .../res/drawable/ic_signal_wifi_badged_ld.xml | 6 +- .../res/drawable/ic_signal_wifi_badged_sd.xml | 6 +- core/res/res/drawable/ic_wifi_signal_0.xml | 26 +++ core/res/res/drawable/ic_wifi_signal_1.xml | 29 ++++ core/res/res/drawable/ic_wifi_signal_2.xml | 29 ++++ core/res/res/drawable/ic_wifi_signal_3.xml | 29 ++++ core/res/res/drawable/ic_wifi_signal_4.xml | 25 +++ core/res/res/values/symbols.xml | 5 + 17 files changed, 326 insertions(+), 27 deletions(-) create mode 100644 core/java/android/net/NetworkBadging.java create mode 100644 core/res/res/drawable/ic_wifi_signal_0.xml create mode 100644 core/res/res/drawable/ic_wifi_signal_1.xml create mode 100644 core/res/res/drawable/ic_wifi_signal_2.xml create mode 100644 core/res/res/drawable/ic_wifi_signal_3.xml create mode 100644 core/res/res/drawable/ic_wifi_signal_4.xml diff --git a/api/system-current.txt b/api/system-current.txt index 7a6d87a3c9f17..bdbeb9bbbeff9 100644 --- a/api/system-current.txt +++ b/api/system-current.txt @@ -26320,6 +26320,10 @@ package android.net { field public static final android.os.Parcelable.Creator CREATOR; } + public class NetworkBadging { + method public static android.graphics.drawable.Drawable getWifiIcon(int, int, android.content.res.Resources.Theme); + } + public final class NetworkCapabilities implements android.os.Parcelable { ctor public NetworkCapabilities(android.net.NetworkCapabilities); method public int describeContents(); diff --git a/core/java/android/net/NetworkBadging.java b/core/java/android/net/NetworkBadging.java new file mode 100644 index 0000000000000..5cf2f965ffa63 --- /dev/null +++ b/core/java/android/net/NetworkBadging.java @@ -0,0 +1,152 @@ +/* + * 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 android.net; + +import android.annotation.DrawableRes; +import android.annotation.IntRange; +import android.annotation.NonNull; +import android.annotation.Nullable; +import android.annotation.SystemApi; +import android.content.res.Resources; +import android.content.res.Resources.Theme; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.LayerDrawable; +import android.net.ScoredNetwork.Badging; +import android.net.wifi.WifiManager; +import android.view.View; + +/** + * Utility methods for working with network badging. + * + * TODO: move ScoredNetwork.Badging and related constants to this class. + * + * @hide + */ +@SystemApi +public class NetworkBadging { + private NetworkBadging() {} + + /** + * Returns a Wi-Fi icon for a network with a given signal level and badging value. + * + * @param signalLevel The level returned by {@link WifiManager#calculateSignalLevel(int, int)} + * for a network. Must be between 0 and {@link WifiManager#RSSI_LEVELS}-1. + * @param badging {@see ScoredNetwork#Badging}, retrieved from + * {@link ScoredNetwork#calculateBadge(int)}. + * @param theme The theme for the current application, may be null. + * @return Drawable for the given icon + * @throws IllegalArgumentException if {@code signalLevel} is out of range or {@code badging} + * is an invalid value + */ + @NonNull public static Drawable getWifiIcon( + @IntRange(from=0, to=4) int signalLevel, @Badging int badging, @Nullable Theme theme) { + Resources resources = Resources.getSystem(); + if (badging == ScoredNetwork.BADGING_NONE) { + return resources.getDrawable(getWifiSignalResource(signalLevel), theme); + } + Drawable[] layers = new Drawable[] { + resources.getDrawable(getBadgedWifiSignalResource(signalLevel), theme), + resources.getDrawable(getWifiBadgeResource(badging), theme) + }; + return new LayerDrawable(layers); + } + + /** + * Returns the wifi signal resource id for the given signal level. + * + *

This wifi signal resource is a wifi icon to be displayed by itself when there is no badge. + * + * @param signalLevel The level returned by {@link WifiManager#calculateSignalLevel(int, int)} + * for a network. Must be between 0 and {@link WifiManager#RSSI_LEVELS}-1. + * @return the @DrawableRes for the icon + * @throws IllegalArgumentException for an invalid signal level + * @hide + */ + @DrawableRes private static int getWifiSignalResource(int signalLevel) { + switch (signalLevel) { + case 0: + return com.android.internal.R.drawable.ic_wifi_signal_0; + case 1: + return com.android.internal.R.drawable.ic_wifi_signal_1; + case 2: + return com.android.internal.R.drawable.ic_wifi_signal_2; + case 3: + return com.android.internal.R.drawable.ic_wifi_signal_3; + case 4: + return com.android.internal.R.drawable.ic_wifi_signal_4; + default: + throw new IllegalArgumentException("Invalid signal level: " + signalLevel); + } + } + + /** + * Returns the badged wifi signal resource id for the given signal level. + * + *

This badged wifi signal resource should be displayed with the quality badge retrieved + * from {@link #getWifiBadgeResource(int)}. If there is no badge, + * {@link #getWifiBadgeResource(int)} should be used instead of this method. + * + * @param signalLevel The level returned by {@link WifiManager#calculateSignalLevel(int, int)} + * for a network. Must be between 0 and {@link WifiManager#RSSI_LEVELS}-1. + * @return the @DrawableRes for the icon + * @throws IllegalArgumentException for an invalid signal level + * @hide + */ + @DrawableRes private static int getBadgedWifiSignalResource(int signalLevel) { + switch (signalLevel) { + case 0: + return com.android.internal.R.drawable.ic_signal_wifi_badged_0_bars; + case 1: + return com.android.internal.R.drawable.ic_signal_wifi_badged_1_bar; + case 2: + return com.android.internal.R.drawable.ic_signal_wifi_badged_2_bars; + case 3: + return com.android.internal.R.drawable.ic_signal_wifi_badged_3_bars; + case 4: + return com.android.internal.R.drawable.ic_signal_wifi_badged_4_bars; + default: + throw new IllegalArgumentException("Invalid signal level: " + signalLevel); + } + } + + /** + * Returns the wifi quality badge resource id for the the given badging balue. + * + *

This badge should be displayed with the badge signal resource retrieved from + * {@link #getBadgedWifiSignalResource(int)}. + * + * @param badging {@see ScoredNetwork#Badging} from {@link ScoredNetwork#calculateBadge(int)}. + * @return the @DrawableRes for the icon or {@link View#NO_ID} for + * {@link ScoredNetwork#BADGING_NONE} + * @throws IllegalArgumentException for an invalid badging value. + * @hide + */ + @DrawableRes private static int getWifiBadgeResource(@Badging int badging) { + switch (badging) { + case ScoredNetwork.BADGING_NONE: + return View.NO_ID; + case ScoredNetwork.BADGING_SD: + return com.android.internal.R.drawable.ic_signal_wifi_badged_sd; + case ScoredNetwork.BADGING_HD: + return com.android.internal.R.drawable.ic_signal_wifi_badged_hd; + case ScoredNetwork.BADGING_4K: + return com.android.internal.R.drawable.ic_signal_wifi_badged_4k; + default: + throw new IllegalArgumentException("No resource found for badge: " + badging); + } + } +} diff --git a/core/res/res/drawable/ic_signal_wifi_badged_0_bars.xml b/core/res/res/drawable/ic_signal_wifi_badged_0_bars.xml index bd1eb41bee183..606b686bd59b8 100644 --- a/core/res/res/drawable/ic_signal_wifi_badged_0_bars.xml +++ b/core/res/res/drawable/ic_signal_wifi_badged_0_bars.xml @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. --> - + android:width="26dp" + android:height="24dp"> diff --git a/core/res/res/drawable/ic_signal_wifi_badged_1_bar.xml b/core/res/res/drawable/ic_signal_wifi_badged_1_bar.xml index aedb12c310e62..a4c5bc290d902 100644 --- a/core/res/res/drawable/ic_signal_wifi_badged_1_bar.xml +++ b/core/res/res/drawable/ic_signal_wifi_badged_1_bar.xml @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. --> - + android:width="26dp" + android:height="24dp"> diff --git a/core/res/res/drawable/ic_signal_wifi_badged_2_bars.xml b/core/res/res/drawable/ic_signal_wifi_badged_2_bars.xml index 6f07cb5f5009a..9c27833ed4e55 100644 --- a/core/res/res/drawable/ic_signal_wifi_badged_2_bars.xml +++ b/core/res/res/drawable/ic_signal_wifi_badged_2_bars.xml @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. --> - + android:width="26dp" + android:height="24dp"> diff --git a/core/res/res/drawable/ic_signal_wifi_badged_3_bars.xml b/core/res/res/drawable/ic_signal_wifi_badged_3_bars.xml index c41a8ca8a47c4..6d693f1cc94a1 100644 --- a/core/res/res/drawable/ic_signal_wifi_badged_3_bars.xml +++ b/core/res/res/drawable/ic_signal_wifi_badged_3_bars.xml @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. --> - + android:width="26dp" + android:height="24dp"> diff --git a/core/res/res/drawable/ic_signal_wifi_badged_4_bars.xml b/core/res/res/drawable/ic_signal_wifi_badged_4_bars.xml index ec0a52f6a43a9..c48fa364b1e66 100644 --- a/core/res/res/drawable/ic_signal_wifi_badged_4_bars.xml +++ b/core/res/res/drawable/ic_signal_wifi_badged_4_bars.xml @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. --> - + android:width="26dp" + android:height="24dp"> diff --git a/core/res/res/drawable/ic_signal_wifi_badged_4k.xml b/core/res/res/drawable/ic_signal_wifi_badged_4k.xml index 78bd0a060d0ee..0868845bee920 100644 --- a/core/res/res/drawable/ic_signal_wifi_badged_4k.xml +++ b/core/res/res/drawable/ic_signal_wifi_badged_4k.xml @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. --> - + android:width="26dp" + android:height="24dp"> diff --git a/core/res/res/drawable/ic_signal_wifi_badged_hd.xml b/core/res/res/drawable/ic_signal_wifi_badged_hd.xml index 78085c2f5b4da..657f5edf751c8 100644 --- a/core/res/res/drawable/ic_signal_wifi_badged_hd.xml +++ b/core/res/res/drawable/ic_signal_wifi_badged_hd.xml @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. --> - + android:width="26dp" + android:height="24dp"> diff --git a/core/res/res/drawable/ic_signal_wifi_badged_ld.xml b/core/res/res/drawable/ic_signal_wifi_badged_ld.xml index f660ab76e83a3..e2971aa1cba01 100644 --- a/core/res/res/drawable/ic_signal_wifi_badged_ld.xml +++ b/core/res/res/drawable/ic_signal_wifi_badged_ld.xml @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. --> - + android:width="26dp" + android:height="24dp"> diff --git a/core/res/res/drawable/ic_signal_wifi_badged_sd.xml b/core/res/res/drawable/ic_signal_wifi_badged_sd.xml index 43b865325a7eb..b073be36e8409 100644 --- a/core/res/res/drawable/ic_signal_wifi_badged_sd.xml +++ b/core/res/res/drawable/ic_signal_wifi_badged_sd.xml @@ -13,11 +13,11 @@ See the License for the specific language governing permissions and limitations under the License. --> - + android:width="26dp" + android:height="24dp"> diff --git a/core/res/res/drawable/ic_wifi_signal_0.xml b/core/res/res/drawable/ic_wifi_signal_0.xml new file mode 100644 index 0000000000000..e732a8dbbf208 --- /dev/null +++ b/core/res/res/drawable/ic_wifi_signal_0.xml @@ -0,0 +1,26 @@ + + + + + \ No newline at end of file diff --git a/core/res/res/drawable/ic_wifi_signal_1.xml b/core/res/res/drawable/ic_wifi_signal_1.xml new file mode 100644 index 0000000000000..3d006953fcaad --- /dev/null +++ b/core/res/res/drawable/ic_wifi_signal_1.xml @@ -0,0 +1,29 @@ + + + + + + diff --git a/core/res/res/drawable/ic_wifi_signal_2.xml b/core/res/res/drawable/ic_wifi_signal_2.xml new file mode 100644 index 0000000000000..2cce9e90c02b1 --- /dev/null +++ b/core/res/res/drawable/ic_wifi_signal_2.xml @@ -0,0 +1,29 @@ + + + + + + diff --git a/core/res/res/drawable/ic_wifi_signal_3.xml b/core/res/res/drawable/ic_wifi_signal_3.xml new file mode 100644 index 0000000000000..d3b3d3ad6025f --- /dev/null +++ b/core/res/res/drawable/ic_wifi_signal_3.xml @@ -0,0 +1,29 @@ + + + + + + diff --git a/core/res/res/drawable/ic_wifi_signal_4.xml b/core/res/res/drawable/ic_wifi_signal_4.xml new file mode 100644 index 0000000000000..aca4551044ecd --- /dev/null +++ b/core/res/res/drawable/ic_wifi_signal_4.xml @@ -0,0 +1,25 @@ + + + + + diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 16356c73f79b0..4294adab843ad 100644 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -1254,6 +1254,11 @@ + + + + +