NewPipe/app/src/main/java/org/schabi/newpipe/fragments/search/SearchFragment.java

441 lines
16 KiB
Java
Raw Normal View History

package org.schabi.newpipe.fragments.search;
2016-08-02 15:06:02 +02:00
import android.app.Activity;
import android.content.Context;
2016-08-02 20:59:53 +02:00
import android.content.Intent;
2016-08-02 15:06:02 +02:00
import android.os.Bundle;
import android.support.annotation.Nullable;
2016-08-02 15:06:02 +02:00
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
2016-08-02 15:06:02 +02:00
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Log;
2016-08-02 15:06:02 +02:00
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
2016-08-03 17:18:05 +02:00
import android.widget.ProgressBar;
2016-08-02 15:06:02 +02:00
import android.widget.Toast;
import org.schabi.newpipe.R;
import org.schabi.newpipe.ReCaptchaActivity;
2016-09-27 22:59:04 +02:00
import org.schabi.newpipe.extractor.NewPipe;
2017-02-13 00:55:05 +01:00
import org.schabi.newpipe.extractor.search.SearchEngine;
2016-09-28 17:13:15 +02:00
import org.schabi.newpipe.extractor.search.SearchResult;
import org.schabi.newpipe.fragments.OnItemSelectedListener;
2016-09-26 17:02:55 +02:00
import org.schabi.newpipe.info_list.InfoItemBuilder;
2016-08-03 00:54:03 +02:00
import org.schabi.newpipe.info_list.InfoListAdapter;
import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.util.NavigationHelper;
2016-08-02 15:06:02 +02:00
2017-02-13 00:55:05 +01:00
import java.util.EnumSet;
import static android.app.Activity.RESULT_OK;
import static org.schabi.newpipe.ReCaptchaActivity.RECAPTCHA_REQUEST;
2016-08-02 15:06:02 +02:00
/**
* Created by Christian Schabesberger on 02.08.16.
* <p>
2016-09-12 00:33:11 +02:00
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
* SearchFragment.java is part of NewPipe.
* <p>
2016-09-12 00:33:11 +02:00
* 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.
* <p>
2016-09-12 00:33:11 +02:00
* 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.
* <p>
2016-09-12 00:33:11 +02:00
* You should have received a copy of the GNU General Public License
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
2016-08-02 15:06:02 +02:00
*/
public class SearchFragment extends Fragment implements SearchView.OnQueryTextListener, SearchWorker.SearchWorkerResultListener {
2016-08-02 15:06:02 +02:00
private static final String TAG = SearchFragment.class.toString();
2016-08-02 15:06:02 +02:00
// savedInstanceBundle arguments
private static final String QUERY = "query";
private static final String STREAMING_SERVICE = "streaming_service";
2016-08-02 15:06:02 +02:00
private int streamingServiceId = -1;
private String searchQuery = "";
2016-08-02 20:59:53 +02:00
private boolean isLoading = false;
2016-08-02 15:06:02 +02:00
@SuppressWarnings("FieldCanBeLocal")
private SearchView searchView;
private RecyclerView recyclerView;
private ProgressBar loadingIndicator;
2016-08-02 20:59:53 +02:00
private int pageNumber = 0;
private SuggestionListAdapter suggestionListAdapter;
private InfoListAdapter infoListAdapter;
private LinearLayoutManager streamInfoListLayoutManager;
2016-08-02 15:06:02 +02:00
private EnumSet<SearchEngine.Filter> filter = EnumSet.of(SearchEngine.Filter.CHANNEL, SearchEngine.Filter.STREAM);
private OnItemSelectedListener onItemSelectedListener;
2016-08-02 15:06:02 +02:00
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public SearchFragment() {
2016-08-02 15:06:02 +02:00
}
@SuppressWarnings("unused")
public static SearchFragment newInstance(int streamingServiceId, String searchQuery) {
Bundle args = new Bundle();
args.putInt(STREAMING_SERVICE, streamingServiceId);
args.putString(QUERY, searchQuery);
SearchFragment fragment = new SearchFragment();
fragment.setArguments(args);
2016-08-02 15:06:02 +02:00
return fragment;
}
/*//////////////////////////////////////////////////////////////////////////
// Fragment's LifeCycle
//////////////////////////////////////////////////////////////////////////*/
@Override
public void onAttach(Context context) {
super.onAttach(context);
onItemSelectedListener = ((OnItemSelectedListener) context);
}
2016-08-02 15:06:02 +02:00
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
searchQuery = "";
isLoading = false;
2017-01-03 14:28:11 +01:00
if (savedInstanceState != null) {
2016-08-02 15:06:02 +02:00
searchQuery = savedInstanceState.getString(QUERY);
streamingServiceId = savedInstanceState.getInt(STREAMING_SERVICE);
} else {
try {
Bundle args = getArguments();
if (args != null) {
searchQuery = args.getString(QUERY);
streamingServiceId = args.getInt(STREAMING_SERVICE);
} else {
streamingServiceId = NewPipe.getIdOfService("Youtube");
}
2017-01-03 14:28:11 +01:00
} catch (Exception e) {
2016-08-02 15:06:02 +02:00
e.printStackTrace();
ErrorActivity.reportError(getActivity(), e, null,
getActivity().findViewById(android.R.id.content),
ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED,
2016-09-27 22:59:04 +02:00
NewPipe.getNameOfService(streamingServiceId),
2016-08-02 15:06:02 +02:00
"", R.string.general_error));
}
}
2016-08-02 18:38:05 +02:00
setHasOptionsMenu(true);
2016-08-02 18:38:05 +02:00
SearchWorker sw = SearchWorker.getInstance();
sw.setSearchWorkerResultListener(this);
2016-08-02 15:06:02 +02:00
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
2016-08-02 15:06:02 +02:00
2016-08-03 17:18:05 +02:00
Context context = view.getContext();
loadingIndicator = (ProgressBar) view.findViewById(R.id.loading_progress_bar);
recyclerView = (RecyclerView) view.findViewById(R.id.list);
2016-08-03 17:18:05 +02:00
streamInfoListLayoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(streamInfoListLayoutManager);
2016-08-02 20:59:53 +02:00
2016-08-03 17:18:05 +02:00
infoListAdapter = new InfoListAdapter(getActivity(),
getActivity().findViewById(android.R.id.content));
2017-02-28 13:24:07 +01:00
infoListAdapter.setFooter(inflater.inflate(R.layout.pignate_footer, recyclerView, false));
infoListAdapter.showFooter(false);
infoListAdapter.setOnStreamInfoItemSelectedListener(new InfoItemBuilder.OnInfoItemSelectedListener() {
2016-08-03 17:18:05 +02:00
@Override
public void selected(int serviceId, String url, String title) {
NavigationHelper.openVideoDetail(onItemSelectedListener, serviceId, url, title);
2016-08-03 17:18:05 +02:00
}
});
2017-02-15 12:59:36 +01:00
infoListAdapter.setOnChannelInfoItemSelectedListener(new InfoItemBuilder.OnInfoItemSelectedListener() {
@Override
public void selected(int serviceId, String url, String title) {
NavigationHelper.openChannel(onItemSelectedListener, serviceId, url, title);
2017-02-15 12:59:36 +01:00
}
});
2016-08-03 17:18:05 +02:00
recyclerView.setAdapter(infoListAdapter);
recyclerView.clearOnScrollListeners();
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
2016-08-03 17:18:05 +02:00
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int pastVisiblesItems, visibleItemCount, totalItemCount;
super.onScrolled(recyclerView, dx, dy);
2017-01-03 14:28:11 +01:00
if (dy > 0) //check for scroll down
2016-08-03 17:18:05 +02:00
{
visibleItemCount = streamInfoListLayoutManager.getChildCount();
totalItemCount = streamInfoListLayoutManager.getItemCount();
pastVisiblesItems = streamInfoListLayoutManager.findFirstVisibleItemPosition();
2017-01-03 14:28:11 +01:00
if ((visibleItemCount + pastVisiblesItems) >= totalItemCount && !isLoading) {
2016-08-03 17:18:05 +02:00
pageNumber++;
recyclerView.post(new Runnable() {
@Override
public void run() {
infoListAdapter.showFooter(true);
}
});
2016-08-03 17:18:05 +02:00
search(searchQuery, pageNumber);
2016-08-02 20:59:53 +02:00
}
}
2016-08-03 17:18:05 +02:00
}
});
2016-08-02 18:38:05 +02:00
2016-08-02 15:06:02 +02:00
return view;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (!searchQuery.isEmpty()) {
search(searchQuery);
}
2016-08-02 15:06:02 +02:00
}
@Override
public void onDestroyView() {
super.onDestroyView();
recyclerView.removeAllViews();
infoListAdapter.clearSteamItemList();
recyclerView = null;
}
@Override
public void onResume() {
super.onResume();
if (isLoading && !searchQuery.isEmpty()) {
search(searchQuery);
}
}
@Override
public void onStop() {
super.onStop();
SearchWorker.getInstance().terminate();
}
2016-08-02 15:06:02 +02:00
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(QUERY, searchQuery);
outState.putInt(STREAMING_SERVICE, streamingServiceId);
2016-08-02 15:06:02 +02:00
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RECAPTCHA_REQUEST:
if (resultCode == RESULT_OK && searchQuery.length() != 0) {
search(searchQuery);
} else Log.e(TAG, "ReCaptcha failed");
break;
default:
Log.e(TAG, "Request code from activity not supported [" + requestCode + "]");
break;
}
}
/*//////////////////////////////////////////////////////////////////////////
// Menu
//////////////////////////////////////////////////////////////////////////*/
2016-08-02 15:06:02 +02:00
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
ActionBar supportActionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setDisplayHomeAsUpEnabled(false);
supportActionBar.setDisplayShowTitleEnabled(false);
//noinspection deprecation
supportActionBar.setNavigationMode(0);
}
2016-08-02 15:06:02 +02:00
inflater.inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) searchItem.getActionView();
2016-08-02 15:06:02 +02:00
setupSearchView(searchView);
}
2017-02-15 15:21:36 +01:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
2017-02-15 15:21:36 +01:00
case R.id.menu_filter_all:
2017-02-16 00:17:43 +01:00
changeFilter(item, EnumSet.of(SearchEngine.Filter.STREAM, SearchEngine.Filter.CHANNEL));
2017-02-15 15:21:36 +01:00
return true;
case R.id.menu_filter_video:
2017-02-16 00:17:43 +01:00
changeFilter(item, EnumSet.of(SearchEngine.Filter.STREAM));
2017-02-15 15:21:36 +01:00
return true;
case R.id.menu_filter_channel:
changeFilter(item, EnumSet.of(SearchEngine.Filter.CHANNEL));
return true;
default:
return false;
}
}
/*//////////////////////////////////////////////////////////////////////////
// Utils
//////////////////////////////////////////////////////////////////////////*/
2017-02-15 15:21:36 +01:00
private void changeFilter(MenuItem item, EnumSet<SearchEngine.Filter> filter) {
this.filter = filter;
item.setChecked(true);
if (searchQuery != null && !searchQuery.isEmpty()) {
Log.e(TAG, "Fuck+ " + searchQuery);
2017-02-15 15:21:36 +01:00
search(searchQuery);
}
}
2016-08-02 15:06:02 +02:00
private void setupSearchView(SearchView searchView) {
suggestionListAdapter = new SuggestionListAdapter(getActivity());
searchView.setSuggestionsAdapter(suggestionListAdapter);
searchView.setOnSuggestionListener(new SearchSuggestionListener(searchView, suggestionListAdapter));
searchView.setOnQueryTextListener(this);
2017-01-03 14:28:11 +01:00
if (searchQuery != null && !searchQuery.isEmpty()) {
2016-08-02 15:06:02 +02:00
searchView.setQuery(searchQuery, false);
searchView.setIconifiedByDefault(false);
}
}
private void search(String query) {
infoListAdapter.clearSteamItemList();
2017-02-28 13:24:07 +01:00
infoListAdapter.showFooter(false);
2016-08-02 20:59:53 +02:00
pageNumber = 0;
searchQuery = query;
2016-08-02 20:59:53 +02:00
search(query, pageNumber);
hideBackground();
2016-08-03 17:18:05 +02:00
loadingIndicator.setVisibility(View.VISIBLE);
2016-08-02 18:38:05 +02:00
}
2016-08-02 15:06:02 +02:00
2016-08-02 18:38:05 +02:00
private void search(String query, int page) {
2016-08-02 20:59:53 +02:00
isLoading = true;
2016-08-02 18:38:05 +02:00
SearchWorker sw = SearchWorker.getInstance();
2017-02-13 00:55:05 +01:00
sw.search(streamingServiceId,
query,
page,
getActivity(),
2017-02-15 15:21:36 +01:00
filter);
2016-08-02 15:06:02 +02:00
}
private void setDoneLoading() {
isLoading = false;
loadingIndicator.setVisibility(View.GONE);
infoListAdapter.showFooter(false);
}
/**
* Hides the "dummy" background when no results are shown
*/
private void hideBackground() {
View view = getView();
if (view == null) return;
view.findViewById(R.id.mainBG).setVisibility(View.GONE);
}
2016-08-02 15:06:02 +02:00
private void searchSuggestions(String query) {
SuggestionSearchRunnable suggestionSearchRunnable =
new SuggestionSearchRunnable(streamingServiceId, query, getActivity(), suggestionListAdapter);
Thread suggestionThread = new Thread(suggestionSearchRunnable);
suggestionThread.start();
}
public boolean isMainBgVisible() {
return getActivity().findViewById(R.id.mainBG).getVisibility() == View.VISIBLE;
}
/*//////////////////////////////////////////////////////////////////////////
// OnQueryTextListener
//////////////////////////////////////////////////////////////////////////*/
@Override
public boolean onQueryTextSubmit(String query) {
Activity a = getActivity();
try {
search(query);
// hide virtual keyboard
InputMethodManager inputManager =
(InputMethodManager) a.getSystemService(Context.INPUT_METHOD_SERVICE);
try {
//noinspection ConstantConditions
inputManager.hideSoftInputFromWindow(
a.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
} catch (NullPointerException e) {
e.printStackTrace();
ErrorActivity.reportError(a, e, null,
a.findViewById(android.R.id.content),
ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED,
NewPipe.getNameOfService(streamingServiceId),
"Could not get widget with focus", R.string.general_error));
}
// 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
a.getCurrentFocus().clearFocus();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if (!newText.isEmpty()) {
searchSuggestions(newText);
}
return true;
}
/*//////////////////////////////////////////////////////////////////////////
// SearchWorkerResultListener
//////////////////////////////////////////////////////////////////////////*/
@Override
public void onResult(SearchResult result) {
infoListAdapter.addInfoItemList(result.resultList);
setDoneLoading();
}
@Override
public void onNothingFound(int stringResource) {
//setListShown(true);
Toast.makeText(getActivity(), getString(stringResource), Toast.LENGTH_SHORT).show();
setDoneLoading();
}
@Override
public void onError(String message) {
//setListShown(true);
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
setDoneLoading();
}
@Override
public void onReCaptchaChallenge() {
Toast.makeText(getActivity(), "ReCaptcha Challenge requested",
Toast.LENGTH_LONG).show();
// Starting ReCaptcha Challenge Activity
startActivityForResult(new Intent(getActivity(), ReCaptchaActivity.class), RECAPTCHA_REQUEST);
}
2016-08-02 15:06:02 +02:00
}