[Android] Fix voice search on TV

Geoffrey Métais git at videolan.org
Thu Oct 12 19:04:44 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Oct 12 18:40:40 2017 +0200| [233ac6093d0ed716a44155cd91c50328ddae8617] | committer: Geoffrey Métais

Fix voice search on TV

> https://code.videolan.org/videolan/vlc-android/commit/233ac6093d0ed716a44155cd91c50328ddae8617
---

 .../org/videolan/vlc/gui/tv/SearchActivity.java    | 25 ++---------------
 .../org/videolan/vlc/gui/tv/SearchFragment.java    | 31 +++++++++++++++++++---
 2 files changed, 29 insertions(+), 27 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/SearchActivity.java b/vlc-android/src/org/videolan/vlc/gui/tv/SearchActivity.java
index 310598ca7..cbceff426 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/SearchActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/SearchActivity.java
@@ -21,45 +21,24 @@
 package org.videolan.vlc.gui.tv;
 
 import android.annotation.TargetApi;
-import android.app.SearchManager;
-import android.content.Intent;
 import android.os.Build;
 import android.os.Bundle;
-import android.support.v17.leanback.widget.SpeechRecognitionCallback;
 import android.support.v4.app.FragmentActivity;
 
 import org.videolan.vlc.R;
-import org.videolan.vlc.util.Util;
 
 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
 public class SearchActivity extends FragmentActivity {
+    private static final String TAG = "VLC/SearchActivity";
 
-    SearchFragment mFragment;
-    private static final int REQUEST_SPEECH = 1;
+    private SearchFragment mFragment;
 
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.tv_search);
-
         mFragment = (SearchFragment) getSupportFragmentManager()
                 .findFragmentById(R.id.search_fragment);
-        final Intent intent = getIntent();
-        if (Intent.ACTION_SEARCH.equals(intent.getAction()) || "com.google.android.gms.actions.SEARCH_ACTION".equals(intent.getAction())) {
-            mFragment.onQueryTextSubmit(intent.getStringExtra(SearchManager.QUERY));
-        } else {
-            final Intent recognitionIntent = mFragment.getRecognizerIntent();
-            if (Util.isCallable(recognitionIntent)) {
-                final SpeechRecognitionCallback speechRecognitionCallback = new SpeechRecognitionCallback() {
-
-                    @Override
-                    public void recognizeSpeech() {
-                        startActivityForResult(recognitionIntent, REQUEST_SPEECH);
-                    }
-                };
-                mFragment.setSpeechRecognitionCallback(speechRecognitionCallback);
-            }
-        }
     }
 
     @Override
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/SearchFragment.java b/vlc-android/src/org/videolan/vlc/gui/tv/SearchFragment.java
index a0832b5c1..0ba895905 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/SearchFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/SearchFragment.java
@@ -22,6 +22,7 @@ package org.videolan.vlc.gui.tv;
 
 import android.annotation.TargetApi;
 import android.app.Activity;
+import android.app.SearchManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -39,6 +40,7 @@ import android.support.v17.leanback.widget.OnItemViewClickedListener;
 import android.support.v17.leanback.widget.Presenter;
 import android.support.v17.leanback.widget.Row;
 import android.support.v17.leanback.widget.RowPresenter;
+import android.support.v17.leanback.widget.SpeechRecognitionCallback;
 import android.support.v4.content.LocalBroadcastManager;
 import android.text.TextUtils;
 
@@ -47,6 +49,7 @@ import org.videolan.medialibrary.media.MediaWrapper;
 import org.videolan.medialibrary.media.SearchAggregate;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
+import org.videolan.vlc.util.Util;
 
 import java.util.Arrays;
 
@@ -54,9 +57,10 @@ import java.util.Arrays;
 public class SearchFragment extends SearchSupportFragment implements SearchSupportFragment.SearchResultProvider {
 
     private static final String TAG = "SearchFragment";
+    private static final int REQUEST_SPEECH = 1;
 
     private ArrayObjectAdapter mRowsAdapter;
-    private Handler mHandler = new Handler();
+    private final Handler mHandler = new Handler();
     private SearchRunnable mDelayedLoad;
     protected Activity mActivity;
 
@@ -69,6 +73,20 @@ public class SearchFragment extends SearchSupportFragment implements SearchSuppo
         setOnItemViewClickedListener(getDefaultItemClickedListener());
         mDelayedLoad = new SearchRunnable();
         mActivity = getActivity();
+        final Intent recognitionIntent = getRecognizerIntent();
+        if (Util.isCallable(recognitionIntent)) {
+            final SpeechRecognitionCallback speechRecognitionCallback = new SpeechRecognitionCallback() {
+                @Override
+                public void recognizeSpeech() {
+                    startActivityForResult(recognitionIntent, REQUEST_SPEECH);
+                }
+            };
+            setSpeechRecognitionCallback(speechRecognitionCallback);
+        }
+        final Intent intent = mActivity.getIntent();
+        if (Intent.ACTION_SEARCH.equals(intent.getAction())
+                || "com.google.android.gms.actions.SEARCH_ACTION".equals(intent.getAction()))
+            onQueryTextSubmit(intent.getStringExtra(SearchManager.QUERY));
     }
 
     @Override
@@ -101,8 +119,7 @@ public class SearchFragment extends SearchSupportFragment implements SearchSuppo
 
     @Override
     public boolean onQueryTextChange(String newQuery) {
-        queryByWords(newQuery);
-        return true;
+        return false;
     }
 
     @Override
@@ -112,7 +129,7 @@ public class SearchFragment extends SearchSupportFragment implements SearchSuppo
     }
 
     private void loadRows(String query) {
-        SearchAggregate searchAggregate = VLCApplication.getMLInstance().search(query);
+        final SearchAggregate searchAggregate = VLCApplication.getMLInstance().search(query);
         CardPresenter cp = new CardPresenter(mActivity);
         final ArrayObjectAdapter videoAdapter = new ArrayObjectAdapter(cp);
         videoAdapter.addAll(0, Arrays.asList(searchAggregate.getMediaSearchAggregate().getOthers()));
@@ -176,4 +193,10 @@ public class SearchFragment extends SearchSupportFragment implements SearchSuppo
             this.searchQuery = value;
         }
     }
+
+    @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        if (requestCode == REQUEST_SPEECH && resultCode == Activity.RESULT_OK)
+            setSearchQuery(data, true);
+    }
 }



More information about the Android mailing list