Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions app/src/main/java/in/co/gorest/grblcontroller/GrblActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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){
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Loading