Permalink
Browse files

Dump Wi-Fi state on the controller screen for convenience.

During testing, it is helpful to see the IP address of nodes that are
being set-up (and to confirm that Wi-Fi is on).  Eventually this should
move beyond convenience and we should ensure that we only bind to the
Wi-Fi interface by default.
  • Loading branch information...
jasta committed Dec 12, 2011
1 parent c228a88 commit 97bceeaa6354d7af1b2f9cc387550cd8644828ee
Showing with 70 additions and 2 deletions.
  1. +4 −0 AndroidManifest.xml
  2. +9 −0 res/layout/controller.xml
  3. +4 −2 res/values/strings.xml
  4. +53 −0 src/org/devtcg/rojocam/ControllerActivity.java
View
@@ -10,6 +10,10 @@
<!-- This permission is needed to talk to the local network via WiFi. -->
<uses-permission android:name="android.permission.INTERNET" />
+ <!-- Necessary to provide the user's Wi-Fi IP address (useful for debugging
+ during initial set-up) -->
+ <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
+
<!-- Required only for debugging at the moment... -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
@@ -25,7 +25,16 @@
android:id="@+id/settings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
+ android:layout_marginBottom="20dp"
android:text="@string/settings"
/>
+ <TextView
+ android:id="@+id/wifi_state"
+ android:layout_width="fill_parent"
+ android:layout_height="wrap_content"
+ android:textAppearance="?android:attr/textAppearanceMedium"
+ android:gravity="center_horizontal"
+ />
+
</LinearLayout>
View
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
-<resources>
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="app_name">Rojocam</string>
<string name="controller_title">Rojocam Console</string>
<string name="started_successfully">Server started successfully!</string>
- <string name="error_starting">Error starting server: %s</string>
+ <string name="error_starting">Error starting server: <xliff:g id="error_text">%s</xliff:g></string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="settings">Settings</string>
@@ -19,4 +19,6 @@
<string name="settings_title">Settings</string>
<string name="no_password_set">No password set!</string>
<string name="password_protected">Stream is password protected.</string>
+ <string name="wifi_state_connected">Wi-Fi connected, <xliff:g id="ip_addr">%s</xliff:g></string>
+ <string name="wifi_state_disconnected">Wi-Fi not connected!</string>
</resources>
@@ -4,28 +4,43 @@
import org.devtcg.rojocam.util.DetachableResultReceiver.Receiver;
import android.app.Activity;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.wifi.WifiInfo;
+import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
+import android.widget.TextView;
import android.widget.Toast;
public class ControllerActivity extends Activity implements OnClickListener {
private static final int COMMAND_NONE = 0;
private static final int COMMAND_START = 1;
private static final int COMMAND_STOP = 2;
+ private WifiManager mWifiMgr;
+
private int mPendingCommand;
private Button mStart;
private Button mStop;
+ private TextView mWifiState;
+
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.controller);
+ mWifiMgr = (WifiManager)getSystemService(WIFI_SERVICE);
+
+ mWifiState = (TextView)findViewById(R.id.wifi_state);
+
mStart = (Button)findViewById(R.id.start);
mStop = (Button)findViewById(R.id.stop);
findViewById(R.id.settings).setOnClickListener(this);
@@ -39,14 +54,52 @@ public void onCreate(Bundle savedInstanceState) {
protected void onResume() {
super.onResume();
sStaticReceiver.setReceiver(mReceiver);
+
+ IntentFilter filter = new IntentFilter();
+ filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
+ registerReceiver(mWifiEventReceiver, filter);
+
+ updateWifiState();
}
@Override
protected void onPause() {
super.onStop();
sStaticReceiver.clearReceiver();
+
+ unregisterReceiver(mWifiEventReceiver);
+ }
+
+ private static String formatAddr(int ipAddr) {
+ StringBuilder ipStr = new StringBuilder();
+ ipStr.append(ipAddr & 0xff);
+ for (int i = 8; i <= 24; i += 8) {
+ ipStr.append('.');
+ ipStr.append((ipAddr >> i) & 0xff);
+ }
+ return ipStr.toString();
}
+ private void updateWifiState() {
+ WifiInfo wifiInfo = mWifiMgr.getConnectionInfo();
+ if (wifiInfo != null) {
+ String ip = formatAddr(wifiInfo.getIpAddress());
+ mWifiState.setText(getString(R.string.wifi_state_connected, ip));
+ } else {
+ mWifiState.setText(getString(R.string.wifi_state_disconnected));
+ }
+ }
+
+ private final BroadcastReceiver mWifiEventReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
+ updateWifiState();
+ }
+ }
+ };
+
private void adjustButtonState() {
boolean isActive = CamcorderNodeService.isActive();
mStart.setEnabled(!isActive);

0 comments on commit 97bceea

Please sign in to comment.