Skip to content

Commit

Permalink
Android activity to add a network, and all associated code for dealing
Browse files Browse the repository at this point in the history
with WIFI: schemes on the Android side.

 Things to be done:
1. Much more testing.
2. Implement SB and test it well.
3. Clean up the UI to make it beautiful.
4. Figure out what to do when the network is connected: exit the
   Barcode Scanner?  Currently, the activity kills itself, restoring
   camera capture.
5. Publicize the WIFI: scheme.



git-svn-id: http://zxing.googlecode.com/svn/trunk/android@1425 59b500cc-1b3d-0410-9834-0bbf25fbcc57
  • Loading branch information
vikrama committed Jun 15, 2010
1 parent 23018c8 commit 586f9aa
Show file tree
Hide file tree
Showing 13 changed files with 614 additions and 6 deletions.
16 changes: 16 additions & 0 deletions AndroidManifest.xml
Expand Up @@ -95,6 +95,15 @@ versionName is 2.31, 2.4, or 3.0. -->
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".wifi.WifiActivity"
android:label="@string/wa_name"
android:screenOrientation="landscape"
android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="com.google.zxing.client.android.WIFI_CONNECT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
<activity android:name=".share.ShareActivity"
android:label="@string/share_name"
android:screenOrientation="user">
Expand Down Expand Up @@ -133,4 +142,11 @@ versionName is 2.31, 2.4, or 3.0. -->
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<!-- Wifi related -->
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>

</manifest>
14 changes: 14 additions & 0 deletions res/layout/network.xml
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- <ImageView android:id="@+id/imageStatus"-->
<!-- android:layout_width="wrap_content" android:layout_height="wrap_content"-->
<!-- android:layout_gravity="center_horizontal" android:padding="10dip"-->
<!-- android:src="@drawable/up" />-->

<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/networkStatus"
android:text="Status" android:textSize="20dip" android:padding="8dip"
android:layout_gravity="center_horizontal|bottom" />
</LinearLayout>
3 changes: 2 additions & 1 deletion res/values-de/strings.xml
Expand Up @@ -105,6 +105,7 @@
<string name="result_sms">SMS Addresse gefunden</string>
<string name="result_tel">Telefonnummer gefunden</string>
<string name="result_text">Text gefunden</string>
<string name="result_wifi">Wifi konfiguration gefunden</string>
<string name="result_uri">URL gefunden</string>
<string name="history_title">Historie</string>
<string name="history_clear_text">Historie löschen</string>
Expand All @@ -114,4 +115,4 @@
<string name="share_name">Weitergabe mittels Barcode</string>
<string name="title_about">Barcode Scanner v</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>
</resources>
3 changes: 2 additions & 1 deletion res/values-es/strings.xml
Expand Up @@ -105,6 +105,7 @@
<string name="result_sms">Dirección de SMS encontrado</string>
<string name="result_tel">Número de teléfono encontrado</string>
<string name="result_text">Texto encontrado</string>
<string name="result_wifi">Wifi configuración encontrado</string>
<string name="result_uri">URL encontrado</string>
<string name="history_title">Historia</string>
<string name="history_clear_text">Borrar historial</string>
Expand All @@ -114,4 +115,4 @@
<string name="share_name">Compartir por códigos de barras</string>
<string name="title_about">Escáner de código de barras v</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>
</resources>
8 changes: 6 additions & 2 deletions res/values/strings.xml
Expand Up @@ -44,6 +44,7 @@
<string name="button_share_contact">Contact</string>
<string name="button_show_map">Show map</string>
<string name="button_sms">Send SMS</string>
<string name="button_wifi">Connect to Network</string>
<string name="button_web_search">Web search</string>
<string name="contents_contact">Contact info</string>
<string name="contents_email">Email address</string>
Expand Down Expand Up @@ -104,14 +105,17 @@
<string name="result_product">Found product</string>
<string name="result_sms">Found SMS address</string>
<string name="result_tel">Found phone number</string>
<string name="result_text">Found plain text</string>
<string name="result_uri">Found URL</string>
<string name="result_wifi">Found Wifi Configuration</string>
<string name="result_text">Found plain text</string>
<string name="history_title">History</string>
<string name="history_clear_text">Clear history</string>
<string name="history_send">Send history</string>
<string name="history_email_title">Barcode Scanner history</string>

<string name="wa_name">Wifi Connection</string>
<string name="sbc_name">Google Book Search</string>
<string name="share_name">Share via barcode</string>
<string name="title_about">Barcode Scanner v</string>
<string name="zxing_url">http://code.google.com/p/zxing</string>
</resources>
</resources>
26 changes: 26 additions & 0 deletions src/com/google/zxing/client/android/Intents.java
Expand Up @@ -148,6 +148,32 @@ private SearchBookContents() {
}
}

public static final class WifiConnect {
/**
* Use Google Book Search to search the contents of the book provided.
*/
public static final String ACTION = "com.google.zxing.client.android.WIFI_CONNECT";

/**
* The network to connect to, all the configuration provided here.
*/
public static final String SSID = "SSID";

/**
* The network to connect to, all the configuration provided here.
*/
public static final String TYPE = "TYPE";

/**
* The network to connect to, all the configuration provided here.
*/
public static final String PASSWORD = "PASSWORD";

private WifiConnect() {
}
}


