@Override
  protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    mUIManager.onNewIntent(intent);

    if (intent != null && !TextUtils.isEmpty(intent.getStringExtra("QRCODE_URL"))) {
      mUIManager.loadUrl(intent.getStringExtra("QRCODE_URL"));
    }
  }
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

      if (prefs.getBoolean(Constants.TOGGLE_MENU_STATUS, false)) {
        mUIManager.onClosePopMenu();
        return true;
      }

      if (mUIManager.onKeyBack()) {
        return true;
      } else {
        moveTaskToBack(true);
        return true;
      }

    } else if (keyCode == KeyEvent.KEYCODE_MENU) {
      if (prefs.getBoolean(Constants.TOGGLE_MENU_STATUS, false)) {
        mUIManager.onClosePopMenu();
        return true;
      } else {
        mUIManager.onShowPopMenu();
        return true;
      }

    } else {
      return super.onKeyDown(keyCode, event);
    }
  }
  private void restoreTabs(Set<String> tabs) {
    boolean first = true;

    for (String url : tabs) {
      if (first) {
        mUIManager.loadUrl(url);
        first = false;
      } else {
        mUIManager.addTab(url, !first, false, false);
      }
    }
  }
  @Override
  protected void onPause() {
    super.onPause();

    mUIManager.onMainActivityPause();
    unregisterReceiver(mDownloadsReceiver);
  }
  @Override
  protected void onResume() {
    super.onResume();

    mUIManager.onMainActivityResume();

    IntentFilter filter = new IntentFilter();
    filter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    filter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
    registerReceiver(mDownloadsReceiver, filter);
  }
  /*
  * @Override public boolean onCreateOptionsMenu(final Menu menu) {
  * getMenuInflater().inflate(UIFactory.getMainMenuLayout(this), menu);
  * return true; }
  *
  * @Override public boolean onPrepareOptionsMenu(Menu menu) {
  * super.onPrepareOptionsMenu(menu);
  *
  * BaseWebViewFragment currentFragment =
  * mUIManager.getCurrentWebViewFragment();
  *
  * menu.setGroupEnabled(R.id.MainActivity_DisabledOnStartPageMenuGroup,
  * currentFragment != null && !currentFragment.isStartPageShown());
  *
  * CustomWebView currentWebView = mUIManager.getCurrentWebView();
  *
  * boolean privateBrowsing = currentWebView != null &&
  * currentWebView.isPrivateBrowsingEnabled();
  *
  * menu.findItem(R.id.MainActivity_MenuIncognitoTab).setChecked(privateBrowsing
  * ); menu.findItem(R.id.MainActivity_MenuFullScreen).setChecked(mUIManager.
  * isFullScreen());
  *
  * menu.removeGroup(R.id.MainActivity_AddonsMenuGroup);
  *
  * if (!privateBrowsing && (currentWebView != null)) { List<AddonMenuItem>
  * contributedMenuItems =
  * Controller.getInstance().getAddonManager().getContributedMainMenuItems
  * (currentWebView); for (AddonMenuItem item : contributedMenuItems) {
  * menu.add(R.id.MainActivity_AddonsMenuGroup, item.getAddon().getMenuId(),
  * 0, item.getMenuItem()); } }
  *
  * return true; }
  *
  * @Override public boolean onOptionsItemSelected(MenuItem item) { Intent i;
  *
  * switch (item.getItemId()) { case R.id.MainActivity_MenuAddTab:
  * mUIManager.addTab(true,
  * PreferenceManager.getDefaultSharedPreferences(this
  * ).getBoolean(Constants.PREFERENCE_INCOGNITO_BY_DEFAULT, false), false);
  * return true;
  *
  * case R.id.MainActivity_MenuCloseTab: mUIManager.closeCurrentTab(); return
  * true;
  *

  * case R.id.MainActivity_MenuIncognitoTab:
  * mUIManager.togglePrivateBrowsing(); return true;


  * default: if
  * (Controller.getInstance().getAddonManager().onContributedMainMenuItemSelected
  * (this, item.getItemId(), mUIManager.getCurrentWebView())) { return true;
  * } else { return super.onOptionsItemSelected(item); } } }
  */
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (requestCode == ACTIVITY_BOOKMARKS) {
      if (resultCode == RESULT_OK) {
        if (intent != null) {
          Bundle b = intent.getExtras();
          if (b != null) {
            if (b.getBoolean(Constants.EXTRA_NEW_TAB)) {
              mUIManager.addTab(
                  false,
                  PreferenceManager.getDefaultSharedPreferences(this)
                      .getBoolean(Constants.PREFERENCE_INCOGNITO_BY_DEFAULT, false),
                  false);
            }

            mUIManager.loadUrl(b.getString(Constants.EXTRA_URL));
          }
        }
      }
    } else if (requestCode == ACTIVITY_OPEN_FILE_CHOOSER) {
      if (mUIManager.getUploadMessage() == null) {
        return;
      }

      Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
      mUIManager.getUploadMessage().onReceiveValue(result);
      mUIManager.setUploadMessage(null);
    }

    mUIManager.onActivityResult(requestCode, resultCode, intent);
  }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(UIFactory.getMainLayout(this));
    mUIManager = UIFactory.createUIManager(this);

    getActionBar().setHomeButtonEnabled(true);
    getActionBar()
        .addOnMenuVisibilityListener(
            new OnMenuVisibilityListener() {
              @Override
              public void onMenuVisibilityChanged(boolean isVisible) {
                mUIManager.onMenuVisibilityChanged(isVisible);
              }
            });

    Controller.getInstance().init(mUIManager, this);
    Controller.getInstance().getAddonManager().bindAddons();

    initializeWebIconDatabase();
    mPreferenceChangeListener =
        new OnSharedPreferenceChangeListener() {
          @Override
          public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
            mUIManager.onSharedPreferenceChanged(sharedPreferences, key);

            // If the user changed the history size, reset the last history
            // truncation date.
            if (Constants.PREFERENCE_HISTORY_SIZE.equals(key)) {
              Editor prefEditor = sharedPreferences.edit();
              prefEditor.putLong(Constants.TECHNICAL_PREFERENCE_LAST_HISTORY_TRUNCATION, -1);
              prefEditor.commit();
            }
          }
        };

    prefs = PreferenceManager.getDefaultSharedPreferences(this);
    prefs.registerOnSharedPreferenceChangeListener(mPreferenceChangeListener);
    mPackagesFilter = new IntentFilter();
    mPackagesFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
    mPackagesFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);
    mPackagesFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
    mPackagesFilter.addCategory(Intent.CATEGORY_DEFAULT);
    mPackagesFilter.addDataScheme("package");

    registerReceiver(mPackagesReceiver, mPackagesFilter);

    Intent startIntent = getIntent();
    boolean firstRun = prefs.getBoolean(Constants.TECHNICAL_PREFERENCE_FIRST_RUN, true);
    if (firstRun) {
      Editor editor = prefs.edit();
      editor.putBoolean(Constants.TECHNICAL_PREFERENCE_FIRST_RUN, false);
      editor.putInt(
          Constants.TECHNICAL_PREFERENCE_LAST_RUN_VERSION_CODE,
          ApplicationUtils.getApplicationVersionCode(this));
      editor.commit();

      BookmarksWrapper.fillDefaultBookmaks(
          getContentResolver(),
          getResources().getStringArray(R.array.DefaultBookmarksTitles),
          getResources().getStringArray(R.array.DefaultBookmarksUrls));
    } else {
      int currentVersionCode = ApplicationUtils.getApplicationVersionCode(this);
      int savedVersionCode = prefs.getInt(Constants.TECHNICAL_PREFERENCE_LAST_RUN_VERSION_CODE, -1);

      if (currentVersionCode != savedVersionCode) {
        Editor editor = prefs.edit();
        editor.putInt(Constants.TECHNICAL_PREFERENCE_LAST_RUN_VERSION_CODE, currentVersionCode);
        editor.commit();
      }
    }

    mUIManager.onNewIntent(startIntent);

    if (prefs.contains(Constants.TECHNICAL_PREFERENCE_SAVED_TABS)) {
      final Set<String> tabs = prefs.getStringSet(Constants.TECHNICAL_PREFERENCE_SAVED_TABS, null);

      if ((tabs != null) && (!tabs.isEmpty())) {

        String tabsRestoreMode = prefs.getString(Constants.PREFERENCE_RESTORE_TABS, "ASK");

        if ("ASK".equals(tabsRestoreMode)) {
          final YesNoRememberDialog dialog = new YesNoRememberDialog(this);

          dialog.setTitle(R.string.RestoreTabsDialogTitle);
          dialog.setMessage(R.string.RestoreTabsDialogMessage);

          dialog.setPositiveButtonListener(
              new OnClickListener() {

                @Override
                public void onClick(View v) {
                  dialog.dismiss();

                  if (dialog.isRememberChecked()) {
                    Editor editor = prefs.edit();
                    editor.putString(Constants.PREFERENCE_RESTORE_TABS, "ALWAYS");
                    editor.commit();
                  }

                  restoreTabs(tabs);
                }
              });

          dialog.setNegativeButtonListener(
              new OnClickListener() {

                @Override
                public void onClick(View v) {
                  dialog.dismiss();

                  if (dialog.isRememberChecked()) {
                    Editor editor = prefs.edit();
                    editor.putString(Constants.PREFERENCE_RESTORE_TABS, "NEVER");
                    editor.commit();
                  }
                }
              });

          dialog.show();
        } else if ("ALWAYS".equals(tabsRestoreMode)) {
          restoreTabs(tabs);
        }
      }
      Editor editor = prefs.edit();
      editor.remove(Constants.TECHNICAL_PREFERENCE_SAVED_TABS);
      editor.commit();
    }
  }
 @Override
 public void onActionModeStarted(ActionMode mode) {
   super.onActionModeStarted(mode);
   mUIManager.onActionModeStarted(mode);
 }
 @Override
 public void onActionModeFinished(ActionMode mode) {
   super.onActionModeFinished(mode);
   mUIManager.onActionModeFinished(mode);
 }
 @Override
 protected void onStop() {
   mUIManager.saveTabs();
   super.onStop();
 }
 public void pageSearch() {
   mUIManager.onKeySearch();
 }
 public void addBookMark() {
   mUIManager.addBookmarkFromCurrentPage();
 }
 public void goToBookMark() {
   mUIManager.openBookmarksActivityForResult();
 }
 public void shareCurrentPage() {
   mUIManager.shareCurrentPage();
 }
 public void toggleFullScreen() {
   mUIManager.toggleFullScreen();
 }