Fields Summary |
---|
private static final String | TAG |
private static final boolean | DEBUG |
private static final String | EXTRA_LEANBACK_BADGE_PRESENT |
private static final String | ARG_PREFIX |
private static final String | ARG_QUERY |
private static final String | ARG_TITLE |
private static final long | SPEECH_RECOGNITION_DELAY_MS |
private static final int | RESULTS_CHANGED |
private static final int | QUERY_COMPLETE |
private final android.support.v17.leanback.widget.ObjectAdapter.DataObserver | mAdapterObserver |
private final android.os.Handler | mHandler |
private final Runnable | mResultsChangedCallback |
private final Runnable | mSetSearchResultProviderRuns when a new provider is set AND when the fragment view is created. |
private final Runnable | mStartRecognitionRunnable |
private RowsFragment | mRowsFragment |
private android.support.v17.leanback.widget.SearchBar | mSearchBar |
private SearchResultProvider | mProvider |
private String | mPendingQuery |
private android.support.v17.leanback.widget.OnItemSelectedListener | mOnItemSelectedListener |
private android.support.v17.leanback.widget.OnItemClickedListener | mOnItemClickedListener |
private android.support.v17.leanback.widget.OnItemViewSelectedListener | mOnItemViewSelectedListener |
private android.support.v17.leanback.widget.OnItemViewClickedListener | mOnItemViewClickedListener |
private android.support.v17.leanback.widget.ObjectAdapter | mResultAdapter |
private android.support.v17.leanback.widget.SpeechRecognitionCallback | mSpeechRecognitionCallback |
private String | mTitle |
private android.graphics.drawable.Drawable | mBadgeDrawable |
private ExternalQuery | mExternalQuery |
private android.speech.SpeechRecognizer | mSpeechRecognizer |
private int | mStatus |
private boolean | mAutoStartRecognition |
Methods Summary |
---|
private void | applyExternalQuery()
if (mExternalQuery == null || mSearchBar == null) {
return;
}
mSearchBar.setSearchQuery(mExternalQuery.mQuery);
if (mExternalQuery.mSubmit) {
submitQuery(mExternalQuery.mQuery);
}
mExternalQuery = null;
|
public static android.os.Bundle | createArgs(android.os.Bundle args, java.lang.String query)
return createArgs(args, query, null);
|
public static android.os.Bundle | createArgs(android.os.Bundle args, java.lang.String query, java.lang.String title)
if (args == null) {
args = new Bundle();
}
args.putString(ARG_QUERY, query);
args.putString(ARG_TITLE, title);
return args;
|
public void | displayCompletions(java.util.List completions)Display the completions shown by the IME. An application may provide
a list of query completions that the system will show in the IME.
mSearchBar.displayCompletions(completions);
|
private void | executePendingQuery()
if (null != mPendingQuery && null != mResultAdapter) {
String query = mPendingQuery;
mPendingQuery = null;
retrieveResults(query);
}
|
private void | focusOnResults()
if (mRowsFragment == null ||
mRowsFragment.getVerticalGridView() == null ||
mResultAdapter.size() == 0) {
return;
}
if (mRowsFragment.getVerticalGridView().requestFocus()) {
mStatus &= ~RESULTS_CHANGED;
}
|
public android.graphics.drawable.Drawable | getBadgeDrawable()Returns the badge drawable in the search bar.
if (null != mSearchBar) {
return mSearchBar.getBadgeDrawable();
}
return null;
|
public android.content.Intent | getRecognizerIntent()Returns an intent that can be used to request speech recognition.
Built from the base {@link RecognizerIntent#ACTION_RECOGNIZE_SPEECH} plus
extras:
- {@link RecognizerIntent#EXTRA_LANGUAGE_MODEL} set to
{@link RecognizerIntent#LANGUAGE_MODEL_FREE_FORM}
- {@link RecognizerIntent#EXTRA_PARTIAL_RESULTS} set to true
- {@link RecognizerIntent#EXTRA_PROMPT} set to the search bar hint text
For handling the intent returned from the service, see
{@link #setSearchQuery(Intent, boolean)}.
Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, true);
if (mSearchBar != null && mSearchBar.getHint() != null) {
recognizerIntent.putExtra(RecognizerIntent.EXTRA_PROMPT, mSearchBar.getHint());
}
recognizerIntent.putExtra(EXTRA_LEANBACK_BADGE_PRESENT, mBadgeDrawable != null);
return recognizerIntent;
|
public java.lang.String | getTitle()Returns the title set in the search bar.
if (null != mSearchBar) {
return mSearchBar.getTitle();
}
return null;
|
public static android.support.v17.leanback.app.SearchFragment | newInstance(java.lang.String query)Create a search fragment with a given search query.
You should only use this if you need to start the search fragment with a
pre-filled query.
SearchFragment fragment = new SearchFragment();
Bundle args = createArgs(null, query);
fragment.setArguments(args);
return fragment;
|
public void | onCreate(android.os.Bundle savedInstanceState)
if (mAutoStartRecognition) {
mAutoStartRecognition = savedInstanceState == null;
}
super.onCreate(savedInstanceState);
|
public android.view.View | onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container, android.os.Bundle savedInstanceState)
View root = inflater.inflate(R.layout.lb_search_fragment, container, false);
FrameLayout searchFrame = (FrameLayout) root.findViewById(R.id.lb_search_frame);
mSearchBar = (SearchBar) searchFrame.findViewById(R.id.lb_search_bar);
mSearchBar.setSearchBarListener(new SearchBar.SearchBarListener() {
@Override
public void onSearchQueryChange(String query) {
if (DEBUG) Log.v(TAG, String.format("onSearchQueryChange %s %s", query,
null == mProvider ? "(null)" : mProvider));
if (null != mProvider) {
retrieveResults(query);
} else {
mPendingQuery = query;
}
}
@Override
public void onSearchQuerySubmit(String query) {
if (DEBUG) Log.v(TAG, String.format("onSearchQuerySubmit %s", query));
submitQuery(query);
}
@Override
public void onKeyboardDismiss(String query) {
if (DEBUG) Log.v(TAG, String.format("onKeyboardDismiss %s", query));
queryComplete();
}
});
mSearchBar.setSpeechRecognitionCallback(mSpeechRecognitionCallback);
applyExternalQuery();
readArguments(getArguments());
if (null != mBadgeDrawable) {
setBadgeDrawable(mBadgeDrawable);
}
if (null != mTitle) {
setTitle(mTitle);
}
// Inject the RowsFragment in the results container
if (getChildFragmentManager().findFragmentById(R.id.lb_results_frame) == null) {
mRowsFragment = new RowsFragment();
getChildFragmentManager().beginTransaction()
.replace(R.id.lb_results_frame, mRowsFragment).commit();
} else {
mRowsFragment = (RowsFragment) getChildFragmentManager()
.findFragmentById(R.id.lb_results_frame);
}
mRowsFragment.setOnItemViewSelectedListener(new OnItemViewSelectedListener() {
@Override
public void onItemSelected(ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
int position = mRowsFragment.getVerticalGridView().getSelectedPosition();
if (DEBUG) Log.v(TAG, String.format("onItemSelected %d", position));
mSearchBar.setVisibility(0 >= position ? View.VISIBLE : View.GONE);
if (null != mOnItemSelectedListener) {
mOnItemSelectedListener.onItemSelected(item, row);
}
if (null != mOnItemViewSelectedListener) {
mOnItemViewSelectedListener.onItemSelected(itemViewHolder, item,
rowViewHolder, row);
}
}
});
mRowsFragment.setOnItemViewClickedListener(new OnItemViewClickedListener() {
@Override
public void onItemClicked(ViewHolder itemViewHolder, Object item,
RowPresenter.ViewHolder rowViewHolder, Row row) {
int position = mRowsFragment.getVerticalGridView().getSelectedPosition();
if (DEBUG) Log.v(TAG, String.format("onItemClicked %d", position));
if (null != mOnItemClickedListener) {
mOnItemClickedListener.onItemClicked(item, row);
}
if (null != mOnItemViewClickedListener) {
mOnItemViewClickedListener.onItemClicked(itemViewHolder, item,
rowViewHolder, row);
}
}
});
mRowsFragment.setExpand(true);
if (null != mProvider) {
onSetSearchResultProvider();
}
return root;
|
public void | onDestroy()
releaseAdapter();
super.onDestroy();
|
public void | onPause()
releaseRecognizer();
super.onPause();
|
public void | onResume()
super.onResume();
if (mSpeechRecognitionCallback == null && null == mSpeechRecognizer) {
mSpeechRecognizer = SpeechRecognizer.createSpeechRecognizer(getActivity());
mSearchBar.setSpeechRecognizer(mSpeechRecognizer);
}
// Ensure search bar state consistency when using external recognizer
mSearchBar.stopRecognition();
|
private void | onSetSearchResultProvider()
mHandler.removeCallbacks(mSetSearchResultProvider);
mHandler.post(mSetSearchResultProvider);
|
public void | onStart()
super.onStart();
VerticalGridView list = mRowsFragment.getVerticalGridView();
int mContainerListAlignTop =
getResources().getDimensionPixelSize(R.dimen.lb_search_browse_rows_align_top);
list.setItemAlignmentOffset(0);
list.setItemAlignmentOffsetPercent(VerticalGridView.ITEM_ALIGN_OFFSET_PERCENT_DISABLED);
list.setWindowAlignmentOffset(mContainerListAlignTop);
list.setWindowAlignmentOffsetPercent(VerticalGridView.WINDOW_ALIGN_OFFSET_PERCENT_DISABLED);
list.setWindowAlignment(VerticalGridView.WINDOW_ALIGN_NO_EDGE);
|
private void | queryComplete()
if (DEBUG) Log.v(TAG, "queryComplete");
mStatus |= QUERY_COMPLETE;
focusOnResults();
|
private void | readArguments(android.os.Bundle args)
if (null == args) {
return;
}
if (args.containsKey(ARG_QUERY)) {
setSearchQuery(args.getString(ARG_QUERY));
}
if (args.containsKey(ARG_TITLE)) {
setTitle(args.getString(ARG_TITLE));
}
|
private void | releaseAdapter()
if (mResultAdapter != null) {
mResultAdapter.unregisterObserver(mAdapterObserver);
mResultAdapter = null;
}
|
private void | releaseRecognizer()
if (null != mSpeechRecognizer) {
mSearchBar.setSpeechRecognizer(null);
mSpeechRecognizer.destroy();
mSpeechRecognizer = null;
}
|
private void | resultsAvailable()
if ((mStatus & QUERY_COMPLETE) != 0) {
focusOnResults();
}
updateSearchBarNextFocusId();
|
private void | retrieveResults(java.lang.String searchQuery)
if (DEBUG) Log.v(TAG, "retrieveResults " + searchQuery);
if (mProvider.onQueryTextChange(searchQuery)) {
mStatus &= ~QUERY_COMPLETE;
}
|
public void | setBadgeDrawable(android.graphics.drawable.Drawable drawable)Sets the badge drawable that will be shown inside the search bar next to
the title.
mBadgeDrawable = drawable;
if (null != mSearchBar) {
mSearchBar.setBadgeDrawable(drawable);
}
|
public void | setOnItemClickedListener(android.support.v17.leanback.widget.OnItemClickedListener listener)Sets an item clicked listener for the results.
mOnItemClickedListener = listener;
|
public void | setOnItemSelectedListener(android.support.v17.leanback.widget.OnItemSelectedListener listener)Sets an item selection listener for the results.
mOnItemSelectedListener = listener;
|
public void | setOnItemViewClickedListener(android.support.v17.leanback.widget.OnItemViewClickedListener listener)Sets an item clicked listener for the results.
mOnItemViewClickedListener = listener;
|
public void | setOnItemViewSelectedListener(android.support.v17.leanback.widget.OnItemViewSelectedListener listener)Sets an item selection listener for the results.
mOnItemViewSelectedListener = listener;
|
public void | setSearchQuery(java.lang.String query, boolean submit)Sets the text of the search query and optionally submits the query. Either
{@link SearchResultProvider#onQueryTextChange onQueryTextChange} or
{@link SearchResultProvider#onQueryTextSubmit onQueryTextSubmit} will be
called on the provider if it is set.
if (DEBUG) Log.v(TAG, "setSearchQuery " + query + " submit " + submit);
if (query == null) {
return;
}
mExternalQuery = new ExternalQuery(query, submit);
applyExternalQuery();
if (mAutoStartRecognition) {
mAutoStartRecognition = false;
mHandler.removeCallbacks(mStartRecognitionRunnable);
}
|
public void | setSearchQuery(android.content.Intent intent, boolean submit)Sets the text of the search query based on the {@link RecognizerIntent#EXTRA_RESULTS} in
the given intent, and optionally submit the query. If more than one result is present
in the results list, the first will be used.
ArrayList<String> matches = intent.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (matches != null && matches.size() > 0) {
setSearchQuery(matches.get(0), submit);
}
|
private void | setSearchQuery(java.lang.String query)
mSearchBar.setSearchQuery(query);
|
public void | setSearchResultProvider(android.support.v17.leanback.app.SearchFragment$SearchResultProvider searchResultProvider)Set the search provider that is responsible for returning results for the
search query.
if (mProvider != searchResultProvider) {
mProvider = searchResultProvider;
onSetSearchResultProvider();
}
|
public void | setSpeechRecognitionCallback(android.support.v17.leanback.widget.SpeechRecognitionCallback callback)Set this callback to have the fragment pass speech recognition requests
to the activity rather than using an internal recognizer.
mSpeechRecognitionCallback = callback;
if (mSearchBar != null) {
mSearchBar.setSpeechRecognitionCallback(mSpeechRecognitionCallback);
}
if (callback != null) {
releaseRecognizer();
}
|
public void | setTitle(java.lang.String title)Sets the title string to be be shown in an empty search bar. The title
may be placed in a call-to-action, such as "Search title" or
"Speak to search title".
mTitle = title;
if (null != mSearchBar) {
mSearchBar.setTitle(title);
}
|
public void | startRecognition()Starts speech recognition. Typical use case is that
activity receives onNewIntent() call when user clicks a MIC button.
Note that SearchFragment automatically starts speech recognition
at first time created, there is no need to call startRecognition()
when fragment is created.
mSearchBar.startRecognition();
|
private void | submitQuery(java.lang.String query)
queryComplete();
if (null != mProvider) {
mProvider.onQueryTextSubmit(query);
}
|
private void | updateFocus()
if (mResultAdapter != null && mResultAdapter.size() > 0 &&
mRowsFragment != null && mRowsFragment.getAdapter() == mResultAdapter) {
focusOnResults();
} else {
mSearchBar.requestFocus();
}
|
private void | updateSearchBarNextFocusId()
if (mSearchBar == null || mResultAdapter == null) {
return;
}
final int viewId = (mResultAdapter.size() == 0 || mRowsFragment == null ||
mRowsFragment.getVerticalGridView() == null) ? 0 :
mRowsFragment.getVerticalGridView().getId();
mSearchBar.setNextFocusDownId(viewId);
|