Skip to content

Commit

Permalink
[telemetry] Remove WAKE_LOCK permission
Browse files Browse the repository at this point in the history
We no longer want to have to specify the WAKE_LOCK permission, so we need to replace JobIntentService with Worker and WorkManager, which doesn't have this dependency.
  • Loading branch information
zach2good committed Jul 7, 2022
1 parent cf8a35e commit ff8c0b0
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 53 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
implementation dependenciesList.supportAppcompat
implementation dependenciesList.supportConstraintLayout
implementation dependenciesList.gmsLocation
implementation dependenciesList.workManager

debugImplementation dependenciesList.leakCanary
releaseImplementation dependenciesList.leakCanaryNoOp
Expand Down
4 changes: 3 additions & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ ext {
espressoVersion : '3.1.0',
archLifecycleVersion: "1.1.0",
assertJ : "3.11.1",
leakCanaryVersion : "1.6.3"
leakCanaryVersion : "1.6.3",
workManagerVersion : "2.6.0",
]

pluginVersion = [
Expand Down Expand Up @@ -58,6 +59,7 @@ ext {
supportAppcompat : "androidx.appcompat:appcompat:${version.androidXVersion}",
supportConstraintLayout: "androidx.constraintlayout:constraintlayout:${version.constraintLayout}",
supportCoreUtils : "androidx.legacy:legacy-support-core-utils:${version.androidXVersion}",
workManager : "androidx.work:work-runtime:${version.workManagerVersion}",

// instrumentation test
testRunner : "androidx.test.ext:junit:${version.testRunnerVersion}",
Expand Down
1 change: 1 addition & 0 deletions libtelemetry/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ android {

dependencies {
implementation dependenciesList.androidCore
implementation dependenciesList.workManager
okhttp3Implementation dependenciesList.okhttp3
okhttp4Implementation dependenciesList.okhttp4
implementation dependenciesList.gson
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.mapbox.android.telemetry.errors;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.work.ExistingWorkPolicy;
import androidx.work.WorkManager;

import org.junit.Test;

import com.mapbox.android.telemetry.MapboxTelemetryConstants;

public class ErrorReporterWorkerInstrumentationTest {

@Test
public void enqueueWork() {
WorkManager.getInstance(InstrumentationRegistry.getInstrumentation().getTargetContext())
.enqueueUniqueWork(
MapboxTelemetryConstants.ACTION_TOKEN_CHANGED,
ExistingWorkPolicy.KEEP,
ErrorReporterWorker.createWorkRequest());
// TODO: verify work is executed
}
}
4 changes: 0 additions & 4 deletions libtelemetry/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mapbox.android.telemetry">

<!--Required for CrashReporterJobIntentService on API levels below 25-->
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
Expand All @@ -15,8 +13,6 @@
android:authorities="${applicationId}.mapboxtelemetryinitprovider"
android:exported="false"
android:initOrder="100"/>
<service android:name=".errors.ErrorReporterJobIntentService"
android:permission="android.permission.BIND_JOB_SERVICE" android:exported="false"/>

<service android:name=".MapboxTelemetryService" android:exported="false"/>

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.mapbox.android.telemetry.errors;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import androidx.work.Constraints;
import androidx.work.NetworkType;
import androidx.work.OneTimeWorkRequest;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

/**
* This is a background job that sends error events to the telemetry endpoint
* at startup.
*/
public final class ErrorReporterWorker extends Worker {
private static final String LOG_TAG = "ErrorReportWorker";

public ErrorReporterWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}

@NonNull
@Override
public Result doWork() {
Log.d(LOG_TAG, "doWork");
try {
ErrorReporterEngine.sendReports(getApplicationContext());
} catch (Throwable throwable) {
// TODO: log silent crash
Log.e(LOG_TAG, throwable.toString());
}
return Result.success();
}

public static OneTimeWorkRequest createWorkRequest() {
return new OneTimeWorkRequest.Builder(ErrorReporterWorker.class)
.setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import android.content.IntentFilter;
import androidx.annotation.NonNull;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.work.ExistingWorkPolicy;
import androidx.work.WorkManager;
import android.util.Log;

import com.mapbox.android.telemetry.MapboxTelemetryConstants;

/**
Expand All @@ -31,7 +34,11 @@ public static void register(@NonNull Context context) {
public void onReceive(Context context, Intent intent) {
try {
// Start background job
ErrorReporterJobIntentService.enqueueWork(context);
WorkManager.getInstance(context)
.enqueueUniqueWork(
MapboxTelemetryConstants.ACTION_TOKEN_CHANGED,
ExistingWorkPolicy.KEEP,
ErrorReporterWorker.createWorkRequest());
// Unregister receiver - we need it once at startup
LocalBroadcastManager.getInstance(context).unregisterReceiver(this);
} catch (Throwable throwable) {
Expand Down

0 comments on commit ff8c0b0

Please sign in to comment.