NewPipe/app/src/main/java/org/schabi/newpipe/search_fragment/SearchInfoItemFragment.java

366 lines
14 KiB
Java
Raw Normal View History

2016-08-02 15:06:02 +02:00
package org.schabi.newpipe.search_fragment;
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.v4.app.Fragment;
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;
2017-02-15 12:59:36 +01:00
import org.schabi.newpipe.ChannelActivity;
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;
2016-09-26 17:02:55 +02:00
import org.schabi.newpipe.info_list.InfoItemBuilder;
2016-09-13 23:39:32 +02:00
import org.schabi.newpipe.report.ErrorActivity;
2016-08-02 15:06:02 +02:00
import org.schabi.newpipe.R;
2016-08-02 21:17:54 +02:00
import org.schabi.newpipe.detail.VideoItemDetailActivity;
import org.schabi.newpipe.detail.VideoItemDetailFragment;
2016-08-03 00:54:03 +02:00
import org.schabi.newpipe.info_list.InfoListAdapter;
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.
2016-09-12 00:33:11 +02:00
*
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
* SearchInfoItemFragment.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/>.
2016-08-02 15:06:02 +02:00
*/
public class SearchInfoItemFragment extends Fragment {
private static final String TAG = SearchInfoItemFragment.class.toString();
/**
* Listener for search queries
*/
2016-08-02 15:06:02 +02:00
public class SearchQueryListener implements SearchView.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);
2017-01-03 14:28:11 +01:00
} catch (NullPointerException e) {
2016-08-02 15:06:02 +02:00
e.printStackTrace();
ErrorActivity.reportError(a, e, null,
a.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
"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();
2017-01-03 14:28:11 +01:00
} catch (Exception e) {
2016-08-02 15:06:02 +02:00
e.printStackTrace();
}
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
2017-01-03 14:28:11 +01:00
if (!newText.isEmpty()) {
2016-08-02 15:06:02 +02:00
searchSuggestions(newText);
}
return true;
}
}
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
2016-08-03 17:18:05 +02:00
private ProgressBar loadingIndicator = null;
2016-08-02 20:59:53 +02:00
private int pageNumber = 0;
2016-08-02 15:06:02 +02:00
private SuggestionListAdapter suggestionListAdapter = null;
2016-08-03 00:54:03 +02:00
private InfoListAdapter infoListAdapter = null;
2016-08-02 20:59:53 +02:00
private LinearLayoutManager streamInfoListLayoutManager = null;
2016-08-02 15:06:02 +02:00
// savedInstanceBundle arguments
private static final String QUERY = "query";
private static final String STREAMING_SERVICE = "streaming_service";
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public SearchInfoItemFragment() {
}
@SuppressWarnings("unused")
public static SearchInfoItemFragment newInstance(int streamingServiceId, String searchQuery) {
Bundle args = new Bundle();
args.putInt(STREAMING_SERVICE, streamingServiceId);
args.putString(QUERY, searchQuery);
2016-08-02 15:06:02 +02:00
SearchInfoItemFragment fragment = new SearchInfoItemFragment();
fragment.setArguments(args);
2016-08-02 15:06:02 +02:00
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
searchQuery = "";
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(new SearchWorker.SearchWorkerResultListener() {
2016-08-02 18:38:05 +02:00
@Override
public void onResult(SearchResult result) {
2017-02-13 00:55:05 +01:00
infoListAdapter.addInfoItemList(result.resultList);
setDoneLoading();
2016-08-02 18:38:05 +02:00
}
@Override
public void onNothingFound(int stringResource) {
//setListShown(true);
Toast.makeText(getActivity(), getString(stringResource),
Toast.LENGTH_SHORT).show();
setDoneLoading();
2016-08-02 18:38:05 +02:00
}
@Override
public void onError(String message) {
//setListShown(true);
Toast.makeText(getActivity(), message,
Toast.LENGTH_LONG).show();
setDoneLoading();
2016-08-02 18:38:05 +02:00
}
@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 18:38:05 +02:00
});
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_searchinfoitem, container, false);
2016-08-03 17:18:05 +02:00
Context context = view.getContext();
loadingIndicator = (ProgressBar) view.findViewById(R.id.progressBar);
RecyclerView 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-15 12:59:36 +01:00
infoListAdapter.setOnStreamInfoItemSelectedListener(
2017-02-13 00:55:05 +01:00
new InfoItemBuilder.OnInfoItemSelectedListener() {
2016-08-03 17:18:05 +02:00
@Override
2017-02-15 12:59:36 +01:00
public void selected(String url, int serviceId) {
startDetailActivity(url);
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(String url, int serviceId) {
startChannelActivity(url, serviceId);
}
});
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++;
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;
}
private void startDetailActivity(String url) {
Intent i = new Intent(getActivity(), VideoItemDetailActivity.class);
i.putExtra(VideoItemDetailFragment.STREAMING_SERVICE, streamingServiceId);
i.putExtra(VideoItemDetailFragment.VIDEO_URL, url);
getActivity().startActivity(i);
}
2017-02-15 12:59:36 +01:00
private void startChannelActivity(String url, int serviceId) {
Intent i = new Intent(getActivity(), ChannelActivity.class);
i.putExtra(ChannelActivity.CHANNEL_URL, url);
i.putExtra(ChannelActivity.SERVICE_ID, serviceId);
startActivity(i);
}
2016-08-02 15:06:02 +02:00
@Override
public void onStart() {
super.onStart();
if(!searchQuery.isEmpty()) {
search(searchQuery);
}
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 onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
2016-08-02 15:06:02 +02:00
setupSearchView(searchView);
}
private void setupSearchView(SearchView searchView) {
suggestionListAdapter = new SuggestionListAdapter(getActivity());
searchView.setSuggestionsAdapter(suggestionListAdapter);
searchView.setOnSuggestionListener(new SearchSuggestionListener(searchView, suggestionListAdapter));
searchView.setOnQueryTextListener(new SearchQueryListener());
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();
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(),
EnumSet.of(SearchEngine.Filter.CHANNEL));
2016-08-02 15:06:02 +02:00
}
private void setDoneLoading() {
this.isLoading = false;
loadingIndicator.setVisibility(View.GONE);
}
/**
* 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();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RECAPTCHA_REQUEST:
if (resultCode == RESULT_OK) {
if (searchQuery.length() != 0) {
search(searchQuery);
}
} else {
Log.d(TAG, "ReCaptcha failed");
}
break;
default:
Log.e(TAG, "Request code from activity not supported [" + requestCode + "]");
break;
}
}
2016-08-02 15:06:02 +02:00
}