Commit 5e03931e authored by Bartek Fabiszewski's avatar Bartek Fabiszewski
Browse files

Allow simultaneous tasks

parent a65ae3b4
Loading
Loading
Loading
Loading
+40 −12
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.location.Location;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
@@ -152,39 +153,51 @@ public class WaypointFragment extends Fragment implements LoggerTask.LoggerTaskC
     * Start logger task
     */
    private void runLoggerTask() {
        if (loggerTask == null || loggerTask.getStatus() != LoggerTask.Status.RUNNING) {
        if (!isTaskRunning(loggerTask)) {
            saveButton.setEnabled(false);
            location = null;
            locationTextView.setText("");
            locationDetailsTextView.setText("");
            loggerTask = new LoggerTask(this);
            loggerTask.execute();
            loggerTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            setRefreshing(true);
        }
    }

    /**
     * Verify if task is running
     * @param task Task
     * @param <T> Task type
     * @return True if running, false otherwise
     */
    private <T extends AsyncTask> boolean isTaskRunning(T task) {
        return task != null && task.getStatus() == T.Status.RUNNING;
    }

    /**
     * Stop logger task
     */
    private void cancelLoggerTask() {
        if (Logger.DEBUG) { Log.d(TAG, "[cancelLoggerTask]"); }
        if (loggerTask != null && loggerTask.getStatus() == LoggerTask.Status.RUNNING) {
        if (isTaskRunning(loggerTask)) {
            if (Logger.DEBUG) { Log.d(TAG, "[cancelLoggerTask effective]"); }
            loggerTask.cancel(false);
            loggerTask = null;
            if (!isTaskRunning(imageTask)) {
                setRefreshing(false);
            }
        }
    }

    /**
     * Start image task
     */
    private void runImageTask(@NonNull Uri uri) {
        if (imageTask == null || imageTask.getStatus() != ImageTask.Status.RUNNING) {
        if (!isTaskRunning(imageTask)) {
            clearImage();
            saveButton.setEnabled(false);
            imageTask = new ImageTask(this);
            imageTask.execute(uri);
            imageTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, uri);
            setRefreshing(true);
        }
    }
@@ -380,8 +393,11 @@ public class WaypointFragment extends Fragment implements LoggerTask.LoggerTaskC

    @Override
    public void onLoggerTaskCompleted(Location location) {
        if (Logger.DEBUG) { Log.d(TAG, "[onLoggerTaskCompleted: " + location + "]"); }
        this.location = location;
        if (!isTaskRunning(imageTask)) {
            setRefreshing(false);
        }
        setLocationText();
        saveButton.setEnabled(true);
    }
@@ -389,7 +405,9 @@ public class WaypointFragment extends Fragment implements LoggerTask.LoggerTaskC
    @Override
    public void onLoggerTaskFailure(int reason) {
        if (Logger.DEBUG) { Log.d(TAG, "[onLoggerTaskFailure: " + reason + "]"); }
        if (!isTaskRunning(imageTask)) {
            setRefreshing(false);
        }
        locationTextView.setText(getString(R.string.logger_task_failure));
        if ((reason & LoggerTask.E_PERMISSION) != 0) {
            showToast(getString(R.string.location_permission_denied));
@@ -401,20 +419,30 @@ public class WaypointFragment extends Fragment implements LoggerTask.LoggerTaskC

    @Override
    public void onImageTaskCompleted(@NonNull Uri uri, @NonNull Bitmap thumbnail) {
        if (Logger.DEBUG) { Log.d(TAG, "[onImageTaskCompleted: " + uri + "]"); }
        photoUri = uri;
        photoThumb = thumbnail;
        setThumbnail(thumbnail);
        if (!isTaskRunning(loggerTask)) {
            setRefreshing(false);
        }
        if (this.location != null) {
            saveButton.setEnabled(true);
        }
    }

    @Override
    public void onImageTaskFailure(@NonNull String error) {
        if (Logger.DEBUG) { Log.d(TAG, "[onImageTaskFailure: " + error + "]"); }
        clearImage();
        if (!isTaskRunning(loggerTask)) {
            setRefreshing(false);
        }
        showToast(error);
        if (this.location != null) {
            saveButton.setEnabled(true);
        }
    }

    private void clearImage() {
        if (photoUri != null) {