diff --git a/app/build.gradle b/app/build.gradle index c9988ff..e584e45 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -193,8 +193,6 @@ dependencies { implementation 'com.squareup.okhttp3:okhttp:3.12.1' // https://square.github.io/okio/#releases implementation 'com.squareup.okio:okio:2.5.0' - // https://github.com/google/conscrypt/ -// implementation 'org.conscrypt:conscrypt-android:2.4.0' // https://github.com/deano2390/MaterialShowcaseView implementation 'com.github.deano2390:MaterialShowcaseView:1.3.4' // https://github.com/PhilJay/MPAndroidChart diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 50c40d1..6c647d4 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -39,8 +39,6 @@ android:name=".activities.PrivacyTermsActivity" /> - diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/settings/ActivityCheckbox.kt b/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/settings/ActivityCheckbox.kt index b2c87ee..ec267bb 100644 --- a/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/settings/ActivityCheckbox.kt +++ b/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/settings/ActivityCheckbox.kt @@ -33,6 +33,7 @@ import com.javinator9889.handwashingreminder.utils.isAtLeast import com.mikepenz.iconics.IconicsDrawable import com.mikepenz.iconics.typeface.library.ionicons.Ionicons import com.mikepenz.iconics.utils.sizeDp +import timber.log.Timber class ActivityCheckbox : CheckBoxPreference { constructor(context: Context) : super(context) @@ -91,8 +92,10 @@ class ActivityCheckbox : CheckBoxPreference { } with(HandwashingApplication.instance) { if (checked) { + Timber.d("Activity is checked so starting tracking") activityHandler.startTrackingActivity() } else { + Timber.d("Activity is not checked so disable tracking") activityHandler.disableActivityTracker() } } diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/settings/ActivityMultiSelectList.kt b/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/settings/ActivityMultiSelectList.kt deleted file mode 100644 index d0c67c5..0000000 --- a/app/src/main/java/com/javinator9889/handwashingreminder/activities/views/fragments/settings/ActivityMultiSelectList.kt +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright © 2020 - present | Handwashing reminder by Javinator9889 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see https://www.gnu.org/licenses/. - * - * Created by Javinator9889 on 16/04/20 - Handwashing reminder. - */ -package com.javinator9889.handwashingreminder.activities.views.fragments.settings - -import android.content.Context -import android.util.AttributeSet -import androidx.preference.MultiSelectListPreference -import com.javinator9889.handwashingreminder.R -import com.javinator9889.handwashingreminder.gms.activity.ActivityHandler -import com.mikepenz.iconics.IconicsDrawable -import com.mikepenz.iconics.typeface.library.ionicons.Ionicons -import com.mikepenz.iconics.utils.sizeDp -import java.util.* - -class ActivityMultiSelectList : MultiSelectListPreference { - private var isFirstCall = true - constructor(context: Context) : super(context) - constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) - constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : - super(context, attrs, defStyleAttr) - - constructor( - context: Context, attrs: AttributeSet?, defStyleAttr: Int, - defStyleRes: Int - ) : super(context, attrs, defStyleAttr, defStyleRes) - - init { - icon = IconicsDrawable(context, Ionicons.Icon.ion_android_alert).apply { - sizeDp = 20 - } - } - - override fun notifyChanged() { - super.notifyChanged() - if (!isFirstCall) { - loadSummary() - reloadActivityHandler() - } - } - - override fun onSetInitialValue(defaultValue: Any?) { - super.onSetInitialValue(defaultValue) - isFirstCall = false - loadSummary() - } - - private fun loadSummary() { - val builder = StringBuilder() - var isFirstItem = true - var allDisabled = true - selectedItems.forEachIndexed { i, enabled -> - if (enabled) { - allDisabled = false - if (!isFirstItem) - builder.append(", ") - else - isFirstItem = false - builder.append(entries[i].toString().toLowerCase(Locale.ROOT)) - } - } - builder.append('.') - summary = if (allDisabled) - context.getText(R.string.activities_disabled) - else - "${context.getString(R.string.activities_info)} $builder" - } - - private fun reloadActivityHandler() { - with(ActivityHandler.getInstance(context)) { - reload() - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityHandler.kt b/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityHandler.kt index 1249f41..5420415 100644 --- a/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityHandler.kt +++ b/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityHandler.kt @@ -22,49 +22,57 @@ import android.app.PendingIntent import android.app.PendingIntent.FLAG_UPDATE_CURRENT import android.content.Context import android.content.Intent -import android.content.IntentFilter -import androidx.localbroadcastmanager.content.LocalBroadcastManager -import androidx.preference.PreferenceManager import com.google.android.gms.location.ActivityRecognition import com.google.android.gms.location.ActivityTransition +import com.google.android.gms.location.ActivityTransition.ACTIVITY_TRANSITION_EXIT import com.google.android.gms.location.ActivityTransitionRequest +import com.google.android.gms.location.DetectedActivity import com.google.android.gms.tasks.Task import com.javinator9889.handwashingreminder.BuildConfig -import com.javinator9889.handwashingreminder.utils.Preferences import timber.log.Timber internal const val ACTIVITY_REQUEST_CODE = 64 internal const val TRANSITIONS_RECEIVER_ACTION = "${BuildConfig.APPLICATION_ID}/TRANSITIONS_RECEIVER_ACTION" +internal val TRANSITIONS = listOf( + ActivityTransition.Builder() + .setActivityType(DetectedActivity.IN_VEHICLE) + .setActivityTransition(ACTIVITY_TRANSITION_EXIT) + .build(), + ActivityTransition.Builder() + .setActivityType(DetectedActivity.ON_BICYCLE) + .setActivityTransition(ACTIVITY_TRANSITION_EXIT) + .build(), + ActivityTransition.Builder() + .setActivityType(DetectedActivity.RUNNING) + .setActivityTransition(ACTIVITY_TRANSITION_EXIT) + .build(), + ActivityTransition.Builder() + .setActivityType(DetectedActivity.WALKING) + .setActivityTransition(ACTIVITY_TRANSITION_EXIT) + .build() +) class ActivityHandler private constructor(private val context: Context) { - private val transitions: MutableList = mutableListOf() - private var pendingIntent: PendingIntent + private var pendingIntent: PendingIntent = createPendingIntent() private var activityRegistered = false - private val transitionsReceiver = ActivityReceiver() - - init { - val activitiesSet = createSetOfTransitions() - addTransitions(activitiesSet, transitions) - registerActivityReceiver() - pendingIntent = createPendingIntent() - } companion object { private var instance: ActivityHandler? = null fun getInstance(context: Context): ActivityHandler { instance?.let { return it } - instance = ActivityHandler(context) - return instance!! + synchronized(this) { + val instance = ActivityHandler(context.applicationContext) + this.instance = instance + return instance + } } } fun startTrackingActivity() { - if (transitions.size == 0) - return Timber.d("Starting activity recognition") - with(ActivityTransitionRequest(transitions)) { + with(ActivityTransitionRequest(TRANSITIONS)) { ActivityRecognition.getClient(context) .requestActivityTransitionUpdates(this, pendingIntent).apply { addOnSuccessListener { activityRegistered = true } @@ -86,46 +94,9 @@ class ActivityHandler private constructor(private val context: Context) { } } - fun reload() = with(createSetOfTransitions()) { - transitions.clear() - addTransitions(this, transitions) - Timber.d("Reloading activity recognition - transitions: $transitions") - disableActivityTracker()?.let { - it.addOnCompleteListener { - pendingIntent = createPendingIntent() - startTrackingActivity() - } - } - } - - private fun createSetOfTransitions(): Set { - val preferences = PreferenceManager.getDefaultSharedPreferences(context) - with(mutableSetOf()) { - preferences.getStringSet( - Preferences.ACTIVITIES_ENABLED, - Preferences.DEFAULT_ACTIVITY_SET - )!!.run { - forEach { this@with += Integer.parseInt(it) } - } - return this - } - } - - private fun addTransitions( - activitiesSet: Set, - transitions: MutableList, - activityTransition: Int = ActivityTransition.ACTIVITY_TRANSITION_EXIT - ) { - for (activity in activitiesSet) { - transitions += ActivityTransition.Builder() - .setActivityType(activity) - .setActivityTransition(activityTransition) - .build() - } - } - private fun createPendingIntent(): PendingIntent = with(Intent(TRANSITIONS_RECEIVER_ACTION)) { + setClass(context, ActivityReceiver::class.java) PendingIntent.getBroadcast( context, ACTIVITY_REQUEST_CODE, @@ -133,9 +104,4 @@ class ActivityHandler private constructor(private val context: Context) { FLAG_UPDATE_CURRENT ) } - - private fun registerActivityReceiver() = - LocalBroadcastManager.getInstance(context).registerReceiver( - transitionsReceiver, IntentFilter(TRANSITIONS_RECEIVER_ACTION) - ) } \ No newline at end of file diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityReceiver.kt b/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityReceiver.kt index 31450aa..7ff9776 100644 --- a/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityReceiver.kt +++ b/app/src/main/java/com/javinator9889/handwashingreminder/gms/activity/ActivityReceiver.kt @@ -111,8 +111,7 @@ class ActivityReceiver : BroadcastReceiver() { val emojiCompat = emojiLoader.await() title = emojiCompat.process(title) content = emojiCompat.process(content) - } catch (_: IllegalStateException) { - } + } catch (_: IllegalStateException) { } withContext(Dispatchers.Main) { notificationsHandler.createNotification( iconDrawable = R.drawable.ic_stat_handwashing, diff --git a/app/src/main/java/com/javinator9889/handwashingreminder/jobs/BootCompletedJob.kt b/app/src/main/java/com/javinator9889/handwashingreminder/jobs/BootCompletedJob.kt index 326a904..b0ecbe3 100644 --- a/app/src/main/java/com/javinator9889/handwashingreminder/jobs/BootCompletedJob.kt +++ b/app/src/main/java/com/javinator9889/handwashingreminder/jobs/BootCompletedJob.kt @@ -36,10 +36,13 @@ class BootCompletedJob : BroadcastReceiver() { Preferences.ACTIVITY_TRACKING_ENABLED, false ) - ) + ) { + Timber.d("Device rebooted so starting activity as it's enabled") activityHandler.startTrackingActivity() - else + } else { + Timber.d("Device rebooted but not starting activity as it isn't enabled") activityHandler.disableActivityTracker() + } Timber.d("Enqueuing notifications as the device has rebooted") with(AlarmHandler(context)) { scheduleAllAlarms() diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 2d52dce..71cb93a 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -154,7 +154,7 @@ No recibirás notificaciones de ninguna actividad Recibirás notificaciones de las - actividades seleccionadas debajo + actividades después de pasear, ir en bici, bajarte de un vehículo o correr Habilitar reconocimiento de la actividad ¿Cuándo recibirás notificaciones? diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d986c7f..e702c2e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -156,8 +156,8 @@ the access to your activity. Enable it from settings You will not receive any activity notifications - You will receive notifications for - the activities selected below + You will receive notifications after having + a walk, going cycling, leaving a vehicle or running Enable activity recognition When you will receive notifications? diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 7d72fd0..6d6758a 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -57,14 +57,6 @@ android:key="activity:gms:tracking" android:title="@string/activity_recognition" app:summary="For enabling the activity recognition, you need both Google Play Services and permissions" /> - -