/**
   * https://android.googlesource.com/platform/frameworks/base/+/cd92588/core/java/android/webkit/SelectActionModeCallback.java
   *
   * @param actionMode
   * @return ActionMode
   */
  @Override
  public ActionMode onWebViewStartActionMode(ActionMode actionMode) {
    Menu menu = actionMode.getMenu();

    for (int i = 0; i < menu.size(); i++) {
      MenuItem item = menu.getItem(i);
      item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    }

    MenuInflater inflater = actionMode.getMenuInflater();
    inflater.inflate(R.menu.browser_action_mode, menu);
    translatePanelFragment.hide();

    menu.findItem(R.id.action_search)
        .setOnMenuItemClickListener(
            new MenuItem.OnMenuItemClickListener() {
              @Override
              public boolean onMenuItemClick(MenuItem menuItem) {
                startGettingSelectionInBrowser(JAVASCRIPT_CALLBACK_SEARCH);
                return false;
              }
            });
    menu.findItem(R.id.action_speech)
        .setOnMenuItemClickListener(
            new MenuItem.OnMenuItemClickListener() {
              @Override
              public boolean onMenuItemClick(MenuItem menuItem) {
                startGettingSelectionInBrowser(JAVASCRIPT_CALLBACK_SPEECH);
                return false;
              }
            });

    return actionMode;
  }
  @Override
  protected void onDestroy() {
    speechHelper.finish();
    webView.setWebViewClient(null);
    webView.removeJavascriptInterface(BrowserActivityJavaScriptInterface.NAME);
    webView.stopLoading();
    webView.destroy();
    translatePanelFragment.setListener(null);

    super.onDestroy();
  }
  @Override
  @UiThread
  public void onDictionaryServiceResult(String query, ArrayList<DicItemListView.Data> result) {
    String dictionaryName = "";
    DicItemListView.Data firstWordData = null;
    for (DicItemListView.Data data : result) {
      if (dictionaryName.equals("") && data.getMode() == DicItemListView.Data.FOOTER) {
        dictionaryName = data.Index.toString();
      }
      if (firstWordData == null && data.getMode() == DicItemListView.Data.WORD) {
        firstWordData = data;
      }
    }

    if (firstWordData == null) {
      activityHelper.showToast(R.string.message_no_result);
      return;
    }

    translatePanelFragment.setDictionaryName(dictionaryName);
    translatePanelFragment.setData(firstWordData);
    translatePanelFragment.show();
  }
  @AfterViews
  void afterViews() {
    Log.d(TAG, "state: " + state.toString());

    commonActivityTrait.initActivity(preferences);

    urlText.setSelectAllOnFocus(true);
    urlText.setOnEditorActionListener(this);

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDefaultTextEncodingName(ENCODING);
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    webView.addJavascriptInterface(
        new BrowserActivityJavaScriptInterface(this), BrowserActivityJavaScriptInterface.NAME);
    webView.setWebViewClient(new BrowserWebViewClient(this));
    webView.setActionModeListener(this);

    translatePanelFragment.setListener(this);

    Intent intent = getIntent();
    if (intent != null) {
      String action = intent.getAction();
      if (action != null && action.equals(Intent.ACTION_VIEW)) {
        Uri url = intent.getData();
        if (url != null) {
          loadUrl(url.toString());
          return;
        }
      }
    }

    if (extraUrlOrKeywords.isEmpty()) {
      loadUrl(state.getLastUrl());
    } else {
      loadUrl(extraUrlOrKeywords);
    }
  }
 @UiThread
 void reloadViews() {
   if (translatePanelFragment != null) {
     translatePanelFragment.hide();
   }
 }