android.app.SearchableInfo Java Examples

The following examples show how to use android.app.SearchableInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java    From PacketSender-Android with MIT License 6 votes vote down vote up
private void setupSearchView(MenuItem searchItem) {

        //searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        if (searchManager != null) {
            List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();

            SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
            for (SearchableInfo inf : searchables) {
                if (inf.getSuggestAuthority() != null
                        && inf.getSuggestAuthority().startsWith("applications")) {
                    info = inf;
                }
            }
            mSearchView.setSearchableInfo(info);
        }

        mSearchView.setOnQueryTextListener(this);
    }
 
Example #2
Source File: SearchView.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void onVoiceClicked() {
    // guard against possible race conditions
    if (mSearchable == null) {
        return;
    }
    SearchableInfo searchable = mSearchable;
    try {
        if (searchable.getVoiceSearchLaunchWebSearch()) {
            Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent,
                    searchable);
            getContext().startActivity(webSearchIntent);
        } else if (searchable.getVoiceSearchLaunchRecognizer()) {
            Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent,
                    searchable);
            getContext().startActivity(appSearchIntent);
        }
    } catch (ActivityNotFoundException e) {
        // Should not happen, since we check the availability of
        // voice search before showing the button. But just in case...
        Log.w(LOG_TAG, "Could not find voice search activity");
    }
}
 
Example #3
Source File: SearchActivity.java    From search-samples with Apache License 2.0 6 votes vote down vote up
private void setupSearchView(MenuItem searchItem) {

        mSearchView.setIconifiedByDefault(false);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        if (searchManager != null) {
            List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();

            SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
            for (SearchableInfo inf : searchables) {
                if (inf.getSuggestAuthority() != null
                        && inf.getSuggestAuthority().startsWith("applications")) {
                    info = inf;
                }
            }
            mSearchView.setSearchableInfo(info);
        }

        mSearchView.setOnQueryTextListener(this);
        mSearchView.setFocusable(false);
        mSearchView.setFocusableInTouchMode(false);
    }
 
Example #4
Source File: SearchViewActionBar.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
private void setupSearchView(MenuItem searchItem) {

        if (isAlwaysExpanded()) {
            mSearchView.setIconifiedByDefault(false);
        } else {
            searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM
                    | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
        }

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        if (searchManager != null) {
            List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();

            // Try to use the "applications" global search provider
            SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
            for (SearchableInfo inf : searchables) {
                if (inf.getSuggestAuthority() != null
                        && inf.getSuggestAuthority().startsWith("applications")) {
                    info = inf;
                }
            }
            mSearchView.setSearchableInfo(info);
        }

        mSearchView.setOnQueryTextListener(this);
    }
 
Example #5
Source File: SearchActivity.java    From io2015-codelabs with Apache License 2.0 6 votes vote down vote up
private void setupSearchView(MenuItem searchItem) {

        mSearchView.setIconifiedByDefault(false);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        if (searchManager != null) {
            List<SearchableInfo> searchables = searchManager.getSearchablesInGlobalSearch();

            SearchableInfo info = searchManager.getSearchableInfo(getComponentName());
            for (SearchableInfo inf : searchables) {
                if (inf.getSuggestAuthority() != null
                        && inf.getSuggestAuthority().startsWith("applications")) {
                    info = inf;
                }
            }
            mSearchView.setSearchableInfo(info);
        }

        mSearchView.setOnQueryTextListener(this);
        mSearchView.setFocusable(false);
        mSearchView.setFocusableInTouchMode(false);
    }
 
Example #6
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.options_menu, menu);

  // Use the Search Manager to find the SearchableInfo related
  // to the Search Result Activity.
  SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

  SearchableInfo searchableInfo = searchManager.getSearchableInfo(
    new ComponentName(getApplicationContext(),
      EarthquakeSearchResultActivity.class));

  SearchView searchView =
    (SearchView)menu.findItem(R.id.search_view).getActionView();
  searchView.setSearchableInfo(searchableInfo);
  searchView.setIconifiedByDefault(false);
  return true;
}
 
Example #7
Source File: SearchView.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * For a given suggestion and a given cursor row, get the action message. If
 * not provided by the specific row/column, also check for a single
 * definition (for the action key).
 *
 * @param c The cursor providing suggestions
 * @param actionKey The actionkey record being examined
 *
 * @return Returns a string, or null if no action key message for this
 *         suggestion
 */
