Fix task switch issues
MainActivity with DataCollector gets sometimes destroyed and then restarted. Things were breaking, because the old running threads were not killed onDestroy!master
							rodzic
							
								
									6c0ecc972f
								
							
						
					
					
						commit
						26fdae0710
					
				| 
						 | 
				
			
			@ -35,6 +35,11 @@ public class MainActivity extends AppCompatActivity {
 | 
			
		|||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void onCreate(Bundle savedInstanceState) {
 | 
			
		||||
        // TODO: Main activity get destroyed on task-switch.
 | 
			
		||||
        //  It probably should be there (but where?), we need further separation of tasks and UI (could help loading times also)
 | 
			
		||||
        //  It should be created in app constructor (notification about running in backgroud?) and non re-crated on UI crate!!!!
 | 
			
		||||
 | 
			
		||||
        // main activy is destroyed and then recreated - kill all other threads on destroy
 | 
			
		||||
        dataCollector = new DataCollector(this);
 | 
			
		||||
 | 
			
		||||
        super.onCreate(savedInstanceState);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -98,22 +98,23 @@ public class DataCollector implements Runnable {
 | 
			
		|||
 | 
			
		||||
                if((++it)%10 == 0 && toGeoPoint(locationProvider.getLastKnownLocation()) == null) {
 | 
			
		||||
                    rootActivity.runOnUiThread(() -> {
 | 
			
		||||
                        System.out.println("rs");
 | 
			
		||||
                        System.out.println((locationProvider.startLocationProvider(null)));
 | 
			
		||||
                        locationProvider.startLocationProvider(null);
 | 
			
		||||
                    });
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        } catch (InterruptedException ignored) {}
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void initCollectors() {
 | 
			
		||||
        System.out.println("INITCOLSRC");
 | 
			
		||||
    private void stopCollectors() {
 | 
			
		||||
        if(rs_col != null)
 | 
			
		||||
            rs_col.stop();
 | 
			
		||||
        if(sh_col != null)
 | 
			
		||||
            sh_col.stop();
 | 
			
		||||
        if(lc_col != null)
 | 
			
		||||
            lc_col.stop();
 | 
			
		||||
    }
 | 
			
		||||
    public void initCollectors() {
 | 
			
		||||
        System.out.println("INITCOLSRC"+lc_col);
 | 
			
		||||
        stopCollectors();
 | 
			
		||||
 | 
			
		||||
        refresh();
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -348,5 +349,6 @@ public class DataCollector implements Runnable {
 | 
			
		|||
        mapUpdater = null;
 | 
			
		||||
        compassUpdater = null;
 | 
			
		||||
        stop = true;
 | 
			
		||||
        stopCollectors();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -89,7 +89,6 @@ public class LocalServerCollector implements Runnable {
 | 
			
		|||
            } catch (InterruptedException ignored) {}
 | 
			
		||||
            boolean ignored = Thread.interrupted();
 | 
			
		||||
        }
 | 
			
		||||
        disable();
 | 
			
		||||
        System.out.println("lcexit");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -223,5 +222,6 @@ public class LocalServerCollector implements Runnable {
 | 
			
		|||
 | 
			
		||||
    public void stop() {
 | 
			
		||||
        stop = true;
 | 
			
		||||
        disable();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -25,7 +25,6 @@ public class MySondyDownloader implements LocalServerDownloader {
 | 
			
		|||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void enable() {
 | 
			
		||||
        disable();
 | 
			
		||||
        // Address and mode should be configured already in passed object
 | 
			
		||||
        bt_runnable = blueAdapter.getRunnable();
 | 
			
		||||
        bt_thread = new Thread(bt_runnable);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -95,6 +95,7 @@ public class BlueAdapter {
 | 
			
		|||
        try {
 | 
			
		||||
            bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(WELL_KNOWN_SERIAL_UUID);
 | 
			
		||||
            device_invalidate = false;
 | 
			
		||||
            System.out.println("BT Device created");
 | 
			
		||||
            return true;
 | 
			
		||||
        } catch (IOException e) {
 | 
			
		||||
            // TODO: Replace with status text, or ignore for now in settings is probably enough
 | 
			
		||||
| 
						 | 
				
			
			@ -105,21 +106,21 @@ public class BlueAdapter {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    @SuppressLint("MissingPermission")
 | 
			
		||||
    public void connectDevice() {
 | 
			
		||||
    public boolean connectDevice() {
 | 
			
		||||
        if (!permissionCheck())
 | 
			
		||||
            return;
 | 
			
		||||
            return false;
 | 
			
		||||
 | 
			
		||||
        failed = true;
 | 
			
		||||
 | 
			
		||||
        if(bluetoothSocket.isConnected())
 | 
			
		||||
            close();
 | 
			
		||||
 | 
			
		||||
        try {
 | 
			
		||||
            bluetoothSocket.connect(); // this fails when device is offline - just silently ignore it
 | 
			
		||||
            // or when there is some error and we need to recreate socket
 | 
			
		||||
            reader = new BufferedReader(new InputStreamReader(bluetoothSocket.getInputStream()));
 | 
			
		||||
            writer = new PrintWriter(new OutputStreamWriter(bluetoothSocket.getOutputStream()));
 | 
			
		||||
            updateFreq();
 | 
			
		||||
        } catch (Exception ignored) {}
 | 
			
		||||
            return true;
 | 
			
		||||
        } catch (Exception e) {e.printStackTrace();}
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String readLine() {
 | 
			
		||||
| 
						 | 
				
			
			@ -189,7 +190,10 @@ public class BlueAdapter {
 | 
			
		|||
                String line = readLine(); // this fails in all cases (device offline, closed transmission error)
 | 
			
		||||
                if (line == null) {
 | 
			
		||||
                    System.out.println("BTTHREAD: Reconnect");
 | 
			
		||||
                    connectDevice();
 | 
			
		||||
                    if(!connectDevice()) {
 | 
			
		||||
                        close();
 | 
			
		||||
                        createDevice();
 | 
			
		||||
                    }
 | 
			
		||||
                    try { Thread.sleep(2000); } catch (InterruptedException ignored) {}
 | 
			
		||||
                } else {
 | 
			
		||||
                    System.out.println("BTHREAD: received " + line);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Ładowanie…
	
		Reference in New Issue