Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions sentry-android-core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
android:name=".SentryInitProvider"
android:authorities="${applicationId}.SentryInitProvider"
android:exported="false"/>

<service android:name=".SentryService"
android:exported="false" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ static void init(
options.setSerializer(new AndroidSerializer(options.getLogger(), envelopeReader));

options.setTransportGate(new AndroidTransportGate(context, options.getLogger()));

try {
context.startService(SentryService.getIntent(context));
} catch (IllegalStateException e) {
// maybe we should make Throwable instead of to be safe than sorry
// services have different behaviours across OS versioms eg foreground etc
options.getLogger().log(SentryLevel.ERROR, e, "SentryService can't be started.");
}
}

private static void installDefaultIntegrations(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.sentry.android.core;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import androidx.annotation.NonNull;
import io.sentry.core.HubAdapter;
import io.sentry.core.hints.DiskFlushNotification;
import io.sentry.core.hints.Flushable;
import io.sentry.core.hints.SessionEnd;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

/**
* This is a best effort to end a session during onTaskRemoved (user swiped up the App. aka killed
* it. It needs to be public.
*/
@ApiStatus.Internal
public final class SentryService extends Service {

@Override
public @Nullable IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
// START_NOT_STICKY, we don't want to recreate the service nor the AppContext.
return START_NOT_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
// TODO: get logger and flush timeout from options thru onStartCommand.intent
final TaskRemovedHint hint = new TaskRemovedHint(15000);
HubAdapter.getInstance().endSession(hint);

hint.waitFlush();

super.onTaskRemoved(rootIntent);
}

@NonNull
public static Intent getIntent(Context context) {
return new Intent(context, SentryService.class);
}

private static final class TaskRemovedHint
implements DiskFlushNotification, Flushable, SessionEnd {

private final CountDownLatch latch;
private final long flushTimeoutMillis;

TaskRemovedHint(final long flushTimeoutMillis) {
this.flushTimeoutMillis = flushTimeoutMillis;
latch = new CountDownLatch(1);
}

@Override
public boolean waitFlush() {
try {
return latch.await(flushTimeoutMillis, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return false;
}

@Override
public void markFlushed() {
latch.countDown();
}
}
}
4 changes: 2 additions & 2 deletions sentry-core/src/main/java/io/sentry/core/Hub.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void startSession() {
}

@Override
public void endSession() {
public void endSession(final @Nullable Object hint) {
if (!isEnabled()) {
options
.getLogger()
Expand All @@ -228,7 +228,7 @@ public void endSession() {
if (item != null) {
final Session previousSession = item.scope.endSession();
if (previousSession != null) {
item.client.captureSession(previousSession, new SessionEndHint());
item.client.captureSession(previousSession, hint);
}
} else {
options.getLogger().log(SentryLevel.FATAL, "Stack peek was null when endSession");
Expand Down
4 changes: 2 additions & 2 deletions sentry-core/src/main/java/io/sentry/core/HubAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public void startSession() {
}

@Override
public void endSession() {
Sentry.endSession();
public void endSession(@Nullable Object hint) {
Sentry.getCurrentHub().endSession(hint);
}

@Override
Expand Down
7 changes: 6 additions & 1 deletion sentry-core/src/main/java/io/sentry/core/IHub.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry.core;

import io.sentry.core.hints.SessionEndHint;
import io.sentry.core.protocol.SentryId;
import io.sentry.core.protocol.User;
import java.util.List;
Expand Down Expand Up @@ -96,7 +97,11 @@ default SentryId captureException(Throwable throwable) {
void startSession();

/** Ends the current session */
void endSession();
default void endSession() {
endSession(new SessionEndHint());
}

void endSession(@Nullable Object hint);

/** Flushes out the queue for up to timeout seconds and disable the Hub. */
void close();
Expand Down
2 changes: 1 addition & 1 deletion sentry-core/src/main/java/io/sentry/core/NoOpHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public SentryId captureException(Throwable throwable, @Nullable Object hint) {
public void startSession() {}

@Override
public void endSession() {}
public void endSession(@Nullable Object hint) {}

@Override
public void close() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ public void run() {

sessionCache.store(envelope, hint);

if (hint instanceof DiskFlushNotification) {
((DiskFlushNotification) hint).markFlushed();
options.getLogger().log(SentryLevel.DEBUG, "Disk flush envelope fired.");
}

// we only flush a session update to the disk, but not to the network
if (hint instanceof SessionUpdate) {
options
Expand Down