Skip to content

Commit

Permalink
Attempt to detect WorkManager exception on init
Browse files Browse the repository at this point in the history
Bump workmanager to 2.6.0.

Not every exception gets caught by the initialization exception handler,
but a lot could be. This will hopefully detect this and raise an alert
to the user that things have gone a bit wonky.

Fixes #1064
  • Loading branch information
growse committed Oct 28, 2021
1 parent c89a249 commit baebe09
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion project/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ dependencies {
implementation("com.takisoft.preferencex:preferencex:1.1.0")
implementation("com.google.android.material:material:1.3.0")

implementation("androidx.work:work-runtime:2.5.0")
implementation("androidx.work:work-runtime:2.6.0")
implementation("androidx.fragment:fragment-ktx:1.3.5")
implementation("androidx.core:core-ktx:1.5.0")
implementation("androidx.test.espresso:espresso-idling-resource:${espressoVersion}")
Expand Down
10 changes: 5 additions & 5 deletions project/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@

<!-- Disables default initialization of WorkManager because custom initializer is used in App.onCreate()-->
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:enabled="false"
android:exported="false"
tools:replace="android:authorities" />
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
tools:node="remove">
</provider>


<!-- Maps SDK requires this as of API v28 -->
<uses-library
Expand Down
13 changes: 12 additions & 1 deletion project/app/src/main/java/org/owntracks/android/App.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.owntracks.android

import android.annotation.SuppressLint
import android.app.Application
import android.app.Notification
import android.app.NotificationChannel
Expand All @@ -9,6 +10,7 @@ import android.os.StrictMode
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.app.NotificationManagerCompat
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.MutableLiveData
import androidx.work.Configuration
import androidx.work.WorkManager
import androidx.work.WorkerFactory
Expand Down Expand Up @@ -50,6 +52,9 @@ class App : Application() {
@Inject
lateinit var bindingComponentProvider: Provider<CustomBindingComponentBuilder>

val workManagerFailedToInitialize = MutableLiveData(false)

@SuppressLint("RestrictedApi")
override fun onCreate() {
// Make sure we use Conscrypt for advanced TLS features on all devices.
// X509ExtendedTrustManager not available pre-24, fall back to device. https://github.com/google/conscrypt/issues/603
Expand All @@ -74,7 +79,13 @@ class App : Application() {

WorkManager.initialize(
this,
Configuration.Builder().setWorkerFactory(workerFactory).build()
Configuration.Builder()
.setWorkerFactory(workerFactory)
.setInitializationExceptionHandler { throwable ->
Timber.e(throwable, "Exception thrown when initializing WorkManager")
workManagerFailedToInitialize.postValue(true)
}
.build()
)
scheduler.cancelAllTasks()
Timber.plant(TimberInMemoryLogTree(BuildConfig.DEBUG))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.hardware.SensorManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS
import android.view.Gravity
import android.view.Menu
import android.view.MenuItem
Expand All @@ -28,12 +29,14 @@ import androidx.test.espresso.IdlingResource
import androidx.test.espresso.idling.CountingIdlingResource
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.greenrobot.eventbus.EventBus
import org.owntracks.android.App
import org.owntracks.android.BuildConfig.FLAVOR
import org.owntracks.android.R
import org.owntracks.android.data.repos.LocationRepo
Expand Down Expand Up @@ -195,6 +198,26 @@ class MapActivity : BaseActivity<UiMapBinding?, MapMvvm.ViewModel<MapMvvm.View?>
// We've been started in the foreground, so cancel the background restriction notification
NotificationManagerCompat.from(this)
.cancel(BACKGROUND_LOCATION_RESTRICTION_NOTIFICATION_TAG, 0)

(applicationContext as App).workManagerFailedToInitialize.observe(this, { value ->
if (value) {
MaterialAlertDialogBuilder(this)
.setIcon(R.drawable.ic_baseline_warning_24)
.setTitle(getString(R.string.workmanagerInitializationErrorDialogTitle))
.setMessage(getString(R.string.workmanagerInitializationErrorDialogMessage))
.setNeutralButton(getString(R.string.workmanagerInitializationErrorDialogOpenSettingsLabel)) { _, _ ->
startActivity(
Intent(
ACTION_APPLICATION_DETAILS_SETTINGS
).apply {
data = Uri.fromParts("package", packageName, "")
}
)
}
.setPositiveButton(android.R.string.ok, null)
.show()
}
})
}
}

Expand Down
10 changes: 10 additions & 0 deletions project/app/src/main/res/drawable/ic_baseline_warning_24.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:tint="#FF3333"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@android:color/white"
android:pathData="M1,21h22L12,2 1,21zM13,18h-2v-2h2v2zM13,14h-2v-4h2v4z" />
</vector>
3 changes: 3 additions & 0 deletions project/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@
<string name="preferencesNotificationGeocoderErrors">Geocoder errors</string>
<string name="preferencesNotificationGeocoderErrorsSummary">Display geocoder errors, rate limits &amp; quota exceeded events </string>
<string name="preferencesAdvancedAutostartWarning">This version of Android restricts OwnTracks from receiving locations when automatically started on device boot until the app is opened.</string>
<string name="workmanagerInitializationErrorDialogTitle">Initialization error</string>
<string name="workmanagerInitializationErrorDialogOpenSettingsLabel">Open Application Settings</string>
<string name="workmanagerInitializationErrorDialogMessage">Some components failed to initialize properly, so OwnTracks may not work properly. It may be necessary to re-install the application or clear the user data.\n\nPlease check the logs for more details.</string>
<string-array name="geocoders">
<item>None</item>
<item>Device (Google)</item>
Expand Down

0 comments on commit baebe09

Please sign in to comment.