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