void Engine::InitUI()

in NearbyConnectionsCpp/app/src/main/cpp/NearbyNativeActivity.cpp [215:302]


void Engine::InitUI() {

  const int32_t LEFT_MARGIN = 20;

  // The window initialization
  jui_helper::JUIWindow::Init(app_->activity, JUIHELPER_CLASS_NAME);

  // Using jui_helper, a support library, to create and bind game management UIs
  int32_t win_width = ANativeWindow_getWidth(app_->window);
  int32_t win_height = ANativeWindow_getHeight(app_->window);

  if (win_height <= 0 || win_width <= 0) {
    LOGE("Failed to get native window size");
    return;
  }
  if (win_height > win_width) {
    int32_t tmp = win_width;
    win_width = win_height;
    win_height = tmp;
  }

  int32_t button_raw_width = win_width / 4;  // we have 4 buttons
  int32_t button_height = win_height / 4;
  int cur_idx = 0;

  // Create 4 buttons to control nearby sign-in
  // The sequence is dictated by enum BUTTON_INDEX,
  // it MUST match the button titles array defined here
  const char *titles[UI_BUTTON_COUNT] = {"Advertise", "Discover", "Play Game",
                                         "Stop"};
  std::function<void(jui_helper::JUIView *, const int32_t)> button_handlers[] = {
          [this](jui_helper::JUIView *button, const int32_t msg) {
            if (msg == jui_helper::JUICALLBACK_BUTTON_UP) {
              OnAdvertiseButtonClick();
            }
          },
          [this](jui_helper::JUIView *button, const int32_t msg) {
            if (msg == jui_helper::JUICALLBACK_BUTTON_UP) {
              OnDiscoverButtonClick();
            }
          },
          [this](jui_helper::JUIView *button, const int32_t msg) {
            if (msg == jui_helper::JUICALLBACK_BUTTON_UP) {
              OnPlayButtonClick();
            }
          },
          [this](jui_helper::JUIView *button, const int32_t msg) {
            if (msg == jui_helper::JUICALLBACK_BUTTON_UP) {
              OnStopButtonClick();
            }
          },
      };

  for (cur_idx = 0; cur_idx < UI_BUTTON_COUNT; cur_idx++) {
    jui_helper::JUIButton *button = new jui_helper::JUIButton(titles[cur_idx]);
    button->AddRule(jui_helper::LAYOUT_PARAMETER_CENTER_VERTICAL,
                    jui_helper::LAYOUT_PARAMETER_TRUE);
    button->AddRule(jui_helper::LAYOUT_PARAMETER_ALIGN_PARENT_LEFT,
                    jui_helper::LAYOUT_PARAMETER_TRUE);
    button->SetAttribute("MinimumWidth", button_raw_width - LEFT_MARGIN);
    button->SetAttribute("MinimumHeight", button_height);
    button->SetMargins(LEFT_MARGIN + cur_idx * button_raw_width, 0, 0, 0);
    button->SetCallback(button_handlers[cur_idx]);
    jui_helper::JUIWindow::GetInstance()->AddView(button);
    ui_buttons_[cur_idx] = button;
  }

  status_text_ = new jui_helper::JUITextView("Nearby Connection is Idle");
  status_text_->AddRule(jui_helper::LAYOUT_PARAMETER_ALIGN_PARENT_BOTTOM,
                        jui_helper::LAYOUT_PARAMETER_TRUE);
  status_text_->AddRule(jui_helper::LAYOUT_PARAMETER_CENTER_IN_PARENT,
                        jui_helper::LAYOUT_PARAMETER_TRUE);
  status_text_->SetAttribute("TextSize", jui_helper::ATTRIBUTE_UNIT_SP, 17.f);
  jui_helper::JUIWindow::GetInstance()->AddView(status_text_);

  // Init nearby connections...
  std::thread([this]() {
    ndk_helper::JNIHelper &helper = *ndk_helper::JNIHelper::GetInstance();
    helper.AttachCurrentThread();

    InitGoogleNearbyConnection();

    helper.DetachCurrentThread();
  }).detach();

  EnableUI(true);
  return;
}