Skip to content

Commit 134b6fd

Browse files
authored
Merge pull request #43 from rmrs/fix/android_5_crash
fix: call getSystemService on main thread for Android <= 5
2 parents eac4697 + 6631981 commit 134b6fd

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

android/src/main/java/io/rumors/reactnativesettings/listeners/CaptioningChangeListener.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,51 @@
33
import android.content.Intent;
44
import android.content.Context;
55
import android.view.accessibility.CaptioningManager;
6+
import android.os.Handler;
7+
import android.os.Looper;
8+
import android.os.Build;
69

710
import io.rumors.reactnativesettings.handlers.CaptioningSettingsHandler;
811
import io.rumors.reactnativesettings.Constants;
912

13+
import java.util.concurrent.CountDownLatch;
14+
15+
1016
public class CaptioningChangeListener extends CaptioningManager.CaptioningChangeListener {
1117
private Context mContext;
1218

19+
private CaptioningManager mCaptioningManager;
20+
21+
private CaptioningManager getCaptioningManager() {
22+
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
23+
return (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE);
24+
} else {
25+
// Android <= 5 bug workaround
26+
// https://github.com/evernote/android-job/issues/48#issuecomment-221789418
27+
final CountDownLatch latch = new CountDownLatch(1);
28+
29+
Handler handler = new Handler(Looper.getMainLooper());
30+
handler.post(new Runnable() {
31+
@Override
32+
public void run() {
33+
mCaptioningManager = (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE);
34+
latch.countDown();
35+
}
36+
});
37+
38+
try {
39+
latch.await();
40+
} catch (InterruptedException e) {
41+
e.printStackTrace();
42+
}
43+
44+
return mCaptioningManager;
45+
}
46+
}
47+
1348
public CaptioningChangeListener(Context context) {
1449
this.mContext = context;
15-
CaptioningManager captioningManager = (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE);
50+
CaptioningManager captioningManager = getCaptioningManager();
1651
captioningManager.addCaptioningChangeListener(this);
1752
}
1853

0 commit comments

Comments
 (0)