private static String getActionKeyMessage(Cursor c, SearchableInfo.ActionKeyInfo actionKey) {
    String result = null;
    // check first in the cursor data, for a suggestion-specific message
    final String column = actionKey.getSuggestActionMsgColumn();
    if (column != null) {
        result = SuggestionsAdapter.getColumnString(c, column);
    }
    // If the cursor didn't give us a message, see if there's a single
    // message defined
    // for the actionkey (for all suggestions)
    if (result == null) {
        result = actionKey.getSuggestActionMsg();
    }
    return result;
}
 
Example #8
Source File: SearchView.java    From Libraries-for-Android-Developers with MIT License 6 votes vote down vote up
/**
 * Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
 * to display labels, hints, suggestions, create intents for launching search results screens
 * and controlling other affordances such as a voice button.
 *
 * @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific
 * activity or a global search provider.
 */
public void setSearchableInfo(SearchableInfo searchable) {
    mSearchable = searchable;
    if (mSearchable != null) {
        updateSearchAutoComplete();
        updateQueryHint();
    }
    // Cache the voice search capability
    mVoiceButtonEnabled = hasVoiceSearch();

    if (mVoiceButtonEnabled) {
        // Disable the microphone on the keyboard, as a mic is displayed near the text box
        // TODO: use imeOptions to disable voice input when the new API will be available
        mQueryTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
    }
    updateViewsVisibility(isIconified());
}
 
Example #9
Source File: SearchView.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Handles the key down event for dealing with action keys.
 *
 * @param keyCode This is the keycode of the typed key, and is the same value as
 *        found in the KeyEvent parameter.
 * @param event The complete event record for the typed key
 *
 * @return true if the event was handled here, or false if not.
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (mSearchable == null) {
        return false;
    }

    // if it's an action specified by the searchable activity, launch the
    // entered query with the action key
    SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode);
    if ((actionKey != null) && (actionKey.getQueryActionMsg() != null)) {
        launchQuerySearch(keyCode, actionKey.getQueryActionMsg(), mSearchSrcTextView.getText()
                .toString());
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
 
Example #10
Source File: SearchView.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
 * to display labels, hints, suggestions, create intents for launching search results screens
 * and controlling other affordances such as a voice button.
 *
 * @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific
 * activity or a global search provider.
 */
public void setSearchableInfo(SearchableInfo searchable) {
    mSearchable = searchable;
    if (mSearchable != null) {
        updateSearchAutoComplete();
        updateQueryHint();
    }
    // Cache the voice search capability
    mVoiceButtonEnabled = hasVoiceSearch();

    if (mVoiceButtonEnabled) {
        // Disable the microphone on the keyboard, as a mic is displayed near the text box
        // TODO: use imeOptions to disable voice input when the new API will be available
        mSearchSrcTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
    }
    updateViewsVisibility(isIconified());
}
 
Example #11
Source File: SearchView.java    From Libraries-for-Android-Developers with MIT License 6 votes vote down vote up
private void onVoiceClicked() {
    // guard against possible race conditions
    if (mSearchable == null) {
        return;
    }
    SearchableInfo searchable = mSearchable;
    try {
        if (searchable.getVoiceSearchLaunchWebSearch()) {
            Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent,
                    searchable);
            getContext().startActivity(webSearchIntent);
        } else if (searchable.getVoiceSearchLaunchRecognizer()) {
            Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent,
                    searchable);
            getContext().startActivity(appSearchIntent);
        }
    } catch (ActivityNotFoundException e) {
        // Should not happen, since we check the availability of
        // voice search before showing the button. But just in case...
        Log.w(LOG_TAG, "Could not find voice search activity");
    }
}
 
Example #12
Source File: SearchView.java    From zen4android with MIT License 6 votes vote down vote up
/**
 * Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
 * to display labels, hints, suggestions, create intents for launching search results screens
 * and controlling other affordances such as a voice button.
 *
 * @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific
 * activity or a global search provider.
 */
