Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.annotation.DrawableRes; |
Sundeep Ghuman | 699deaf | 2017-02-13 15:32:13 -0800 | [diff] [blame] | 20 | import android.annotation.IntDef; |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 21 | import android.annotation.IntRange; |
| 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 24 | import android.content.res.Resources; |
| 25 | import android.content.res.Resources.Theme; |
| 26 | import android.graphics.drawable.Drawable; |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 27 | import android.net.wifi.WifiManager; |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 28 | |
Sundeep Ghuman | 699deaf | 2017-02-13 15:32:13 -0800 | [diff] [blame] | 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
| 31 | |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 32 | /** |
| 33 | * Utility methods for working with network badging. |
| 34 | * |
Joe LaPenna | 2c60ef0 | 2017-05-01 09:46:01 -0700 | [diff] [blame] | 35 | * @removed |
| 36 | * |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 37 | */ |
Joe LaPenna | 2c60ef0 | 2017-05-01 09:46:01 -0700 | [diff] [blame] | 38 | @Deprecated |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 39 | public class NetworkBadging { |
Sundeep Ghuman | 699deaf | 2017-02-13 15:32:13 -0800 | [diff] [blame] | 40 | |
| 41 | @IntDef({BADGING_NONE, BADGING_SD, BADGING_HD, BADGING_4K}) |
| 42 | @Retention(RetentionPolicy.SOURCE) |
| 43 | public @interface Badging {} |
| 44 | |
| 45 | public static final int BADGING_NONE = 0; |
| 46 | public static final int BADGING_SD = 10; |
| 47 | public static final int BADGING_HD = 20; |
| 48 | public static final int BADGING_4K = 30; |
| 49 | |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 50 | private NetworkBadging() {} |
| 51 | |
| 52 | /** |
| 53 | * Returns a Wi-Fi icon for a network with a given signal level and badging value. |
| 54 | * |
| 55 | * @param signalLevel The level returned by {@link WifiManager#calculateSignalLevel(int, int)} |
| 56 | * for a network. Must be between 0 and {@link WifiManager#RSSI_LEVELS}-1. |
Stephen Chen | d831be9 | 2017-03-29 15:51:42 -0700 | [diff] [blame] | 57 | * @param badging {@see NetworkBadging#Badging}, retrieved from |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 58 | * {@link ScoredNetwork#calculateBadge(int)}. |
| 59 | * @param theme The theme for the current application, may be null. |
| 60 | * @return Drawable for the given icon |
| 61 | * @throws IllegalArgumentException if {@code signalLevel} is out of range or {@code badging} |
| 62 | * is an invalid value |
| 63 | */ |
| 64 | @NonNull public static Drawable getWifiIcon( |
| 65 | @IntRange(from=0, to=4) int signalLevel, @Badging int badging, @Nullable Theme theme) { |
Sundeep Ghuman | 19f64d0 | 2017-06-06 13:02:15 -0700 | [diff] [blame] | 66 | return Resources.getSystem().getDrawable(getWifiSignalResource(signalLevel), theme); |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the wifi signal resource id for the given signal level. |
| 71 | * |
| 72 | * <p>This wifi signal resource is a wifi icon to be displayed by itself when there is no badge. |
| 73 | * |
| 74 | * @param signalLevel The level returned by {@link WifiManager#calculateSignalLevel(int, int)} |
| 75 | * for a network. Must be between 0 and {@link WifiManager#RSSI_LEVELS}-1. |
| 76 | * @return the @DrawableRes for the icon |
| 77 | * @throws IllegalArgumentException for an invalid signal level |
| 78 | * @hide |
| 79 | */ |
| 80 | @DrawableRes private static int getWifiSignalResource(int signalLevel) { |
| 81 | switch (signalLevel) { |
| 82 | case 0: |
| 83 | return com.android.internal.R.drawable.ic_wifi_signal_0; |
| 84 | case 1: |
| 85 | return com.android.internal.R.drawable.ic_wifi_signal_1; |
| 86 | case 2: |
| 87 | return com.android.internal.R.drawable.ic_wifi_signal_2; |
| 88 | case 3: |
| 89 | return com.android.internal.R.drawable.ic_wifi_signal_3; |
| 90 | case 4: |
| 91 | return com.android.internal.R.drawable.ic_wifi_signal_4; |
| 92 | default: |
| 93 | throw new IllegalArgumentException("Invalid signal level: " + signalLevel); |
| 94 | } |
| 95 | } |
Amin Shaikh | 15902ee | 2017-01-13 11:02:16 -0800 | [diff] [blame] | 96 | } |