/*
 * Copyright (c) 2017 the ACRA team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.acra.interaction;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.NonNull;
import android.widget.Toast;

import com.google.auto.service.AutoService;

import org.acra.config.ConfigUtils;
import org.acra.config.CoreConfiguration;
import org.acra.config.ToastConfiguration;
import org.acra.plugins.HasConfigPlugin;
import org.acra.util.ToastSender;

import java.io.File;

/**
 * @author F43nd1r
 * @since 04.06.2017
 */
@AutoService(ReportInteraction.class)
public class ToastInteraction extends HasConfigPlugin implements ReportInteraction {
    /**
     * Number of milliseconds to wait after displaying a toast.
     */
    private static final int TOAST_WAIT_DURATION = 2000;

    public ToastInteraction() {
        super(ToastConfiguration.class);
    }

    @Override
    public boolean performInteraction(@NonNull Context context, @NonNull CoreConfiguration config, @NonNull File reportFile) {
        Looper.prepare();
        ToastSender.sendToast(context, ConfigUtils.getPluginConfiguration(config, ToastConfiguration.class).text(), Toast.LENGTH_LONG);
        final Looper looper = Looper.myLooper();
        if(looper != null) {
            new Handler(looper).postDelayed(() -> {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                    looper.quitSafely();
                } else {
                    looper.quit();
                }
            }, TOAST_WAIT_DURATION);
            Looper.loop();
        }
        return true;
    }
}

@@ -754,59 +754,58 @@
 roid
-x.annotation.NonNull;%0Aimport android.widget.Toast;%0A
+.widget.Toast;%0Aimport androidx.annotation.NonNull;
 %0Aimp
@@ -845,17 +845,16 @@
 ervice;%0A
-%0A
 import o
@@ -1238,145 +1238,8 @@
 on %7B
-%0A    /**%0A     * Number of milliseconds to wait after displaying a toast.%0A     */%0A    private static final int TOAST_WAIT_DURATION = 2000;
 %0A%0A  
@@ -1500,33 +1500,36 @@
 oast
-Sender.sendToast(context,
+Configuration pluginConfig =
  Con
@@ -1597,34 +1597,91 @@
 ass)
-.text(), Toast.LENGTH_LONG
+;%0A        ToastSender.sendToast(context, pluginConfig.text(), pluginConfig.length()
 );%0A 
@@ -1738,16 +1738,17 @@
       if
+ 
 (looper 
@@ -2031,27 +2031,50 @@
  %7D, 
-TOAST_WAIT_DURATION
+getLengthInMs(pluginConfig.length()) + 100
 );%0A 
@@ -2135,11 +2135,290 @@
 e;%0A    %7D
+%0A%0A    private int getLengthInMs(int toastDuration) %7B%0A        switch (toastDuration) %7B%0A            case Toast.LENGTH_LONG:%0A                return 3500;%0A            case Toast.LENGTH_SHORT:%0A                return 2000;%0A            default:%0A                return 0;%0A        %7D%0A    %7D
 %0A%7D%0A

Fix toastLength not respected & wait not long enough for toast length long (#797)