/**
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.react.modules.toast;

import android.widget.Toast;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.common.MapBuilder;

import java.util.Map;

/**
 * {@link NativeModule} that allows JS to show an Android Toast.
 */
public class ToastModule extends ReactContextBaseJavaModule {

  private static final String DURATION_SHORT_KEY = "SHORT";
  private static final String DURATION_LONG_KEY = "LONG";

  public ToastModule(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "ToastAndroid";
  }

  @Override
  public Map<String, Object> getConstants() {
    final Map<String, Object> constants = MapBuilder.newHashMap();
    constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT);
    constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG);
    return constants;
  }

  @ReactMethod
  public void show(String message, int duration) {
    Toast.makeText(getReactApplicationContext(), message, duration).show();
  }
}

@@ -679,16 +679,63 @@
 Builder;
+%0Aimport com.facebook.react.bridge.UiThreadUtil;
 %0A%0Aimport
@@ -1471,16 +1471,22 @@
 id show(
+final 
 String m
@@ -1492,16 +1492,22 @@
 message,
+ final
  int dur
@@ -1511,24 +1511,117 @@
 duration) %7B%0A
+    UiThreadUtil.runOnUiThread(new Runnable() %7B%0A      @Override%0A      public void run()%7B%0A    
     Toast.ma
@@ -1683,15 +1683,39 @@
 .show();
+        %0A      %7D%0A    %7D);
 %0A  %7D%0A%7D%0A

Fix #5599: toast doesn't disappear after app exited.