1
1
package com .example .tobiastrumm .freifunkautoconnect ;
2
2
3
+ import android .app .ProgressDialog ;
3
4
import android .app .SearchManager ;
4
5
import android .content .Context ;
5
6
import android .media .MediaScannerConnection ;
6
7
import android .net .Uri ;
7
8
import android .net .wifi .WifiConfiguration ;
8
9
import android .net .wifi .WifiManager ;
10
+ import android .os .AsyncTask ;
9
11
import android .os .Bundle ;
10
12
import android .os .Environment ;
11
13
import android .support .v4 .view .MenuItemCompat ;
@@ -42,6 +44,8 @@ public class MainActivity extends ActionBarActivity implements AdapterView.OnIte
42
44
private NetworkAdapter na ;
43
45
private WifiManager wm ;
44
46
47
+
48
+
45
49
private void getSSIDs () throws IOException {
46
50
InputStreamReader is = new InputStreamReader (getAssets ().open ("ssids.csv" ));
47
51
BufferedReader reader = new BufferedReader (is );
@@ -199,22 +203,63 @@ public void onClickAddAllNetworks(View view){
199
203
df .show (this .getFragmentManager (),"" );
200
204
}
201
205
202
- public void addAllNetworks (){
203
- for (Network n : networks ){
204
- if (!n .active ){
205
- n .active = true ;
206
- // Create WifiConfiguration and add it to the known networks.
207
- WifiConfiguration wc = new WifiConfiguration ();
208
- wc .SSID = n .ssid ;
209
- wc .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
210
- int networkId = wm .addNetwork (wc );
211
- wm .enableNetwork (networkId , false );
206
+ private class AddAllTask extends AsyncTask <Void , Integer , Void > {
207
+ ProgressDialog progress ;
208
+ @ Override
209
+ protected void onProgressUpdate (Integer ... values ) {
210
+ super .onProgressUpdate (values );
211
+ // Update progressbar
212
+ progress .setProgress (values [0 ]);
213
+ }
214
+
215
+ @ Override
216
+ protected Void doInBackground (Void ... params ) {
217
+ // Add all networks to network configuration
218
+ int i = 0 ;
219
+ WifiManager wmAsync = (WifiManager ) getSystemService (Context .WIFI_SERVICE );
220
+ for (Network n : networks ){
221
+ if (!n .active ){
222
+ n .active = true ;
223
+ // Create WifiConfiguration and add it to the known networks.
224
+ WifiConfiguration wc = new WifiConfiguration ();
225
+ wc .SSID = n .ssid ;
226
+ wc .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
227
+ int networkId = wmAsync .addNetwork (wc );
228
+ wmAsync .enableNetwork (networkId , false );
229
+ }
230
+ i ++;
231
+ publishProgress (i );
212
232
}
233
+ // Save configuration
234
+ wmAsync .saveConfiguration ();
235
+ return null ;
236
+ }
237
+
238
+ @ Override
239
+ protected void onPreExecute () {
240
+ super .onPreExecute ();
241
+ // Create ProgressDialog and show it
242
+ progress = new ProgressDialog (MainActivity .this );
243
+ progress .setProgressStyle (ProgressDialog .STYLE_HORIZONTAL );
244
+ progress .setIndeterminate (false );
245
+ progress .setMax (networks .size ());
246
+ progress .setCancelable (false );
247
+ progress .show ();
248
+ }
249
+
250
+ @ Override
251
+ protected void onPostExecute (Void aVoid ) {
252
+ super .onPostExecute (aVoid );
253
+ // Notify the NetworkAdapter that the content of ArrayList networks was changed.
254
+ na .notifyDataSetChanged ();
255
+ // Close ProgressDialog
256
+ progress .cancel ();
213
257
}
214
- // Save configuration
215
- wm .saveConfiguration ();
216
- // Notify the NetworkAdapter that the content of ArrayList networks was changed.
217
- na .notifyDataSetChanged ();
258
+ }
259
+
260
+ public void addAllNetworks (){
261
+ // Start AsyncTask to add all networks.
262
+ new AddAllTask ().execute ();
218
263
}
219
264
220
265
public void onClickRemoveAllNetworks (View view ){
@@ -223,9 +268,66 @@ public void onClickRemoveAllNetworks(View view){
223
268
df .show (this .getFragmentManager (),"" );
224
269
}
225
270
271
+ private class RemoveAllTask extends AsyncTask <Void , Integer , Void > {
272
+ ProgressDialog progress ;
273
+
274
+ @ Override
275
+ protected void onProgressUpdate (Integer ... values ) {
276
+ super .onProgressUpdate (values );
277
+ // Update progressbar
278
+ progress .setProgress (values [0 ]);
279
+ }
280
+
281
+ @ Override
282
+ protected Void doInBackground (Void ... params ) {
283
+ // Remove all networks from network configuration
284
+ // WARNING: This could cause some performance issues depending of the number of networks and saved networks.
285
+ int i = 0 ;
286
+ WifiManager wmAsync = (WifiManager ) getSystemService (Context .WIFI_SERVICE );
287
+ List <WifiConfiguration > wificonf = wmAsync .getConfiguredNetworks ();
288
+ for (Network n : networks ){
289
+ if (n .active ){
290
+ n .active = false ;
291
+ if (wificonf != null ) {
292
+ for (WifiConfiguration wc : wificonf ) {
293
+ if (wc .SSID .equals (n .ssid )) {
294
+ wmAsync .removeNetwork (wc .networkId );
295
+ }
296
+ }
297
+ }
298
+ }
299
+ i ++;
300
+ publishProgress (i );
301
+ }
302
+ // Save configuration
303
+ wmAsync .saveConfiguration ();
304
+ return null ;
305
+ }
306
+
307
+ @ Override
308
+ protected void onPreExecute () {
309
+ super .onPreExecute ();
310
+ // Create ProgressDialog and show it
311
+ progress = new ProgressDialog (MainActivity .this );
312
+ progress .setProgressStyle (ProgressDialog .STYLE_HORIZONTAL );
313
+ progress .setIndeterminate (false );
314
+ progress .setMax (networks .size ());
315
+ progress .setCancelable (false );
316
+ progress .show ();
317
+ }
318
+
319
+ @ Override
320
+ protected void onPostExecute (Void aVoid ) {
321
+ super .onPostExecute (aVoid );
322
+ // Notify the NetworkAdapter that the content of ArrayList networks was changed.
323
+ na .notifyDataSetChanged ();
324
+ // Close ProgressDialog
325
+ progress .cancel ();
326
+ }
327
+ }
226
328
public void removeAllNetworks (){
227
329
// WARNING: This could cause some performance issues depending of the number of networks and saved networks.
228
- for (Network n : networks ){
330
+ /* for(Network n: networks){
229
331
if(n.active){
230
332
n.active = false;
231
333
List<WifiConfiguration> wificonf = wm.getConfiguredNetworks();
@@ -241,7 +343,8 @@ public void removeAllNetworks(){
241
343
// Save configuration
242
344
wm.saveConfiguration();
243
345
// Notify the NetworkAdapter that the content of ArrayList networks was changed.
244
- na .notifyDataSetChanged ();
346
+ na.notifyDataSetChanged();*/
347
+ new RemoveAllTask ().execute ();
245
348
}
246
349
247
350
private void setupUI (){
0 commit comments