diff --git a/app/src/main/java/in/co/gorest/grblcontroller/BluetoothConnectionActivity.java b/app/src/main/java/in/co/gorest/grblcontroller/BluetoothConnectionActivity.java index 21dc3bc..99ad64b 100644 --- a/app/src/main/java/in/co/gorest/grblcontroller/BluetoothConnectionActivity.java +++ b/app/src/main/java/in/co/gorest/grblcontroller/BluetoothConnectionActivity.java @@ -144,14 +144,15 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in @Override public void onDestroy(){ super.onDestroy(); - onGcodeCommandReceived("$10=1"); + // On a recreate keep the connection: a connected service is started, so it outlives the unbind. + if(isFinishing()) onGcodeCommandReceived("$10=1"); if(mBound){ grblBluetoothSerialService.setMessageHandler(null); unbindService(serviceConnection); mBound = false; } - stopService(new Intent(this, GrblBluetoothSerialService.class)); + if(isFinishing()) stopService(new Intent(this, GrblBluetoothSerialService.class)); EventBus.getDefault().unregister(this); } @@ -271,6 +272,9 @@ public void onServiceConnected(ComponentName className, IBinder service) { mBound = true; grblBluetoothSerialService.setMessageHandler(grblServiceMessageHandler); grblBluetoothSerialService.setStatusUpdatePoolInterval(Long.parseLong(sharedPref.getString(getString(R.string.preference_update_pool_interval), String.valueOf(Constants.GRBL_STATUS_UPDATE_INTERVAL)))); + // After a recreate the link may still be up; show it. STATE_NONE is left out, as it posts "connection lost". + int state = grblBluetoothSerialService.getState(); + if(state == GrblBluetoothSerialService.STATE_CONNECTED || state == GrblBluetoothSerialService.STATE_CONNECTING) onBluetoothStateChange(state); } @Override diff --git a/app/src/main/java/in/co/gorest/grblcontroller/GrblActivity.java b/app/src/main/java/in/co/gorest/grblcontroller/GrblActivity.java index 433143c..9a5cf86 100644 --- a/app/src/main/java/in/co/gorest/grblcontroller/GrblActivity.java +++ b/app/src/main/java/in/co/gorest/grblcontroller/GrblActivity.java @@ -105,6 +105,13 @@ public abstract class GrblActivity extends AppCompatActivity implements BaseFrag @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // A fresh start gets fresh state; a recreate (rotation, fold, resize) keeps it. Reset here, not + // in onDestroy: a closing activity is destroyed after the new one has read these objects. + if(savedInstanceState == null){ + ConsoleLoggerListener.resetClass(); + FileSenderListener.resetClass(); + MachineStatusListener.resetClass(); + } setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main); @@ -130,7 +137,10 @@ protected void onCreate(Bundle savedInstanceState) { Iconify.with(new FontAwesomeModule()); setupTabLayout(); - checkPowerManagement(); + // Ask once per start. On a recreate the dialog would cover the Stop button of a running job. + if(savedInstanceState == null) checkPowerManagement(); + // A recreate during a job gets a new window, so set keep screen on again. + if(FileStreamerIntentService.getIsServiceRunning()) OnStreamingStartEvent(new StreamingStartedEvent()); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED){ @@ -160,10 +170,8 @@ private boolean hasPaidVersion() { public void onDestroy(){ super.onDestroy(); - stopService(new Intent(this, FileStreamerIntentService.class)); - ConsoleLoggerListener.resetClass(); - FileSenderListener.resetClass(); - MachineStatusListener.resetClass(); + // Only a real exit stops the job. A recreate must not stop it in the middle. + if(isFinishing()) stopService(new Intent(this, FileStreamerIntentService.class)); isAppRunning = false; getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); lastToast = null; diff --git a/app/src/main/java/in/co/gorest/grblcontroller/UsbConnectionActivity.java b/app/src/main/java/in/co/gorest/grblcontroller/UsbConnectionActivity.java index f2146b1..6170a8b 100644 --- a/app/src/main/java/in/co/gorest/grblcontroller/UsbConnectionActivity.java +++ b/app/src/main/java/in/co/gorest/grblcontroller/UsbConnectionActivity.java @@ -92,14 +92,15 @@ public void onStart(){ @Override public void onDestroy() { super.onDestroy(); - onGcodeCommandReceived("$10=1"); + // On a recreate keep the connection: the service is started, so it outlives the unbind. + if(isFinishing()) onGcodeCommandReceived("$10=1"); unregisterReceiver(mUsbReceiver); if(mBound){ grblUsbSerialService.setMessageHandler(null); unbindService(usbConnection); mBound = false; } - stopService(new Intent(this, GrblUsbSerialService.class)); + if(isFinishing()) stopService(new Intent(this, GrblUsbSerialService.class)); EventBus.getDefault().unregister(this); } @@ -232,6 +233,8 @@ public void onServiceConnected(ComponentName className, IBinder service) { mBound = true; grblUsbSerialService.setMessageHandler(grblServiceMessageHandler); grblUsbSerialService.setStatusUpdatePoolInterval(Long.parseLong(sharedPref.getString(getString(R.string.preference_update_pool_interval), String.valueOf(Constants.GRBL_STATUS_UPDATE_INTERVAL)))); + // After a recreate the port may still be open; show it. + if(grblUsbSerialService.isSerialPortConnected() && getSupportActionBar() != null) getSupportActionBar().setSubtitle(getString(R.string.text_connected)); } @Override diff --git a/app/src/main/java/in/co/gorest/grblcontroller/service/GrblUsbSerialService.java b/app/src/main/java/in/co/gorest/grblcontroller/service/GrblUsbSerialService.java index 7879b0e..16f651a 100644 --- a/app/src/main/java/in/co/gorest/grblcontroller/service/GrblUsbSerialService.java +++ b/app/src/main/java/in/co/gorest/grblcontroller/service/GrblUsbSerialService.java @@ -115,7 +115,7 @@ public class GrblUsbSerialService extends Service { private UsbDeviceConnection connection; private UsbSerialDevice serialPort; - private boolean serialPortConnected; + private volatile boolean serialPortConnected; public static volatile boolean isGrblFound = false; private SerialUsbCommunicationHandler serialUsbCommunicationHandler; @@ -167,6 +167,8 @@ public void onDestroy() { EventBus.getDefault().unregister(this); } + public boolean isSerialPortConnected(){ return serialPortConnected; } + public void serialWriteByte(byte b){ byte[] c = {b}; serialWriteBytes(c);