mirror of
				https://github.com/TeamNewPipe/NewPipe
				synced 2025-10-31 07:13:00 +00:00 
			
		
		
		
	Merge branch 'feature-improve-search-fragment' of git://github.com/coffeemakr/NewPipe into cofe
This commit is contained in:
		| @@ -51,8 +51,6 @@ public class MainActivity extends ThemableActivity { | |||||||
|         MenuInflater inflater = getMenuInflater(); |         MenuInflater inflater = getMenuInflater(); | ||||||
|  |  | ||||||
|         inflater.inflate(R.menu.main_menu, menu); |         inflater.inflate(R.menu.main_menu, menu); | ||||||
|  |  | ||||||
|         mainFragment.onCreateOptionsMenu(menu, inflater); |  | ||||||
|         return true; |         return true; | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -81,8 +79,7 @@ public class MainActivity extends ThemableActivity { | |||||||
|                 return true; |                 return true; | ||||||
|             } |             } | ||||||
|             default: |             default: | ||||||
|                 return mainFragment.onOptionsItemSelected(item) || |                 return super.onOptionsItemSelected(item); | ||||||
|                         super.onOptionsItemSelected(item); |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -164,19 +164,10 @@ public class VideoItemDetailActivity extends ThemableActivity { | |||||||
|             NavUtils.navigateUpTo(this, intent); |             NavUtils.navigateUpTo(this, intent); | ||||||
|             return true; |             return true; | ||||||
|         } else { |         } else { | ||||||
|             return fragment.onOptionsItemSelected(item) || |             return super.onOptionsItemSelected(item); | ||||||
|                     super.onOptionsItemSelected(item); |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public boolean onCreateOptionsMenu(Menu menu) { |  | ||||||
|         super.onCreateOptionsMenu(menu); |  | ||||||
|         fragment.onCreateOptionsMenu(menu, getMenuInflater()); |  | ||||||
|         return true; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * Retrieves all Strings which look remotely like URLs from a text. |      * Retrieves all Strings which look remotely like URLs from a text. | ||||||
|      * Used if NewPipe was called through share menu. |      * Used if NewPipe was called through share menu. | ||||||
|   | |||||||
| @@ -633,6 +633,7 @@ public class VideoItemDetailFragment extends Fragment { | |||||||
|                 onNotSpecifiedContentError(); |                 onNotSpecifiedContentError(); | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |         setHasOptionsMenu(true); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|   | |||||||
| @@ -261,8 +261,7 @@ public class DownloadActivity extends ThemableActivity implements AdapterView.On | |||||||
|                 return true; |                 return true; | ||||||
|             } |             } | ||||||
|             default: |             default: | ||||||
|                 return mFragment.onOptionsItemSelected(item) || |                 return super.onOptionsItemSelected(item); | ||||||
|                         super.onOptionsItemSelected(item); |  | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -4,7 +4,6 @@ import org.schabi.newpipe.extractor.UrlIdHandler; | |||||||
| import org.schabi.newpipe.extractor.exceptions.ExtractionException; | import org.schabi.newpipe.extractor.exceptions.ExtractionException; | ||||||
|  |  | ||||||
| import java.io.IOException; | import java.io.IOException; | ||||||
| import java.util.List; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * Created by Christian Schabesberger on 10.08.15. |  * Created by Christian Schabesberger on 10.08.15. | ||||||
| @@ -26,7 +25,6 @@ import java.util.List; | |||||||
|  * along with NewPipe.  If not, see <http://www.gnu.org/licenses/>. |  * along with NewPipe.  If not, see <http://www.gnu.org/licenses/>. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| @SuppressWarnings("ALL") |  | ||||||
| public abstract class SearchEngine { | public abstract class SearchEngine { | ||||||
|     public static class NothingFoundException extends ExtractionException { |     public static class NothingFoundException extends ExtractionException { | ||||||
|         public NothingFoundException(String message) { |         public NothingFoundException(String message) { | ||||||
|   | |||||||
| @@ -85,18 +85,18 @@ public class YoutubeSearchEngine extends SearchEngine { | |||||||
|             Element el; |             Element el; | ||||||
|  |  | ||||||
|             // both types of spell correction item |             // both types of spell correction item | ||||||
|             if (!((el = item.select("div[class*=\"spell-correction\"]").first()) == null)) { |             if ((el = item.select("div[class*=\"spell-correction\"]").first()) != null) { | ||||||
|                 collector.setSuggestion(el.select("a").first().text()); |                 collector.setSuggestion(el.select("a").first().text()); | ||||||
|                 if(list.children().size() == 1) { |                 if(list.children().size() == 1) { | ||||||
|                     throw new NothingFoundException("Did you mean: " + el.select("a").first().text()); |                     throw new NothingFoundException("Did you mean: " + el.select("a").first().text()); | ||||||
|                 } |                 } | ||||||
|                 // search message item |                 // search message item | ||||||
|             } else if (!((el = item.select("div[class*=\"search-message\"]").first()) == null)) { |             } else if ((el = item.select("div[class*=\"search-message\"]").first()) != null) { | ||||||
|                 //result.errorMessage = el.text(); |                 //result.errorMessage = el.text(); | ||||||
|                 throw new NothingFoundException(el.text()); |                 throw new NothingFoundException(el.text()); | ||||||
|  |  | ||||||
|                 // video item type |                 // video item type | ||||||
|             } else if (!((el = item.select("div[class*=\"yt-lockup-video\"").first()) == null)) { |             } else if ((el = item.select("div[class*=\"yt-lockup-video\"").first()) != null) { | ||||||
|                 collector.commit(extractPreviewInfo(el)); |                 collector.commit(extractPreviewInfo(el)); | ||||||
|             } else { |             } else { | ||||||
|                 //noinspection ConstantConditions |                 //noinspection ConstantConditions | ||||||
|   | |||||||
| @@ -30,13 +30,13 @@ import org.schabi.newpipe.R; | |||||||
|  |  | ||||||
| public class InfoItemHolder extends RecyclerView.ViewHolder { | public class InfoItemHolder extends RecyclerView.ViewHolder { | ||||||
|  |  | ||||||
|     public ImageView itemThumbnailView; |     public final ImageView itemThumbnailView; | ||||||
|     public TextView itemVideoTitleView, |     public final TextView itemVideoTitleView, | ||||||
|             itemUploaderView, |             itemUploaderView, | ||||||
|             itemDurationView, |             itemDurationView, | ||||||
|             itemUploadDateView, |             itemUploadDateView, | ||||||
|             itemViewCountView; |             itemViewCountView; | ||||||
|     public Button itemButton; |     public final Button itemButton; | ||||||
|  |  | ||||||
|     public InfoItemHolder(View v) { |     public InfoItemHolder(View v) { | ||||||
|         super(v); |         super(v); | ||||||
|   | |||||||
| @@ -34,11 +34,12 @@ import java.util.Vector; | |||||||
|  |  | ||||||
| public class InfoListAdapter extends RecyclerView.Adapter<InfoItemHolder> { | public class InfoListAdapter extends RecyclerView.Adapter<InfoItemHolder> { | ||||||
|  |  | ||||||
|     InfoItemBuilder infoItemBuilder = null; |     private final InfoItemBuilder infoItemBuilder; | ||||||
|     List<StreamPreviewInfo> streamList = new Vector<>(); |     private final List<StreamPreviewInfo> streamList; | ||||||
|  |  | ||||||
|     public InfoListAdapter(Activity a, View rootView) { |     public InfoListAdapter(Activity a, View rootView) { | ||||||
|         infoItemBuilder = new InfoItemBuilder(a, rootView); |         infoItemBuilder = new InfoItemBuilder(a, rootView); | ||||||
|  |         streamList = new Vector<>(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void setOnItemSelectedListener |     public void setOnItemSelectedListener | ||||||
| @@ -54,7 +55,7 @@ public class InfoListAdapter extends RecyclerView.Adapter<InfoItemHolder> { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void clearSteamItemList() { |     public void clearSteamItemList() { | ||||||
|         streamList = new Vector<>(); |         streamList.clear(); | ||||||
|         notifyDataSetChanged(); |         notifyDataSetChanged(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -56,13 +56,15 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|  |  | ||||||
|     private static final String TAG = SearchInfoItemFragment.class.toString(); |     private static final String TAG = SearchInfoItemFragment.class.toString(); | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Listener for search queries | ||||||
|  |      */ | ||||||
|     public class SearchQueryListener implements SearchView.OnQueryTextListener { |     public class SearchQueryListener implements SearchView.OnQueryTextListener { | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
|         public boolean onQueryTextSubmit(String query) { |         public boolean onQueryTextSubmit(String query) { | ||||||
|             Activity a = getActivity(); |             Activity a = getActivity(); | ||||||
|             try { |             try { | ||||||
|                 searchQuery = query; |  | ||||||
|                 search(query); |                 search(query); | ||||||
|  |  | ||||||
|                 // hide virtual keyboard |                 // hide virtual keyboard | ||||||
| @@ -89,8 +91,6 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|             } catch (Exception e) { |             } catch (Exception e) { | ||||||
|                 e.printStackTrace(); |                 e.printStackTrace(); | ||||||
|             } |             } | ||||||
|             View bg = a.findViewById(R.id.mainBG); |  | ||||||
|             bg.setVisibility(View.GONE); |  | ||||||
|             return true; |             return true; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -108,12 +108,10 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|     private boolean isLoading = false; |     private boolean isLoading = false; | ||||||
|  |  | ||||||
|     private ProgressBar loadingIndicator = null; |     private ProgressBar loadingIndicator = null; | ||||||
|     private SearchView searchView = null; |  | ||||||
|     private int pageNumber = 0; |     private int pageNumber = 0; | ||||||
|     private SuggestionListAdapter suggestionListAdapter = null; |     private SuggestionListAdapter suggestionListAdapter = null; | ||||||
|     private InfoListAdapter infoListAdapter = null; |     private InfoListAdapter infoListAdapter = null; | ||||||
|     private LinearLayoutManager streamInfoListLayoutManager = null; |     private LinearLayoutManager streamInfoListLayoutManager = null; | ||||||
|     private RecyclerView recyclerView = null; |  | ||||||
|  |  | ||||||
|     // savedInstanceBundle arguments |     // savedInstanceBundle arguments | ||||||
|     private static final String QUERY = "query"; |     private static final String QUERY = "query"; | ||||||
| @@ -126,23 +124,32 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|     public SearchInfoItemFragment() { |     public SearchInfoItemFragment() { | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     // TODO: Customize parameter initialization |  | ||||||
|     @SuppressWarnings("unused") |     @SuppressWarnings("unused") | ||||||
|     public static SearchInfoItemFragment newInstance(int columnCount) { |     public static SearchInfoItemFragment newInstance(int streamingServiceId, String searchQuery) { | ||||||
|  |         Bundle args = new Bundle(); | ||||||
|  |         args.putInt(STREAMING_SERVICE, streamingServiceId); | ||||||
|  |         args.putString(QUERY, searchQuery); | ||||||
|         SearchInfoItemFragment fragment = new SearchInfoItemFragment(); |         SearchInfoItemFragment fragment = new SearchInfoItemFragment(); | ||||||
|  |         fragment.setArguments(args); | ||||||
|         return fragment; |         return fragment; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void onCreate(Bundle savedInstanceState) { |     public void onCreate(Bundle savedInstanceState) { | ||||||
|         super.onCreate(savedInstanceState); |         super.onCreate(savedInstanceState); | ||||||
|  |         searchQuery = ""; | ||||||
|         if (savedInstanceState != null) { |         if (savedInstanceState != null) { | ||||||
|             searchQuery = savedInstanceState.getString(QUERY); |             searchQuery = savedInstanceState.getString(QUERY); | ||||||
|             streamingServiceId = savedInstanceState.getInt(STREAMING_SERVICE); |             streamingServiceId = savedInstanceState.getInt(STREAMING_SERVICE); | ||||||
|         } else { |         } else { | ||||||
|             try { |             try { | ||||||
|                 streamingServiceId = NewPipe.getIdOfService("Youtube"); |                 Bundle args = getArguments(); | ||||||
|  |                 if(args != null) { | ||||||
|  |                     searchQuery = args.getString(QUERY); | ||||||
|  |                     streamingServiceId = args.getInt(STREAMING_SERVICE); | ||||||
|  |                 } else { | ||||||
|  |                     streamingServiceId = NewPipe.getIdOfService("Youtube"); | ||||||
|  |                 } | ||||||
|             } catch (Exception e) { |             } catch (Exception e) { | ||||||
|                 e.printStackTrace(); |                 e.printStackTrace(); | ||||||
|                 ErrorActivity.reportError(getActivity(), e, null, |                 ErrorActivity.reportError(getActivity(), e, null, | ||||||
| @@ -153,13 +160,14 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|  |         setHasOptionsMenu(true); | ||||||
|  |  | ||||||
|         SearchWorker sw = SearchWorker.getInstance(); |         SearchWorker sw = SearchWorker.getInstance(); | ||||||
|         sw.setSearchWorkerResultListener(new SearchWorker.SearchWorkerResultListener() { |         sw.setSearchWorkerResultListener(new SearchWorker.SearchWorkerResultListener() { | ||||||
|             @Override |             @Override | ||||||
|             public void onResult(SearchResult result) { |             public void onResult(SearchResult result) { | ||||||
|                 infoListAdapter.addStreamItemList(result.resultList); |                 infoListAdapter.addStreamItemList(result.resultList); | ||||||
|                 isLoading = false; |                 setDoneLoading(); | ||||||
|                 loadingIndicator.setVisibility(View.GONE); |  | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             @Override |             @Override | ||||||
| @@ -167,8 +175,7 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|                 //setListShown(true); |                 //setListShown(true); | ||||||
|                 Toast.makeText(getActivity(), getString(stringResource), |                 Toast.makeText(getActivity(), getString(stringResource), | ||||||
|                         Toast.LENGTH_SHORT).show(); |                         Toast.LENGTH_SHORT).show(); | ||||||
|                 isLoading = false; |                 setDoneLoading(); | ||||||
|                 loadingIndicator.setVisibility(View.GONE); |  | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             @Override |             @Override | ||||||
| @@ -176,8 +183,7 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|                 //setListShown(true); |                 //setListShown(true); | ||||||
|                 Toast.makeText(getActivity(), message, |                 Toast.makeText(getActivity(), message, | ||||||
|                         Toast.LENGTH_LONG).show(); |                         Toast.LENGTH_LONG).show(); | ||||||
|                 isLoading = false; |                 setDoneLoading(); | ||||||
|                 loadingIndicator.setVisibility(View.GONE); |  | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             @Override |             @Override | ||||||
| @@ -191,6 +197,7 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|                         RECAPTCHA_REQUEST); |                         RECAPTCHA_REQUEST); | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -200,7 +207,7 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|  |  | ||||||
|         Context context = view.getContext(); |         Context context = view.getContext(); | ||||||
|         loadingIndicator = (ProgressBar) view.findViewById(R.id.progressBar); |         loadingIndicator = (ProgressBar) view.findViewById(R.id.progressBar); | ||||||
|         recyclerView = (RecyclerView) view.findViewById(R.id.list); |         RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list); | ||||||
|         streamInfoListLayoutManager = new LinearLayoutManager(context); |         streamInfoListLayoutManager = new LinearLayoutManager(context); | ||||||
|         recyclerView.setLayoutManager(streamInfoListLayoutManager); |         recyclerView.setLayoutManager(streamInfoListLayoutManager); | ||||||
|  |  | ||||||
| @@ -209,15 +216,12 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|         infoListAdapter.setOnItemSelectedListener(new InfoItemBuilder.OnItemSelectedListener() { |         infoListAdapter.setOnItemSelectedListener(new InfoItemBuilder.OnItemSelectedListener() { | ||||||
|             @Override |             @Override | ||||||
|             public void selected(String url) { |             public void selected(String url) { | ||||||
|                 Intent i = new Intent(getActivity(), VideoItemDetailActivity.class); |                 startDetailActivity(url); | ||||||
|                 i.putExtra(VideoItemDetailFragment.STREAMING_SERVICE, streamingServiceId); |  | ||||||
|                 i.putExtra(VideoItemDetailFragment.VIDEO_URL, url); |  | ||||||
|                 getActivity().startActivity(i); |  | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|         recyclerView.setAdapter(infoListAdapter); |         recyclerView.setAdapter(infoListAdapter); | ||||||
|  |         recyclerView.clearOnScrollListeners(); | ||||||
|         recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() { |         recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { | ||||||
|             @Override |             @Override | ||||||
|             public void onScrolled(RecyclerView recyclerView, int dx, int dy) { |             public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | ||||||
|                 int pastVisiblesItems, visibleItemCount, totalItemCount; |                 int pastVisiblesItems, visibleItemCount, totalItemCount; | ||||||
| @@ -239,14 +243,26 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|         return view; |         return view; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     private void startDetailActivity(String url) { | ||||||
|     public void onAttach(Context context) { |         Intent i = new Intent(getActivity(), VideoItemDetailActivity.class); | ||||||
|         super.onAttach(context); |         i.putExtra(VideoItemDetailFragment.STREAMING_SERVICE, streamingServiceId); | ||||||
|  |         i.putExtra(VideoItemDetailFragment.VIDEO_URL, url); | ||||||
|  |         getActivity().startActivity(i); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void onDetach() { |     public void onStart() { | ||||||
|         super.onDetach(); |         super.onStart(); | ||||||
|  |         if(!searchQuery.isEmpty()) { | ||||||
|  |             search(searchQuery); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public void onSaveInstanceState(Bundle outState) { | ||||||
|  |         super.onSaveInstanceState(outState); | ||||||
|  |         outState.putString(QUERY, searchQuery); | ||||||
|  |         outState.putInt(STREAMING_SERVICE, streamingServiceId); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
| @@ -255,15 +271,10 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|         inflater.inflate(R.menu.search_menu, menu); |         inflater.inflate(R.menu.search_menu, menu); | ||||||
|  |  | ||||||
|         MenuItem searchItem = menu.findItem(R.id.action_search); |         MenuItem searchItem = menu.findItem(R.id.action_search); | ||||||
|         searchView = (SearchView) searchItem.getActionView(); |         SearchView searchView = (SearchView) searchItem.getActionView(); | ||||||
|         setupSearchView(searchView); |         setupSearchView(searchView); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |  | ||||||
|     public boolean onOptionsItemSelected(MenuItem item) { |  | ||||||
|         return super.onOptionsItemSelected(item); |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private void setupSearchView(SearchView searchView) { |     private void setupSearchView(SearchView searchView) { | ||||||
|         suggestionListAdapter = new SuggestionListAdapter(getActivity()); |         suggestionListAdapter = new SuggestionListAdapter(getActivity()); | ||||||
|         searchView.setSuggestionsAdapter(suggestionListAdapter); |         searchView.setSuggestionsAdapter(suggestionListAdapter); | ||||||
| @@ -278,7 +289,9 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|     private void search(String query) { |     private void search(String query) { | ||||||
|         infoListAdapter.clearSteamItemList(); |         infoListAdapter.clearSteamItemList(); | ||||||
|         pageNumber = 0; |         pageNumber = 0; | ||||||
|  |         searchQuery = query; | ||||||
|         search(query, pageNumber); |         search(query, pageNumber); | ||||||
|  |         hideBackground(); | ||||||
|         loadingIndicator.setVisibility(View.VISIBLE); |         loadingIndicator.setVisibility(View.VISIBLE); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -288,6 +301,20 @@ public class SearchInfoItemFragment extends Fragment { | |||||||
|         sw.search(streamingServiceId, query, page, getActivity()); |         sw.search(streamingServiceId, query, page, getActivity()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     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); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     private void searchSuggestions(String query) { |     private void searchSuggestions(String query) { | ||||||
|         SuggestionSearchRunnable suggestionSearchRunnable = |         SuggestionSearchRunnable suggestionSearchRunnable = | ||||||
|                 new SuggestionSearchRunnable(streamingServiceId, query, getActivity(), suggestionListAdapter); |                 new SuggestionSearchRunnable(streamingServiceId, query, getActivity(), suggestionListAdapter); | ||||||
|   | |||||||
| @@ -25,8 +25,8 @@ import android.support.v7.widget.SearchView; | |||||||
|  |  | ||||||
| public class SearchSuggestionListener implements SearchView.OnSuggestionListener{ | public class SearchSuggestionListener implements SearchView.OnSuggestionListener{ | ||||||
|  |  | ||||||
|     private SearchView searchView; |     private final SearchView searchView; | ||||||
|     private SuggestionListAdapter adapter; |     private final SuggestionListAdapter adapter; | ||||||
|  |  | ||||||
|     public SearchSuggestionListener(SearchView searchView, SuggestionListAdapter adapter) { |     public SearchSuggestionListener(SearchView searchView, SuggestionListAdapter adapter) { | ||||||
|         this.searchView = searchView; |         this.searchView = searchView; | ||||||
|   | |||||||
| @@ -82,6 +82,7 @@ public class SearchWorker { | |||||||
|         } |         } | ||||||
|         @Override |         @Override | ||||||
|         public void run() { |         public void run() { | ||||||
|  |             final String serviceName = NewPipe.getNameOfService(serviceId); | ||||||
|             SearchResult result = null; |             SearchResult result = null; | ||||||
|             SearchEngine engine = null; |             SearchEngine engine = null; | ||||||
|  |  | ||||||
| @@ -119,7 +120,7 @@ public class SearchWorker { | |||||||
|                     View rootView = a.findViewById(android.R.id.content); |                     View rootView = a.findViewById(android.R.id.content); | ||||||
|                     ErrorActivity.reportError(h, a, result.errors, null, rootView, |                     ErrorActivity.reportError(h, a, result.errors, null, rootView, | ||||||
|                             ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED, |                             ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED, | ||||||
|                         /* todo: this shoudl not be assigned static */  YOUTUBE, query, R.string.light_parsing_error)); |                             serviceName, query, R.string.light_parsing_error)); | ||||||
|  |  | ||||||
|                 } |                 } | ||||||
|                 // hard errors: |                 // hard errors: | ||||||
| @@ -148,8 +149,7 @@ public class SearchWorker { | |||||||
|             } catch(ExtractionException e) { |             } catch(ExtractionException e) { | ||||||
|                 ErrorActivity.reportError(h, a, e, null, null, |                 ErrorActivity.reportError(h, a, e, null, null, | ||||||
|                         ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED, |                         ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED, | ||||||
|                         /* todo: this shoudl not be assigned static */ |                                 serviceName, query, R.string.parsing_error)); | ||||||
|                                 YOUTUBE, query, R.string.parsing_error)); |  | ||||||
|                 //postNewErrorToast(h, R.string.parsing_error); |                 //postNewErrorToast(h, R.string.parsing_error); | ||||||
|                 e.printStackTrace(); |                 e.printStackTrace(); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -3,10 +3,8 @@ package org.schabi.newpipe.search_fragment; | |||||||
| import android.content.Context; | import android.content.Context; | ||||||
| import android.database.Cursor; | import android.database.Cursor; | ||||||
| import android.database.MatrixCursor; | import android.database.MatrixCursor; | ||||||
| import android.support.v4.widget.CursorAdapter; | import android.support.v4.widget.ResourceCursorAdapter; | ||||||
| import android.view.LayoutInflater; |  | ||||||
| import android.view.View; | import android.view.View; | ||||||
| import android.view.ViewGroup; |  | ||||||
| import android.widget.TextView; | import android.widget.TextView; | ||||||
|  |  | ||||||
| import java.util.List; | import java.util.List; | ||||||
| @@ -31,52 +29,56 @@ import java.util.List; | |||||||
|  * along with NewPipe.  If not, see <http://www.gnu.org/licenses/>. |  * along with NewPipe.  If not, see <http://www.gnu.org/licenses/>. | ||||||
|  */ |  */ | ||||||
|  |  | ||||||
| public class SuggestionListAdapter extends CursorAdapter { | /** | ||||||
|  |  * {@link ResourceCursorAdapter} to display suggestions. | ||||||
|  |  */ | ||||||
|  | public class SuggestionListAdapter extends ResourceCursorAdapter { | ||||||
|  |  | ||||||
|  |     private static final String[] columns = new String[]{"_id", "title"}; | ||||||
|  |     private static final int INDEX_ID = 0; | ||||||
|  |     private static final int INDEX_TITLE = 1; | ||||||
|  |  | ||||||
|     private String[] columns = new String[]{"_id", "title"}; |  | ||||||
|  |  | ||||||
|     public SuggestionListAdapter(Context context) { |     public SuggestionListAdapter(Context context) { | ||||||
|         super(context, null, false); |         super(context, android.R.layout.simple_list_item_1, null, 0); | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @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 |     @Override | ||||||
|     public void bindView(View view, Context context, Cursor cursor) { |     public void bindView(View view, Context context, Cursor cursor) { | ||||||
|         ViewHolder viewHolder = (ViewHolder) view.getTag(); |         ViewHolder viewHolder = new ViewHolder(view); | ||||||
|         viewHolder.suggestionTitle.setText(cursor.getString(1)); |         viewHolder.suggestionTitle.setText(cursor.getString(INDEX_TITLE)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Update the suggestion list | ||||||
|  |      * @param suggestions the list of suggestions | ||||||
|  |      */ | ||||||
|     public void updateAdapter(List<String> suggestions) { |     public void updateAdapter(List<String> suggestions) { | ||||||
|         MatrixCursor cursor = new MatrixCursor(columns); |         MatrixCursor cursor = new MatrixCursor(columns, suggestions.size()); | ||||||
|         int i = 0; |         int i = 0; | ||||||
|         for (String s : suggestions) { |         for (String suggestion : suggestions) { | ||||||
|             String[] temp = new String[2]; |             String[] columnValues = new String[columns.length]; | ||||||
|             temp[0] = Integer.toString(i); |             columnValues[INDEX_TITLE] = suggestion; | ||||||
|             temp[1] = s; |             columnValues[INDEX_ID] = Integer.toString(i); | ||||||
|  |             cursor.addRow(columnValues); | ||||||
|             i++; |             i++; | ||||||
|             cursor.addRow(temp); |  | ||||||
|         } |         } | ||||||
|         changeCursor(cursor); |         changeCursor(cursor); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Get the suggestion for a position | ||||||
|  |      * @param position the position of the suggestion | ||||||
|  |      * @return the suggestion | ||||||
|  |      */ | ||||||
|     public String getSuggestion(int position) { |     public String getSuggestion(int position) { | ||||||
|         return ((Cursor) getItem(position)).getString(1); |         return ((Cursor) getItem(position)).getString(INDEX_TITLE); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     private class ViewHolder { |     private class ViewHolder { | ||||||
|         public TextView suggestionTitle; |         private final TextView suggestionTitle; | ||||||
|  |         private ViewHolder(View view) { | ||||||
|  |             this.suggestionTitle = (TextView) view.findViewById(android.R.id.text1); | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -37,14 +37,15 @@ import java.util.List; | |||||||
|  |  | ||||||
| public class SuggestionSearchRunnable implements Runnable{ | public class SuggestionSearchRunnable implements Runnable{ | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * Runnable to update a {@link SuggestionListAdapter} | ||||||
|  |      */ | ||||||
|     private class SuggestionResultRunnable implements Runnable{ |     private class SuggestionResultRunnable implements Runnable{ | ||||||
|  |  | ||||||
|         private List<String> suggestions; |         private final List<String> suggestions; | ||||||
|         private SuggestionListAdapter adapter; |  | ||||||
|  |  | ||||||
|         private SuggestionResultRunnable(List<String> suggestions, SuggestionListAdapter adapter) { |         private SuggestionResultRunnable(List<String> suggestions) { | ||||||
|             this.suggestions = suggestions; |             this.suggestions = suggestions; | ||||||
|             this.adapter = adapter; |  | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         @Override |         @Override | ||||||
| @@ -55,9 +56,9 @@ public class SuggestionSearchRunnable implements Runnable{ | |||||||
|  |  | ||||||
|     private final int serviceId; |     private final int serviceId; | ||||||
|     private final String query; |     private final String query; | ||||||
|     final Handler h = new Handler(); |     private final Handler h = new Handler(); | ||||||
|     private Activity a = null; |     private final Activity a; | ||||||
|     private SuggestionListAdapter adapter; |     private final SuggestionListAdapter adapter; | ||||||
|     public SuggestionSearchRunnable(int serviceId, String query, |     public SuggestionSearchRunnable(int serviceId, String query, | ||||||
|                                      Activity activity, SuggestionListAdapter adapter) { |                                      Activity activity, SuggestionListAdapter adapter) { | ||||||
|         this.serviceId = serviceId; |         this.serviceId = serviceId; | ||||||
| @@ -76,7 +77,7 @@ public class SuggestionSearchRunnable implements Runnable{ | |||||||
|             String searchLanguage = sp.getString(searchLanguageKey, |             String searchLanguage = sp.getString(searchLanguageKey, | ||||||
|                     a.getString(R.string.default_language_value)); |                     a.getString(R.string.default_language_value)); | ||||||
|             List<String> suggestions = se.suggestionList(query, searchLanguage); |             List<String> suggestions = se.suggestionList(query, searchLanguage); | ||||||
|             h.post(new SuggestionResultRunnable(suggestions, adapter)); |             h.post(new SuggestionResultRunnable(suggestions)); | ||||||
|         } catch (ExtractionException e) { |         } catch (ExtractionException e) { | ||||||
|             ErrorActivity.reportError(h, a, e, null, a.findViewById(android.R.id.content), |             ErrorActivity.reportError(h, a, e, null, a.findViewById(android.R.id.content), | ||||||
|                     ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED, |                     ErrorActivity.ErrorInfo.make(ErrorActivity.SEARCHED, | ||||||
|   | |||||||
| @@ -6,8 +6,6 @@ | |||||||
|     tools:context="org.schabi.newpipe.MainActivity" |     tools:context="org.schabi.newpipe.MainActivity" | ||||||
|     android:orientation="vertical"> |     android:orientation="vertical"> | ||||||
|  |  | ||||||
|     <include layout="@layout/main_bg" /> |  | ||||||
|  |  | ||||||
|     <fragment |     <fragment | ||||||
|         android:id="@+id/search_fragment" |         android:id="@+id/search_fragment" | ||||||
|         android:name="org.schabi.newpipe.search_fragment.SearchInfoItemFragment" |         android:name="org.schabi.newpipe.search_fragment.SearchInfoItemFragment" | ||||||
|   | |||||||
| @@ -7,6 +7,8 @@ | |||||||
|     android:name="org.schabi.newpipe.SearchInfoItemFragment" |     android:name="org.schabi.newpipe.SearchInfoItemFragment" | ||||||
|     tools:context=".search_fragment.SearchInfoItemFragment"> |     tools:context=".search_fragment.SearchInfoItemFragment"> | ||||||
|  |  | ||||||
|  |     <include layout="@layout/main_bg" /> | ||||||
|  |  | ||||||
|     <android.support.v7.widget.RecyclerView |     <android.support.v7.widget.RecyclerView | ||||||
|         android:id="@+id/list" |         android:id="@+id/list" | ||||||
|         android:layout_width="match_parent" |         android:layout_width="match_parent" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Christian Schabesberger
					Christian Schabesberger