Commit 72d32f44 authored by Bartek Fabiszewski's avatar Bartek Fabiszewski
Browse files

Fix autostart, closes #48

- crash when track name not set
- backround service denied on Android 8.0 and newer
parent b0f63654
Loading
Loading
Loading
Loading
+8 −1
Original line number Diff line number Diff line
@@ -14,6 +14,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.ContextCompat;

/**
 * Receiver for boot completed broadcast
@@ -34,8 +35,14 @@ public class BootCompletedReceiver extends BroadcastReceiver {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        boolean autoStart = prefs.getBoolean("prefAutoStart", false);
        if (autoStart && Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            DbAccess db = DbAccess.getInstance();
            db.open(context);
            if (db.getTrackName() == null) {
                db.newAutoTrack();
            }
            db.close();
            Intent i = new Intent(context, LoggerService.class);
            context.startService(i);
            ContextCompat.startForegroundService(context, i);
        }
    }
}
+23 −0
Original line number Diff line number Diff line
@@ -15,10 +15,12 @@ import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.support.annotation.NonNull;
import android.util.Log;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

@@ -288,6 +290,19 @@ class DbAccess {
        return trackName;
    }

    /**
     * Get auto-generated track name (Auto_[yyyy.MM.dd_HH.mm.ss]).
     *
     * @return Track name
     */
    @NonNull
    static String getAutoTrackName() {
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss", Locale.getDefault());
        sdf.setTimeZone(TimeZone.getDefault());
        final String dateSuffix = sdf.format(Calendar.getInstance().getTime());
        return "Auto_" + dateSuffix;
    }

    /**
     * Update current track, set id.
     *
@@ -315,6 +330,14 @@ class DbAccess {
        db.insert(DbContract.Track.TABLE_NAME, null, values);
    }

    /**
     * Start new track with auto-generated name.
     * @see DbAccess#newTrack(String)
     */
    void newAutoTrack() {
        newTrack(getAutoTrackName());
    }

    /**
     * Get track summary
     *
+5 −5
Original line number Diff line number Diff line
@@ -136,14 +136,14 @@ public class LoggerService extends Service {
        final boolean prefsUpdated = (intent != null) && intent.getBooleanExtra(MainActivity.UPDATED_PREFS, false);
        if (prefsUpdated) {
            handlePrefsUpdated();
        } else if (isRunning) {
            // first start
        } else {
            final Notification notification = showNotification(NOTIFICATION_ID);
            startForeground(NOTIFICATION_ID, notification);
        } else {
            if (!isRunning) {
                // onCreate failed to start updates
                stopSelf();
            }
        }

        return START_STICKY;
    }
+1 −5
Original line number Diff line number Diff line
@@ -486,11 +486,7 @@ public class MainActivity extends AppCompatActivity {
        if (editText == null) {
            return;
        }
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss", Locale.getDefault());
        sdf.setTimeZone(TimeZone.getDefault());
        final String dateSuffix = sdf.format(Calendar.getInstance().getTime());
        final String autoName = "Auto_" + dateSuffix;
        editText.setText(autoName);
        editText.setText(DbAccess.getAutoTrackName());
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {