/** Method that initializes the component of the activity. */
  private void initComponents() {
    // Empty list view
    this.mEmptyListMsg = findViewById(R.id.search_empty_msg);
    // The list view
    this.mSearchListView = (ListView) findViewById(R.id.search_listview);
    this.mSearchListView.setOnItemClickListener(this);
    this.mSearchListView.setOnItemLongClickListener(this);

    // If we should set the listview to response to flinger gesture detection
    boolean useFlinger =
        Preferences.getSharedPreferences()
            .getBoolean(
                FileManagerSettings.SETTINGS_USE_FLINGER.getId(),
                ((Boolean) FileManagerSettings.SETTINGS_USE_FLINGER.getDefaultValue())
                    .booleanValue());
    if (useFlinger) {
      ((FlingerListView) this.mSearchListView)
          .setOnItemFlingerListener(this.mOnItemFlingerListener);
    }

    // Other components
    this.mSearchWaiting = (ProgressBar) findViewById(R.id.search_waiting);
    this.mSearchFoundItems = (TextView) findViewById(R.id.search_status_found_items);
    setFoundItems(0, ""); // $NON-NLS-1$
    this.mSearchTerms = (TextView) findViewById(R.id.search_status_query_terms);
    this.mSearchTerms.setText(Html.fromHtml(getString(R.string.search_terms, ""))); // $NON-NLS-1$
  }
  /**
   * Method that do the search.
   *
   * @param voiceQuery Indicates if the query is from voice recognition
   * @param query The terms of the search
   * @param searchDirectory The directory of the search
   * @hide
   */
  void doSearch(final boolean voiceQuery, final Query query, final String searchDirectory) {

    // Recovers the user preferences about save suggestions
    boolean saveSuggestions =
        Preferences.getSharedPreferences()
            .getBoolean(
                FileManagerSettings.SETTINGS_SAVE_SEARCH_TERMS.getId(),
                ((Boolean) FileManagerSettings.SETTINGS_SAVE_SEARCH_TERMS.getDefaultValue())
                    .booleanValue());
    if (saveSuggestions) {
      // Save every query for use as recent suggestions
      SearchRecentSuggestions suggestions =
          new SearchRecentSuggestions(
              this, RecentSearchesContentProvider.AUTHORITY, RecentSearchesContentProvider.MODE);
      if (!voiceQuery) {
        List<String> queries = query.getQueries();
        int cc = queries.size();
        for (int i = 0; i < cc; i++) {
          suggestions.saveRecentQuery(queries.get(i), null);
        }
      }
    }

    // Set the listview
    this.mResultList = new ArrayList<FileSystemObject>();
    SearchResultAdapter adapter =
        new SearchResultAdapter(
            this, new ArrayList<SearchResult>(), R.layout.search_item, this.mQuery);
    this.mSearchListView.setAdapter(adapter);

    // Set terms
    this.mSearchTerms.setText(Html.fromHtml(getString(R.string.search_terms, query.getTerms())));

    // Now, do the search in background
    this.mSearchListView.post(
        new Runnable() {
          @Override
          public void run() {
            try {
              // Retrieve the terms of the search
              String label = getString(R.string.searching_action_label);

              // Show a dialog for the progress
              SearchActivity.this.mDialog =
                  new MessageProgressDialog(
                      SearchActivity.this, 0, R.string.searching, label, true);
              // Initialize the
              setProgressMsg(0);

              // Set the cancel listener
              SearchActivity.this.mDialog.setOnCancelListener(
                  new MessageProgressDialog.OnCancelListener() {
                    @Override
                    public boolean onCancel() {
                      // User has requested the cancellation of the search
                      // Broadcast the cancellation
                      if (!SearchActivity.this.mExecutable.isCancelled()) {
                        if (SearchActivity.this.mExecutable.cancel()) {
                          ListAdapter listAdapter =
                              SearchActivity.this.mSearchListView.getAdapter();
                          if (listAdapter != null) {
                            SearchActivity.this.toggleResults(listAdapter.getCount() > 0, true);
                          }
                          return true;
                        }
                        return false;
                      }
                      return true;
                    }
                  });
              SearchActivity.this.mDialog.show();

              // Execute the query (search are process in background)
              SearchActivity.this.mExecutable =
                  CommandHelper.findFiles(
                      SearchActivity.this,
                      searchDirectory,
                      SearchActivity.this.mQuery,
                      SearchActivity.this,
                      null);

            } catch (Throwable ex) {
              // Remove all elements
              try {
                SearchActivity.this.removeAll();
              } catch (Throwable ex2) {
                /** NON BLOCK* */
              }
              try {
                if (SearchActivity.this.mDialog != null) {
                  SearchActivity.this.mDialog.dismiss();
                }
              } catch (Throwable ex2) {
                /** NON BLOCK* */
              }

              // Capture the exception
              Log.e(TAG, "Search failed", ex); // $NON-NLS-1$
              DialogHelper.showToast(
                  SearchActivity.this, R.string.search_error_msg, Toast.LENGTH_SHORT);
              SearchActivity.this.mSearchListView.setVisibility(View.GONE);
            }
          }
        });
  }