public void setSearchableInfo(SearchableInfo searchable) {
    mSearchable = searchable;
    if (mSearchable != null) {
        updateSearchAutoComplete();
        updateQueryHint();
    }
    // Cache the voice search capability
    mVoiceButtonEnabled = hasVoiceSearch();

    if (mVoiceButtonEnabled) {
        // Disable the microphone on the keyboard, as a mic is displayed near the text box
        // TODO: use imeOptions to disable voice input when the new API will be available
        mQueryTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
    }
    updateViewsVisibility(isIconified());
}
 
Example #13
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.options_menu, menu);

  // Use the Search Manager to find the SearchableInfo related
  // to the Search Result Activity.
  SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

  SearchableInfo searchableInfo = searchManager.getSearchableInfo(
    new ComponentName(getApplicationContext(),
      EarthquakeSearchResultActivity.class));

  SearchView searchView =
    (SearchView)menu.findItem(R.id.search_view).getActionView();
  searchView.setSearchableInfo(searchableInfo);
  searchView.setIconifiedByDefault(false);
  return true;
}
 
Example #14
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.options_menu, menu);

  // Use the Search Manager to find the SearchableInfo related
  // to the Search Result Activity.
  SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

  SearchableInfo searchableInfo = searchManager.getSearchableInfo(
    new ComponentName(getApplicationContext(),
      EarthquakeSearchResultActivity.class));

  SearchView searchView =
    (SearchView)menu.findItem(R.id.search_view).getActionView();
  searchView.setSearchableInfo(searchableInfo);
  searchView.setIconifiedByDefault(false);
  return true;
}
 
Example #15
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.options_menu, menu);

  // Use the Search Manager to find the SearchableInfo related
  // to the Search Result Activity.
  SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

  SearchableInfo searchableInfo = searchManager.getSearchableInfo(
    new ComponentName(getApplicationContext(),
      EarthquakeSearchResultActivity.class));

  SearchView searchView =
    (SearchView)menu.findItem(R.id.search_view).getActionView();
  searchView.setSearchableInfo(searchableInfo);
  searchView.setIconifiedByDefault(false);
  return true;
}
 
Example #16
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.options_menu, menu);

  // Use the Search Manager to find the SearchableInfo related
  // to the Search Result Activity.
  SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

  SearchableInfo searchableInfo = searchManager.getSearchableInfo(
    new ComponentName(getApplicationContext(),
      EarthquakeSearchResultActivity.class));

  SearchView searchView =
    (SearchView)menu.findItem(R.id.search_view).getActionView();
  searchView.setSearchableInfo(searchableInfo);
  searchView.setIconifiedByDefault(false);
  return true;
}
 
Example #17
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.options_menu, menu);

  // Use the Search Manager to find the SearchableInfo related
  // to the Search Result Activity.
  SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

  SearchableInfo searchableInfo = searchManager.getSearchableInfo(
    new ComponentName(getApplicationContext(),
      EarthquakeSearchResultActivity.class));

  SearchView searchView =
    (SearchView)menu.findItem(R.id.search_view).getActionView();
  searchView.setSearchableInfo(searchableInfo);
  searchView.setIconifiedByDefault(false);
  return true;
}
 
Example #18
Source File: MainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.menu_main, menu);

  // Use the Search Manager to find the SearchableInfo related
  // to this Activity.
  SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  SearchableInfo searchableInfo =
    searchManager.getSearchableInfo(getComponentName());

  SearchView searchView =
    (SearchView) menu.findItem(R.id.search_view).getActionView();
  searchView.setSearchableInfo(searchableInfo);
  searchView.setIconifiedByDefault(false);
  return true;
}
 
Example #19
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.options_menu, menu);

  // Use the Search Manager to find the SearchableInfo related
  // to the Search Result Activity.
  SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

  SearchableInfo searchableInfo = searchManager.getSearchableInfo(
    new ComponentName(getApplicationContext(),
      EarthquakeSearchResultActivity.class));

  SearchView searchView =
    (SearchView)menu.findItem(R.id.search_view).getActionView();
  searchView.setSearchableInfo(searchableInfo);
  searchView.setIconifiedByDefault(false);
  return true;
}
 
