@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 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();
    }
  }