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