1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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);
}
}
}
|