Example #20
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.options_menu, menu);

  // Use the Search Manager to find the SearchableInfo related
  // to the Search Result Activity.
  SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

  SearchableInfo searchableInfo = searchManager.getSearchableInfo(
    new ComponentName(getApplicationContext(),
      EarthquakeSearchResultActivity.class));

  SearchView searchView =
    (SearchView)menu.findItem(R.id.search_view).getActionView();
  searchView.setSearchableInfo(searchableInfo);
  searchView.setIconifiedByDefault(false);
  return true;
}
 
Example #21
Source File: EarthquakeMainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
  super.onCreateOptionsMenu(menu);

  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.options_menu, menu);

  // Use the Search Manager to find the SearchableInfo related
  // to the Search Result Activity.
  SearchManager searchManager =
    (SearchManager) getSystemService(Context.SEARCH_SERVICE);

  SearchableInfo searchableInfo = searchManager.getSearchableInfo(
    new ComponentName(getApplicationContext(),
      EarthquakeSearchResultActivity.class));

  SearchView searchView =
    (SearchView)menu.findItem(R.id.search_view).getActionView();
  searchView.setSearchableInfo(searchableInfo);
  searchView.setIconifiedByDefault(false);
  return true;
}
 
Example #22
Source File: SearchView.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
 * to display labels, hints, suggestions, create intents for launching search results screens
 * and controlling other affordances such as a voice button.
 *
 * @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific
 * activity or a global search provider.
 */
public void setSearchableInfo(SearchableInfo searchable) {
    mSearchable = searchable;
    if (mSearchable != null) {
        updateSearchAutoComplete();
        updateQueryHint();
    }
    // Cache the voice search capability
    mVoiceButtonEnabled = hasVoiceSearch();

    if (mVoiceButtonEnabled) {
        // Disable the microphone on the keyboard, as a mic is displayed near the text box
        // TODO: use imeOptions to disable voice input when the new API will be available
        mQueryTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
    }
    updateViewsVisibility(isIconified());
}
 
Example #23
Source File: SearchView.java    From CSipSimple with GNU General Public License v3.0 6 votes vote down vote up
private void onVoiceClicked() {
    // guard against possible race conditions
    if (mSearchable == null) {
        return;
    }
    SearchableInfo searchable = mSearchable;
    try {
        if (searchable.getVoiceSearchLaunchWebSearch()) {
            Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent,
                    searchable);
            getContext().startActivity(webSearchIntent);
        } else if (searchable.getVoiceSearchLaunchRecognizer()) {
            Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent,
                    searchable);
            getContext().startActivity(appSearchIntent);
        }
    } catch (ActivityNotFoundException e) {
        // Should not happen, since we check the availability of
        // voice search before showing the button. But just in case...
        Log.w(LOG_TAG, "Could not find voice search activity");
    }
}
 
Example #24
Source File: SubsonicFragment.java    From Popeens-DSub with GNU General Public License v3.0 6 votes vote down vote up
protected void onFinishSetupOptionsMenu(final Menu menu) {
	searchItem = menu.findItem(R.id.menu_global_search);
	if(searchItem != null) {
		searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
		SearchManager searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
		SearchableInfo searchableInfo = searchManager.getSearchableInfo(context.getComponentName());
		if(searchableInfo == null) {
			Log.w(TAG, "Failed to get SearchableInfo");
		} else {
			searchView.setSearchableInfo(searchableInfo);
		}

		String currentQuery = getCurrentQuery();
		if(currentQuery != null) {
			searchView.setOnSearchClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					searchView.setQuery(getCurrentQuery(), false);
				}
			});
		}
	}
}
 
Example #25
Source File: SearchView.java    From zhangshangwuda with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the SearchableInfo for this SearchView. Properties in the SearchableInfo are used
 * to display labels, hints, suggestions, create intents for launching search results screens
 * and controlling other affordances such as a voice button.
 *
 * @param searchable a SearchableInfo can be retrieved from the SearchManager, for a specific
 * activity or a global search provider.
 */
public void setSearchableInfo(SearchableInfo searchable) {
    mSearchable = searchable;
    if (mSearchable != null) {
        updateSearchAutoComplete();
        updateQueryHint();
    }
    // Cache the voice search capability
    mVoiceButtonEnabled = hasVoiceSearch();

    if (mVoiceButtonEnabled) {
        // Disable the microphone on the keyboard, as a mic is displayed near the text box
        // TODO: use imeOptions to disable voice input when the new API will be available
        mQueryTextView.setPrivateImeOptions(IME_OPTION_NO_MICROPHONE);
    }
    updateViewsVisibility(isIconified());
}
 
