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

263 lines
10 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.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;
import android.widget.Toast;
import org.schabi.newpipe.ErrorActivity;
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-02 18:38:05 +02:00
import org.schabi.newpipe.extractor.SearchResult;
2016-08-02 15:06:02 +02:00
import org.schabi.newpipe.extractor.ServiceList;
2016-08-03 00:54:03 +02:00
import org.schabi.newpipe.info_list.InfoListAdapter;
2016-08-02 15:06:02 +02:00
/**
* Created by Christian Schabesberger on 02.08.16.
*/
public class SearchInfoItemFragment extends Fragment {
private static final String TAG = SearchInfoItemFragment.class.toString();
public class SearchQueryListener implements SearchView.OnQueryTextListener {
@Override
public boolean onQueryTextSubmit(String query) {
Activity a = getActivity();
try {
searchQuery = query;
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,
ServiceList.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();
}
View bg = a.findViewById(R.id.mainBG);
bg.setVisibility(View.GONE);
return true;
}
@Override
public boolean onQueryTextChange(String newText) {
if(!newText.isEmpty()) {
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
private SearchView searchView = 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;
private RecyclerView recyclerView = 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() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static SearchInfoItemFragment newInstance(int columnCount) {
SearchInfoItemFragment fragment = new SearchInfoItemFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState != null) {
searchQuery = savedInstanceState.getString(QUERY);
streamingServiceId = savedInstanceState.getInt(STREAMING_SERVICE);
} else {
try {
streamingServiceId = ServiceList.getIdOfService("Youtube");
} catch(Exception e) {
e.printStackTrace();
ErrorActivity.reportError(getActivity(), e, null,
getActivity().findViewById(android.R.id.content),
ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED,
ServiceList.getNameOfService(streamingServiceId),
"", R.string.general_error));
}
}
2016-08-02 18:38:05 +02:00
SearchWorker sw = SearchWorker.getInstance();
sw.setSearchWorkerResultListner(new SearchWorker.SearchWorkerResultListner() {
@Override
public void onResult(SearchResult result) {
2016-08-03 00:54:03 +02:00
infoListAdapter.addVideoList(result.resultList);
2016-08-02 20:59:53 +02:00
isLoading = false;
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();
2016-08-02 20:59:53 +02:00
isLoading = false;
2016-08-02 18:38:05 +02:00
}
@Override
public void onError(String message) {
//setListShown(true);
Toast.makeText(getActivity(), message,
Toast.LENGTH_LONG).show();
2016-08-02 20:59:53 +02:00
isLoading = false;
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);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
2016-08-02 20:59:53 +02:00
recyclerView = (RecyclerView) view;
streamInfoListLayoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(streamInfoListLayoutManager);
2016-08-02 18:38:05 +02:00
2016-08-03 00:54:03 +02:00
infoListAdapter = new InfoListAdapter(getActivity(),
2016-08-02 18:38:05 +02:00
getActivity().findViewById(android.R.id.content));
2016-08-03 00:54:03 +02:00
infoListAdapter.setOnItemSelectedListener(new InfoListAdapter.OnItemSelectedListener() {
2016-08-02 20:59:53 +02:00
@Override
public void selected(String url) {
Intent i = new Intent(getActivity(), VideoItemDetailActivity.class);
i.putExtra(VideoItemDetailFragment.STREAMING_SERVICE, streamingServiceId);
i.putExtra(VideoItemDetailFragment.VIDEO_URL, url);
getActivity().startActivity(i);
}
});
2016-08-03 00:54:03 +02:00
recyclerView.setAdapter(infoListAdapter);
2016-08-02 20:59:53 +02:00
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int pastVisiblesItems, visibleItemCount, totalItemCount;
super.onScrolled(recyclerView, dx, dy);
if(dy > 0) //check for scroll down
{
visibleItemCount = streamInfoListLayoutManager.getChildCount();
totalItemCount = streamInfoListLayoutManager.getItemCount();
pastVisiblesItems = streamInfoListLayoutManager.findFirstVisibleItemPosition();
if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount && !isLoading)
{
pageNumber++;
search(searchQuery, pageNumber);
}
}
}
});
2016-08-02 15:06:02 +02:00
}
2016-08-02 18:38:05 +02:00
2016-08-02 15:06:02 +02:00
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
}
@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) searchItem.getActionView();
setupSearchView(searchView);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
private void setupSearchView(SearchView searchView) {
suggestionListAdapter = new SuggestionListAdapter(getActivity());
searchView.setSuggestionsAdapter(suggestionListAdapter);
searchView.setOnSuggestionListener(new SearchSuggestionListener(searchView, suggestionListAdapter));
searchView.setOnQueryTextListener(new SearchQueryListener());
if(!searchQuery.isEmpty()) {
searchView.setQuery(searchQuery, false);
searchView.setIconifiedByDefault(false);
}
}
private void search(String query) {
2016-08-03 00:54:03 +02:00
infoListAdapter.clearVideoList();
2016-08-02 20:59:53 +02:00
pageNumber = 0;
search(query, pageNumber);
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();
sw.search(streamingServiceId, query, page, getActivity());
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();
}
}