Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oreo Background Service Crash #157

Merged
merged 7 commits into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ ext {

androidVersions = [
minSdkVersion : 14,
targetSdkVersion : 25,
compileSdkVersion: 25,
targetSdkVersion : 26,
compileSdkVersion: 26,
buildToolsVersion: '27.0.2'
]

version = [
mapboxServices : '2.2.6',
gmsLocation : '11.0.4',
junit : '4.12',
supportLibVersion: '25.4.0',
constraintLayout : '1.0.2',
mockito : '2.11.0',
testRunnerVersion: '1.0.1',
okhttp3 : '3.10.0',
gson : '2.8.2',
espressoVersion : '3.0.1'
mapboxServices : '2.2.6',
gmsLocation : '11.0.4',
junit : '4.12',
supportLibVersion : '26.1.0',
constraintLayout : '1.0.2',
mockito : '2.11.0',
testRunnerVersion : '1.0.1',
okhttp3 : '3.10.0',
gson : '2.8.2',
espressoVersion : '3.0.1',
archLifecycleVersion: "1.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be any benefits to bumping arch components version? I think 1.1.1 requires support lib 27+, but maybe we could use 1.1.0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope 1.1.1 is good with supportLib of 26.1.0

]

pluginVersion = [
Expand All @@ -33,6 +34,10 @@ ext {
// play services
gmsLocation : "com.google.android.gms:play-services-location:${version.gmsLocation}",

//architecture components
archLifecycleExtensions: "android.arch.lifecycle:extensions:${version.archLifecycleVersion}",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can save some weight here by using android.arch.lifecycle:runtime:${version.archLifecycleVersion} dependency instead of extensions as we are not using ViewModel or LiveData.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capturing from a chat that it seems like runtime does not contain ProcessLifecycleOwner so we need to stick to extensions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@electrostat any good reason why @LukasPaczos suggestion to cut on weight was not addressed and PR still got merged?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrlee Yes, per the above comment we discussed that ProcessLifecycleOwner' is not included in runtime, so we had to keep extensions`. We tested and discussed over slack the issue and was just captures in the brief above comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could still exclude livedata and viewmodel right? We shouldn't be including unnecessary deps.

archLifecycleCompiler : "android.arch.lifecycle:compiler:${version.archLifecycleVersion}",

// okhttp
okhttp3 : "com.squareup.okhttp3:okhttp:${version.okhttp3}",
okhttp3Mockwebserver : "com.squareup.okhttp3:mockwebserver:${version.okhttp3}",
Expand Down
2 changes: 2 additions & 0 deletions libtelemetry/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ dependencies {
compile dependenciesList.okhttp3
compile dependenciesList.gson
compile dependenciesList.supportAppcompatV7
compile dependenciesList.archLifecycleExtensions
annotationProcessor dependenciesList.archLifecycleCompiler

testCompile dependenciesList.junit
testCompile dependenciesList.mockito
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.mapbox.android.telemetry;

import android.annotation.SuppressLint;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;
import android.content.Context;
import android.content.Intent;

class ApplicationLifecycleObserver implements LifecycleObserver {
private static TelemetryService telemetryService;
private Context applicationContext;
private Intent locationServiceIntent = null;

ApplicationLifecycleObserver(Context applicationContext) {
this.applicationContext = applicationContext;
}

static void setTelemetryService(TelemetryService telemeService) {
telemetryService = telemeService;
}

@SuppressLint("NewApi")
@OnLifecycleEvent(Lifecycle.Event.ON_START)
void onEnterForeground() {
if (telemetryService != null) {
locationServiceIntent = new Intent(applicationContext, TelemetryService.class);
applicationContext.startForegroundService(locationServiceIntent);
}

}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
void onEnterBackground() {
if (telemetryService != null) {
telemetryService.stopForegroundService();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@


import android.app.ActivityManager;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.ProcessLifecycleOwner;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.IBinder;

import com.mapbox.android.core.location.LocationEnginePriority;
Expand Down Expand Up @@ -488,7 +491,14 @@ private PermissionCheckRunnable obtainPermissionCheckRunnable() {
}

private void startLocation() {
applicationContext.startService(obtainLocationServiceIntent());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (ProcessLifecycleOwner.get().getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
applicationContext.startForegroundService(obtainLocationServiceIntent());
initializeLifecycleMonitor();
}
} else {
applicationContext.startService(obtainLocationServiceIntent());
}
}

private void startAlarm() {
Expand Down Expand Up @@ -528,4 +538,9 @@ private boolean unbindServiceConnection() {

return false;
}

private void initializeLifecycleMonitor() {
ApplicationLifecycleObserver applicationLifecycleMonitor = new ApplicationLifecycleObserver(applicationContext);
ProcessLifecycleOwner.get().getLifecycle().addObserver(applicationLifecycleMonitor);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.mapbox.android.telemetry;


import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
Expand Down Expand Up @@ -41,6 +43,10 @@ public void onCreate() {
createLocationReceiver();
createTelemetryReceiver();
createServiceTaskCallbacks();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
initiateForegroundService();
}
}

@Override
Expand Down Expand Up @@ -254,4 +260,16 @@ TelemetryService obtainService() {
return TelemetryService.this;
}
}

private void initiateForegroundService() {
Notification notification = new Notification();
startForeground(1375, notification);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this result in showing a notification to the end user?

If your service is started (running through Context.startService(Intent)), then also make this service run in the foreground, supplying the ongoing notification to be shown to the user while in this state.

also:

Apps targeting API Build.VERSION_CODES.P or later must request the permission Manifest.permission.FOREGROUND_SERVICE in order to use this API.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the above comment, I actually disable the foreground service when the application enters the background, thus removing the notification.

The Android P permission is a new one to me. Haven't looked at the documentation for that yet, but will give it a look. I'm planning on being P compliant much earlier than we are getting to Oreo.


ApplicationLifecycleObserver.setTelemetryService(this);
}

void stopForegroundService() {
stopForeground(true);
stopSelf();
}
}