2015-09-04 00:15:03 +00:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2016-02-23 10:22:49 +00:00
|
|
|
import android.content.SharedPreferences;
|
2015-09-04 00:15:03 +00:00
|
|
|
import android.os.Bundle;
|
2016-02-23 09:01:59 +00:00
|
|
|
import android.os.Handler;
|
2016-01-01 21:09:36 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2015-11-02 18:57:47 +00:00
|
|
|
import android.support.v4.app.NavUtils;
|
2015-09-04 00:15:03 +00:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2015-10-15 21:25:53 +00:00
|
|
|
import android.support.v7.widget.SearchView;
|
2015-11-29 12:06:27 +00:00
|
|
|
import android.util.Log;
|
2015-09-04 00:15:03 +00:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
2016-02-23 15:34:14 +00:00
|
|
|
import android.widget.Toast;
|
2015-09-04 00:15:03 +00:00
|
|
|
|
2016-02-23 09:01:59 +00:00
|
|
|
import org.schabi.newpipe.extractor.ExtractionException;
|
|
|
|
import org.schabi.newpipe.extractor.SearchEngine;
|
2016-02-18 10:50:22 +00:00
|
|
|
import org.schabi.newpipe.extractor.ServiceList;
|
2016-02-23 09:01:59 +00:00
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
2015-11-02 18:57:47 +00:00
|
|
|
|
2015-09-04 00:15:03 +00:00
|
|
|
/**
|
|
|
|
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
|
|
|
|
* VideoItemListActivity.java is part of NewPipe.
|
|
|
|
*
|
|
|
|
* NewPipe is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* NewPipe is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class VideoItemListActivity extends AppCompatActivity
|
|
|
|
implements VideoItemListFragment.Callbacks {
|
|
|
|
|
|
|
|
private static final String TAG = VideoItemListFragment.class.toString();
|
2015-11-02 18:57:47 +00:00
|
|
|
|
|
|
|
// arguments to give to this activity
|
|
|
|
public static final String VIDEO_INFO_ITEMS = "video_info_items";
|
|
|
|
|
|
|
|
// savedInstanceBundle arguments
|
2015-09-04 00:15:03 +00:00
|
|
|
private static final String QUERY = "query";
|
|
|
|
private static final String STREAMING_SERVICE = "streaming_service";
|
|
|
|
|
2015-11-02 18:57:47 +00:00
|
|
|
// activity modes
|
|
|
|
private static final int SEARCH_MODE = 0;
|
|
|
|
private static final int PRESENT_VIDEOS_MODE = 1;
|
|
|
|
|
|
|
|
private int mode = SEARCH_MODE;
|
2015-09-04 00:15:03 +00:00
|
|
|
private int currentStreamingServiceId = -1;
|
|
|
|
private String searchQuery = "";
|
|
|
|
|
|
|
|
private VideoItemListFragment listFragment;
|
2015-10-29 16:56:35 +00:00
|
|
|
private VideoItemDetailFragment videoFragment = null;
|
2015-11-29 12:06:27 +00:00
|
|
|
private Menu menu = null;
|
2015-09-04 00:15:03 +00:00
|
|
|
|
2016-02-23 09:01:59 +00:00
|
|
|
private SuggestionListAdapter suggestionListAdapter;
|
|
|
|
private StreamingService streamingService;
|
|
|
|
private SuggestionSearchRunnable suggestionSearchRunnable;
|
|
|
|
private Thread searchThread;
|
|
|
|
|
2015-11-29 12:06:27 +00:00
|
|
|
private class SearchVideoQueryListener implements SearchView.OnQueryTextListener {
|
2015-09-04 00:15:03 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextSubmit(String query) {
|
|
|
|
try {
|
|
|
|
searchQuery = query;
|
|
|
|
listFragment.search(query);
|
|
|
|
|
|
|
|
// hide virtual keyboard
|
|
|
|
InputMethodManager inputManager =
|
|
|
|
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
2015-11-29 12:06:27 +00:00
|
|
|
try {
|
|
|
|
//noinspection ConstantConditions
|
|
|
|
inputManager.hideSoftInputFromWindow(
|
|
|
|
getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
|
|
|
|
} catch(NullPointerException e) {
|
|
|
|
Log.e(TAG, "Could not get widget with focus");
|
2016-02-23 15:34:14 +00:00
|
|
|
Toast.makeText(VideoItemListActivity.this, "Could not get widget with focus",
|
|
|
|
Toast.LENGTH_SHORT).show();
|
2015-11-29 12:06:27 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2015-09-04 00:15:03 +00:00
|
|
|
// clear focus
|
|
|
|
// 1. to not open up the keyboard after switching back to this
|
|
|
|
// 2. It's a workaround to a seeming bug by the Android OS it self, causing
|
|
|
|
// onQueryTextSubmit to trigger twice when focus is not cleared.
|
|
|
|
// See: http://stackoverflow.com/questions/17874951/searchview-onquerytextsubmit-runs-twice-while-i-pressed-once
|
|
|
|
getCurrentFocus().clearFocus();
|
|
|
|
} catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onQueryTextChange(String newText) {
|
2016-02-23 22:36:02 +00:00
|
|
|
if(!newText.isEmpty()) {
|
|
|
|
searchSuggestions(newText);
|
|
|
|
}
|
2015-09-04 00:15:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-02-23 09:01:59 +00:00
|
|
|
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();
|
2016-02-23 10:22:49 +00:00
|
|
|
private Context context;
|
2016-02-23 09:01:59 +00:00
|
|
|
private SuggestionSearchRunnable(SearchEngine engine, String query) {
|
|
|
|
this.engine = engine;
|
|
|
|
this.query = query;
|
2016-02-23 10:22:49 +00:00
|
|
|
context = VideoItemListActivity.this;
|
2016-02-23 09:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2016-02-23 10:22:49 +00:00
|
|
|
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());
|
2016-02-23 09:01:59 +00:00
|
|
|
h.post(new SuggestionResultRunnable(suggestions));
|
|
|
|
} catch (ExtractionException e) {
|
2016-03-01 17:43:36 +00:00
|
|
|
ErrorActivity.reportError(h, VideoItemListActivity.this, e, null, findViewById(R.id.videoitem_list),
|
|
|
|
ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED,
|
|
|
|
/* todo: this shoudl not be assigned static */ "Youtube", query, R.string.parsing_error));
|
2016-02-23 09:01:59 +00:00
|
|
|
e.printStackTrace();
|
|
|
|
} catch (IOException e) {
|
2016-02-23 15:42:04 +00:00
|
|
|
postNewErrorToast(h, R.string.network_error);
|
2016-02-23 09:01:59 +00:00
|
|
|
e.printStackTrace();
|
2016-03-01 17:43:36 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
ErrorActivity.reportError(h, VideoItemListActivity.this, e, null, findViewById(R.id.videoitem_list),
|
|
|
|
ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED,
|
|
|
|
/* todo: this shoudl not be assigned static */ "Youtube", query, R.string.general_error));
|
2016-02-23 09:01:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-04 00:15:03 +00:00
|
|
|
/**
|
|
|
|
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
|
|
|
|
* device.
|
|
|
|
*/
|
|
|
|
private boolean mTwoPane;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.activity_videoitem_list);
|
|
|
|
|
2016-03-04 13:10:41 +00:00
|
|
|
try {
|
|
|
|
//------ todo: remove this line when multiservice support is implemented ------
|
|
|
|
currentStreamingServiceId = ServiceList.getIdOfService("Youtube");
|
|
|
|
streamingService = ServiceList.getService(currentStreamingServiceId);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
ErrorActivity.reportError(VideoItemListActivity.this, e, null, findViewById(R.id.videoitem_list),
|
|
|
|
ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED,
|
|
|
|
/* todo: this shoudl not be assigned static */ "Youtube", "", R.string.general_error));
|
|
|
|
}
|
2015-09-04 00:15:03 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2016-01-29 14:37:15 +00:00
|
|
|
//to solve issue 38
|
2015-11-02 15:03:11 +00:00
|
|
|
listFragment = (VideoItemListFragment) getSupportFragmentManager()
|
2015-09-04 00:15:03 +00:00
|
|
|
.findFragmentById(R.id.videoitem_list);
|
2016-02-23 09:01:59 +00:00
|
|
|
listFragment.setStreamingService(streamingService);
|
2015-09-04 00:15:03 +00:00
|
|
|
|
2015-11-02 18:57:47 +00:00
|
|
|
if(savedInstanceState != null
|
|
|
|
&& mode != PRESENT_VIDEOS_MODE) {
|
2015-09-04 00:15:03 +00:00
|
|
|
searchQuery = savedInstanceState.getString(QUERY);
|
|
|
|
currentStreamingServiceId = savedInstanceState.getInt(STREAMING_SERVICE);
|
|
|
|
if(!searchQuery.isEmpty()) {
|
|
|
|
listFragment.search(searchQuery);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (findViewById(R.id.videoitem_detail_container) != null) {
|
|
|
|
// The detail container view will be present only in the
|
|
|
|
// large-screen layouts (res/values-large and
|
|
|
|
// res/values-sw600dp). If this view is present, then the
|
|
|
|
// activity should be in two-pane mode.
|
|
|
|
mTwoPane = true;
|
|
|
|
|
|
|
|
// In two-pane mode, list items should be given the
|
|
|
|
// 'activated' state when touched.
|
|
|
|
|
|
|
|
((VideoItemListFragment) getSupportFragmentManager()
|
|
|
|
.findFragmentById(R.id.videoitem_list))
|
|
|
|
.setActivateOnItemClick(true);
|
|
|
|
|
|
|
|
SearchView searchView = (SearchView)findViewById(R.id.searchViewTablet);
|
2015-11-02 18:57:47 +00:00
|
|
|
if(mode != PRESENT_VIDEOS_MODE) {
|
|
|
|
// Somehow the seticonifiedbydefault property set by the layout xml is not working on
|
|
|
|
// the support version on SearchView, so it needs to be set programmatically.
|
|
|
|
searchView.setIconifiedByDefault(false);
|
|
|
|
searchView.setIconified(false);
|
|
|
|
searchView.setOnQueryTextListener(new SearchVideoQueryListener());
|
2016-02-23 09:01:59 +00:00
|
|
|
suggestionListAdapter = new SuggestionListAdapter(this);
|
|
|
|
searchView.setSuggestionsAdapter(suggestionListAdapter);
|
|
|
|
searchView.setOnSuggestionListener(new SearchSuggestionListener(searchView));
|
2015-11-02 18:57:47 +00:00
|
|
|
} else {
|
|
|
|
searchView.setVisibility(View.GONE);
|
|
|
|
}
|
2015-09-04 00:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-01-05 19:56:40 +00:00
|
|
|
PreferenceManager.setDefaultValues(this, R.xml.settings, false);
|
2015-09-04 00:15:03 +00:00
|
|
|
}
|
|
|
|
|
2016-01-01 22:04:29 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
App.checkStartTor(this);
|
|
|
|
}
|
|
|
|
|
2015-09-04 00:15:03 +00:00
|
|
|
/**
|
|
|
|
* Callback method from {@link VideoItemListFragment.Callbacks}
|
|
|
|
* indicating that the item with the given ID was selected.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void onItemSelected(String id) {
|
|
|
|
VideoListAdapter listAdapter = (VideoListAdapter) ((VideoItemListFragment)
|
|
|
|
getSupportFragmentManager()
|
|
|
|
.findFragmentById(R.id.videoitem_list))
|
|
|
|
.getListAdapter();
|
|
|
|
String webpage_url = listAdapter.getVideoList().get((int) Long.parseLong(id)).webpage_url;
|
|
|
|
if (mTwoPane) {
|
|
|
|
// In two-pane mode, show the detail view in this activity by
|
|
|
|
// adding or replacing the detail fragment using a
|
|
|
|
// fragment transaction.
|
|
|
|
Bundle arguments = new Bundle();
|
2015-12-24 23:09:35 +00:00
|
|
|
//arguments.putString(VideoItemDetailFragment.ARG_ITEM_ID, id);
|
2015-09-04 00:15:03 +00:00
|
|
|
arguments.putString(VideoItemDetailFragment.VIDEO_URL, webpage_url);
|
|
|
|
arguments.putInt(VideoItemDetailFragment.STREAMING_SERVICE, currentStreamingServiceId);
|
2015-10-29 16:56:35 +00:00
|
|
|
videoFragment = new VideoItemDetailFragment();
|
|
|
|
videoFragment.setArguments(arguments);
|
|
|
|
videoFragment.setOnInvokeCreateOptionsMenuListener(new VideoItemDetailFragment.OnInvokeCreateOptionsMenuListener() {
|
|
|
|
@Override
|
|
|
|
public void createOptionsMenu() {
|
|
|
|
menu.clear();
|
|
|
|
onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
});
|
2015-09-04 00:15:03 +00:00
|
|
|
getSupportFragmentManager().beginTransaction()
|
2015-10-29 16:56:35 +00:00
|
|
|
.replace(R.id.videoitem_detail_container, videoFragment)
|
2015-09-04 00:15:03 +00:00
|
|
|
.commit();
|
|
|
|
} else {
|
|
|
|
// In single-pane mode, simply start the detail activity
|
|
|
|
// for the selected item ID.
|
|
|
|
Intent detailIntent = new Intent(this, VideoItemDetailActivity.class);
|
2015-12-24 23:09:35 +00:00
|
|
|
//detailIntent.putExtra(VideoItemDetailFragment.ARG_ITEM_ID, id);
|
2015-09-04 00:15:03 +00:00
|
|
|
detailIntent.putExtra(VideoItemDetailFragment.VIDEO_URL, webpage_url);
|
|
|
|
detailIntent.putExtra(VideoItemDetailFragment.STREAMING_SERVICE, currentStreamingServiceId);
|
|
|
|
startActivity(detailIntent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-29 16:56:35 +00:00
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
super.onCreateOptionsMenu(menu);
|
|
|
|
this.menu = menu;
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
2015-11-02 18:57:47 +00:00
|
|
|
if(mode != PRESENT_VIDEOS_MODE &&
|
|
|
|
findViewById(R.id.videoitem_detail_container) == null) {
|
2015-09-04 00:15:03 +00:00
|
|
|
inflater.inflate(R.menu.videoitem_list, menu);
|
|
|
|
MenuItem searchItem = menu.findItem(R.id.action_search);
|
|
|
|
SearchView searchView = (SearchView) searchItem.getActionView();
|
|
|
|
searchView.setFocusable(false);
|
|
|
|
searchView.setOnQueryTextListener(
|
|
|
|
new SearchVideoQueryListener());
|
2016-02-23 09:01:59 +00:00
|
|
|
suggestionListAdapter = new SuggestionListAdapter(this);
|
|
|
|
searchView.setSuggestionsAdapter(suggestionListAdapter);
|
|
|
|
searchView.setOnSuggestionListener(new SearchSuggestionListener(searchView));
|
2015-09-04 00:15:03 +00:00
|
|
|
|
2015-10-29 16:56:35 +00:00
|
|
|
} else if (videoFragment != null){
|
|
|
|
videoFragment.onCreateOptionsMenu(menu, inflater);
|
2015-09-04 00:15:03 +00:00
|
|
|
} else {
|
2015-10-29 16:56:35 +00:00
|
|
|
inflater.inflate(R.menu.videoitem_two_pannel, menu);
|
2015-09-04 00:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
int id = item.getItemId();
|
2015-11-02 18:57:47 +00:00
|
|
|
|
|
|
|
switch(id) {
|
|
|
|
case android.R.id.home: {
|
|
|
|
Intent intent = new Intent(this, VideoItemListActivity.class);
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
|
|
NavUtils.navigateUpTo(this, intent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
case R.id.action_settings: {
|
|
|
|
Intent intent = new Intent(this, SettingsActivity.class);
|
|
|
|
startActivity(intent);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return videoFragment.onOptionsItemSelected(item) ||
|
2015-10-29 16:56:35 +00:00
|
|
|
super.onOptionsItemSelected(item);
|
2015-09-04 00:15:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
/*
|
|
|
|
if (mActivatedPosition != ListView.INVALID_POSITION) {
|
|
|
|
// Serialize and persist the activated item position.
|
|
|
|
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
outState.putString(QUERY, searchQuery);
|
|
|
|
outState.putInt(STREAMING_SERVICE, currentStreamingServiceId);
|
|
|
|
}
|
2016-02-23 09:01:59 +00:00
|
|
|
|
|
|
|
private void searchSuggestions(String query) {
|
2016-03-04 12:36:06 +00:00
|
|
|
suggestionSearchRunnable =
|
|
|
|
new SuggestionSearchRunnable(streamingService.getSearchEngineInstance(new Downloader()),
|
2016-02-23 09:01:59 +00:00
|
|
|
query);
|
|
|
|
searchThread = new Thread(suggestionSearchRunnable);
|
|
|
|
searchThread.start();
|
|
|
|
|
|
|
|
}
|
2016-02-23 15:42:04 +00:00
|
|
|
|
|
|
|
private void postNewErrorToast(Handler h, final int stringResource) {
|
|
|
|
h.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Toast.makeText(VideoItemListActivity.this, getString(stringResource),
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-09-04 00:15:03 +00:00
|
|
|
}
|