2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.wifi;
19 import com.android.settings.R;
20 import com.android.settings.SettingsPreferenceFragment;
22 import android.app.Activity;
23 import android.content.Context;
24 import android.net.wifi.WifiInfo;
25 import android.net.wifi.WifiManager;
26 import android.os.Bundle;
27 import android.os.SystemProperties;
28 import android.preference.ListPreference;
29 import android.preference.Preference;
30 import android.text.TextUtils;
31 import android.widget.Toast;
32 import android.util.Log;
34 public class AdvancedSettings extends SettingsPreferenceFragment
35 implements Preference.OnPreferenceChangeListener {
37 private static final String TAG = "AdvancedSettings";
38 private static final String KEY_MAC_ADDRESS = "mac_address";
39 private static final String KEY_CURRENT_IP_ADDRESS = "current_ip_address";
40 private static final String KEY_FREQUENCY_BAND = "frequency_band";
42 private WifiManager mWifiManager;
45 public void onCreate(Bundle savedInstanceState) {
46 super.onCreate(savedInstanceState);
47 addPreferencesFromResource(R.xml.wifi_advanced_settings);
51 public void onActivityCreated(Bundle savedInstanceState) {
52 super.onActivityCreated(savedInstanceState);
53 mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
57 public void onResume() {
63 private void initPreferences() {
65 ListPreference pref = (ListPreference) findPreference(KEY_FREQUENCY_BAND);
67 if (mWifiManager.isDualBandSupported()) {
68 pref.setOnPreferenceChangeListener(this);
69 int value = mWifiManager.getFrequencyBand();
71 pref.setValue(String.valueOf(value));
73 Log.e(TAG, "Failed to fetch frequency band");
76 getPreferenceScreen().removePreference(pref);
80 public boolean onPreferenceChange(Preference preference, Object newValue) {
81 String key = preference.getKey();
82 if (key == null) return true;
84 if (key.equals(KEY_FREQUENCY_BAND)) {
86 mWifiManager.setFrequencyBand(Integer.parseInt(((String) newValue)), true);
87 } catch (NumberFormatException e) {
88 Toast.makeText(getActivity(), R.string.wifi_setting_frequency_band_error,
89 Toast.LENGTH_SHORT).show();
97 private void refreshWifiInfo() {
98 WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
100 Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS);
101 String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
102 wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress
103 : getActivity().getString(R.string.status_unavailable));
105 Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS);
106 String ipAddress = null;
107 if (wifiInfo != null) {
108 long addr = wifiInfo.getIpAddress();
110 // handle negative values whe first octet > 127
111 if (addr < 0) addr += 0x100000000L;
112 ipAddress = String.format("%d.%d.%d.%d",
113 addr & 0xFF, (addr >> 8) & 0xFF, (addr >> 16) & 0xFF, (addr >> 24) & 0xFF);
116 wifiIpAddressPref.setSummary(ipAddress == null ?
117 getActivity().getString(R.string.status_unavailable) : ipAddress);