/** 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);
            }
          }
        });
  }
  /** Method that initializes the titlebar of the activity. */
  private void initSearch() {
    // Stop any pending action
    try {
      if (SearchActivity.this.mDrawingSearchResultTask != null
          && SearchActivity.this.mDrawingSearchResultTask.isRunning()) {
        SearchActivity.this.mDrawingSearchResultTask.cancel(true);
      }
    } catch (Throwable ex2) {
      /** NON BLOCK* */
    }
    try {
      if (SearchActivity.this.mDialog != null) {
        SearchActivity.this.mDialog.dismiss();
      }
    } catch (Throwable ex2) {
      /** NON BLOCK* */
    }

    // Recovery the search directory
    Bundle bundle = getIntent().getBundleExtra(SearchManager.APP_DATA);
    // If data is not present, use root directory to do the search
    this.mSearchDirectory = FileHelper.ROOT_DIRECTORY;
    if (bundle != null) {
      this.mSearchDirectory = bundle.getString(EXTRA_SEARCH_DIRECTORY, FileHelper.ROOT_DIRECTORY);
    }

    // Retrieve the query ¿from voice recognizer?
    boolean voiceQuery = true;
    List<String> userQueries =
        getIntent().getStringArrayListExtra(android.speech.RecognizerIntent.EXTRA_RESULTS);
    if (userQueries == null || userQueries.size() == 0) {
      // From input text
      userQueries = new ArrayList<String>();
      // Recovers and save the last term search in the memory
      Preferences.setLastSearch(getIntent().getStringExtra(SearchManager.QUERY));
      userQueries.add(Preferences.getLastSearch());
      voiceQuery = false;
    }

    // Filter the queries? Needed if queries come from voice recognition
    final List<String> filteredUserQueries = (voiceQuery) ? filterQuery(userQueries) : userQueries;

    // Create the queries
    this.mQuery = new Query().fillSlots(filteredUserQueries);
    List<String> queries = this.mQuery.getQueries();

    // Check if some queries has lower than allowed, in this case
    // request the user for stop the search
    boolean ask = false;
    int cc = queries.size();
    for (int i = 0; i < cc; i++) {
      if (queries.get(i).trim().length() < MIN_CHARS_SEARCH) {
        ask = true;
        break;
      }
    }
    if (ask) {
      askUserBeforeSearch(voiceQuery, this.mQuery, this.mSearchDirectory);
    } else {
      doSearch(voiceQuery, this.mQuery, this.mSearchDirectory);
    }
  }