1 /*
2  * Copyright (C) 2020 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 com.android.settings.wifi.slice;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 
22 import com.android.settingslib.R;
23 import com.android.wifitrackerlib.HotspotNetworkEntry;
24 import com.android.wifitrackerlib.WifiEntry;
25 
26 /**
27  * The data set which is needed by a Wi-Fi Slice, it collects necessary data from {@link WifiEntry}
28  * and provides similar getter methods for corresponding data.
29  *
30  * @deprecated this is not used after V and will be removed.
31  */
32 @Deprecated(forRemoval = true)
33 public class WifiSliceItem {
34 
35     private final Context mContext;
36     private final String mKey;
37     private final String mTitle;
38     private final int mSecurity;
39     private final int mConnectedState;
40     private final int mLevel;
41     private final boolean mShouldShowXLevelIcon;
42     private final boolean mShouldEditBeforeConnect;
43     private final boolean mHasInternetAccess;
44     private final String mSummary;
45 
46     private boolean mIsInstantHotspotNetwork;
47     private int mInstantHotspotDeviceType;
48 
49     // These values must be kept within [WifiEntry.WIFI_LEVEL_MIN, WifiEntry.WIFI_LEVEL_MAX]
50     private static final int[] WIFI_CONNECTION_STRENGTH = {
51             R.string.accessibility_no_wifi,
52             R.string.accessibility_wifi_one_bar,
53             R.string.accessibility_wifi_two_bars,
54             R.string.accessibility_wifi_three_bars,
55             R.string.accessibility_wifi_signal_full
56     };
57 
WifiSliceItem(Context context, WifiEntry wifiEntry)58     public WifiSliceItem(Context context, WifiEntry wifiEntry) {
59         mContext = context;
60         mKey = wifiEntry.getKey();
61         mTitle = wifiEntry.getTitle();
62         mSecurity = wifiEntry.getSecurity();
63         mConnectedState = wifiEntry.getConnectedState();
64         mLevel = wifiEntry.getLevel();
65         mShouldShowXLevelIcon = wifiEntry.shouldShowXLevelIcon();
66         mShouldEditBeforeConnect = wifiEntry.shouldEditBeforeConnect();
67         mHasInternetAccess = wifiEntry.hasInternetAccess();
68         mSummary = wifiEntry.getSummary(false /* concise */);
69         mIsInstantHotspotNetwork = wifiEntry instanceof HotspotNetworkEntry;
70         if (mIsInstantHotspotNetwork) {
71             mInstantHotspotDeviceType = ((HotspotNetworkEntry) wifiEntry).getDeviceType();
72         }
73     }
74 
75     @Override
equals(Object other)76     public boolean equals(Object other) {
77         if (!(other instanceof WifiSliceItem)) {
78             return false;
79         }
80 
81         final WifiSliceItem otherItem = (WifiSliceItem) other;
82         if (!TextUtils.equals(getKey(), otherItem.getKey())) {
83             return false;
84         }
85         if (getConnectedState() != otherItem.getConnectedState()) {
86             return false;
87         }
88         if (getLevel() != otherItem.getLevel()) {
89             return false;
90         }
91         if (shouldShowXLevelIcon() != otherItem.shouldShowXLevelIcon()) {
92             return false;
93         }
94         if (!TextUtils.equals(getSummary(), otherItem.getSummary())) {
95             return false;
96         }
97         if (isInstantHotspotNetwork() != otherItem.isInstantHotspotNetwork()) {
98             return false;
99         }
100         if (getInstantHotspotDeviceType() != otherItem.getInstantHotspotDeviceType()) {
101             return false;
102         }
103         return true;
104     }
105 
getKey()106     public String getKey() {
107         return mKey;
108     }
109 
getTitle()110     public String getTitle() {
111         return mTitle;
112     }
113 
getSecurity()114     public int getSecurity() {
115         return mSecurity;
116     }
117 
getConnectedState()118     public int getConnectedState() {
119         return mConnectedState;
120     }
121 
getLevel()122     public int getLevel() {
123         return mLevel;
124     }
125 
126     /**
127      * Returns whether the level icon for this network should show an X or not.
128      */
shouldShowXLevelIcon()129     public boolean shouldShowXLevelIcon() {
130         return mShouldShowXLevelIcon;
131     }
132 
133     /**
134      * Returns true when the Wi-Fi network has Internet access.
135      */
hasInternetAccess()136     public boolean hasInternetAccess() {
137         return mHasInternetAccess;
138     }
139 
140     /**
141      * In Wi-Fi picker, when users click a saved network, it will connect to the Wi-Fi network.
142      * However, for some special cases, Wi-Fi picker should show Wi-Fi editor UI for users to edit
143      * security or password before connecting. Or users will always get connection fail results.
144      */
shouldEditBeforeConnect()145     public boolean shouldEditBeforeConnect() {
146         return mShouldEditBeforeConnect;
147     }
148 
149     /**
150      * Returns a 'NOT' concise summary, this is different from WifiEntry#getSummary().
151      */
getSummary()152     public String getSummary() {
153         return mSummary;
154     }
155 
156     /**
157      * Returns true if this is a Instant Hotspot network.
158      */
isInstantHotspotNetwork()159     public boolean isInstantHotspotNetwork() {
160         return mIsInstantHotspotNetwork;
161     }
162 
163     /**
164      * Returns DeviceType of Instant Hotspot network.
165      */
getInstantHotspotDeviceType()166     public int getInstantHotspotDeviceType() {
167         return mInstantHotspotDeviceType;
168     }
169 
170     /**
171      * This method has similar code as WifiEntryPreference#buildContentDescription().
172      * TODO(b/154191825): Adds WifiEntry#getContentDescription() to replace the duplicate code.
173      */
getContentDescription()174     public CharSequence getContentDescription() {
175         CharSequence contentDescription = mTitle;
176         if (!TextUtils.isEmpty(mSummary)) {
177             contentDescription = TextUtils.concat(contentDescription, ",", mSummary);
178         }
179         if (mLevel >= 0 && mLevel < WIFI_CONNECTION_STRENGTH.length) {
180             contentDescription = TextUtils.concat(contentDescription, ",",
181                     mContext.getString(WIFI_CONNECTION_STRENGTH[mLevel]));
182         }
183         return TextUtils.concat(contentDescription, ",", mSecurity == WifiEntry.SECURITY_NONE
184                 ? mContext.getString(R.string.accessibility_wifi_security_type_none)
185                 : mContext.getString(R.string.accessibility_wifi_security_type_secured));
186     }
187 }
188