|
3 | 3 | import android.content.Intent;
|
4 | 4 | import android.content.Context;
|
5 | 5 | import android.view.accessibility.CaptioningManager;
|
| 6 | +import android.os.Handler; |
| 7 | +import android.os.Looper; |
| 8 | +import android.os.Build; |
6 | 9 |
|
7 | 10 | import io.rumors.reactnativesettings.handlers.CaptioningSettingsHandler;
|
8 | 11 | import io.rumors.reactnativesettings.Constants;
|
9 | 12 |
|
| 13 | +import java.util.concurrent.CountDownLatch; |
| 14 | + |
| 15 | + |
10 | 16 | public class CaptioningChangeListener extends CaptioningManager.CaptioningChangeListener {
|
11 | 17 | private Context mContext;
|
12 | 18 |
|
| 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 | + |
13 | 48 | public CaptioningChangeListener(Context context) {
|
14 | 49 | this.mContext = context;
|
15 |
| - CaptioningManager captioningManager = (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE); |
| 50 | + CaptioningManager captioningManager = getCaptioningManager(); |
16 | 51 | captioningManager.addCaptioningChangeListener(this);
|
17 | 52 | }
|
18 | 53 |
|
|
0 commit comments