1. Instead of killing the activity when done, load up a URL in a browser instead.

2. More error checking:
   a. Missing SSID
   b. Incorrect network type (hey, it's just a string, and the user could get creative)

TODOs:
1. Localize the strings this is introducing.
2. Errors not handled:
   a. Tell if network not found.
   b. Incorrect SSID codes still crash the app: WIFI:S:winders;T:WPA;P:secret;badjunk;;
   c. For an open network, giving a password crashes:  WIFI:S:winders;T:nopass;P:secret;;
3. When connected, it should say, "Connected to <ssid>".



git-svn-id: https://zxing.googlecode.com/svn/trunk@1455 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
vikrama 2010-06-23 04:08:35 +00:00
parent dc9fcff5cb
commit c2e3604aea
3 changed files with 60 additions and 6 deletions

View file

@ -1,6 +1,10 @@
package com.google.zxing.client.android.wifi;
import com.google.zxing.client.android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

View file

@ -19,7 +19,13 @@ package com.google.zxing.client.android.wifi;
import java.util.Timer;
import java.util.TimerTask;
import com.google.zxing.client.android.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
/**
@ -36,6 +42,20 @@ final class Killer implements Runnable {
Killer(Activity parent) {
this.parent = parent;
}
void launchIntent(Intent intent) {
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
try {
parent.startActivity(intent);
} catch (ActivityNotFoundException e) {
AlertDialog.Builder builder = new AlertDialog.Builder(parent);
builder.setTitle(R.string.app_name);
builder.setMessage(R.string.msg_intent_failed);
builder.setPositiveButton(R.string.button_ok, null);
builder.show();
}
}
}
public void run() {
final Handler handler = new Handler();
@ -45,7 +65,10 @@ final class Killer implements Runnable {
public void run() {
handler.post(new Runnable() {
public void run() {
parent.finish();
// This will kill the parent, a bad idea.
// parent.finish();
// This will start the browser, a better idea
launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/")));
}
});
}

View file

@ -19,9 +19,12 @@ package com.google.zxing.client.android.wifi;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
@ -34,6 +37,14 @@ import com.google.zxing.client.android.R;
/**
* A new activity showing the progress of Wifi connection
*
* TODO(viki): Tell the user when the network is not available here
* TODO(viki): Incorrect password, could not connect, give an error
* TODO(viki): Should never crash: crashes on S:ssid;P:pass;T:something;else;
* TODO(viki):
* TODO(viki):
* TODO(viki):
* TODO(viki):
*
* @author Vikram Aggarwal
*/
public class WifiActivity extends Activity {
@ -45,10 +56,21 @@ public class WifiActivity extends Activity {
private ConnectedReceiver connectedReceiver;
public enum NetworkType {
NETWORK_WEP, NETWORK_WPA, NETWORK_NOPASS,
NETWORK_WEP, NETWORK_WPA, NETWORK_NOPASS, NETWORK_INVALID,
}
private int changeNetwork(NetworkSetting setting) {
// All the ways this can be wrong:
// If the SSID is empty, throw an error and return
if (setting.getSsid() == null || setting.getSsid().length() == 0) {
return doError("SSID name missing");
}
// If the network type is invalid
if (setting.getNetworkType() == NetworkType.NETWORK_INVALID){
return doError("Network type incorrect");
}
// If the password is empty, this is an unencrypted network
if (setting.getPassword() == null || setting.getPassword().length() == 0 ||
setting.getNetworkType() == null ||
@ -62,6 +84,11 @@ public class WifiActivity extends Activity {
}
}
private int doError(String string) {
statusView.setText(string);
return -1;
}
private WifiConfiguration changeNetworkCommon(NetworkSetting input){
statusView.setText("Creating settings...");
Log.d(TAG, "Adding new configuration: \nSSID: " + input.getSsid() + "\nType: " + input.getNetworkType());
@ -152,6 +179,8 @@ public class WifiActivity extends Activity {
String ssid = intent.getStringExtra(Intents.WifiConnect.SSID);
String password = intent.getStringExtra(Intents.WifiConnect.PASSWORD);
String networkType = intent.getStringExtra(Intents.WifiConnect.TYPE);
setContentView(R.layout.network);
statusView = (TextView) findViewById(R.id.networkStatus);
// TODO(vikrama): Error checking here, to ensure ssid exists.
NetworkType networkT;
@ -162,13 +191,11 @@ public class WifiActivity extends Activity {
} else if (networkType.contains("nopass")) {
networkT = NetworkType.NETWORK_NOPASS;
} else {
// Got an incorrect network type
finish();
// Got an incorrect network type. Give an error
doError("Incorrect Network type: " + networkType);
return;
}
setContentView(R.layout.network);
statusView = (TextView) findViewById(R.id.networkStatus);
// This is not available before onCreate
wifiManager = (WifiManager) this.getSystemService(WIFI_SERVICE);