Example #1
0
  @Override
  protected Dialog onCreateDialog(int ignore, Bundle args) {

    // TODO set values from "flags" to messagebox dialog

    // get colors

    int[] colors = args.getIntArray("colors");
    int backgroundColor;
    int textColor;
    int buttonBorderColor;
    int buttonBackgroundColor;
    int buttonSelectedColor;
    if (colors != null) {
      int i = -1;
      backgroundColor = colors[++i];
      textColor = colors[++i];
      buttonBorderColor = colors[++i];
      buttonBackgroundColor = colors[++i];
      buttonSelectedColor = colors[++i];
    } else {
      backgroundColor = Color.TRANSPARENT;
      textColor = Color.TRANSPARENT;
      buttonBorderColor = Color.TRANSPARENT;
      buttonBackgroundColor = Color.TRANSPARENT;
      buttonSelectedColor = Color.TRANSPARENT;
    }

    // create dialog with title and a listener to wake up calling thread

    final Dialog dialog = new Dialog(this);
    dialog.setTitle(args.getString("title"));
    dialog.setCancelable(false);
    dialog.setOnDismissListener(
        new DialogInterface.OnDismissListener() {
          @Override
          public void onDismiss(DialogInterface unused) {
            synchronized (messageboxSelection) {
              messageboxSelection.notify();
            }
          }
        });

    // create text

    TextView message = new TextView(this);
    message.setGravity(Gravity.CENTER);
    message.setText(args.getString("message"));
    if (textColor != Color.TRANSPARENT) {
      message.setTextColor(textColor);
    }

    // create buttons

    int[] buttonFlags = args.getIntArray("buttonFlags");
    int[] buttonIds = args.getIntArray("buttonIds");
    String[] buttonTexts = args.getStringArray("buttonTexts");

    final SparseArray<Button> mapping = new SparseArray<Button>();

    LinearLayout buttons = new LinearLayout(this);
    buttons.setOrientation(LinearLayout.HORIZONTAL);
    buttons.setGravity(Gravity.CENTER);
    for (int i = 0; i < buttonTexts.length; ++i) {
      Button button = new Button(this);
      final int id = buttonIds[i];
      button.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              messageboxSelection[0] = id;
              dialog.dismiss();
            }
          });
      if (buttonFlags[i] != 0) {
        // see SDL_messagebox.h
        if ((buttonFlags[i] & 0x00000001) != 0) {
          mapping.put(KeyEvent.KEYCODE_ENTER, button);
        }
        if ((buttonFlags[i] & 0x00000002) != 0) {
          mapping.put(111, button); /* API 11: KeyEvent.KEYCODE_ESCAPE */
        }
      }
      button.setText(buttonTexts[i]);
      if (textColor != Color.TRANSPARENT) {
        button.setTextColor(textColor);
      }
      if (buttonBorderColor != Color.TRANSPARENT) {
        // TODO set color for border of messagebox button
      }
      if (buttonBackgroundColor != Color.TRANSPARENT) {
        Drawable drawable = button.getBackground();
        if (drawable == null) {
          // setting the color this way removes the style
          button.setBackgroundColor(buttonBackgroundColor);
        } else {
          // setting the color this way keeps the style (gradient, padding, etc.)
          drawable.setColorFilter(buttonBackgroundColor, PorterDuff.Mode.MULTIPLY);
        }
      }
      if (buttonSelectedColor != Color.TRANSPARENT) {
        // TODO set color for selected messagebox button
      }
      buttons.addView(button);
    }

    // create content

    LinearLayout content = new LinearLayout(this);
    content.setOrientation(LinearLayout.VERTICAL);
    content.addView(message);
    content.addView(buttons);
    if (backgroundColor != Color.TRANSPARENT) {
      content.setBackgroundColor(backgroundColor);
    }

    // add content to dialog and return

    dialog.setContentView(content);
    dialog.setOnKeyListener(
        new Dialog.OnKeyListener() {
          @Override
          public boolean onKey(DialogInterface d, int keyCode, KeyEvent event) {
            Button button = mapping.get(keyCode);
            if (button != null) {
              if (event.getAction() == KeyEvent.ACTION_UP) {
                button.performClick();
              }
              return true; // also for ignored actions
            }
            return false;
          }
        });

    return dialog;
  }