Permalink
Browse files

activity instead of dialog for WiFi turning on

  • Loading branch information...
1 parent eabaecf commit f161a31831181134b3183793ff1f44e780e79da9 @geometer geometer committed Dec 24, 2011
View
7 assets/resources/application/en.xml
@@ -123,8 +123,8 @@
<node name="localCatalogs" value="Local OPDS catalogs"/>
<node name="noCatalogsFound" value="No catalogs found, sorry"/>
<node name="noLocalConnection" value="Your device is not connected to a local network"/>
- <node name="wifiDisabled" value="Wifi is disabled"/>
- <node name="enableWifi" value="Enable wifi?"/>
+ <node name="wifiIsTurnedOff" value="Wifi is turned off"/>
+ <node name="turnWiFiOn" value="Please, turn WiFi on and try again"/>
</node>
<node name="menu">
<node name="networkSearch" value="Network search"/>
@@ -269,6 +269,7 @@
<node name="refresh" value="Refresh"/>
<node name="authorize" value="Authorize"/>
<node name="rescan" value="Rescan" />
+ <node name="turnOn" value="Turn on" />
</node>
<node name="plugin">
<node name="installTitle" value="Install plugin"/>
@@ -661,7 +662,7 @@
<node name="updatingCatalogsList" value="Updating catalogs list. Please, wait&#8230;"/>
<node name="updatingAccountInformation" value="Updating account information. Please, wait&#8230;"/>
<node name="scanningLocalNetwork" value="Scanning local network. Please, wait&#8230;"/>
- <node name="enablingWifi" value="Enabling wifi. Please, wait&#8230;"/>
+ <node name="turningWiFiOn" value="Turning WiFi on. Please, wait&#8230;"/>
</node>
<node name="networkError">
<node name="internalError" value="Internal server error"/>
View
1 res/layout/scan_local_network.xml
@@ -21,7 +21,6 @@
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
- android:textColor="#ff0000"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
View
109 src/org/geometerplus/android/fbreader/network/ScanLocalNetworkActivity.java
@@ -22,18 +22,18 @@
import java.util.*;
import java.net.*;
+import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
+
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
+import android.graphics.Color;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
-import android.app.AlertDialog;
-import android.app.AlertDialog.Builder;
-import android.content.DialogInterface.OnClickListener;
-import android.content.DialogInterface;
import javax.jmdns.*;
@@ -44,15 +44,14 @@
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.android.util.UIUtil;
-
+
public class ScanLocalNetworkActivity extends ListActivity {
private final static String[] ourServiceTypes = { "_stanza._tcp.local." };
private final ZLResource myResource =
NetworkLibrary.Instance().resource().getResource("addCatalog");
private WifiManager.MulticastLock myLock;
- private WifiManager myWifi;
@Override
public void onCreate(Bundle icicle) {
@@ -66,14 +65,6 @@ public void onCreate(Bundle icicle) {
final View buttonView = findViewById(R.id.scan_local_network_buttons);
final ZLResource buttonResource = ZLResource.resource("dialog").getResource("button");
- final Button rescanButton = (Button)buttonView.findViewById(R.id.ok_button);
- rescanButton.setText(buttonResource.getResource("rescan").getValue());
- rescanButton.setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- scan();
- }
- });
-
final Button cancelButton = (Button)buttonView.findViewById(R.id.cancel_button);
cancelButton.setText(buttonResource.getResource("cancel").getValue());
cancelButton.setOnClickListener(new View.OnClickListener() {
@@ -82,12 +73,41 @@ public void onClick(View view) {
}
});
- myWifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
- myLock = myWifi.createMulticastLock("FBReader_lock");
- myLock.setReferenceCounted(true);
- myLock.acquire();
+ final WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
+ final int state = wifiManager.getWifiState();
+ if (state != WifiManager.WIFI_STATE_ENABLED && state != WifiManager.WIFI_STATE_ENABLING) {
+ setTitle(myResource.getResource("wifiIsTurnedOff").getValue());
+ final View listView = findViewById(android.R.id.list);
+ final TextView errorView = (TextView)findViewById(R.id.scan_local_network_error);
+ listView.setVisibility(View.GONE);
+ errorView.setVisibility(View.VISIBLE);
+ errorView.setText(myResource.getResource("turnWiFiOn").getValue());
+
+ final Button turnOnButton = (Button)buttonView.findViewById(R.id.ok_button);
+ turnOnButton.setText(buttonResource.getResource("turnOn").getValue());
+ turnOnButton.setOnClickListener(new View.OnClickListener() {
+ public void onClick(View view) {
+ wifiManager.setWifiEnabled(true);
+ finish();
+ }
+ });
+
+ myLock = null;
+ } else {
+ final Button rescanButton = (Button)buttonView.findViewById(R.id.ok_button);
+ rescanButton.setText(buttonResource.getResource("rescan").getValue());
+ rescanButton.setOnClickListener(new View.OnClickListener() {
+ public void onClick(View view) {
+ scan();
+ }
+ });
+
+ myLock = wifiManager.createMulticastLock("FBReader_lock");
+ myLock.setReferenceCounted(true);
+ myLock.acquire();
- scan();
+ scan();
+ }
}
@Override
@@ -100,10 +120,19 @@ protected void onDestroy() {
private List<InetAddress> getLocalIpAddresses() {
final List<InetAddress> addresses = new LinkedList<InetAddress>();
+ Method testPtoPMethod = null;
+ try {
+ testPtoPMethod = NetworkInterface.class.getMethod("isPointToPoint");
+ } catch (NoSuchMethodException e) {
+ }
try {
for (NetworkInterface iface : Collections.list(NetworkInterface.getNetworkInterfaces())) {
- if (iface.isPointToPoint()) {
- continue;
+ try {
+ if (testPtoPMethod != null && (Boolean)testPtoPMethod.invoke(iface)) {
+ continue;
+ }
+ } catch (IllegalAccessException e) {
+ } catch (InvocationTargetException e) {
}
for (InetAddress addr : Collections.list(iface.getInetAddresses())) {
if (!addr.isLoopbackAddress() && addr instanceof Inet4Address) {
@@ -118,9 +147,6 @@ protected void onDestroy() {
}
private void scan() {
- if (myWifi.getWifiState() != WifiManager.WIFI_STATE_ENABLED && myWifi.getWifiState() != WifiManager.WIFI_STATE_ENABLING) {
- showWifiDialog();
- }
final Runnable scanRunnable = new Runnable() {
public void run() {
final ArrayList<ServiceInfoItem> services = new ArrayList<ServiceInfoItem>();
@@ -154,36 +180,8 @@ public void run() {
UIUtil.wait("scanningLocalNetwork", scanRunnable, this);
}
- private void showWifiDialog() {
- AlertDialog alertDialog = new AlertDialog.Builder(this).create();
- alertDialog.setTitle(myResource.getResource("wifiDisabled").getValue());
- alertDialog.setMessage(myResource.getResource("enableWifi").getValue());
- final ZLResource buttonResource = ZLResource.resource("dialog").getResource("button");
- alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, buttonResource.getResource("ok").getValue(), new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- final Runnable wifiRunnable = new Runnable() {
- public void run() {
- if (!myWifi.setWifiEnabled(true)) {
- finish();
- }
- while(!myWifi.isWifiEnabled()) {
- }
- }
- };
- UIUtil.wait("enablingWifi", wifiRunnable, ScanLocalNetworkActivity.this);
- scan();
- }
- });
- alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, buttonResource.getResource("cancel").getValue(), new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- finish();
- }
- });
- alertDialog.show();
- }
-
-
private void setup(final ArrayList<ServiceInfoItem> services, final String errorText) {
+ System.err.println("setup " + services.size() + " " + errorText);
runOnUiThread(new Runnable() {
public void run() {
setListAdapter(new ArrayAdapter<ServiceInfoItem>(
@@ -192,11 +190,12 @@ public void run() {
services
));
final View listView = findViewById(android.R.id.list);
- final View errorView = findViewById(R.id.scan_local_network_error);
+ final TextView errorView = (TextView)findViewById(R.id.scan_local_network_error);
if (errorText != null) {
listView.setVisibility(View.GONE);
errorView.setVisibility(View.VISIBLE);
- ((TextView)errorView).setText(errorText);
+ errorView.setText(errorText);
+ errorView.setTextColor(Color.RED);
} else {
listView.setVisibility(View.VISIBLE);
errorView.setVisibility(View.GONE);

0 comments on commit f161a31

Please sign in to comment.