Example #26
Source File: SearchView.java    From zhangshangwuda with Apache License 2.0 6 votes vote down vote up
private void onVoiceClicked() {
    // guard against possible race conditions
    if (mSearchable == null) {
        return;
    }
    SearchableInfo searchable = mSearchable;
    try {
        if (searchable.getVoiceSearchLaunchWebSearch()) {
            Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent,
                    searchable);
            getContext().startActivity(webSearchIntent);
        } else if (searchable.getVoiceSearchLaunchRecognizer()) {
            Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent,
                    searchable);
            getContext().startActivity(appSearchIntent);
        }
    } catch (ActivityNotFoundException e) {
        // Should not happen, since we check the availability of
        // voice search before showing the button. But just in case...
        Log.w(LOG_TAG, "Could not find voice search activity");
    }
}
 
Example #27
Source File: SubsonicFragment.java    From Audinaut with GNU General Public License v3.0 6 votes vote down vote up
void onFinishSetupOptionsMenu(final Menu menu) {
    searchItem = menu.findItem(R.id.menu_global_search);
    if (searchItem != null) {
        searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        SearchManager searchManager = (SearchManager) context.getSystemService(Context.SEARCH_SERVICE);
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(context.getComponentName());
        if (searchableInfo == null) {
            Log.w(TAG, "Failed to get SearchableInfo");
        } else {
            searchView.setSearchableInfo(searchableInfo);
        }

        String currentQuery = getCurrentQuery();
        if (currentQuery != null) {
            searchView.setOnSearchClickListener(v -> searchView.setQuery(getCurrentQuery(), false));
        }
    }
}
 
Example #28
Source File: SearchActivity.java    From Toutiao with Apache License 2.0 6 votes vote down vote up
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search, menu);
        MenuItem item = menu.findItem(R.id.action_search);
        searchView = (SearchView) MenuItemCompat.getActionView(item);
        // 关联检索配置与 SearchActivity
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(
                new ComponentName(getApplicationContext(), SearchActivity.class));
        searchView.setSearchableInfo(searchableInfo);
        searchView.onActionViewExpanded();
//        // 设置搜索文字样式
//        EditText searchEditText = (EditText) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
//        searchEditText.setTextColor(getResources().getColor(R.color.textColorPrimary));
//        searchEditText.setHintTextColor(getResources().getColor(R.color.textColorPrimary));
//        searchEditText.setBackgroundColor(Color.WHITE);
        setOnQuenyTextChangeListener();

        return super.onCreateOptionsMenu(menu);
    }
 
Example #29
Source File: SearchView.java    From zen4android with MIT License 6 votes vote down vote up
private void onVoiceClicked() {
    // guard against possible race conditions
    if (mSearchable == null) {
        return;
    }
    SearchableInfo searchable = mSearchable;
    try {
        if (searchable.getVoiceSearchLaunchWebSearch()) {
            Intent webSearchIntent = createVoiceWebSearchIntent(mVoiceWebSearchIntent,
                    searchable);
            getContext().startActivity(webSearchIntent);
        } else if (searchable.getVoiceSearchLaunchRecognizer()) {
            Intent appSearchIntent = createVoiceAppSearchIntent(mVoiceAppSearchIntent,
                    searchable);
            getContext().startActivity(appSearchIntent);
        }
    } catch (ActivityNotFoundException e) {
        // Should not happen, since we check the availability of
        // voice search before showing the button. But just in case...
        Log.w(LOG_TAG, "Could not find voice search activity");
    }
}
 
Example #30
Source File: SearchView.java    From zen4android with MIT License 5 votes vote down vote up
/**
 * Create and return an Intent that can launch the voice search activity for web search.
 */
private Intent createVoiceWebSearchIntent(Intent baseIntent, SearchableInfo searchable) {
    Intent voiceIntent = new Intent(baseIntent);
    ComponentName searchActivity = searchable.getSearchActivity();
    voiceIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, searchActivity == null ? null
            : searchActivity.flattenToShortString());
    return voiceIntent;
}