close and reopen socket on same executor, pause recording on sleep

This commit is contained in:
Daniel van Paassen committed 2026-05-13 01:20:28 +02:00
1 parent 314b3f99c6
commit 09b5284aee
4 files changed
+55 -10

No files matched your search

@@ -347,6 +347,16 @@ public final class HyperionScreenEncoder extends HyperionScreenEncoderBase {
}
}
@Override
public void pauseRecording() {
if (DEBUG) Log.i(TAG, "Pausing");
mRunning = false;
setCapturing(false);
if (mCaptureHandler != null) {
mCaptureHandler.removeCallbacksAndMessages(null);
}
}
@Override
public void resumeRecording() {
if (DEBUG) Log.i(TAG, "Resuming");
@@ -111,6 +111,7 @@ abstract class HyperionScreenEncoderBase {
}
public abstract void stopRecording();
public abstract void pauseRecording();
public abstract void resumeRecording();
public abstract void setOrientation(int orientation);
}
@@ -100,19 +100,13 @@ public class HyperionScreenService extends Service {
public void onReceive(Context context, Intent intent) {
switch (Objects.requireNonNull(intent.getAction())) {
case Intent.ACTION_SCREEN_ON:
if (DEBUG) Log.v(TAG, "ACTION_SCREEN_ON intent received");
if (mHyperionEncoder != null && !isCapturing()) {
if (DEBUG) Log.v(TAG, "Encoder not grabbing, attempting to restart");
mHyperionEncoder.resumeRecording();
}
if (mHyperionThread != null) mHyperionThread.resumeConnection();
if (mHyperionEncoder != null) mHyperionEncoder.resumeRecording();
notifyActivity();
break;
case Intent.ACTION_SCREEN_OFF:
if (DEBUG) Log.v(TAG, "ACTION_SCREEN_OFF intent received");
if (mHyperionEncoder != null) {
if (DEBUG) Log.v(TAG, "Clearing current light data");
mHyperionEncoder.clearLights();
}
if (mHyperionEncoder != null) mHyperionEncoder.pauseRecording();
if (mHyperionThread != null) mHyperionThread.pauseConnection();
break;
case Intent.ACTION_CONFIGURATION_CHANGED:
if (DEBUG) Log.v(TAG, "ACTION_CONFIGURATION_CHANGED intent received");
@@ -113,6 +113,46 @@ class HyperionThread(
connect()
}
/**
* Closes the current socket without shutting down the executor.
* Call [resumeConnection] to reconnect (e.g. on screen wake).
*/
fun pauseConnection() {
val pending = pendingTask
if (pending != null) {
pending.cancel(false)
pendingTask = null
}
pendingFrame = null
val client = clientRef.getAndSet(null)
if (client != null) {
try { client.disconnect() } catch (ignored: IOException) {}
}
connected.set(false)
}
/**
* Opens a fresh socket on the executor thread.
* Call after [pauseConnection] when the screen wakes up.
*/
fun resumeConnection() {
if (executor.isShutdown) return
executor.submit {
try {
val client = createClient()
if (client.isConnected()) {
clientRef.set(client)
connected.set(true)
callback.onConnected()
} else {
callback.onConnectionError(0, "Failed to reconnect")
}
} catch (e: IOException) {
callback.onConnectionError(e.hashCode(), e.message ?: "Unknown error")
}
}
}
private fun createClient(): HyperionClient {
return if (wledEnabled && wledIp != null) {
WledDdpClient(InetAddress.getByName(wledIp), 4048)