Added new API method pause/resume Video or Navdata.Added immediate channel reconnect when process is paused
Esse commit está contido em:
@@ -316,12 +316,30 @@ public class MainActivity extends Activity implements DroneVideoListener, OnShar
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
|
||||
if (null != drone) {
|
||||
drone.resumeNavData();
|
||||
drone.resumeVideo();
|
||||
}
|
||||
|
||||
/*
|
||||
if (prefs.getBoolean(PREF_AUTOCONNECT_DRONE, false)) {
|
||||
connectButton.performClick();
|
||||
}
|
||||
if (prefs.getBoolean(PREF_AUTOCONNECT_PS3, false)) {
|
||||
btnConnectUsbControllerButton.performClick();
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
|
||||
if (null != drone) {
|
||||
drone.pauseNavData();
|
||||
drone.pauseVideo();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -645,5 +645,29 @@ public class ARDrone
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void pauseNavData() {
|
||||
if (null != nav_data_reader) {
|
||||
nav_data_reader.pauseReading();
|
||||
}
|
||||
}
|
||||
|
||||
public void resumeNavData() {
|
||||
if (null != nav_data_reader) {
|
||||
nav_data_reader.resumeReading();
|
||||
}
|
||||
}
|
||||
|
||||
public void pauseVideo() {
|
||||
if (null != video_reader_thread) {
|
||||
video_reader.pauseReading();
|
||||
}
|
||||
}
|
||||
|
||||
public void resumeVideo() {
|
||||
if (null != video_reader_thread) {
|
||||
video_reader.resumeReading();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -31,7 +31,9 @@ public abstract class DataProcessor extends Thread {
|
||||
if (nextEmpty) {
|
||||
synchronized (lock) {
|
||||
try {
|
||||
lock.wait();
|
||||
if (nextEmpty) {
|
||||
lock.wait();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
done = true;
|
||||
}
|
||||
@@ -67,11 +69,11 @@ public abstract class DataProcessor extends Thread {
|
||||
nextBuffer.clear();
|
||||
nextBuffer.put(infBuffer);
|
||||
nextBuffer.flip();
|
||||
nextEmpty = false;
|
||||
nextDataBufferLength = len;
|
||||
}
|
||||
|
||||
synchronized (lock) {
|
||||
nextEmpty = false;
|
||||
lock.notify();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ public abstract class DataReader implements Runnable {
|
||||
ARDrone drone;
|
||||
protected Selector selector;
|
||||
private boolean done;
|
||||
private boolean pauseFlag;
|
||||
private InetAddress drone_addr;
|
||||
private int data_port;
|
||||
|
||||
@@ -65,9 +66,9 @@ public abstract class DataReader implements Runnable {
|
||||
selector.close();
|
||||
} catch (IOException iox)
|
||||
{
|
||||
// Ignore
|
||||
iox.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
if (!channel.socket().isClosed()) {
|
||||
channel.socket().close();
|
||||
}
|
||||
@@ -77,8 +78,15 @@ public abstract class DataReader implements Runnable {
|
||||
channel.disconnect();
|
||||
} catch (IOException iox)
|
||||
{
|
||||
// Ignore
|
||||
iox.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
channel.close();
|
||||
} catch (IOException iox) {
|
||||
iox.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -91,6 +99,15 @@ public abstract class DataReader implements Runnable {
|
||||
timeOfLastMessage = System.currentTimeMillis();
|
||||
while(!done)
|
||||
{
|
||||
if (pauseFlag) {
|
||||
synchronized(this) {
|
||||
if (pauseFlag) {
|
||||
wait();
|
||||
timeOfLastMessage = 1; // will automatically reconnect channel
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
selector.select(MAX_TMEOUT);
|
||||
if(done)
|
||||
{
|
||||
@@ -122,6 +139,10 @@ public abstract class DataReader implements Runnable {
|
||||
{
|
||||
channel.write(trigger_buffer);
|
||||
channel.register(selector, SelectionKey.OP_READ);
|
||||
// prepare buffer for new reconnection attempt
|
||||
trigger_buffer.clear();
|
||||
trigger_buffer.put(TRIGGER_BYTES);
|
||||
trigger_buffer.flip();
|
||||
}
|
||||
else if(key.isReadable())
|
||||
{
|
||||
@@ -143,12 +164,23 @@ public abstract class DataReader implements Runnable {
|
||||
|
||||
abstract void handleData(ByteBuffer buf, int len) throws Exception;
|
||||
|
||||
public void finish()
|
||||
{
|
||||
public synchronized void finish()
|
||||
{
|
||||
if (pauseFlag) {
|
||||
resumeReading();
|
||||
}
|
||||
done = true;
|
||||
if (null != selector) {
|
||||
selector.wakeup();
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void pauseReading() {
|
||||
pauseFlag = true;
|
||||
}
|
||||
|
||||
public synchronized void resumeReading() {
|
||||
pauseFlag = false;
|
||||
notify();
|
||||
}
|
||||
}
|
||||
Referência em uma Nova Issue
Bloquear um usuário