package io.rumors.reactnativesettings.listeners;

import android.content.Intent;
import android.content.Context;
import android.view.accessibility.CaptioningManager;

import io.rumors.reactnativesettings.handlers.CaptioningSettingsHandler;
import io.rumors.reactnativesettings.Constants;

public class CaptioningChangeListener extends CaptioningManager.CaptioningChangeListener {
  private Context mContext;

  public CaptioningChangeListener(Context context) {
    this.mContext = context;
    CaptioningManager captioningManager = (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE);
    captioningManager.addCaptioningChangeListener(this);
  }

  @Override
  public void onEnabledChanged(boolean enabled) {
    String captioning = (new CaptioningSettingsHandler(mContext)).getSetting();

    Intent i = new Intent(Constants.CAPTIONING_CHANGED);
    i.putExtra(Constants.CAPTIONING_SETTINGS, captioning);
    mContext.sendBroadcast(i);
  }
}

@@ -158,16 +158,94 @@
 Manager;
+%0Aimport android.os.Handler;%0Aimport android.os.Looper;%0Aimport android.os.Build;
 %0A%0Aimport
@@ -360,16 +360,62 @@
 tants;%0A%0A
+import java.util.concurrent.CountDownLatch;%0A%0A%0A
 public c
@@ -529,21 +529,22 @@
 xt;%0A%0A  p
-ublic
+rivate
  Caption
@@ -550,185 +550,1057 @@
 ning
-ChangeListener(Context context) %7B%0A    this.m
+Manager mCaptioningManager;%0A%0A  private CaptioningManager getCaptioningManager() %7B%0A    if (Build.VERSION.SDK_INT %3E Build.VERSION_CODES.LOLLIPOP) %7B%0A      return (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE);%0A    %7D else %7B%0A      // Android %3C= 5 bug workaround%0A      // https://github.com/evernote/android-job/issues/48#issuecomment-221789418%0A      final 
 Co
+u
 nt
-ext = context;%0A    CaptioningManager captioningManager = (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE
+DownLatch latch = new CountDownLatch(1);%0A%0A      Handler handler = new Handler(Looper.getMainLooper());%0A      handler.post(new Runnable() %7B%0A          @Override%0A          public void run() %7B%0A              mCaptioningManager = (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE);%0A              latch.countDown();%0A          %7D%0A      %7D);%0A  %0A      try %7B%0A          latch.await();%0A      %7D catch (InterruptedException e) %7B%0A          e.printStackTrace();%0A      %7D%0A  %0A      return mCaptioningManager;%0A    %7D%0A  %7D%0A%0A  public CaptioningChangeListener(Context context) %7B%0A    this.mContext = context;%0A    CaptioningManager captioningManager = getCaptioningManager(
 );%0A 

call getSystemService on main thread for Android <= 5