• 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.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
onDestroy()96     public void onDestroy() {
97         super.onDestroy();
98         if (mConnectivityListener != null) {
99             mConnectivityListener.destroy();
100         }
101     }
102 
103     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)104     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
105         setPreferencesFromResource(R.xml.wifi_details, null);
106 
107         getPreferenceScreen().setTitle(mAccessPoint.getSsid());
108 
109         mConnectionStatusPref = findPreference(KEY_CONNECTION_STATUS);
110         mIpAddressPref = findPreference(KEY_IP_ADDRESS);
111         mMacAddressPref = findPreference(KEY_MAC_ADDRESS);
112         mSignalStrengthPref = findPreference(KEY_SIGNAL_STRENGTH);
113         mProxySettingsPref = findPreference(KEY_PROXY_SETTINGS);
114         mIpSettingsPref = findPreference(KEY_IP_SETTINGS);
115         mForgetNetworkPref = findPreference(KEY_FORGET_NETWORK);
116     }
117 
118     @Override
onPreferenceTreeClick(Preference preference)119     public boolean onPreferenceTreeClick(Preference preference) {
120         return super.onPreferenceTreeClick(preference);
121     }
122 
123     @Override
onConnectivityChange()124     public void onConnectivityChange() {
125         update();
126     }
127 
128     @Override
onWifiListChanged()129     public void onWifiListChanged() {
130         final List<AccessPoint> accessPoints = mConnectivityListener.getAvailableNetworks();
131         for (final AccessPoint accessPoint : accessPoints) {
132             if (TextUtils.equals(mAccessPoint.getSsidStr(), accessPoint.getSsidStr())
133                     && mAccessPoint.getSecurity() == accessPoint.getSecurity()) {
134                 // Make sure we're not holding on to the one we inflated from the bundle, because
135                 // it won't be updated
136                 mAccessPoint = accessPoint;
137                 break;
138             }
139         }
140         update();
141     }
142 
update()143     private void update() {
144         if (!isAdded()) {
145             return;
146         }
147 
148         final boolean active = mAccessPoint.isActive();
149 
150         mConnectionStatusPref.setSummary(active ? R.string.connected : R.string.not_connected);
151         mIpAddressPref.setVisible(active);
152         mMacAddressPref.setVisible(active);
153         mSignalStrengthPref.setVisible(active);
154 
155         if (active) {
156             mIpAddressPref.setSummary(mConnectivityListener.getWifiIpAddress());
157             mMacAddressPref.setSummary(mConnectivityListener.getWifiMacAddress());
158             mSignalStrengthPref.setSummary(getSignalStrength());
159         }
160 
161         WifiConfiguration wifiConfiguration = mAccessPoint.getConfig();
162         if (wifiConfiguration != null) {
163             final int networkId = wifiConfiguration.networkId;
164             mProxySettingsPref.setSummary(
165                     wifiConfiguration.getProxySettings() == IpConfiguration.ProxySettings.NONE
166                             ? R.string.wifi_action_proxy_none : R.string.wifi_action_proxy_manual);
167             mProxySettingsPref.setIntent(EditProxySettingsActivity.createIntent(getContext(),
168                     networkId));
169 
170             mIpSettingsPref.setSummary(
171                     wifiConfiguration.getIpAssignment() == IpConfiguration.IpAssignment.STATIC
172                             ? R.string.wifi_action_static : R.string.wifi_action_dhcp);
173             mIpSettingsPref.setIntent(EditIpSettingsActivity.createIntent(getContext(), networkId));
174 
175             mForgetNetworkPref.setFragment(ForgetNetworkConfirmFragment.class.getName());
176             ForgetNetworkConfirmFragment.prepareArgs(mForgetNetworkPref.getExtras(), mAccessPoint);
177         }
178 
179         mProxySettingsPref.setVisible(wifiConfiguration != null);
180         mIpSettingsPref.setVisible(wifiConfiguration != null);
181         mForgetNetworkPref.setVisible(wifiConfiguration != null);
182     }
183 
getSignalStrength()184     private String getSignalStrength() {
185         String[] signalLevels = getResources().getStringArray(R.array.wifi_signal_strength);
186         int strength = mConnectivityListener.getWifiSignalStrength(signalLevels.length);
187         return signalLevels[strength];
188     }
189 
190     public static class ForgetNetworkConfirmFragment extends GuidedStepFragment {
191 
192         private AccessPoint mAccessPoint;
193 
prepareArgs(@onNull Bundle args, AccessPoint accessPoint)194         public static void prepareArgs(@NonNull Bundle args, AccessPoint accessPoint) {
195             final Bundle apBundle = new Bundle();
196             accessPoint.saveWifiState(apBundle);
197             args.putParcelable(ARG_ACCESS_POINT_STATE, apBundle);
198         }
199 
200         @Override
onCreate(Bundle savedInstanceState)201         public void onCreate(Bundle savedInstanceState) {
202             mAccessPoint = new AccessPoint(getContext(),
203                     getArguments().getBundle(ARG_ACCESS_POINT_STATE));
204             super.onCreate(savedInstanceState);
205         }
206 
207         @NonNull
208         @Override
onCreateGuidance(Bundle savedInstanceState)209         public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) {
210             return new GuidanceStylist.Guidance(
211                     getString(R.string.wifi_forget_network),
212                     getString(R.string.wifi_forget_network_description),
213                     mAccessPoint.getSsidStr(),
214                     getContext().getDrawable(R.drawable.ic_wifi_signal_4_white_132dp));
215         }
216 
217         @Override
onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)218         public void onCreateActions(@NonNull List<GuidedAction> actions,
219                 Bundle savedInstanceState) {
220             final Context context = getContext();
221             actions.add(new GuidedAction.Builder(context)
222                     .clickAction(GuidedAction.ACTION_ID_OK)
223                     .build());
224             actions.add(new GuidedAction.Builder(context)
225                     .clickAction(GuidedAction.ACTION_ID_CANCEL)
226                     .build());
227         }
228 
229         @Override
onGuidedActionClicked(GuidedAction action)230         public void onGuidedActionClicked(GuidedAction action) {
231             if (action.getId() == GuidedAction.ACTION_ID_OK) {
232                 WifiManager wifiManager =
233                         (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
234                 wifiManager.forget(mAccessPoint.getConfig().networkId, null);
235             }
236             getFragmentManager().popBackStack();
237         }
238     }
239 }
240