Skip to content

Commit f951b74

Browse files
committed
Show ProgressDialogs when adding or removing all networks.
1 parent 531f4c0 commit f951b74

File tree

8 files changed

+139
-54
lines changed

8 files changed

+139
-54
lines changed

.idea/encodings.xml

+4-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FreifunkAutoConnectApp.iml

-19
This file was deleted.

app/app.iml

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="FreifunkAutoConnectApp" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.id=":app" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="FreifunkAutoConnect" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>
@@ -12,8 +12,9 @@
1212
<option name="SELECTED_TEST_ARTIFACT" value="_android_test_" />
1313
<option name="ASSEMBLE_TASK_NAME" value="assembleDebug" />
1414
<option name="COMPILE_JAVA_TASK_NAME" value="compileDebugSources" />
15-
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugAndroidTest" />
1615
<option name="SOURCE_GEN_TASK_NAME" value="generateDebugSources" />
16+
<option name="ASSEMBLE_TEST_TASK_NAME" value="assembleDebugAndroidTest" />
17+
<option name="COMPILE_JAVA_TEST_TASK_NAME" value="compileDebugAndroidTestSources" />
1718
<option name="TEST_SOURCE_GEN_TASK_NAME" value="generateDebugAndroidTestSources" />
1819
<option name="ALLOW_USER_CONFIGURATION" value="false" />
1920
<option name="MANIFEST_FILE_RELATIVE_PATH" value="/src/main/AndroidManifest.xml" />
@@ -69,6 +70,8 @@
6970
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
7071
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex" />
7172
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dex-cache" />
73+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/22.1.1/jars" />
74+
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/22.1.1/jars" />
7275
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
7376
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jacoco" />
7477
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaResources" />
@@ -86,9 +89,8 @@
8689
</content>
8790
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
8891
<orderEntry type="sourceFolder" forTests="false" />
89-
<orderEntry type="library" exported="" name="support-annotations-22.1.1" level="project" />
90-
<orderEntry type="library" exported="" name="support-v4-22.1.1" level="project" />
9192
<orderEntry type="library" exported="" name="appcompat-v7-22.1.1" level="project" />
93+
<orderEntry type="library" exported="" name="support-v4-22.1.1" level="project" />
94+
<orderEntry type="library" exported="" name="support-annotations-22.1.1" level="project" />
9295
</component>
93-
</module>
94-
96+
</module>

app/src/main/java/com/example/tobiastrumm/freifunkautoconnect/MainActivity.java

+119-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.example.tobiastrumm.freifunkautoconnect;
22

3+
import android.app.ProgressDialog;
34
import android.app.SearchManager;
45
import android.content.Context;
56
import android.media.MediaScannerConnection;
67
import android.net.Uri;
78
import android.net.wifi.WifiConfiguration;
89
import android.net.wifi.WifiManager;
10+
import android.os.AsyncTask;
911
import android.os.Bundle;
1012
import android.os.Environment;
1113
import android.support.v4.view.MenuItemCompat;
@@ -42,6 +44,8 @@ public class MainActivity extends ActionBarActivity implements AdapterView.OnIte
4244
private NetworkAdapter na;
4345
private WifiManager wm;
4446

47+
48+
4549
private void getSSIDs() throws IOException {
4650
InputStreamReader is = new InputStreamReader(getAssets().open("ssids.csv"));
4751
BufferedReader reader = new BufferedReader(is);
@@ -199,22 +203,63 @@ public void onClickAddAllNetworks(View view){
199203
df.show(this.getFragmentManager(),"");
200204
}
201205

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);
212232
}
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();
213257
}
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();
218263
}
219264

220265
public void onClickRemoveAllNetworks(View view){
@@ -223,9 +268,66 @@ public void onClickRemoveAllNetworks(View view){
223268
df.show(this.getFragmentManager(),"");
224269
}
225270

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+
}
226328
public void removeAllNetworks(){
227329
// 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){
229331
if(n.active){
230332
n.active = false;
231333
List<WifiConfiguration> wificonf = wm.getConfiguredNetworks();
@@ -241,7 +343,8 @@ public void removeAllNetworks(){
241343
// Save configuration
242344
wm.saveConfiguration();
243345
// Notify the NetworkAdapter that the content of ArrayList networks was changed.
244-
na.notifyDataSetChanged();
346+
na.notifyDataSetChanged();*/
347+
new RemoveAllTask().execute();
245348
}
246349

247350
private void setupUI(){

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:1.2.2'
8+
classpath 'com.android.tools.build:gradle:1.2.3'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files

0 commit comments

Comments
 (0)