Check for audio recording permissions before opening MonitorActivity
This commit is contained in:
@@ -21,8 +21,6 @@ import java.io.OutputStream;
|
|||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.concurrent.Executor;
|
|
||||||
import java.util.concurrent.Executors;
|
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
@@ -38,8 +36,7 @@ import android.text.format.Formatter;
|
|||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
public class MonitorActivity extends Activity
|
public class MonitorActivity extends Activity {
|
||||||
{
|
|
||||||
final static String TAG = "BabyMonitor";
|
final static String TAG = "BabyMonitor";
|
||||||
|
|
||||||
private NsdManager nsdManager;
|
private NsdManager nsdManager;
|
||||||
@@ -52,10 +49,8 @@ public class MonitorActivity extends Activity
|
|||||||
|
|
||||||
private int currentPort;
|
private int currentPort;
|
||||||
|
|
||||||
private void serviceConnection(Socket socket) throws IOException
|
private void serviceConnection(Socket socket) {
|
||||||
{
|
runOnUiThread(new Runnable() {
|
||||||
MonitorActivity.this.runOnUiThread(new Runnable()
|
|
||||||
{
|
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
@@ -80,49 +75,43 @@ public class MonitorActivity extends Activity
|
|||||||
final int byteBufferSize = bufferSize*2;
|
final int byteBufferSize = bufferSize*2;
|
||||||
final byte[] buffer = new byte[byteBufferSize];
|
final byte[] buffer = new byte[byteBufferSize];
|
||||||
|
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
audioRecord.startRecording();
|
audioRecord.startRecording();
|
||||||
|
|
||||||
final OutputStream out = socket.getOutputStream();
|
final OutputStream out = socket.getOutputStream();
|
||||||
|
|
||||||
socket.setSendBufferSize(byteBufferSize);
|
socket.setSendBufferSize(byteBufferSize);
|
||||||
Log.d(TAG, "Socket send buffer size: " + socket.getSendBufferSize());
|
Log.d(TAG, "Socket send buffer size: " + socket.getSendBufferSize());
|
||||||
|
|
||||||
while (socket.isConnected() && Thread.currentThread().isInterrupted() == false)
|
while (socket.isConnected() && !Thread.currentThread().isInterrupted()) {
|
||||||
{
|
|
||||||
final int read = audioRecord.read(buffer, 0, bufferSize);
|
final int read = audioRecord.read(buffer, 0, bufferSize);
|
||||||
out.write(buffer, 0, read);
|
out.write(buffer, 0, read);
|
||||||
}
|
}
|
||||||
}
|
} catch (IOException e) {
|
||||||
finally
|
Log.e(TAG, "Connection failed", e);
|
||||||
{
|
} finally {
|
||||||
audioRecord.stop();
|
audioRecord.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
{
|
|
||||||
Log.i(TAG, "Baby monitor start");
|
Log.i(TAG, "Baby monitor start");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_monitor);
|
setContentView(R.layout.activity_monitor);
|
||||||
|
|
||||||
Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
|
|
||||||
nsdManager = (NsdManager)this.getSystemService(Context.NSD_SERVICE);
|
nsdManager = (NsdManager)this.getSystemService(Context.NSD_SERVICE);
|
||||||
currentPort = 10000;
|
currentPort = 10000;
|
||||||
currentSocket = null;
|
currentSocket = null;
|
||||||
final Object currentToken = new Object();
|
final Object currentToken = new Object();
|
||||||
connectionToken = currentToken;
|
connectionToken = currentToken;
|
||||||
|
|
||||||
singleThreadExecutor.execute(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
while(Objects.equals(connectionToken, currentToken))
|
while(Objects.equals(connectionToken, currentToken)) {
|
||||||
{
|
try (ServerSocket serverSocket = new ServerSocket(currentPort)) {
|
||||||
try (ServerSocket serverSocket = new ServerSocket(currentPort))
|
|
||||||
{
|
|
||||||
currentSocket = serverSocket;
|
currentSocket = serverSocket;
|
||||||
// Store the chosen port.
|
// Store the chosen port.
|
||||||
final int localPort = serverSocket.getLocalPort();
|
final int localPort = serverSocket.getLocalPort();
|
||||||
@@ -132,8 +121,7 @@ public class MonitorActivity extends Activity
|
|||||||
registerService(localPort);
|
registerService(localPort);
|
||||||
|
|
||||||
// Wait for a parent to find us and connect
|
// Wait for a parent to find us and connect
|
||||||
try {
|
try (Socket socket = serverSocket.accept()) {
|
||||||
Socket socket = serverSocket.accept();
|
|
||||||
Log.i(TAG, "Connection from parent device received");
|
Log.i(TAG, "Connection from parent device received");
|
||||||
|
|
||||||
// We now have a client connection.
|
// We now have a client connection.
|
||||||
@@ -141,19 +129,15 @@ public class MonitorActivity extends Activity
|
|||||||
// attempt to connect
|
// attempt to connect
|
||||||
unregisterService();
|
unregisterService();
|
||||||
serviceConnection(socket);
|
serviceConnection(socket);
|
||||||
|
}
|
||||||
} catch(IOException e) {
|
} catch(IOException e) {
|
||||||
Log.e(TAG, "Connection failed", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch(IOException e)
|
|
||||||
{
|
|
||||||
// Just in case
|
// Just in case
|
||||||
currentPort++;
|
currentPort++;
|
||||||
Log.e(TAG, "Failed to open server socket. Port increased to " + currentPort, e);
|
Log.e(TAG, "Failed to open server socket. Port increased to " + currentPort, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}).start();
|
||||||
|
|
||||||
final TextView addressText = (TextView) findViewById(R.id.address);
|
final TextView addressText = (TextView) findViewById(R.id.address);
|
||||||
|
|
||||||
@@ -162,29 +146,24 @@ public class MonitorActivity extends Activity
|
|||||||
(WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
|
(WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
|
||||||
final WifiInfo info = wifiManager.getConnectionInfo();
|
final WifiInfo info = wifiManager.getConnectionInfo();
|
||||||
final int address = info.getIpAddress();
|
final int address = info.getIpAddress();
|
||||||
if(address != 0)
|
if(address != 0) {
|
||||||
{
|
|
||||||
@SuppressWarnings("deprecation")
|
@SuppressWarnings("deprecation")
|
||||||
final String ipAddress = Formatter.formatIpAddress(address);
|
final String ipAddress = Formatter.formatIpAddress(address);
|
||||||
addressText.setText(ipAddress);
|
addressText.setText(ipAddress);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
addressText.setText(R.string.wifiNotConnected);
|
addressText.setText(R.string.wifiNotConnected);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy()
|
protected void onDestroy() {
|
||||||
{
|
|
||||||
Log.i(TAG, "Baby monitor stop");
|
Log.i(TAG, "Baby monitor stop");
|
||||||
|
|
||||||
unregisterService();
|
unregisterService();
|
||||||
|
|
||||||
connectionToken = null;
|
connectionToken = null;
|
||||||
if(currentSocket != null)
|
if(currentSocket != null) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
currentSocket.close();
|
currentSocket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -195,15 +174,13 @@ public class MonitorActivity extends Activity
|
|||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void registerService(final int port)
|
private void registerService(final int port) {
|
||||||
{
|
|
||||||
final NsdServiceInfo serviceInfo = new NsdServiceInfo();
|
final NsdServiceInfo serviceInfo = new NsdServiceInfo();
|
||||||
serviceInfo.setServiceName("ProtectBabyMonitor");
|
serviceInfo.setServiceName("ProtectBabyMonitor");
|
||||||
serviceInfo.setServiceType("_babymonitor._tcp.");
|
serviceInfo.setServiceType("_babymonitor._tcp.");
|
||||||
serviceInfo.setPort(port);
|
serviceInfo.setPort(port);
|
||||||
|
|
||||||
registrationListener = new NsdManager.RegistrationListener()
|
registrationListener = new NsdManager.RegistrationListener() {
|
||||||
{
|
|
||||||
@Override
|
@Override
|
||||||
public void onServiceRegistered(NsdServiceInfo nsdServiceInfo) {
|
public void onServiceRegistered(NsdServiceInfo nsdServiceInfo) {
|
||||||
// Save the service name. Android may have changed it in order to
|
// Save the service name. Android may have changed it in order to
|
||||||
@@ -230,15 +207,13 @@ public class MonitorActivity extends Activity
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode)
|
public void onRegistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
|
||||||
{
|
|
||||||
// Registration failed! Put debugging code here to determine why.
|
// Registration failed! Put debugging code here to determine why.
|
||||||
Log.e(TAG, "Registration failed: " + errorCode);
|
Log.e(TAG, "Registration failed: " + errorCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onServiceUnregistered(NsdServiceInfo arg0)
|
public void onServiceUnregistered(NsdServiceInfo arg0) {
|
||||||
{
|
|
||||||
// Service has been unregistered. This only happens when you call
|
// Service has been unregistered. This only happens when you call
|
||||||
// NsdManager.unregisterService() and pass in this listener.
|
// NsdManager.unregisterService() and pass in this listener.
|
||||||
|
|
||||||
@@ -246,8 +221,7 @@ public class MonitorActivity extends Activity
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode)
|
public void onUnregistrationFailed(NsdServiceInfo serviceInfo, int errorCode) {
|
||||||
{
|
|
||||||
// Unregistration failed. Put debugging code here to determine why.
|
// Unregistration failed. Put debugging code here to determine why.
|
||||||
|
|
||||||
Log.e(TAG, "Unregistration failed: " + errorCode);
|
Log.e(TAG, "Unregistration failed: " + errorCode);
|
||||||
@@ -262,10 +236,8 @@ public class MonitorActivity extends Activity
|
|||||||
* Uhregistered the service and assigns the listener
|
* Uhregistered the service and assigns the listener
|
||||||
* to null.
|
* to null.
|
||||||
*/
|
*/
|
||||||
private void unregisterService()
|
private void unregisterService() {
|
||||||
{
|
if(registrationListener != null) {
|
||||||
if(registrationListener != null)
|
|
||||||
{
|
|
||||||
Log.i(TAG, "Unregistering monitoring service");
|
Log.i(TAG, "Unregistering monitoring service");
|
||||||
|
|
||||||
nsdManager.unregisterService(registrationListener);
|
nsdManager.unregisterService(registrationListener);
|
||||||
|
|||||||
@@ -16,9 +16,13 @@
|
|||||||
*/
|
*/
|
||||||
package protect.babymonitor;
|
package protect.babymonitor;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.v4.app.ActivityCompat;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
@@ -26,10 +30,10 @@ import android.widget.Button;
|
|||||||
public class StartActivity extends Activity
|
public class StartActivity extends Activity
|
||||||
{
|
{
|
||||||
static final String TAG = "BabyMonitor";
|
static final String TAG = "BabyMonitor";
|
||||||
|
private final static int PERMISSIONS_REQUEST_RECORD_AUDIO = 298349824;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
{
|
|
||||||
Log.i(TAG, "Baby monitor launched");
|
Log.i(TAG, "Baby monitor launched");
|
||||||
|
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@@ -43,8 +47,11 @@ public class StartActivity extends Activity
|
|||||||
{
|
{
|
||||||
Log.i(TAG, "Starting up monitor");
|
Log.i(TAG, "Starting up monitor");
|
||||||
|
|
||||||
Intent i = new Intent(getApplicationContext(), MonitorActivity.class);
|
if (isAudioRecordingPermissionGranted()) {
|
||||||
startActivity(i);
|
startActivity(new Intent(getApplicationContext(), MonitorActivity.class));
|
||||||
|
} else {
|
||||||
|
requestAudioPermission();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -55,10 +62,28 @@ public class StartActivity extends Activity
|
|||||||
public void onClick(View v)
|
public void onClick(View v)
|
||||||
{
|
{
|
||||||
Log.i(TAG, "Starting connection activity");
|
Log.i(TAG, "Starting connection activity");
|
||||||
|
|
||||||
Intent i = new Intent(getApplicationContext(), DiscoverActivity.class);
|
Intent i = new Intent(getApplicationContext(), DiscoverActivity.class);
|
||||||
startActivity(i);
|
startActivity(i);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isAudioRecordingPermissionGranted() {
|
||||||
|
return ContextCompat.checkSelfPermission(StartActivity.this, Manifest.permission.RECORD_AUDIO)
|
||||||
|
== PackageManager.PERMISSION_GRANTED;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void requestAudioPermission() {
|
||||||
|
ActivityCompat.requestPermissions(StartActivity.this,
|
||||||
|
new String[]{Manifest.permission.RECORD_AUDIO},
|
||||||
|
PERMISSIONS_REQUEST_RECORD_AUDIO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||||
|
if (requestCode == PERMISSIONS_REQUEST_RECORD_AUDIO && grantResults.length > 0
|
||||||
|
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
startActivity(new Intent(getApplicationContext(), MonitorActivity.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user