Added suggestion to the searching

This commit is contained in:
madiyar 2016-02-23 15:01:59 +06:00
parent 3301d8b4fb
commit c7679bec87
2 changed files with 89 additions and 5 deletions

View file

@ -3,10 +3,10 @@ package org.schabi.newpipe;
import android.content.Context;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;
import java.util.ArrayList;

View file

@ -3,6 +3,7 @@ package org.schabi.newpipe;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
@ -14,10 +15,14 @@ import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import java.util.ArrayList;
import org.schabi.newpipe.extractor.VideoPreviewInfo;
import org.schabi.newpipe.extractor.ExtractionException;
import org.schabi.newpipe.extractor.SearchEngine;
import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.StreamingService;
import org.schabi.newpipe.extractor.VideoPreviewInfo;
import java.io.IOException;
import java.util.ArrayList;
/**
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
@ -61,6 +66,11 @@ public class VideoItemListActivity extends AppCompatActivity
private VideoItemDetailFragment videoFragment = null;
private Menu menu = null;
private SuggestionListAdapter suggestionListAdapter;
private StreamingService streamingService;
private SuggestionSearchRunnable suggestionSearchRunnable;
private Thread searchThread;
private class SearchVideoQueryListener implements SearchView.OnQueryTextListener {
@Override
@ -94,11 +104,70 @@ public class VideoItemListActivity extends AppCompatActivity
@Override
public boolean onQueryTextChange(String newText) {
searchSuggestions(newText);
return true;
}
}
private class SearchSuggestionListener implements SearchView.OnSuggestionListener{
private SearchView searchView;
private SearchSuggestionListener(SearchView searchView) {
this.searchView = searchView;
}
@Override
public boolean onSuggestionSelect(int position) {
String suggestion = suggestionListAdapter.getSuggestion(position);
searchView.setQuery(suggestion,true);
return false;
}
@Override
public boolean onSuggestionClick(int position) {
String suggestion = suggestionListAdapter.getSuggestion(position);
searchView.setQuery(suggestion,true);
return false;
}
}
private class SuggestionResultRunnable implements Runnable{
private ArrayList<String>suggestions;
private SuggestionResultRunnable(ArrayList<String> suggestions) {
this.suggestions = suggestions;
}
@Override
public void run() {
suggestionListAdapter.updateAdapter(suggestions);
}
}
private class SuggestionSearchRunnable implements Runnable{
private final SearchEngine engine;
private final String query;
final Handler h = new Handler();
private SuggestionSearchRunnable(SearchEngine engine, String query) {
this.engine = engine;
this.query = query;
}
@Override
public void run() {
try {
ArrayList<String>suggestions = engine.suggestionList(query,new Downloader());
h.post(new SuggestionResultRunnable(suggestions));
} catch (ExtractionException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
@ -112,11 +181,12 @@ public class VideoItemListActivity extends AppCompatActivity
//------ todo: remove this line when multiservice support is implemented ------
currentStreamingServiceId = ServiceList.getIdOfService("Youtube");
streamingService=ServiceList.getService(currentStreamingServiceId);
//-----------------------------------------------------------------------------
//to solve issue 38
listFragment = (VideoItemListFragment) getSupportFragmentManager()
.findFragmentById(R.id.videoitem_list);
listFragment.setStreamingService(ServiceList.getService(currentStreamingServiceId));
listFragment.setStreamingService(streamingService);
Bundle arguments = getIntent().getExtras();
@ -168,6 +238,9 @@ public class VideoItemListActivity extends AppCompatActivity
searchView.setIconifiedByDefault(false);
searchView.setIconified(false);
searchView.setOnQueryTextListener(new SearchVideoQueryListener());
suggestionListAdapter = new SuggestionListAdapter(this);
searchView.setSuggestionsAdapter(suggestionListAdapter);
searchView.setOnSuggestionListener(new SearchSuggestionListener(searchView));
} else {
searchView.setVisibility(View.GONE);
}
@ -237,6 +310,9 @@ public class VideoItemListActivity extends AppCompatActivity
searchView.setFocusable(false);
searchView.setOnQueryTextListener(
new SearchVideoQueryListener());
suggestionListAdapter = new SuggestionListAdapter(this);
searchView.setSuggestionsAdapter(suggestionListAdapter);
searchView.setOnSuggestionListener(new SearchSuggestionListener(searchView));
} else if (videoFragment != null){
videoFragment.onCreateOptionsMenu(menu, inflater);
@ -281,4 +357,12 @@ public class VideoItemListActivity extends AppCompatActivity
outState.putString(QUERY, searchQuery);
outState.putInt(STREAMING_SERVICE, currentStreamingServiceId);
}
private void searchSuggestions(String query) {
suggestionSearchRunnable = new SuggestionSearchRunnable(streamingService.getSearchEngineInstance(),
query);
searchThread = new Thread(suggestionSearchRunnable);
searchThread.start();
}
}