1 /* 2 * Copyright (C) 2016 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.tv.settings.connectivity; 18 19 import android.content.Context; 20 import android.net.IpConfiguration; 21 import android.net.wifi.WifiConfiguration; 22 import android.net.wifi.WifiManager; 23 import android.os.Bundle; 24 import android.support.annotation.NonNull; 25 import android.support.v17.leanback.app.GuidedStepFragment; 26 import android.support.v17.leanback.widget.GuidanceStylist; 27 import android.support.v17.leanback.widget.GuidedAction; 28 import android.support.v17.preference.LeanbackPreferenceFragment; 29 import android.support.v7.preference.Preference; 30 import android.text.TextUtils; 31 32 import com.android.settingslib.wifi.AccessPoint; 33 import com.android.tv.settings.R; 34 35 import java.util.List; 36 37 public class WifiDetailsFragment extends LeanbackPreferenceFragment 38 implements ConnectivityListener.Listener, ConnectivityListener.WifiNetworkListener { 39 40 private static final String ARG_ACCESS_POINT_STATE = "apBundle"; 41 42 private static final String KEY_CONNECTION_STATUS = "connection_status"; 43 private static final String KEY_IP_ADDRESS = "ip_address"; 44 private static final String KEY_MAC_ADDRESS = "mac_address"; 45 private static final String KEY_SIGNAL_STRENGTH = "signal_strength"; 46 private static final String KEY_PROXY_SETTINGS = "proxy_settings"; 47 private static final String KEY_IP_SETTINGS = "ip_settings"; 48 private static final String KEY_FORGET_NETWORK = "forget_network"; 49 50 private Preference mConnectionStatusPref; 51 private Preference mIpAddressPref; 52 private Preference mMacAddressPref; 53 private Preference mSignalStrengthPref; 54 private Preference mProxySettingsPref; 55 private Preference mIpSettingsPref; 56 private Preference mForgetNetworkPref; 57 58 private ConnectivityListener mConnectivityListener; 59 private AccessPoint mAccessPoint; 60 prepareArgs(@onNull Bundle args, AccessPoint accessPoint)61 public static void prepareArgs(@NonNull Bundle args, AccessPoint accessPoint) { 62 final Bundle apBundle = new Bundle(); 63 accessPoint.saveWifiState(apBundle); 64 args.putParcelable(ARG_ACCESS_POINT_STATE, apBundle); 65 } 66 67 @Override onCreate(Bundle savedInstanceState)68 public void onCreate(Bundle savedInstanceState) { 69 mConnectivityListener = new ConnectivityListener(getContext(), this); 70 71 mAccessPoint = new AccessPoint(getContext(), 72 getArguments().getBundle(ARG_ACCESS_POINT_STATE)); 73 super.onCreate(savedInstanceState); 74 } 75 76 @Override onStart()77 public void onStart() { 78 super.onStart(); 79 mConnectivityListener.start(); 80 mConnectivityListener.setWifiListener(this); 81 } 82 83 @Override onResume()84 public void onResume() { 85 super.onResume(); 86 update(); 87 } 88 89 @Override onStop()90 public void onStop() { 91 super.onStop(); 92 mConnectivityListener.stop(); 93 } 94 95 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)96 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 97 setPreferencesFromResource(R.xml.wifi_details, null); 98 99 getPreferenceScreen().setTitle(mAccessPoint.getSsid()); 100 101 mConnectionStatusPref = findPreference(KEY_CONNECTION_STATUS); 102 mIpAddressPref = findPreference(KEY_IP_ADDRESS); 103 mMacAddressPref = findPreference(KEY_MAC_ADDRESS); 104 mSignalStrengthPref = findPreference(KEY_SIGNAL_STRENGTH); 105 mProxySettingsPref = findPreference(KEY_PROXY_SETTINGS); 106 mIpSettingsPref = findPreference(KEY_IP_SETTINGS); 107 mForgetNetworkPref = findPreference(KEY_FORGET_NETWORK); 108 } 109 110 @Override onPreferenceTreeClick(Preference preference)111 public boolean onPreferenceTreeClick(Preference preference) { 112 return super.onPreferenceTreeClick(preference); 113 } 114 115 @Override onConnectivityChange()116 public void onConnectivityChange() { 117 update(); 118 } 119 120 @Override onWifiListChanged()121 public void onWifiListChanged() { 122 final List<AccessPoint> accessPoints = mConnectivityListener.getAvailableNetworks(); 123 for (final AccessPoint accessPoint : accessPoints) { 124 if (TextUtils.equals(mAccessPoint.getSsidStr(), accessPoint.getSsidStr()) 125 && mAccessPoint.getSecurity() == accessPoint.getSecurity()) { 126 // Make sure we're not holding on to the one we inflated from the bundle, because 127 // it won't be updated 128 mAccessPoint = accessPoint; 129 break; 130 } 131 } 132 update(); 133 } 134 update()135 private void update() { 136 if (!isAdded()) { 137 return; 138 } 139 140 final boolean active = mAccessPoint.isActive(); 141 142 mConnectionStatusPref.setSummary(active ? R.string.connected : R.string.not_connected); 143 mIpAddressPref.setVisible(active); 144 mMacAddressPref.setVisible(active); 145 mSignalStrengthPref.setVisible(active); 146 147 if (active) { 148 mIpAddressPref.setSummary(mConnectivityListener.getWifiIpAddress()); 149 mMacAddressPref.setSummary(mConnectivityListener.getWifiMacAddress()); 150 mSignalStrengthPref.setSummary(getSignalStrength()); 151 } 152 153 WifiConfiguration wifiConfiguration = mAccessPoint.getConfig(); 154 if (wifiConfiguration != null) { 155 final int networkId = wifiConfiguration.networkId; 156 mProxySettingsPref.setSummary( 157 wifiConfiguration.getProxySettings() == IpConfiguration.ProxySettings.NONE 158 ? R.string.wifi_action_proxy_none : R.string.wifi_action_proxy_manual); 159 mProxySettingsPref.setIntent(EditProxySettingsActivity.createIntent(getContext(), 160 networkId)); 161 162 mIpSettingsPref.setSummary( 163 wifiConfiguration.getIpAssignment() == IpConfiguration.IpAssignment.STATIC 164 ? R.string.wifi_action_static : R.string.wifi_action_dhcp); 165 mIpSettingsPref.setIntent(EditIpSettingsActivity.createIntent(getContext(), networkId)); 166 167 mForgetNetworkPref.setFragment(ForgetNetworkConfirmFragment.class.getName()); 168 ForgetNetworkConfirmFragment.prepareArgs(mForgetNetworkPref.getExtras(), mAccessPoint); 169 } 170 171 mProxySettingsPref.setVisible(wifiConfiguration != null); 172 mIpSettingsPref.setVisible(wifiConfiguration != null); 173 mForgetNetworkPref.setVisible(wifiConfiguration != null); 174 } 175 getSignalStrength()176 private String getSignalStrength() { 177 String[] signalLevels = getResources().getStringArray(R.array.wifi_signal_strength); 178 int strength = mConnectivityListener.getWifiSignalStrength(signalLevels.length); 179 return signalLevels[strength]; 180 } 181 182 public static class ForgetNetworkConfirmFragment extends GuidedStepFragment { 183 184 private AccessPoint mAccessPoint; 185 prepareArgs(@onNull Bundle args, AccessPoint accessPoint)186 public static void prepareArgs(@NonNull Bundle args, AccessPoint accessPoint) { 187 final Bundle apBundle = new Bundle(); 188 accessPoint.saveWifiState(apBundle); 189 args.putParcelable(ARG_ACCESS_POINT_STATE, apBundle); 190 } 191 192 @Override onCreate(Bundle savedInstanceState)193 public void onCreate(Bundle savedInstanceState) { 194 mAccessPoint = new AccessPoint(getContext(), 195 getArguments().getBundle(ARG_ACCESS_POINT_STATE)); 196 super.onCreate(savedInstanceState); 197 } 198 199 @NonNull 200 @Override onCreateGuidance(Bundle savedInstanceState)201 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 202 return new GuidanceStylist.Guidance( 203 getString(R.string.wifi_forget_network), 204 getString(R.string.wifi_forget_network_description), 205 mAccessPoint.getSsidStr(), 206 getContext().getDrawable(R.drawable.ic_wifi_signal_4_white_132dp)); 207 } 208 209 @Override onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)210 public void onCreateActions(@NonNull List<GuidedAction> actions, 211 Bundle savedInstanceState) { 212 final Context context = getContext(); 213 actions.add(new GuidedAction.Builder(context) 214 .clickAction(GuidedAction.ACTION_ID_OK) 215 .build()); 216 actions.add(new GuidedAction.Builder(context) 217 .clickAction(GuidedAction.ACTION_ID_CANCEL) 218 .build()); 219 } 220 221 @Override onGuidedActionClicked(GuidedAction action)222 public void onGuidedActionClicked(GuidedAction action) { 223 if (action.getId() == GuidedAction.ACTION_ID_OK) { 224 WifiManager wifiManager = 225 (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); 226 wifiManager.forget(mAccessPoint.getConfig().networkId, null); 227 } 228 getFragmentManager().popBackStack(); 229 } 230 } 231 } 232