Merge branch 'master' of https://github.com/kamadi/NewPipe into kamadi-master

This commit is contained in:
Christian Schabesberger 2016-02-23 16:21:16 +01:00
commit 799a27ec84
5 changed files with 163 additions and 7 deletions

View file

@ -40,7 +40,7 @@ public class YoutubeSearchEngineTest extends AndroidTestCase {
result = engine.search("https://www.youtube.com/results?search_query=bla", result = engine.search("https://www.youtube.com/results?search_query=bla",
0, "de", new Downloader()); 0, "de", new Downloader());
suggestionReply = engine.suggestionList("hello", new Downloader()); suggestionReply = engine.suggestionList("hello","de",new Downloader());
} }
public void testIfNoErrorOccur() { public void testIfNoErrorOccur() {

View file

@ -0,0 +1,65 @@
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.TextView;
import java.util.ArrayList;
/**
* Created by Madiyar on 23.02.2016.
*/
public class SuggestionListAdapter extends CursorAdapter {
private String[] columns = new String[]{"_id", "title"};
public SuggestionListAdapter(Context context) {
super(context, null, false);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ViewHolder viewHolder;
View view = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_1, parent, false);
viewHolder = new ViewHolder();
viewHolder.suggestionTitle = (TextView) view.findViewById(android.R.id.text1);
view.setTag(viewHolder);
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.suggestionTitle.setText(cursor.getString(1));
}
public void updateAdapter(ArrayList<String> suggestions) {
MatrixCursor cursor = new MatrixCursor(columns);
int i = 0;
for (String s : suggestions) {
String[] temp = new String[2];
temp[0] = Integer.toString(i);
temp[1] = s;
i++;
cursor.addRow(temp);
}
changeCursor(cursor);
}
public String getSuggestion(int position) {
return ((Cursor) getItem(position)).getString(1);
}
private class ViewHolder {
public TextView suggestionTitle;
}
}

View file

@ -2,7 +2,9 @@ package org.schabi.newpipe;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils; import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
@ -14,10 +16,14 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
import java.util.ArrayList; import org.schabi.newpipe.extractor.ExtractionException;
import org.schabi.newpipe.extractor.SearchEngine;
import org.schabi.newpipe.extractor.VideoPreviewInfo;
import org.schabi.newpipe.extractor.ServiceList; 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> * Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
@ -61,6 +67,11 @@ public class VideoItemListActivity extends AppCompatActivity
private VideoItemDetailFragment videoFragment = null; private VideoItemDetailFragment videoFragment = null;
private Menu menu = null; private Menu menu = null;
private SuggestionListAdapter suggestionListAdapter;
private StreamingService streamingService;
private SuggestionSearchRunnable suggestionSearchRunnable;
private Thread searchThread;
private class SearchVideoQueryListener implements SearchView.OnQueryTextListener { private class SearchVideoQueryListener implements SearchView.OnQueryTextListener {
@Override @Override
@ -94,11 +105,75 @@ public class VideoItemListActivity extends AppCompatActivity
@Override @Override
public boolean onQueryTextChange(String newText) { public boolean onQueryTextChange(String newText) {
searchSuggestions(newText);
return true; 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 Context context;
private SuggestionSearchRunnable(SearchEngine engine, String query) {
this.engine = engine;
this.query = query;
context = VideoItemListActivity.this;
}
@Override
public void run() {
try {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
String searchLanguageKey = context.getString(R.string.search_language_key);
String searchLanguage = sp.getString(searchLanguageKey,
getString(R.string.default_language_value));
ArrayList<String>suggestions = engine.suggestionList(query,searchLanguage,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 * Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device. * device.
@ -112,11 +187,12 @@ public class VideoItemListActivity extends AppCompatActivity
//------ todo: remove this line when multiservice support is implemented ------ //------ todo: remove this line when multiservice support is implemented ------
currentStreamingServiceId = ServiceList.getIdOfService("Youtube"); currentStreamingServiceId = ServiceList.getIdOfService("Youtube");
streamingService=ServiceList.getService(currentStreamingServiceId);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
//to solve issue 38 //to solve issue 38
listFragment = (VideoItemListFragment) getSupportFragmentManager() listFragment = (VideoItemListFragment) getSupportFragmentManager()
.findFragmentById(R.id.videoitem_list); .findFragmentById(R.id.videoitem_list);
listFragment.setStreamingService(ServiceList.getService(currentStreamingServiceId)); listFragment.setStreamingService(streamingService);
Bundle arguments = getIntent().getExtras(); Bundle arguments = getIntent().getExtras();
@ -168,6 +244,9 @@ public class VideoItemListActivity extends AppCompatActivity
searchView.setIconifiedByDefault(false); searchView.setIconifiedByDefault(false);
searchView.setIconified(false); searchView.setIconified(false);
searchView.setOnQueryTextListener(new SearchVideoQueryListener()); searchView.setOnQueryTextListener(new SearchVideoQueryListener());
suggestionListAdapter = new SuggestionListAdapter(this);
searchView.setSuggestionsAdapter(suggestionListAdapter);
searchView.setOnSuggestionListener(new SearchSuggestionListener(searchView));
} else { } else {
searchView.setVisibility(View.GONE); searchView.setVisibility(View.GONE);
} }
@ -237,6 +316,9 @@ public class VideoItemListActivity extends AppCompatActivity
searchView.setFocusable(false); searchView.setFocusable(false);
searchView.setOnQueryTextListener( searchView.setOnQueryTextListener(
new SearchVideoQueryListener()); new SearchVideoQueryListener());
suggestionListAdapter = new SuggestionListAdapter(this);
searchView.setSuggestionsAdapter(suggestionListAdapter);
searchView.setOnSuggestionListener(new SearchSuggestionListener(searchView));
} else if (videoFragment != null){ } else if (videoFragment != null){
videoFragment.onCreateOptionsMenu(menu, inflater); videoFragment.onCreateOptionsMenu(menu, inflater);
@ -281,4 +363,12 @@ public class VideoItemListActivity extends AppCompatActivity
outState.putString(QUERY, searchQuery); outState.putString(QUERY, searchQuery);
outState.putInt(STREAMING_SERVICE, currentStreamingServiceId); outState.putInt(STREAMING_SERVICE, currentStreamingServiceId);
} }
private void searchSuggestions(String query) {
suggestionSearchRunnable = new SuggestionSearchRunnable(streamingService.getSearchEngineInstance(),
query);
searchThread = new Thread(suggestionSearchRunnable);
searchThread.start();
}
} }

View file

@ -33,7 +33,7 @@ public interface SearchEngine {
public final List<VideoPreviewInfo> resultList = new Vector<>(); public final List<VideoPreviewInfo> resultList = new Vector<>();
} }
ArrayList<String> suggestionList(String query, Downloader dl) ArrayList<String> suggestionList(String query,String contentCountry, Downloader dl)
throws ExtractionException, IOException; throws ExtractionException, IOException;
//Result search(String query, int page); //Result search(String query, int page);

View file

@ -147,7 +147,7 @@ public class YoutubeSearchEngine implements SearchEngine {
} }
@Override @Override
public ArrayList<String> suggestionList(String query, Downloader dl) public ArrayList<String> suggestionList(String query,String contentCountry, Downloader dl)
throws IOException, ParsingException { throws IOException, ParsingException {
ArrayList<String> suggestions = new ArrayList<>(); ArrayList<String> suggestions = new ArrayList<>();
@ -160,6 +160,7 @@ public class YoutubeSearchEngine implements SearchEngine {
.appendQueryParameter("client", "") .appendQueryParameter("client", "")
.appendQueryParameter("output", "toolbar") .appendQueryParameter("output", "toolbar")
.appendQueryParameter("ds", "yt") .appendQueryParameter("ds", "yt")
.appendQueryParameter("hl",contentCountry)
.appendQueryParameter("q", query); .appendQueryParameter("q", query);
String url = builder.build().toString(); String url = builder.build().toString();