Fix another crash and some reformatting
This commit is contained in:
@@ -35,8 +35,7 @@ import android.widget.ListView;
|
|||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
|
||||||
public class DiscoverActivity extends Activity
|
public class DiscoverActivity extends Activity {
|
||||||
{
|
|
||||||
private static final String PREF_KEY_CHILD_DEVICE_ADDRESS = "childDeviceAddress";
|
private static final String PREF_KEY_CHILD_DEVICE_ADDRESS = "childDeviceAddress";
|
||||||
private static final String PREF_KEY_CHILD_DEVICE_PORT = "childDevicePort";
|
private static final String PREF_KEY_CHILD_DEVICE_PORT = "childDevicePort";
|
||||||
final String TAG = "ChildMonitor";
|
final String TAG = "ChildMonitor";
|
||||||
@@ -46,8 +45,7 @@ public class DiscoverActivity extends Activity
|
|||||||
NsdManager.DiscoveryListener _discoveryListener;
|
NsdManager.DiscoveryListener _discoveryListener;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
{
|
|
||||||
Log.i(TAG, "ChildMonitor start");
|
Log.i(TAG, "ChildMonitor start");
|
||||||
|
|
||||||
_nsdManager = (NsdManager)this.getSystemService(Context.NSD_SERVICE);
|
_nsdManager = (NsdManager)this.getSystemService(Context.NSD_SERVICE);
|
||||||
@@ -56,34 +54,18 @@ public class DiscoverActivity extends Activity
|
|||||||
setContentView(R.layout.activity_discover);
|
setContentView(R.layout.activity_discover);
|
||||||
|
|
||||||
final Button discoverChildButton = (Button) findViewById(R.id.discoverChildButton);
|
final Button discoverChildButton = (Button) findViewById(R.id.discoverChildButton);
|
||||||
discoverChildButton.setOnClickListener(new View.OnClickListener()
|
discoverChildButton.setOnClickListener(v -> loadDiscoveryViaMdns());
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void onClick(View v)
|
|
||||||
{
|
|
||||||
loadDiscoveryViaMdns();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
final Button enterChildAddressButton = (Button) findViewById(R.id.enterChildAddressButton);
|
final Button enterChildAddressButton = (Button) findViewById(R.id.enterChildAddressButton);
|
||||||
enterChildAddressButton.setOnClickListener(new View.OnClickListener()
|
enterChildAddressButton.setOnClickListener(v -> loadDiscoveryViaAddress());
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void onClick(View v)
|
|
||||||
{
|
|
||||||
loadDiscoveryViaAddress();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadDiscoveryViaMdns()
|
private void loadDiscoveryViaMdns() {
|
||||||
{
|
|
||||||
setContentView(R.layout.activity_discover_mdns);
|
setContentView(R.layout.activity_discover_mdns);
|
||||||
startServiceDiscovery("_childmonitor._tcp.");
|
startServiceDiscovery("_childmonitor._tcp.");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadDiscoveryViaAddress()
|
private void loadDiscoveryViaAddress() {
|
||||||
{
|
|
||||||
setContentView(R.layout.activity_discover_address);
|
setContentView(R.layout.activity_discover_address);
|
||||||
|
|
||||||
final Button connectButton = (Button) findViewById(R.id.connectViaAddressButton);
|
final Button connectButton = (Button) findViewById(R.id.connectViaAddressButton);
|
||||||
@@ -98,48 +80,41 @@ public class DiscoverActivity extends Activity
|
|||||||
portField.setText(String.valueOf(preferredPort));
|
portField.setText(String.valueOf(preferredPort));
|
||||||
}
|
}
|
||||||
|
|
||||||
connectButton.setOnClickListener(new View.OnClickListener()
|
connectButton.setOnClickListener(v -> {
|
||||||
{
|
Log.i(TAG, "Connecting to child device via address");
|
||||||
@Override
|
final String addressString = addressField.getText().toString();
|
||||||
public void onClick(View v)
|
final String portString = portField.getText().toString();
|
||||||
|
|
||||||
|
if(addressString.length() == 0)
|
||||||
{
|
{
|
||||||
Log.i(TAG, "Connecting to child device via address");
|
Toast.makeText(DiscoverActivity.this, R.string.invalidAddress, Toast.LENGTH_LONG).show();
|
||||||
final String addressString = addressField.getText().toString();
|
return;
|
||||||
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;
|
|
||||||
}
|
|
||||||
SharedPreferences.Editor preferencesEditor = getPreferences(MODE_PRIVATE).edit();
|
|
||||||
preferencesEditor.putString(PREF_KEY_CHILD_DEVICE_ADDRESS, addressString);
|
|
||||||
preferencesEditor.putInt(PREF_KEY_CHILD_DEVICE_PORT, port);
|
|
||||||
preferencesEditor.apply();
|
|
||||||
connectToChild(addressString, port, addressString);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int port = 0;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
port = Integer.parseInt(portString);
|
||||||
|
}
|
||||||
|
catch(NumberFormatException e)
|
||||||
|
{
|
||||||
|
Toast.makeText(DiscoverActivity.this, R.string.invalidPort, Toast.LENGTH_LONG).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SharedPreferences.Editor preferencesEditor = getPreferences(MODE_PRIVATE).edit();
|
||||||
|
preferencesEditor.putString(PREF_KEY_CHILD_DEVICE_ADDRESS, addressString);
|
||||||
|
preferencesEditor.putInt(PREF_KEY_CHILD_DEVICE_PORT, port);
|
||||||
|
preferencesEditor.apply();
|
||||||
|
connectToChild(addressString, port, addressString);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy()
|
protected void onDestroy() {
|
||||||
{
|
|
||||||
Log.i(TAG, "ChildMonitoring stop");
|
Log.i(TAG, "ChildMonitoring stop");
|
||||||
|
|
||||||
if(_discoveryListener != null)
|
if(_discoveryListener != null) {
|
||||||
{
|
|
||||||
Log.i(TAG, "Unregistering monitoring service");
|
Log.i(TAG, "Unregistering monitoring service");
|
||||||
|
|
||||||
_nsdManager.stopServiceDiscovery(_discoveryListener);
|
_nsdManager.stopServiceDiscovery(_discoveryListener);
|
||||||
@@ -149,8 +124,7 @@ public class DiscoverActivity extends Activity
|
|||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void startServiceDiscovery(final String serviceType)
|
public void startServiceDiscovery(final String serviceType) {
|
||||||
{
|
|
||||||
final NsdManager nsdManager = (NsdManager)this.getSystemService(Context.NSD_SERVICE);
|
final NsdManager nsdManager = (NsdManager)this.getSystemService(Context.NSD_SERVICE);
|
||||||
if (nsdManager == null) {
|
if (nsdManager == null) {
|
||||||
Log.e(TAG, "Could not obtain nsdManager");
|
Log.e(TAG, "Could not obtain nsdManager");
|
||||||
@@ -163,17 +137,15 @@ public class DiscoverActivity extends Activity
|
|||||||
final WifiManager.MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
|
final WifiManager.MulticastLock multicastLock = wifi.createMulticastLock("multicastLock");
|
||||||
multicastLock.setReferenceCounted(true);
|
multicastLock.setReferenceCounted(true);
|
||||||
multicastLock.acquire();
|
multicastLock.acquire();
|
||||||
multicastReleaser = new Runnable() {
|
multicastReleaser = () -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
|
||||||
multicastLock.release();
|
multicastLock.release();
|
||||||
|
} catch (Exception ignored) {
|
||||||
|
//dont really care
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
multicastReleaser = new Runnable() {
|
multicastReleaser = () -> {
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,20 +156,13 @@ public class DiscoverActivity extends Activity
|
|||||||
R.layout.available_children_list);
|
R.layout.available_children_list);
|
||||||
serviceTable.setAdapter(availableServicesAdapter);
|
serviceTable.setAdapter(availableServicesAdapter);
|
||||||
|
|
||||||
serviceTable.setOnItemClickListener(new OnItemClickListener()
|
serviceTable.setOnItemClickListener((parent, view, position, id) -> {
|
||||||
{
|
final ServiceInfoWrapper info = (ServiceInfoWrapper) parent.getItemAtPosition(position);
|
||||||
@Override
|
connectToChild(info.getAddress(), info.getPort(), info.getName());
|
||||||
public void onItemClick(AdapterView<?> parent, View view,
|
|
||||||
int position, long id)
|
|
||||||
{
|
|
||||||
final ServiceInfoWrapper info = (ServiceInfoWrapper) parent.getItemAtPosition(position);
|
|
||||||
connectToChild(info.getAddress(), info.getPort(), info.getName());
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Instantiate a new DiscoveryListener
|
// Instantiate a new DiscoveryListener
|
||||||
_discoveryListener = new NsdManager.DiscoveryListener()
|
_discoveryListener = new NsdManager.DiscoveryListener() {
|
||||||
{
|
|
||||||
// Called as soon as service discovery begins.
|
// Called as soon as service discovery begins.
|
||||||
@Override
|
@Override
|
||||||
public void onDiscoveryStarted(String regType)
|
public void onDiscoveryStarted(String regType)
|
||||||
@@ -210,50 +175,34 @@ public class DiscoverActivity extends Activity
|
|||||||
// A service was found! Do something with it.
|
// A service was found! Do something with it.
|
||||||
Log.d(TAG, "Service discovery success: " + service);
|
Log.d(TAG, "Service discovery success: " + service);
|
||||||
|
|
||||||
if (!service.getServiceType().equals(serviceType))
|
if (!service.getServiceType().equals(serviceType)) {
|
||||||
{
|
|
||||||
// Service type is the string containing the protocol and
|
// Service type is the string containing the protocol and
|
||||||
// transport layer for this service.
|
// transport layer for this service.
|
||||||
Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
|
Log.d(TAG, "Unknown Service Type: " + service.getServiceType());
|
||||||
}
|
} else if (service.getServiceName().contains("ChildMonitor")) {
|
||||||
else if (service.getServiceName().contains("ChildMonitor"))
|
NsdManager.ResolveListener resolver = new NsdManager.ResolveListener() {
|
||||||
{
|
|
||||||
NsdManager.ResolveListener resolver = new NsdManager.ResolveListener()
|
|
||||||
{
|
|
||||||
@Override
|
@Override
|
||||||
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode)
|
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
|
||||||
{
|
|
||||||
// Called when the resolve fails. Use the error code to debug.
|
// Called when the resolve fails. Use the error code to debug.
|
||||||
Log.e(TAG, "Resolve failed: error " + errorCode + " for service: " + serviceInfo);
|
Log.e(TAG, "Resolve failed: error " + errorCode + " for service: " + serviceInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onServiceResolved(final NsdServiceInfo serviceInfo)
|
public void onServiceResolved(final NsdServiceInfo serviceInfo) {
|
||||||
{
|
|
||||||
Log.i(TAG, "Resolve Succeeded: " + serviceInfo);
|
Log.i(TAG, "Resolve Succeeded: " + serviceInfo);
|
||||||
|
|
||||||
DiscoverActivity.this.runOnUiThread(new Runnable()
|
DiscoverActivity.this.runOnUiThread(() -> availableServicesAdapter.add(new ServiceInfoWrapper(serviceInfo)));
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
availableServicesAdapter.add(new ServiceInfoWrapper(serviceInfo));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
_nsdManager.resolveService(service, resolver);
|
_nsdManager.resolveService(service, resolver);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
Log.d(TAG, "Unknown Service name: " + service.getServiceName());
|
Log.d(TAG, "Unknown Service name: " + service.getServiceName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onServiceLost(NsdServiceInfo service)
|
public void onServiceLost(NsdServiceInfo service) {
|
||||||
{
|
|
||||||
// When the network service is no longer available.
|
// When the network service is no longer available.
|
||||||
// Internal bookkeeping code goes here.
|
// Internal bookkeeping code goes here.
|
||||||
Log.e(TAG, "Service lost: " + service);
|
Log.e(TAG, "Service lost: " + service);
|
||||||
@@ -261,23 +210,20 @@ public class DiscoverActivity extends Activity
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDiscoveryStopped(String serviceType)
|
public void onDiscoveryStopped(String serviceType) {
|
||||||
{
|
|
||||||
Log.i(TAG, "Discovery stopped: " + serviceType);
|
Log.i(TAG, "Discovery stopped: " + serviceType);
|
||||||
multicastReleaser.run();
|
multicastReleaser.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStartDiscoveryFailed(String serviceType, int errorCode)
|
public void onStartDiscoveryFailed(String serviceType, int errorCode) {
|
||||||
{
|
|
||||||
Log.e(TAG, "Discovery failed: Error code: " + errorCode);
|
Log.e(TAG, "Discovery failed: Error code: " + errorCode);
|
||||||
nsdManager.stopServiceDiscovery(this);
|
nsdManager.stopServiceDiscovery(this);
|
||||||
multicastReleaser.run();
|
multicastReleaser.run();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onStopDiscoveryFailed(String serviceType, int errorCode)
|
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
|
||||||
{
|
|
||||||
Log.e(TAG, "Discovery failed: Error code: " + errorCode);
|
Log.e(TAG, "Discovery failed: Error code: " + errorCode);
|
||||||
nsdManager.stopServiceDiscovery(this);
|
nsdManager.stopServiceDiscovery(this);
|
||||||
multicastReleaser.run();
|
multicastReleaser.run();
|
||||||
@@ -285,7 +231,8 @@ public class DiscoverActivity extends Activity
|
|||||||
};
|
};
|
||||||
|
|
||||||
nsdManager.discoverServices(
|
nsdManager.discoverServices(
|
||||||
serviceType, NsdManager.PROTOCOL_DNS_SD, _discoveryListener);
|
serviceType, NsdManager.PROTOCOL_DNS_SD, _discoveryListener
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -295,8 +242,7 @@ public class DiscoverActivity extends Activity
|
|||||||
* @param port
|
* @param port
|
||||||
* @param name
|
* @param name
|
||||||
*/
|
*/
|
||||||
private void connectToChild(final String address, final int port, final String name)
|
private void connectToChild(final String address, final int port, final String name) {
|
||||||
{
|
|
||||||
final Intent i = new Intent(getApplicationContext(), ListenActivity.class);
|
final Intent i = new Intent(getApplicationContext(), ListenActivity.class);
|
||||||
final Bundle b = new Bundle();
|
final Bundle b = new Bundle();
|
||||||
b.putString("address", address);
|
b.putString("address", address);
|
||||||
@@ -307,8 +253,7 @@ public class DiscoverActivity extends Activity
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ServiceInfoWrapper
|
class ServiceInfoWrapper {
|
||||||
{
|
|
||||||
private NsdServiceInfo _info;
|
private NsdServiceInfo _info;
|
||||||
public ServiceInfoWrapper(NsdServiceInfo info)
|
public ServiceInfoWrapper(NsdServiceInfo info)
|
||||||
{
|
{
|
||||||
@@ -325,8 +270,7 @@ class ServiceInfoWrapper
|
|||||||
return _info.getPort();
|
return _info.getPort();
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName()
|
public String getName() {
|
||||||
{
|
|
||||||
// If there is more than one service on the network, it will
|
// If there is more than one service on the network, it will
|
||||||
// have a number at the end, but will appear as the following:
|
// have a number at the end, but will appear as the following:
|
||||||
// "ChildMonitor\\032(number)
|
// "ChildMonitor\\032(number)
|
||||||
|
|||||||
@@ -33,8 +33,7 @@ import java.io.InputStream;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
public class ListenActivity extends Activity
|
public class ListenActivity extends Activity {
|
||||||
{
|
|
||||||
final String TAG = "ChildMonitor";
|
final String TAG = "ChildMonitor";
|
||||||
// Sets an ID for the notification
|
// Sets an ID for the notification
|
||||||
final static int mNotificationId = 1;
|
final static int mNotificationId = 1;
|
||||||
@@ -51,8 +50,7 @@ public class ListenActivity extends Activity
|
|||||||
private final int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
|
private final int bufferSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
|
||||||
private final int byteBufferSize = bufferSize*2;
|
private final int byteBufferSize = bufferSize*2;
|
||||||
|
|
||||||
private void streamAudio(final Socket socket, AudioListener listener) throws IllegalArgumentException, IllegalStateException, IOException
|
private void streamAudio(final Socket socket, AudioListener listener) throws IllegalArgumentException, IllegalStateException, IOException {
|
||||||
{
|
|
||||||
Log.i(TAG, "Setting up stream");
|
Log.i(TAG, "Setting up stream");
|
||||||
|
|
||||||
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
|
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
|
||||||
@@ -69,38 +67,31 @@ public class ListenActivity extends Activity
|
|||||||
|
|
||||||
audioTrack.play();
|
audioTrack.play();
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
final byte [] readBuffer = new byte[byteBufferSize];
|
final byte [] readBuffer = new byte[byteBufferSize];
|
||||||
final short [] decodedBuffer = new short[byteBufferSize*2];
|
final short [] decodedBuffer = new short[byteBufferSize*2];
|
||||||
|
|
||||||
while(socket.isConnected() && read != -1 && Thread.currentThread().isInterrupted() == false)
|
while(socket.isConnected() && read != -1 && !Thread.currentThread().isInterrupted()) {
|
||||||
{
|
|
||||||
read = is.read(readBuffer);
|
read = is.read(readBuffer);
|
||||||
int decoded = CODEC.decode(decodedBuffer, readBuffer, read, 0);
|
int decoded = CODEC.decode(decodedBuffer, readBuffer, read, 0);
|
||||||
|
|
||||||
if(decoded > 0)
|
if(decoded > 0) {
|
||||||
{
|
|
||||||
audioTrack.write(decodedBuffer, 0, decoded);
|
audioTrack.write(decodedBuffer, 0, decoded);
|
||||||
short[] decodedBytes = new short[decoded];
|
short[] decodedBytes = new short[decoded];
|
||||||
System.arraycopy(decodedBuffer, 0, decodedBytes, 0, decoded);
|
System.arraycopy(decodedBuffer, 0, decodedBytes, 0, decoded);
|
||||||
listener.onAudio(decodedBytes);
|
listener.onAudio(decodedBytes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e) {
|
|
||||||
Log.e(TAG, "Connection failed", e);
|
Log.e(TAG, "Connection failed", e);
|
||||||
}
|
} finally {
|
||||||
finally
|
|
||||||
{
|
|
||||||
audioTrack.stop();
|
audioTrack.stop();
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
{
|
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
final Bundle b = getIntent().getExtras();
|
final Bundle b = getIntent().getExtras();
|
||||||
@@ -130,66 +121,38 @@ public class ListenActivity extends Activity
|
|||||||
|
|
||||||
final VolumeView volumeView = (VolumeView) findViewById(R.id.volume);
|
final VolumeView volumeView = (VolumeView) findViewById(R.id.volume);
|
||||||
|
|
||||||
final AudioListener listener = new AudioListener() {
|
final AudioListener listener = audioData -> runOnUiThread(() -> volumeView.onAudioData(audioData));
|
||||||
@Override
|
|
||||||
public void onAudio(final short[] audioData) {
|
|
||||||
runOnUiThread(new Runnable() {
|
_listenThread = new Thread(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
final Socket socket = new Socket(_address, _port);
|
||||||
volumeView.onAudioData(audioData);
|
streamAudio(socket, listener);
|
||||||
}
|
} catch (IOException e) {
|
||||||
});
|
Log.e(TAG, "Failed to stream audio", e);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
if(!Thread.currentThread().isInterrupted()) {
|
||||||
|
// If this thread has not been interrupted, likely something
|
||||||
|
// bad happened with the connection to the child device. Play
|
||||||
|
// an alert to notify the user that the connection has been
|
||||||
|
// interrupted.
|
||||||
|
playAlert();
|
||||||
|
|
||||||
_listenThread = new Thread(new Runnable()
|
ListenActivity.this.runOnUiThread(() -> {
|
||||||
{
|
final TextView connectedText1 = (TextView) findViewById(R.id.connectedTo);
|
||||||
@Override
|
connectedText1.setText("");
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
final Socket socket = new Socket(_address, _port);
|
|
||||||
streamAudio(socket, listener);
|
|
||||||
}
|
|
||||||
catch (UnknownHostException e)
|
|
||||||
{
|
|
||||||
Log.e(TAG, "Failed to stream audio", e);
|
|
||||||
}
|
|
||||||
catch (IOException e)
|
|
||||||
{
|
|
||||||
Log.e(TAG, "Failed to stream audio", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(Thread.currentThread().isInterrupted() == false)
|
final TextView statusText1 = (TextView) findViewById(R.id.textStatus);
|
||||||
{
|
statusText1.setText(R.string.disconnected);
|
||||||
// If this thread has not been interrupted, likely something
|
NotificationCompat.Builder mBuilder1 =
|
||||||
// bad happened with the connection to the child device. Play
|
new NotificationCompat.Builder(ListenActivity.this)
|
||||||
// an alert to notify the user that the connection has been
|
.setOngoing(false)
|
||||||
// interrupted.
|
.setSmallIcon(R.drawable.listening_notification)
|
||||||
playAlert();
|
.setContentTitle(getString(R.string.app_name))
|
||||||
|
.setContentText(getString(R.string.disconnected));
|
||||||
ListenActivity.this.runOnUiThread(new Runnable()
|
_mNotifyMgr.notify(mNotificationId, mBuilder1.build());
|
||||||
{
|
});
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
final TextView connectedText = (TextView) findViewById(R.id.connectedTo);
|
|
||||||
connectedText.setText("");
|
|
||||||
|
|
||||||
final TextView statusText = (TextView) findViewById(R.id.textStatus);
|
|
||||||
statusText.setText(R.string.disconnected);
|
|
||||||
NotificationCompat.Builder mBuilder =
|
|
||||||
new NotificationCompat.Builder(ListenActivity.this)
|
|
||||||
.setOngoing(false)
|
|
||||||
.setSmallIcon(R.drawable.listening_notification)
|
|
||||||
.setContentTitle(getString(R.string.app_name))
|
|
||||||
.setContentText(getString(R.string.disconnected));
|
|
||||||
_mNotifyMgr.notify(mNotificationId, mBuilder.build());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -197,32 +160,24 @@ public class ListenActivity extends Activity
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy()
|
protected void onStop() {
|
||||||
{
|
|
||||||
_listenThread.interrupt();
|
_listenThread.interrupt();
|
||||||
_listenThread = null;
|
_listenThread = null;
|
||||||
|
super.onStop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void playAlert()
|
private void playAlert() {
|
||||||
{
|
|
||||||
final MediaPlayer mp = MediaPlayer.create(this, R.raw.upward_beep_chromatic_fifths);
|
final MediaPlayer mp = MediaPlayer.create(this, R.raw.upward_beep_chromatic_fifths);
|
||||||
if(mp != null)
|
if(mp != null) {
|
||||||
{
|
|
||||||
Log.i(TAG, "Playing alert");
|
Log.i(TAG, "Playing alert");
|
||||||
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
|
mp.setOnCompletionListener(mp1 -> mp1.release());
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void onCompletion(MediaPlayer mp)
|
|
||||||
{
|
|
||||||
mp.release();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
mp.start();
|
mp.start();
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
Log.e(TAG, "Failed to play alert");
|
Log.e(TAG, "Failed to play alert");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,8 +27,7 @@ import android.util.Log;
|
|||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
|
||||||
public class StartActivity extends Activity
|
public class StartActivity extends Activity {
|
||||||
{
|
|
||||||
static final String TAG = "ChildMonitor";
|
static final String TAG = "ChildMonitor";
|
||||||
private final static int PERMISSIONS_REQUEST_RECORD_AUDIO = 298349824;
|
private final static int PERMISSIONS_REQUEST_RECORD_AUDIO = 298349824;
|
||||||
private final static int PERMISSIONS_REQUEST_MULTICAST = 298349825;
|
private final static int PERMISSIONS_REQUEST_MULTICAST = 298349825;
|
||||||
@@ -41,34 +40,24 @@ public class StartActivity extends Activity
|
|||||||
setContentView(R.layout.activity_start);
|
setContentView(R.layout.activity_start);
|
||||||
|
|
||||||
final Button monitorButton = (Button) findViewById(R.id.useChildDevice);
|
final Button monitorButton = (Button) findViewById(R.id.useChildDevice);
|
||||||
monitorButton.setOnClickListener(new View.OnClickListener()
|
monitorButton.setOnClickListener(v -> {
|
||||||
{
|
Log.i(TAG, "Starting up monitor");
|
||||||
@Override
|
|
||||||
public void onClick(View v)
|
|
||||||
{
|
|
||||||
Log.i(TAG, "Starting up monitor");
|
|
||||||
|
|
||||||
if (isAudioRecordingPermissionGranted()) {
|
if (isAudioRecordingPermissionGranted()) {
|
||||||
startActivity(new Intent(getApplicationContext(), MonitorActivity.class));
|
startActivity(new Intent(getApplicationContext(), MonitorActivity.class));
|
||||||
} else {
|
} else {
|
||||||
requestAudioPermission();
|
requestAudioPermission();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
final Button connectButton = (Button) findViewById(R.id.useParentDevice);
|
final Button connectButton = (Button) findViewById(R.id.useParentDevice);
|
||||||
connectButton.setOnClickListener(new View.OnClickListener()
|
connectButton.setOnClickListener(v -> {
|
||||||
{
|
Log.i(TAG, "Starting connection activity");
|
||||||
@Override
|
if (isMulticastPermissionGranted()) {
|
||||||
public void onClick(View v)
|
Intent i = new Intent(getApplicationContext(), DiscoverActivity.class);
|
||||||
{
|
startActivity(i);
|
||||||
Log.i(TAG, "Starting connection activity");
|
} else {
|
||||||
if (isMulticastPermissionGranted()) {
|
requestMulticastPermission();
|
||||||
Intent i = new Intent(getApplicationContext(), DiscoverActivity.class);
|
|
||||||
startActivity(i);
|
|
||||||
} else {
|
|
||||||
requestMulticastPermission();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user