public static final class Share {
/**
* Give the user a choice of items to encode as a barcode, then render it as a QR Code and
Expand Down
14 changes: 12 additions & 2 deletions src/com/google/zxing/client/android/result/ResultHandler.java
Expand Up @@ -22,8 +22,10 @@
import com.google.zxing.client.android.PreferencesActivity;
import com.google.zxing.client.android.R;
import com.google.zxing.client.android.book.SearchBookContentsActivity;
import com.google.zxing.client.android.wifi.WifiActivity;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ParsedResultType;
import com.google.zxing.client.result.WifiParsedResult;

import android.app.Activity;
import android.app.AlertDialog;
Expand Down Expand Up @@ -321,6 +323,15 @@ final void searchBookContents(String isbn) {
launchIntent(intent);
}

final void wifiConnect(WifiParsedResult wifiResult) {
Intent intent = new Intent(Intents.WifiConnect.ACTION);
intent.setClassName(activity, WifiActivity.class.getName());
putExtra(intent, Intents.WifiConnect.SSID, wifiResult.getSsid());
putExtra(intent, Intents.WifiConnect.TYPE, wifiResult.getNetworkEncryption());
putExtra(intent, Intents.WifiConnect.PASSWORD, wifiResult.getPassword());
launchIntent(intent);
}

final void openURL(String url) {
launchIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
Expand Down Expand Up @@ -395,5 +406,4 @@ protected String parseCustomSearchURL() {
}
return customProductSearch;
}

}
}
Expand Up @@ -43,6 +43,8 @@ public static ResultHandler makeResultHandler(Activity activity, Result rawResul
return new ProductResultHandler(activity, result);
} else if (type.equals(ParsedResultType.URI)) {
return new URIResultHandler(activity, result);
} else if (type.equals(ParsedResultType.WIFI)) {
return new WifiResultHandler(activity, result);
} else if (type.equals(ParsedResultType.TEXT)) {
return new TextResultHandler(activity, result);
} else if (type.equals(ParsedResultType.GEO)) {
Expand Down
80 changes: 80 additions & 0 deletions src/com/google/zxing/client/android/result/WifiResultHandler.java
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2008 ZXing authors
*
* 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.google.zxing.client.android.result;

import android.app.Activity;

import com.google.zxing.client.android.R;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.WifiParsedResult;

/**
* Handles address book entries.
*
* @author viki@google.com (Vikram Aggarwal)
*/
public final class WifiResultHandler extends ResultHandler {
Activity parentActivity = null;
public WifiResultHandler(Activity activity, ParsedResult result) {
super(activity, result);
parentActivity = activity;
}

@Override
public int getButtonCount() {
// We just need one button, and that is to configure the wireless. This could change in the future.
return 1;
}

@Override
public int getButtonText(int index) {
switch (index) {
case 0:
return R.string.button_wifi;
default:
throw new ArrayIndexOutOfBoundsException();
}
}

@Override
public void handleButtonPress(int index) {
// Get the underlying wifi config
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
switch (index) {
case 0:
wifiConnect(wifiResult);
break;
default:
break;
}
}

// Display the name of the network and the network type to the user.
@Override
public CharSequence getDisplayContents() {
WifiParsedResult wifiResult = (WifiParsedResult) getResult();
StringBuffer contents = new StringBuffer();
ParsedResult.maybeAppend(wifiResult.getSsid(), contents);
ParsedResult.maybeAppend(wifiResult.getNetworkEncryption(), contents);
return contents.toString();
}

@Override
public int getDisplayTitle() {
return R.string.result_wifi;
}
}
52 changes: 52 additions & 0 deletions src/com/google/zxing/client/android/wifi/Killer.java
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2010 ZXing authors
*
* 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.google.zxing.client.android.wifi;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Handler;

/**
* Close the parent after a delay.
* @author Vikram Aggarwal
*
*/
class Killer implements Runnable {
// Three full seconds
final int delay_millis = 3 * 1000;
Activity parent = null;
public Killer(Activity parent) {
this.parent = parent;
}
@Override
public void run() {
final Handler handler = new Handler();
Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
parent.finish();
}
});
}
}, delay_millis);
}
}
66 changes: 66 additions & 0 deletions src/com/google/zxing/client/android/wifi/NetworkSetting.java
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2010 ZXing authors
*
* 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.google.zxing.client.android.wifi;

import java.util.Vector;

import com.google.zxing.client.android.wifi.WifiActivity.NetworkType;

/**
* Everything we could get from the barcode is to be here
* @author Vikram Aggarwal
*
*/
class NetworkSetting {
// The ancillary network setting from the barcode
private NetworkType mNetworkType;
// The password this ssid has
private String mPassword;
// The ssid we read from the barcode
private String mSsid;

static String[] toStringArray(Vector<String> strings) {
int size = strings.size();
String[] result = new String[size];
for (int j = 0; j < size; j++) {
result[j] = (String) strings.elementAt(j);
}
return result;
}
/**
* Create a new NetworkSetting object.
* @param ssid: The SSID
* @param password: Password for the setting, blank if unsecured network
* @param networkType: WPA for WPA/WPA2, or WEP for WEP or unsecured
*/
public NetworkSetting(String ssid, String password, NetworkType networkType){
mSsid = ssid;
mPassword = password;
mNetworkType = networkType;
}

public NetworkType getNetworkType() {
return mNetworkType;
}
public String getPassword() {
return mPassword;
}

public String getSsid() {
return mSsid;
}
}

0 comments on commit 586f9aa

Please sign in to comment.