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
17package android.net;
18
19import android.annotation.DrawableRes;
20import android.annotation.IntDef;
21import android.annotation.IntRange;
22import android.annotation.NonNull;
23import android.annotation.Nullable;
24import android.annotation.SystemApi;
25import android.content.res.Resources;
26import android.content.res.Resources.Theme;
27import android.graphics.drawable.Drawable;
28import android.graphics.drawable.LayerDrawable;
29import android.net.wifi.WifiManager;
30import android.view.View;
31
32import java.lang.annotation.Retention;
33import java.lang.annotation.RetentionPolicy;
34
35/**
36 * Utility methods for working with network badging.
37 *
38 * @removed
39 *
40 */
41@Deprecated
42public class NetworkBadging {
43
44    @IntDef({BADGING_NONE, BADGING_SD, BADGING_HD, BADGING_4K})
45    @Retention(RetentionPolicy.SOURCE)
46    public @interface Badging {}
47
48    public static final int BADGING_NONE = 0;
49    public static final int BADGING_SD = 10;
50    public static final int BADGING_HD = 20;
51    public static final int BADGING_4K = 30;
52
53    private NetworkBadging() {}
54
55    /**
56     * Returns a Wi-Fi icon for a network with a given signal level and badging value.
57     *
58     * @param signalLevel The level returned by {@link WifiManager#calculateSignalLevel(int, int)}
59     *                    for a network. Must be between 0 and {@link WifiManager#RSSI_LEVELS}-1.
60     * @param badging  {@see NetworkBadging#Badging}, retrieved from
61     *                 {@link ScoredNetwork#calculateBadge(int)}.
62     * @param theme The theme for the current application, may be null.
63     * @return Drawable for the given icon
64     * @throws IllegalArgumentException if {@code signalLevel} is out of range or {@code badging}
65     *                                  is an invalid value
66     */
67    @NonNull public static Drawable getWifiIcon(
68            @IntRange(from=0, to=4) int signalLevel, @Badging int badging, @Nullable Theme theme) {
69        return Resources.getSystem().getDrawable(getWifiSignalResource(signalLevel), theme);
70    }
71
72    /**
73     * Returns the wifi signal resource id for the given signal level.
74     *
75     * <p>This wifi signal resource is a wifi icon to be displayed by itself when there is no badge.
76     *
77     * @param signalLevel The level returned by {@link WifiManager#calculateSignalLevel(int, int)}
78     *                    for a network. Must be between 0 and {@link WifiManager#RSSI_LEVELS}-1.
79     * @return the @DrawableRes for the icon
80     * @throws IllegalArgumentException for an invalid signal level
81     * @hide
82     */
83    @DrawableRes private static int getWifiSignalResource(int signalLevel) {
84        switch (signalLevel) {
85            case 0:
86                return com.android.internal.R.drawable.ic_wifi_signal_0;
87            case 1:
88                return com.android.internal.R.drawable.ic_wifi_signal_1;
89            case 2:
90                return com.android.internal.R.drawable.ic_wifi_signal_2;
91            case 3:
92                return com.android.internal.R.drawable.ic_wifi_signal_3;
93            case 4:
94                return com.android.internal.R.drawable.ic_wifi_signal_4;
95            default:
96                throw new IllegalArgumentException("Invalid signal level: " + signalLevel);
97        }
98    }
99
100    /**
101     * Returns the badged wifi signal resource id for the given signal level.
102     *
103     * <p>This badged wifi signal resource should be displayed with the quality badge retrieved
104     * from {@link #getWifiBadgeResource(int)}. If there is no badge,
105     * {@link #getWifiBadgeResource(int)} should be used instead of this method.
106     *
107     * @param signalLevel The level returned by {@link WifiManager#calculateSignalLevel(int, int)}
108     *                    for a network. Must be between 0 and {@link WifiManager#RSSI_LEVELS}-1.
109     * @return the @DrawableRes for the icon
110     * @throws IllegalArgumentException for an invalid signal level
111     * @hide
112     */
113    @DrawableRes private static int getBadgedWifiSignalResource(int signalLevel) {
114        switch (signalLevel) {
115            case 0:
116                return com.android.internal.R.drawable.ic_signal_wifi_badged_0_bars;
117            case 1:
118                return com.android.internal.R.drawable.ic_signal_wifi_badged_1_bar;
119            case 2:
120                return com.android.internal.R.drawable.ic_signal_wifi_badged_2_bars;
121            case 3:
122                return com.android.internal.R.drawable.ic_signal_wifi_badged_3_bars;
123            case 4:
124                return com.android.internal.R.drawable.ic_signal_wifi_badged_4_bars;
125            default:
126                throw new IllegalArgumentException("Invalid signal level: " + signalLevel);
127        }
128    }
129}
130