package com.jarwen.scanner.util;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.jarwen.scanner.R;
import com.jarwen.scanner.ScannerApplication;
public class ToastManager {
private static final boolean IS_WEAK_TOAST = true;
private Context context;
private Toast toast;
private int txtColor;
private int txtSize;
private Drawable bgDrawable;
private AlertDialog dialog;
private OnMakeChoiceResult onMakeChoiceResult;
private OnStrongToastListener onStrongToastListener;
public ToastManager(Context context){
this.context = context;
toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, UnitManager.px2dp(80));
txtSize = 13;
txtColor = context.getResources().getColor(R.color.gray_dark_1);
bgDrawable = context.getResources().getDrawable(R.drawable.round_corner_gray_r5);
}
public void toast(String msg) {
toast(IS_WEAK_TOAST, msg);
}
public void toast(boolean isWeakToast, String msg){
if(Thread.currentThread().getId() != 1){
Logger.e("Cannot toast on sub-thread.");
return;
}
dismissDialog();
if(isWeakToast) {
weakToast(msg);
}else{
strongToast(msg);
}
}
public void makeChoice(String content, OnMakeChoiceResult callback){
Logger.v("makeChoice()");
if(Thread.currentThread().getId() != 1){
Logger.e("Cannot toast on sub-thread.");
return;
}
dismissDialog();
if(onMakeChoiceResult != null) {
Logger.e("Cannot popup the make choice dialog cause current already shown a 'mc' dialog.");
return;
}
int windowWidth = (int) (ScannerApplication.getInstance().getHardware().getAppWidth() * 0.618f);
int windowHeight = UnitManager.px2dp(123);
Logger.d("dimension:" + windowWidth + "*" + windowHeight);
// 1. make layout.
GridLayout layout = new GridLayout(context);
layout.setColumnCount(2);
layout.setRowCount(2);
layout.setBackground(context.getResources().getDrawable(R.drawable.round_corner_makechoice_dialog_bg));
TextView tvContent = new TextView(context);
tvContent.setText(content);
tvContent.setTextSize(15);
tvContent.setTextColor(context.getResources().getColor(R.color.gray_text_333));
tvContent.setGravity(Gravity.CENTER);
tvContent.setBackground(context.getResources().getDrawable(R.drawable.round_corner_makechoice_dialog_content_bg));
GridLayout.LayoutParams glp = new GridLayout.LayoutParams(GridLayout.spec(0), GridLayout.spec(0, 2));
glp.width = -1;
glp.height = (int) (windowHeight * 0.6f);
tvContent.setLayoutParams(glp);
TextView tvCancel = new TextView(context);
tvCancel.setTextColor(context.getResources().getColor(R.color.gray_text_888));
tvCancel.setTextSize(15);
tvCancel.setText(context.getText(R.string.no));
tvCancel.setBackground(context.getResources().getDrawable(R.drawable.round_corner_makechoice_dialog_cancel_bg));
tvCancel.setGravity(Gravity.CENTER);
glp = new GridLayout.LayoutParams(GridLayout.spec(1), GridLayout.spec(0, 1.0f));
if(Build.VERSION.SDK_INT <= 22){
glp.width = (int) ((float) windowWidth / 2.0f);
}
glp.height = (int) (windowHeight * 0.4f);
glp.topMargin = UnitManager.px2dp(1);
tvCancel.setLayoutParams(glp);
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Logger.d("cancel the make choice dialog");
if(onMakeChoiceResult != null) {
onMakeChoiceResult.onMakeChoice(false);
onMakeChoiceResult = null;
}
dismissDialog();
notifyStrongToastListener(false);
}
});
TextView tvOk = new TextView(context);
tvOk.setTextColor(context.getResources().getColor(R.color.toast_makechoice_txt_ok));
tvOk.setTextSize(15);
tvOk.setText(context.getText(R.string.yes));
tvOk.setGravity(Gravity.CENTER);
tvOk.setBackground(context.getResources().getDrawable(R.drawable.round_corner_makechoice_dialog_ok_bg));
glp = new GridLayout.LayoutParams(GridLayout.spec(1), GridLayout.spec(1, 1.0f));
if(Build.VERSION.SDK_INT <= 22){
glp.width = (int) ((float) windowWidth / 2.0f) - UnitManager.px2dp(1);
}
glp.height = (int) (windowHeight * 0.4f);
glp.topMargin = UnitManager.px2dp(1);
glp.leftMargin = glp.topMargin;
tvOk.setLayoutParams(glp);
tvOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Logger.d("ok the make choice dialog");
if(onMakeChoiceResult != null) {
onMakeChoiceResult.onMakeChoice(true);
onMakeChoiceResult = null;
}
dismissDialog();
notifyStrongToastListener(false);
}
});
layout.addView(tvContent);
layout.addView(tvCancel);
layout.addView(tvOk);
// 2. decorate dialog and show it.
if(dialog != null) {
dialog.dismiss();
}
dialog = new AlertDialog.Builder(context).create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
dialog.setContentView(layout); //Must behind on 'dialog.show()'.
if(dialog.getWindow() != null) {
dialog.getWindow().setLayout(windowWidth, windowHeight);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
}
onMakeChoiceResult = callback;
notifyStrongToastListener(true);
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private void weakToast(String msg){
if(Build.VERSION.SDK_INT > 25){
toast = null;
Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, UnitManager.px2dp(80));
toast.setView(getTextView(msg));
toast.show();
}else{
if(toast.getView() != null){
((TextView)toast.getView()).setText(msg);
}else{
toast.setView(getTextView(msg));
}
toast.show();
}
}
private void strongToast(String msg){
dismissDialog();
dialog = new AlertDialog.Builder(context).create();
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.show();
dialog.setContentView(getDialogView(msg));
if(dialog.getWindow() != null) {
Logger.d("poping strong toast,screen:" +
ScannerApplication.getInstance().getHardware().getAppWidth() + "*" +
ScannerApplication.getInstance().getHardware().getAppHeight());
dialog.getWindow().setLayout((int) (ScannerApplication.getInstance().getHardware().getAppWidth() * 0.618f), -2);
}
notifyStrongToastListener(true);
}
private void dismissDialog(){
if(dialog != null) {
dialog.dismiss();
dialog = null;
}
}
private TextView getTextView(String txt){
TextView tv = new TextView(context);
int padding = UnitManager.pix10();
tv.setPadding(padding, padding, padding, padding);
tv.setBackground(bgDrawable);
tv.setGravity(Gravity.CENTER);
tv.setTextColor(txtColor);
tv.setTextSize(txtSize);
tv.setText(txt);
return tv;
}
private View getDialogView(String txt){
final LinearLayout dialogLayout = new LinearLayout(context);
dialogLayout.setGravity(Gravity.CENTER);
dialogLayout.setBackground(context.getResources().getDrawable(R.drawable.round_corner_white_r5));
dialogLayout.setOrientation(LinearLayout.VERTICAL);
dialogLayout.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
// 1. Information view.
TextView tv = new TextView(context);
tv.setPadding(UnitManager.pix10(), UnitManager.pix10(), UnitManager.pix10(), UnitManager.pix10());
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llp.topMargin = UnitManager.px2dp(20);
llp.bottomMargin = UnitManager.px2dp(20);
tv.setLayoutParams(llp);
tv.setGravity(Gravity.CENTER);
tv.setTextColor(context.getResources().getColor(R.color.gray_textview_original));
tv.setTextSize(12);
tv.setText(txt);
// 2. Divider line.
View divider = new View(context);
divider.setBackgroundColor(context.getResources().getColor(R.color.gray_background));
llp = new LinearLayout.LayoutParams(-1, UnitManager.px2dp(2));
divider.setLayoutParams(llp);
// 3. Button.
Button btn = new Button(context);
btn.setText(R.string.ok);
btn.setTextColor(context.getResources().getColor(R.color.basically_color));
btn.setTextSize(16);
btn.setBackground(context.getResources().getDrawable(R.drawable.round_corner_white_r5));
btn.setLayoutParams(new LinearLayout.LayoutParams(-1, UnitManager.px2dp(40)));
btn.setGravity(Gravity.CENTER);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismissDialog();
notifyStrongToastListener(false);
}
});
dialogLayout.addView(tv);
dialogLayout.addView(divider);
dialogLayout.addView(btn);
return dialogLayout;
}
public void showWaitingDialog(String info){
dismissDialog();
dialog = new AlertDialog.Builder(context).create();
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
dialog.setContentView(getTextView(info));
if(dialog.getWindow() != null) {
dialog.getWindow().setLayout((int) (ScannerApplication.getInstance().getHardware().getAppWidth() * 0.382f), -2);
}
}
public void dismissWaitingDialog(){
dismissDialog();
}
private void notifyStrongToastListener(boolean isShown){
if(onStrongToastListener != null) {
onStrongToastListener.onStrongToastEvent(isShown);
if(!isShown) {
onStrongToastListener = null; //一次性通知。
}
}
}
public void setOnStrongToastListener(OnStrongToastListener listener){
onStrongToastListener = listener;
}
public interface OnMakeChoiceResult{
void onMakeChoice(boolean yes);
}
public interface OnStrongToastListener {
void onStrongToastEvent(boolean isShown);
}
}