Merge pull request #11 from brarcher/enter-address

Allow parent to enter child's address and port to connect
This commit is contained in:
brarcher
2016-01-01 17:01:37 -05:00
9 changed files with 331 additions and 24 deletions

View File

@@ -9,6 +9,7 @@
android:targetSdkVersion="17" />
<uses-permission android:required="true" android:name="android.permission.INTERNET"/>
<uses-permission android:required="true" android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:required="true" android:name="android.permission.ACCESS_WIFI_STATE"/>
<application
android:allowBackup="true"
@@ -35,6 +36,7 @@
android:name="protect.babymonitor.DiscoverActivity"
android:label="@string/app_name"
android:configChanges="orientation|screenSize"
android:windowSoftInputMode="adjustPan"
android:parentActivityName="protect.babymonitor.StartActivity"
/>
<activity

3
NEWS
View File

@@ -3,6 +3,9 @@ In development:
* Improve and simplify audio playback to reduce dropped
audio samples
* A parent device may pair with a child device by entering
the child's IP address and port.
Mon Dec 28, 2015: Release 0.1

View File

@@ -22,20 +22,38 @@
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/selectChildDeviceTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selectChildDevice"
android:textSize="20sp" />
<ListView
android:id="@+id/ServiceTable"
android:scrollbars="vertical"
<Button
android:id="@+id/discoverChildButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.32"
android:padding="10dp" >
</ListView>
android:layout_height="wrap_content"
android:text="@string/discoverChild"
android:textSize="17sp" />
<TextView
android:id="@+id/discoverChildDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/discoverChildDescription"
android:textSize="14sp" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<Button
android:id="@+id/enterChildAddressButton"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/enterChildAddress"
android:textSize="17sp" />
<TextView
android:id="@+id/enterChildAddressDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/enterChildAddressDescription"
android:textSize="14sp" />
</LinearLayout>

View File

@@ -0,0 +1,83 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="protect.babymonitor.MonitorActivity" >
<TextView
android:id="@+id/parentDeviceTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/parentDevice"
android:textSize="25sp" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/enterAddressInstructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enterAddressInstructions" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/ipAddressTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/addressTitle" />
<EditText
android:id="@+id/ipAddressField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/exampleAddress"
android:inputType="textUri"
android:textSize="20sp"
android:nextFocusForward="@+id/portField" >
</EditText>
<TextView
android:id="@+id/portTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="@string/portTitle" />
<EditText
android:id="@+id/portField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:ems="10"
android:inputType="number"
android:maxLength="5"
android:hint="@string/examplePort"
android:nextFocusForward="@+id/connectViaAddressButton"/>
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<Button
android:id="@+id/connectViaAddressButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/connect" />
</LinearLayout>

View File

@@ -0,0 +1,41 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="protect.babymonitor.MonitorActivity" >
<TextView
android:id="@+id/parentDeviceTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/parentDevice"
android:textSize="25sp" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/selectChildDeviceTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/selectChildDevice"
android:textSize="20sp" />
<ListView
android:id="@+id/ServiceTable"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.32"
android:padding="10dp" >
</ListView>
</LinearLayout>

View File

@@ -47,6 +47,44 @@
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/addressTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addressTitle"
android:textSize="20sp" />
<TextView
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"/>
<TextView
android:id="@+id/portTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/portTitle"
android:textSize="20sp" />
<TextView
android:id="@+id/port"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/loading"
android:textSize="20sp"/>
<TextView
android:id="@+id/addressDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addressDescription" />
<Space
android:layout_width="match_parent"
android:layout_height="15dip" />
<TextView
android:id="@+id/textStatus"
android:layout_width="wrap_content"

View File

@@ -20,5 +20,18 @@
<string name="connectedTo">Connected To:</string>
<string name="disconnected">Disconnected</string>
<string name="listening">Listening...</string>
<string name="addressTitle">Address:</string>
<string name="portTitle">Port:</string>
<string name="addressDescription">Address and port that parent must pair to</string>
<string name="wifiNotConnected">Not connected to a Wi-Fi network</string>
<string name="enterAddressInstructions">Enter the IP Address and port of a child and click Connect</string>
<string name="connect">Connect</string>
<string name="exampleAddress">192.168.1.2</string>
<string name="examplePort">10000</string>
<string name="invalidPort">Either no port was entered or it is invalid</string>
<string name="invalidAddress">The address field needs to be filled out</string>
<string name="discoverChild">Discover Child on Network</string>
<string name="discoverChildDescription">Select child from a list of discovered children on the network</string>
<string name="enterChildAddress">Select Child by Address</string>
<string name="enterChildAddressDescription">Enter the address and port of the child</string>
</resources>

