diff options
author | Jorge Ruesga <jorge@ruesga.com> | 2013-01-19 02:22:49 +0100 |
---|---|---|
committer | Gerrit Code Review <gerrit@review.cyanogenmod.com> | 2013-01-26 21:26:17 -0800 |
commit | 7d1af4cf5075b382ef0b6c87362d61207ebe86e1 (patch) | |
tree | 9d7aedd8b5deba8c6ff23c7bc3217baf660e21f8 /src/com | |
parent | b1d2a4b12a35ff767709d6626732dbc71ec53f16 (diff) | |
download | packages_apps_settings-7d1af4cf5075b382ef0b6c87362d61207ebe86e1.zip packages_apps_settings-7d1af4cf5075b382ef0b6c87362d61207ebe86e1.tar.gz packages_apps_settings-7d1af4cf5075b382ef0b6c87362d61207ebe86e1.tar.bz2 |
Wi-Fi: Manage Wi-Fi AP priorities
AOSP has a basic LRU priority for Wi-Fi networks (last connected has more priority).
This changes added the ability of graphically set the priority of any of the configured
Wi-Fi networks through Settings -> Wi-Fi -> Advanced Settings -> Wi-Fi priority.
Patchset 2: Move WiFiPriority from cyanogenmod folder to wifi folder
Remove double quotes in SSID name
Patchset 3: Fixed code issues
Change-Id: Ief590e799cdb7ef760b631d0b22d9719f867393f
Signed-off-by: Jorge Ruesga <jorge@ruesga.com>
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/android/settings/wifi/AdvancedWifiSettings.java | 5 | ||||
-rw-r--r-- | src/com/android/settings/wifi/WifiPriority.java | 187 |
2 files changed, 192 insertions, 0 deletions
diff --git a/src/com/android/settings/wifi/AdvancedWifiSettings.java b/src/com/android/settings/wifi/AdvancedWifiSettings.java index 99b2f10..3777eb2 100644 --- a/src/com/android/settings/wifi/AdvancedWifiSettings.java +++ b/src/com/android/settings/wifi/AdvancedWifiSettings.java @@ -17,6 +17,7 @@ package com.android.settings.wifi; import android.content.Context; +import android.content.Intent; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.WifiWatchdogStateMachine; @@ -47,6 +48,7 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment private static final String KEY_SLEEP_POLICY = "sleep_policy"; private static final String KEY_POOR_NETWORK_DETECTION = "wifi_poor_network_detection"; private static final String KEY_SUSPEND_OPTIMIZATIONS = "suspend_optimizations"; + private static final String KEY_WIFI_PRIORITY = "wifi_priority"; private WifiManager mWifiManager; @@ -135,6 +137,9 @@ public class AdvancedWifiSettings extends SettingsPreferenceFragment sleepPolicyPref.setValue(stringValue); updateSleepPolicySummary(sleepPolicyPref, stringValue); } + + Preference wifiPriority = findPreference(KEY_WIFI_PRIORITY); + wifiPriority.setEnabled(mWifiManager.isWifiEnabled()); } private void updateSleepPolicySummary(Preference sleepPolicyPref, String value) { diff --git a/src/com/android/settings/wifi/WifiPriority.java b/src/com/android/settings/wifi/WifiPriority.java new file mode 100644 index 0000000..74f997a --- /dev/null +++ b/src/com/android/settings/wifi/WifiPriority.java @@ -0,0 +1,187 @@ +/* + * Copyright (C) 2013 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.wifi; + +import android.app.ListFragment; +import android.content.Context; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiManager; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ListView; +import android.widget.TextView; + +import com.android.settings.R; +import com.android.settings.cyanogenmod.TouchInterceptor; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +public class WifiPriority extends ListFragment { + + private final TouchInterceptor.DropListener mDropListener = + new TouchInterceptor.DropListener() { + public void drop(int from, int to) { + if (from == to) return; + + // Sort networks by user selection + List<WifiConfiguration> mNetworks = mAdapter.getNetworks(); + WifiConfiguration o = mNetworks.remove(from); + mNetworks.add(to, o); + + // Set the new priorities of the networks + int cc = mNetworks.size(); + for (int i = 0; i < cc; i++) { + WifiConfiguration network = mNetworks.get(i); + network.priority = cc - i; + + // Update the priority + mWifiManager.updateNetwork(network); + } + + // Now, save all the Wi-Fi configuration with its new priorities + mWifiManager.saveConfiguration(); + + // Reload the networks + mAdapter.reloadNetworks(); + mNetworksListView.invalidateViews(); + } + }; + + private WifiManager mWifiManager; + private TouchInterceptor mNetworksListView; + private WifiPriorityAdapter mAdapter; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + return inflater.inflate(R.layout.wifi_network_priority, null); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + Context ctx = getActivity().getApplicationContext(); + mWifiManager = (WifiManager)ctx.getSystemService(Context.WIFI_SERVICE); + + // Set the touchable listview + mNetworksListView = (TouchInterceptor)getListView(); + mNetworksListView.setDropListener(mDropListener); + mAdapter = new WifiPriorityAdapter(ctx, mWifiManager); + setListAdapter(mAdapter); + } + + @Override + public void onDestroy() { + mNetworksListView.setDropListener(null); + setListAdapter(null); + super.onDestroy(); + } + + @Override + public void onResume() { + super.onResume(); + + // Reload the networks + mAdapter.reloadNetworks(); + mNetworksListView.invalidateViews(); + } + + private class WifiPriorityAdapter extends BaseAdapter { + + private final WifiManager mWifiManager; + private final LayoutInflater mInflater; + private List<WifiConfiguration> mNetworks; + + public WifiPriorityAdapter(Context ctx, WifiManager wifiManager) { + mWifiManager = wifiManager; + mInflater = LayoutInflater.from(ctx); + reloadNetworks(); + } + + private void reloadNetworks() { + mNetworks = mWifiManager.getConfiguredNetworks(); + if (mNetworks == null) { + mNetworks = new ArrayList<WifiConfiguration>(); + } + + // Sort network list by priority (or by network id if the priority is the same) + Collections.sort(mNetworks, new Comparator<WifiConfiguration>() { + @Override + public int compare(WifiConfiguration lhs, WifiConfiguration rhs) { + // > priority -- > lower position + if (lhs.priority < rhs.priority) return 1; + if (lhs.priority > rhs.priority) return -1; + // < network id -- > lower position + if (lhs.networkId < rhs.networkId) return -1; + if (lhs.networkId > rhs.networkId) return 1; + return 0; + } + }); + } + + /**package**/ List<WifiConfiguration> getNetworks() { + return mNetworks; + } + + @Override + public int getCount() { + return mNetworks.size(); + } + + @Override + public Object getItem(int position) { + return mNetworks.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + final View v; + if (convertView == null) { + v = mInflater.inflate(R.layout.order_power_widget_button_list_item, null); + } else { + v = convertView; + } + + WifiConfiguration network = (WifiConfiguration)getItem(position); + + final TextView name = (TextView) v.findViewById(R.id.name); + // wpa_suplicant returns the SSID between double quotes. Remove them if are present. + name.setText(filterSSID(network.SSID)); + + return v; + } + + private String filterSSID(String ssid) { + // Filter only if has start and end double quotes + if (ssid == null || !ssid.startsWith("\"") || !ssid.endsWith("\"")) { + return ssid; + } + return ssid.substring(1, ssid.length()-1); + } + } +} |