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