View File

@@ -27,7 +27,10 @@ import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class DiscoverActivity extends Activity
{
@@ -47,9 +50,74 @@ public class DiscoverActivity extends Activity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discover);
final Button discoverChildButton = (Button) findViewById(R.id.discoverChildButton);
discoverChildButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
loadDiscoveryViaMdns();
}
});
final Button enterChildAddressButton = (Button) findViewById(R.id.enterChildAddressButton);
enterChildAddressButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
loadDiscoveryViaAddress();
}
});
}
private void loadDiscoveryViaMdns()
{
setContentView(R.layout.activity_discover_mdns);
startServiceDiscovery("_babymonitor._tcp.");
}
private void loadDiscoveryViaAddress()
{
setContentView(R.layout.activity_discover_address);
final Button connectButton = (Button) findViewById(R.id.connectViaAddressButton);
connectButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Log.i(TAG, "Connecting to child device via address");
final EditText addressField = (EditText) findViewById(R.id.ipAddressField);
final EditText portField = (EditText) findViewById(R.id.portField);
final String addressString = addressField.getText().toString();
final String portString = portField.getText().toString();
if(addressString.length() == 0)
{
Toast.makeText(DiscoverActivity.this, R.string.invalidAddress, Toast.LENGTH_LONG).show();
return;
}
int port = 0;
try
{
port = Integer.parseInt(portString);
}
catch(NumberFormatException e)
{
Toast.makeText(DiscoverActivity.this, R.string.invalidPort, Toast.LENGTH_LONG).show();
return;
}
connectToChild(addressString, port, addressString);
}
});
}
@Override
protected void onDestroy()
{
@@ -83,13 +151,7 @@ public class DiscoverActivity extends Activity
int position, long id)
{
final ServiceInfoWrapper info = (ServiceInfoWrapper) parent.getItemAtPosition(position);
final Intent i = new Intent(getApplicationContext(), ListenActivity.class);
final Bundle b = new Bundle();
b.putString("address", info.getAddress());
b.putInt("port", info.getPort());
b.putString("name", info.getName());
i.putExtras(b);
startActivity(i);
connectToChild(info.getAddress(), info.getPort(), info.getName());
}
});
@@ -181,6 +243,24 @@ public class DiscoverActivity extends Activity
nsdManager.discoverServices(
serviceType, NsdManager.PROTOCOL_DNS_SD, _discoveryListener);
}
/**
* Launch the ListenActivity to connect to the given child device
*
* @param address
* @param port
* @param name
*/
private void connectToChild(final String address, final int port, final String name)
{
final Intent i = new Intent(getApplicationContext(), ListenActivity.class);
final Bundle b = new Bundle();
b.putString("address", address);
b.putInt("port", port);
b.putString("name", name);
i.putExtras(b);
startActivity(i);
}
}
class ServiceInfoWrapper

View File

@@ -28,7 +28,10 @@ import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
@@ -163,6 +166,29 @@ public class MonitorActivity extends Activity
}
});
_serviceThread.start();
MonitorActivity.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
final TextView addressText = (TextView) findViewById(R.id.address);
final WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
final WifiInfo info = wifiManager.getConnectionInfo();
final int address = info.getIpAddress();
if(address != 0)
{
@SuppressWarnings("deprecation")
final String ipAddress = Formatter.formatIpAddress(address);
addressText.setText(ipAddress);
}
else
{
addressText.setText(R.string.wifiNotConnected);
}
}
});
}
@Override
@@ -212,11 +238,11 @@ public class MonitorActivity extends Activity
_registrationListener = new NsdManager.RegistrationListener()
{
@Override
public void onServiceRegistered(NsdServiceInfo NsdServiceInfo) {
public void onServiceRegistered(NsdServiceInfo nsdServiceInfo) {
// Save the service name. Android may have changed it in order to
// resolve a conflict, so update the name you initially requested
// with the name Android actually used.
final String serviceName = NsdServiceInfo.getServiceName();
final String serviceName = nsdServiceInfo.getServiceName();
Log.i(TAG, "Service name: " + serviceName);
@@ -229,6 +255,9 @@ public class MonitorActivity extends Activity
final TextView serviceText = (TextView) findViewById(R.id.textService);
serviceText.setText(serviceName);
final TextView portText = (TextView) findViewById(R.id.port);
portText.setText(Integer.toString(port));
}
});
}