<commit_before>/*
 * TrackingToggleTileService.kt
 * Implements the TrackingToggleTileService service
 * A TrackingToggleTileService toggles the recording state from a quick settings tile
 *
 * This file is part of
 * TRACKBOOK - Movement Recorder for Android
 *
 * Copyright (c) 2016-20 - Y20K.org
 * Licensed under the MIT-License
 * http://opensource.org/licenses/MIT
 *
 * Trackbook uses osmdroid - OpenStreetMap-Tools for Android
 * https://github.com/osmdroid/osmdroid
 */


package org.y20k.trackbook

import android.content.*
import android.graphics.drawable.Icon
import android.os.IBinder
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import androidx.preference.PreferenceManager
import org.y20k.trackbook.helpers.LogHelper
import org.y20k.trackbook.helpers.PreferencesHelper


/*
 * TrackingToggleTileService class
 */
class TrackingToggleTileService(): TileService() {

    /* Define log tag */
    private val TAG: String = LogHelper.makeLogTag(TrackingToggleTileService::class.java)


    /* Main class variables */
    private var bound: Boolean = false
    private var trackingState: Int = Keys.STATE_TRACKING_NOT
    private lateinit var trackerService: TrackerService


    /* Overrides onTileAdded from TileService */
    override fun onTileAdded() {
        super.onTileAdded()
        // get saved tracking state
        trackingState = PreferencesHelper.loadTrackingState(this)
    }

    /* Overrides onTileRemoved from TileService */
    override fun onTileRemoved() {
        super.onTileRemoved()
    }


    /* Overrides onStartListening from TileService */
    override fun onStartListening() {
        super.onStartListening()
        // tile becomes visible - bind tracker service
        bindService(Intent(this, TrackerService::class.java), connection, Context.BIND_AUTO_CREATE)
    }


    /* Overrides onClick from TileService */
    override fun onClick() {
        super.onClick()
        when (trackingState) {
            Keys.STATE_TRACKING_ACTIVE -> {
                trackerService.stopTracking()
            }
            else -> {
                trackerService.startTracking(newTrack = false)
            }
        }
    }


    /* Overrides onStopListening from TileService */
    override fun onStopListening() {
        super.onStopListening()
        // tile no longer visible - unbind tracker service
        unbindService(connection)<commit_msg>Fixed a crash when tracking was started from quick settings tile<commit_after>/*
 * TrackingToggleTileService.kt
 * Implements the TrackingToggleTileService service
 * A TrackingToggleTileService toggles the recording state from a quick settings tile
 *
 * This file is part of
 * TRACKBOOK - Movement Recorder for Android
 *
 * Copyright (c) 2016-20 - Y20K.org
 * Licensed under the MIT-License
 * http://opensource.org/licenses/MIT
 *
 * Trackbook uses osmdroid - OpenStreetMap-Tools for Android
 * https://github.com/osmdroid/osmdroid
 */


package org.y20k.trackbook

import android.content.*
import android.graphics.drawable.Icon
import android.os.Build
import android.os.IBinder
import android.service.quicksettings.Tile
import android.service.quicksettings.TileService
import androidx.preference.PreferenceManager
import org.y20k.trackbook.helpers.LogHelper
import org.y20k.trackbook.helpers.PreferencesHelper


/*
 * TrackingToggleTileService class
 */
class TrackingToggleTileService(): TileService() {

    /* Define log tag */
    private val TAG: String = LogHelper.makeLogTag(TrackingToggleTileService::class.java)


    /* Main class variables */
    private var bound: Boolean = false
    private var trackingState: Int = Keys.STATE_TRACKING_NOT
    private lateinit var trackerService: TrackerService


    /* Overrides onTileAdded from TileService */
    override fun onTileAdded() {
        super.onTileAdded()
        // get saved tracking state
        trackingState = PreferencesHelper.loadTrackingState(this)
    }

    /* Overrides onTileRemoved from TileService */
    override fun onTileRemoved() {
        super.onTileRemoved()
    }


    /* Overrides onStartListening from TileService */
    override fun onStartListening() {
        super.onStartListening()
        // tile becomes visible - bind tracker service
        bindService(Intent(this, TrackerService::class.java), connection, Context.BIND_AUTO_CREATE)
    }


    /* Overrides onClick from TileService */
    override fun onClick() {
        super.onClick()
        when (trackingState) {
            Keys.STATE_TRACKING_ACTIVE -> {
                trackerService.stopTracking()
            }
            else -> {
                // start service via intent so that it keeps running after unbind
                startTrackerService()
                trackerService.startTracking(newTrack = false)
            }
        }
    }


    /* Start tracker service */
    private fun startTrackerService() {
        val intent = Intent(application, TrackerService::class.java)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // ... start service in foreground to prevent it being killed on Oreo
            application.startForegroundService(intent)
        } else {
            application.startService(intent)
        }
    }


    /* Overrides onStopListening from TileService */
    override fun onStopListening() {
        super.onStopListening()
        // tile no longer visible - unbind tracker service
        unbindService(connection)
{'fraction_non_alphanumeric': 0.08083014746040415, 'fraction_numerical': 0.005097396686692153, 'mean_word_length': 2.899219304471256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2652914', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1460, 'n_words': 376}