@Override
  protected void onDestroy() {
    Controller.getInstance().getAddonManager().unbindAddons();
    WebIconDatabase.getInstance().close();
    PreferenceManager.getDefaultSharedPreferences(this)
        .unregisterOnSharedPreferenceChangeListener(mPreferenceChangeListener);
    unregisterReceiver(mPackagesReceiver);

    super.onDestroy();
  }
  @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 onReceive(Context context, Intent intent) {
   Controller.getInstance().getAddonManager().unbindAddons();
   Controller.getInstance().getAddonManager().bindAddons();
 }
  private void onReceivedDownloadNotification(Context context, Intent intent) {
    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
      long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
      DownloadItem item = Controller.getInstance().getDownloadItemById(id);

      if (item != null) {
        // This is one of our downloads.
        final DownloadManager downloadManager =
            (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        Query query = new Query();
        query.setFilterById(id);
        Cursor cursor = downloadManager.query(query);

        if (cursor.moveToFirst()) {
          int localUriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
          int reasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
          int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);

          int status = cursor.getInt(statusIndex);

          if (status == DownloadManager.STATUS_SUCCESSFUL) {
            String localUri = cursor.getString(localUriIndex);

            Toast.makeText(
                    context,
                    String.format(getString(R.string.DownloadComplete), localUri),
                    Toast.LENGTH_SHORT)
                .show();
            Controller.getInstance().getDownloadsList().remove(item);

            showNotification(
                getString(R.string.DownloadComplete),
                item.getFileName(),
                getString(R.string.DownloadComplete));

          } else if (status == DownloadManager.STATUS_FAILED) {
            int reason = cursor.getInt(reasonIndex);

            String message;
            switch (reason) {
              case DownloadManager.ERROR_FILE_ERROR:
              case DownloadManager.ERROR_DEVICE_NOT_FOUND:
              case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                message = getString(R.string.DownloadErrorDisk);
                break;
              case DownloadManager.ERROR_HTTP_DATA_ERROR:
              case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                message = getString(R.string.DownloadErrorHttp);
                break;
              case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                message = getString(R.string.DownloadErrorRedirection);
                break;
              default:
                message = getString(R.string.DownloadErrorUnknown);
                break;
            }

            Toast.makeText(
                    context,
                    String.format(getString(R.string.DownloadFailedWithErrorMessage), message),
                    Toast.LENGTH_SHORT)
                .show();
            Controller.getInstance().getDownloadsList().remove(item);
          }
        }
      }
    } else if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())) {
      Intent i = new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS);
      startActivity(i);
    }
  }