Initialize member vars in onCreate
This commit is contained in:
@@ -36,25 +36,23 @@ import android.net.wifi.WifiManager;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.text.format.Formatter;
|
import android.text.format.Formatter;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
public class MonitorActivity extends Activity
|
public class MonitorActivity extends Activity
|
||||||
{
|
{
|
||||||
final String TAG = "BabyMonitor";
|
final static String TAG = "BabyMonitor";
|
||||||
|
|
||||||
NsdManager _nsdManager;
|
NsdManager nsdManager;
|
||||||
|
|
||||||
NsdManager.RegistrationListener _registrationListener;
|
NsdManager.RegistrationListener registrationListener;
|
||||||
|
|
||||||
ServerSocket currentSocket = null;
|
ServerSocket currentSocket;
|
||||||
|
|
||||||
Executor singleThreadExecutor = Executors.newSingleThreadExecutor();
|
Executor singleThreadExecutor;
|
||||||
|
|
||||||
Object connectionToken = null;
|
Object connectionToken;
|
||||||
|
|
||||||
int currentPort = 10000;
|
int currentPort;
|
||||||
|
|
||||||
private void serviceConnection(Socket socket) throws IOException
|
private void serviceConnection(Socket socket) throws IOException
|
||||||
{
|
{
|
||||||
@@ -73,9 +71,13 @@ public class MonitorActivity extends Activity
|
|||||||
final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
|
final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
|
||||||
|
|
||||||
final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
|
final int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
|
||||||
final AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
|
final AudioRecord audioRecord = new AudioRecord(
|
||||||
frequency, channelConfiguration,
|
MediaRecorder.AudioSource.MIC,
|
||||||
audioEncoding, bufferSize);
|
frequency,
|
||||||
|
channelConfiguration,
|
||||||
|
audioEncoding,
|
||||||
|
bufferSize
|
||||||
|
);
|
||||||
|
|
||||||
final int byteBufferSize = bufferSize*2;
|
final int byteBufferSize = bufferSize*2;
|
||||||
final byte[] buffer = new byte[byteBufferSize];
|
final byte[] buffer = new byte[byteBufferSize];
|
||||||
@@ -105,11 +107,13 @@ public class MonitorActivity extends Activity
|
|||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
Log.i(TAG, "Baby monitor start");
|
Log.i(TAG, "Baby monitor start");
|
||||||
|
|
||||||
_nsdManager = (NsdManager)this.getSystemService(Context.NSD_SERVICE);
|
|
||||||
|
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_monitor);
|
setContentView(R.layout.activity_monitor);
|
||||||
|
|
||||||
|
singleThreadExecutor = Executors.newSingleThreadExecutor();
|
||||||
|
nsdManager = (NsdManager)this.getSystemService(Context.NSD_SERVICE);
|
||||||
|
currentPort = 10000;
|
||||||
|
currentSocket = null;
|
||||||
final Object currentToken = new Object();
|
final Object currentToken = new Object();
|
||||||
connectionToken = currentToken;
|
connectionToken = currentToken;
|
||||||
|
|
||||||
@@ -200,7 +204,7 @@ public class MonitorActivity extends Activity
|
|||||||
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) {
|
||||||
@@ -252,8 +256,8 @@ public class MonitorActivity extends Activity
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
_nsdManager.registerService(
|
nsdManager.registerService(
|
||||||
serviceInfo, NsdManager.PROTOCOL_DNS_SD, _registrationListener);
|
serviceInfo, NsdManager.PROTOCOL_DNS_SD, registrationListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -262,12 +266,12 @@ public class MonitorActivity extends Activity
|
|||||||
*/
|
*/
|
||||||
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);
|
||||||
_registrationListener = null;
|
registrationListener = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ import java.util.LinkedList;
|
|||||||
|
|
||||||
public class VolumeView extends View {
|
public class VolumeView extends View {
|
||||||
private static final int MAX_HISTORY = 10_000;
|
private static final int MAX_HISTORY = 10_000;
|
||||||
private double volume = 0;
|
private double volume;
|
||||||
private double maxVolume = 0;
|
private double maxVolume;
|
||||||
private Paint paint = new Paint();
|
private Paint paint;
|
||||||
private LinkedList<Double> volumeHistory = new LinkedList<>();
|
private LinkedList<Double> volumeHistory;
|
||||||
|
|
||||||
public VolumeView(Context context) {
|
public VolumeView(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
@@ -34,6 +34,10 @@ public class VolumeView extends View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void init() {
|
private void init() {
|
||||||
|
volume = 0;
|
||||||
|
maxVolume = 0;
|
||||||
|
paint = new Paint();
|
||||||
|
volumeHistory = new LinkedList<>();
|
||||||
paint.setColor(Color.rgb(255, 127, 0));
|
paint.setColor(Color.rgb(255, 127, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user