1 package org.coolreader.crengine; 2 3 import android.content.Context; 4 import android.os.Handler; 5 import android.view.Gravity; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.WindowManager; 9 import android.widget.PopupWindow; 10 import android.widget.TextView; 11 12 import org.coolreader.R; 13 14 import java.util.concurrent.LinkedBlockingQueue; 15 import java.util.concurrent.atomic.AtomicBoolean; 16 17 /** 18 * User: Victor Soskin 19 * Date: 11/3/11 20 * Time: 2:51 PM 21 */ 22 public class ToastView { 23 private static class Toast { 24 private View anchor; 25 private String msg; 26 private int duration; 27 Toast(View anchor, String msg, int duration)28 private Toast(View anchor, String msg, int duration) { 29 this.anchor = anchor; 30 this.msg = msg; 31 this.duration = duration; 32 } 33 } 34 35 private static View mReaderView; 36 private static LinkedBlockingQueue<Toast> queue = new LinkedBlockingQueue<>(); 37 private static AtomicBoolean showing = new AtomicBoolean(false); 38 private static Handler mHandler = new Handler(); 39 private static PopupWindow window = null; 40 41 private static final Runnable handleDismiss = () -> { 42 if (window != null) { 43 window.dismiss(); 44 show(); 45 } 46 }; 47 48 static int fontSize = 24; showToast(View anchor, String msg, int duration, int textSize)49 public static void showToast(View anchor, String msg, int duration, int textSize) { 50 mReaderView = anchor; 51 fontSize = textSize; 52 try { 53 queue.put(new Toast(anchor, msg, duration)); 54 } catch (InterruptedException e) { 55 e.printStackTrace(); 56 } 57 if (showing.compareAndSet(false, true)) { 58 show(); 59 } 60 } 61 show()62 private static void show() { 63 if (queue.size() == 0) { 64 showing.compareAndSet(true, false); 65 return; 66 } 67 Toast t = queue.poll(); 68 window = new PopupWindow(t.anchor.getContext()); 69 window.setWidth(WindowManager.LayoutParams.FILL_PARENT); 70 window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 71 window.setTouchable(false); 72 window.setFocusable(false); 73 window.setOutsideTouchable(true); 74 window.setBackgroundDrawable(null); 75 /* LinearLayout ll = new LinearLayout(t.anchor.getContext()); 76 ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 77 78 TextView tv = new TextView(t.anchor.getContext()); 79 tv.setText(t.msg); 80 ll.setGravity(Gravity.CENTER); 81 ll.addView(tv);*/ 82 LayoutInflater inflater = (LayoutInflater) t.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 83 window.setContentView(inflater.inflate(R.layout.custom_toast, null, true)); 84 TextView tv = (TextView) window.getContentView().findViewById(R.id.toast); 85 tv.setTextSize(fontSize); //Integer.valueOf(Services.getSettings().getInt(ReaderView.PROP_FONT_SIZE, 20) ) ); 86 tv.setText(t.msg); 87 tv.setGravity(Gravity.CENTER); 88 window.showAtLocation(t.anchor, Gravity.NO_GRAVITY, 0, 0); 89 mHandler.postDelayed(handleDismiss, t.duration == 0 ? 2000 : 3000); 90 } 91 } 92