Skip to content

Commit f25fb8b

Browse files
committed
Reuse sentry-shake handler thread
1 parent fb1120c commit f25fb8b

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

sentry-android-core/src/main/java/io/sentry/android/core/FeedbackShakeIntegration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions
6060
@Override
6161
public void close() throws IOException {
6262
application.unregisterActivityLifecycleCallbacks(this);
63-
stopShakeDetection();
63+
shakeDetector.close();
6464
// Restore onFormClose if a dialog is still showing, since lifecycle callbacks
6565
// are now unregistered and onActivityDestroyed cleanup won't fire.
6666
if (isDialogShowing) {

sentry-android-core/src/main/java/io/sentry/android/core/SentryShakeDetector.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public final class SentryShakeDetector implements SensorEventListener {
3838
private @Nullable SensorManager sensorManager;
3939
private @Nullable Sensor accelerometer;
4040
private @Nullable HandlerThread handlerThread;
41+
private @Nullable Handler handler;
4142
private final @NotNull AtomicLong lastShakeTimestamp = new AtomicLong(0);
4243
private volatile @Nullable Listener listener;
4344
private @NotNull ILogger logger;
@@ -69,6 +70,11 @@ private void init(final @NotNull Context context) {
6970
if (sensorManager != null && accelerometer == null) {
7071
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER, false);
7172
}
73+
if (accelerometer != null && handlerThread == null) {
74+
handlerThread = new HandlerThread("sentry-shake");
75+
handlerThread.start();
76+
handler = new Handler(handlerThread.getLooper());
77+
}
7278
}
7379

7480
public void start(final @NotNull Context context, final @NotNull Listener shakeListener) {
@@ -83,9 +89,6 @@ public void start(final @NotNull Context context, final @NotNull Listener shakeL
8389
SentryLevel.WARNING, "Accelerometer sensor not available. Shake detection disabled.");
8490
return;
8591
}
86-
handlerThread = new HandlerThread("sentry-shake");
87-
handlerThread.start();
88-
final Handler handler = new Handler(handlerThread.getLooper());
8992
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL, handler);
9093
}
9194

@@ -96,9 +99,15 @@ public void stop() {
9699
if (sensorManager != null) {
97100
sensorManager.unregisterListener(this);
98101
}
102+
}
103+
104+
/** Stops detection and releases the background thread. */
105+
public void close() {
106+
stop();
99107
if (handlerThread != null) {
100108
handlerThread.quitSafely();
101109
handlerThread = null;
110+
handler = null;
102111
}
103112
}
104113

0 commit comments

Comments
 (0)