add search filter menu

This commit is contained in:
Christian Schabesberger 2017-02-15 15:21:36 +01:00
parent 3f0078f38a
commit a5252bb765
17 changed files with 205 additions and 122 deletions

View File

@ -48,4 +48,5 @@ dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
compile 'ch.acra:acra:4.9.0'
compile 'com.google.android.gms:play-services-appindexing:8.4.0'
}

View File

@ -170,7 +170,11 @@
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</provider><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>

View File

@ -5,10 +5,14 @@ import android.media.AudioManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import org.schabi.newpipe.download.DownloadActivity;
import org.schabi.newpipe.settings.SettingsActivity;
import org.schabi.newpipe.util.PermissionHelper;
@ -33,8 +37,8 @@ import org.schabi.newpipe.util.PermissionHelper;
*/
public class MainActivity extends ThemableActivity {
private Fragment mainFragment = null;
private static final String TAG = MainActivity.class.toString();
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -58,7 +62,7 @@ public class MainActivity extends ThemableActivity {
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
switch (id) {
case android.R.id.home: {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
@ -71,10 +75,10 @@ public class MainActivity extends ThemableActivity {
return true;
}
case R.id.action_show_downloads: {
if(!PermissionHelper.checkStoragePermissions(this)) {
if (!PermissionHelper.checkStoragePermissions(this)) {
return false;
}
Intent intent = new Intent(this, org.schabi.newpipe.download.DownloadActivity.class);
Intent intent = new Intent(this, DownloadActivity.class);
startActivity(intent);
return true;
}

View File

@ -32,38 +32,41 @@ public class ChannelInfoItemCollector extends InfoItemCollector {
super(serviceId);
}
public ChannelInfoItem extract(ChannelInfoItemExtractor extractor) throws ParsingException {
ChannelInfoItem resultItem = new ChannelInfoItem();
// importand information
resultItem.channelName = extractor.getChannelName();
resultItem.serviceId = getServiceId();
resultItem.webPageUrl = extractor.getWebPageUrl();
// optional information
try {
resultItem.subscriberCount = extractor.getSubscriberCount();
} catch (Exception e) {
addError(e);
}
try {
resultItem.videoAmount = extractor.getVideoAmount();
} catch (Exception e) {
addError(e);
}
try {
resultItem.thumbnailUrl = extractor.getThumbnailUrl();
} catch (Exception e) {
addError(e);
}
try {
resultItem.description = extractor.getDescription();
} catch (Exception e) {
addError(e);
}
return resultItem;
}
public void commit(ChannelInfoItemExtractor extractor) throws ParsingException {
try {
ChannelInfoItem resultItem = new ChannelInfoItem();
// importand information
resultItem.channelName = extractor.getChannelName();
resultItem.serviceId = getServiceId();
resultItem.webPageUrl = extractor.getWebPageUrl();
// optional information
try {
resultItem.subscriberCount = extractor.getSubscriberCount();
} catch (Exception e) {
addError(e);
}
try {
resultItem.videoAmount = extractor.getVideoAmount();
} catch (Exception e) {
addError(e);
}
try {
resultItem.thumbnailUrl = extractor.getThumbnailUrl();
} catch (Exception e) {
addError(e);
}
try {
resultItem.description = extractor.getDescription();
} catch (Exception e) {
addError(e);
}
addItem(resultItem);
addItem(extract(extractor));
} catch (Exception e) {
addError(e);
}

View File

@ -5,6 +5,7 @@ import org.schabi.newpipe.extractor.UrlIdHandler;
import org.schabi.newpipe.extractor.channel.ChannelInfoItemCollector;
import org.schabi.newpipe.extractor.channel.ChannelInfoItemExtractor;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.FoundAdException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.stream_info.StreamInfoItemCollector;
import org.schabi.newpipe.extractor.stream_info.StreamInfoItemExtractor;
@ -34,6 +35,8 @@ public class InfoItemSearchCollector extends InfoItemCollector {
private StreamInfoItemCollector streamCollector;
private ChannelInfoItemCollector channelCollector;
SearchResult result = new SearchResult();
InfoItemSearchCollector(UrlIdHandler handler, int serviceId) {
super(serviceId);
streamCollector = new StreamInfoItemCollector(handler, serviceId);
@ -45,22 +48,32 @@ public class InfoItemSearchCollector extends InfoItemCollector {
}
public SearchResult getSearchResult() throws ExtractionException {
SearchResult result = new SearchResult();
addFromCollector(channelCollector);
addFromCollector(streamCollector);
result.suggestion = suggestion;
result.errors = getErrors();
result.resultList = getItemList();
return result;
}
public void commit(StreamInfoItemExtractor extractor) throws ParsingException {
streamCollector.commit(extractor);
public void commit(StreamInfoItemExtractor extractor) {
try {
result.resultList.add(streamCollector.extract(extractor));
} catch(FoundAdException ae) {
System.err.println("Found add");
} catch (Exception e) {
addError(e);
}
}
public void commit(ChannelInfoItemExtractor extractor) throws ParsingException {
channelCollector.commit(extractor);
public void commit(ChannelInfoItemExtractor extractor) {
try {
result.resultList.add(channelCollector.extract(extractor));
} catch(FoundAdException ae) {
System.err.println("Found add");
} catch (Exception e) {
addError(e);
}
}
}

View File

@ -136,7 +136,7 @@ public class YoutubeChannelExtractor extends ChannelExtractor {
if(!isAjaxPage) {
Element el = doc.select("div[id=\"gh-banner\"]").first().select("style").first();
String cssContent = el.html();
String url = Parser.matchGroup1("url\\(([^)]+)\\)", cssContent);
String url = "https:" + Parser.matchGroup1("url\\(([^)]+)\\)", cssContent);
if (url.contains("s.ytimg.com") || url.contains("default_banner")) {
bannerUrl = null;

View File

@ -55,8 +55,12 @@ public class YoutubeChannelInfoItemExtractor implements ChannelInfoItemExtractor
}
public long getSubscriberCount() throws ParsingException {
return Long.parseLong(el.select("span[class*=\"yt-subscriber-count\"]").first()
.text().replaceAll("\\D+",""));
Element subsEl = el.select("span[class*=\"yt-subscriber-count\"]").first();
if(subsEl == null) {
return 0;
} else {
return Integer.parseInt(subsEl.text().replaceAll("\\D+",""));
}
}
public int getVideoAmount() throws ParsingException {

View File

@ -108,8 +108,9 @@ public class YoutubeSearchEngine extends SearchEngine {
} else if((el = item.select("div[class*=\"yt-lockup-channel\"]").first()) != null) {
collector.commit(new YoutubeChannelInfoItemExtractor(el));
} else {
//noinspection ConstantConditions
throw new ExtractionException("unexpected element found: \"" + el + "\"");
// noinspection ConstantConditions
// simply ignore not known items
// throw new ExtractionException("unexpected element found: \"" + item + "\"");
}
}

View File

@ -83,9 +83,12 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
@Override
public String getUploadDate() throws ParsingException {
try {
return item.select("div[class=\"yt-lockup-meta\"]").first()
.select("li").first()
.text();
Element div = item.select("div[class=\"yt-lockup-meta\"]").first();
if(div == null) {
return null;
} else {
return div.select("li").first().text();
}
} catch(Exception e) {
throw new ParsingException("Could not get uplaod date", e);
}
@ -96,9 +99,13 @@ public class YoutubeStreamInfoItemExtractor implements StreamInfoItemExtractor {
String output;
String input;
try {
input = item.select("div[class=\"yt-lockup-meta\"]").first()
.select("li").get(1)
.text();
Element div = item.select("div[class=\"yt-lockup-meta\"]").first();
if(div == null) {
return -1;
} else {
input = div.select("li").get(1)
.text();
}
} catch (IndexOutOfBoundsException e) {
if(isLiveStream(item)) {
// -1 for no view count

View File

@ -42,49 +42,54 @@ public class StreamInfoItemCollector extends InfoItemCollector {
return urlIdHandler;
}
public StreamInfoItem extract(StreamInfoItemExtractor extractor) throws Exception {
StreamInfoItem resultItem = new StreamInfoItem();
// importand information
resultItem.service_id = getServiceId();
resultItem.webpage_url = extractor.getWebPageUrl();
if (getUrlIdHandler() == null) {
throw new ParsingException("Error: UrlIdHandler not set");
} else if (!resultItem.webpage_url.isEmpty()) {
resultItem.id = NewPipe.getService(getServiceId())
.getUrlIdHandlerInstance()
.getId(resultItem.webpage_url);
}
resultItem.title = extractor.getTitle();
resultItem.stream_type = extractor.getStreamType();
// optional information
try {
resultItem.duration = extractor.getDuration();
} catch (Exception e) {
addError(e);
}
try {
resultItem.uploader = extractor.getUploader();
} catch (Exception e) {
addError(e);
}
try {
resultItem.upload_date = extractor.getUploadDate();
} catch (Exception e) {
addError(e);
}
try {
resultItem.view_count = extractor.getViewCount();
} catch (Exception e) {
addError(e);
}
try {
resultItem.thumbnail_url = extractor.getThumbnailUrl();
} catch (Exception e) {
addError(e);
}
return resultItem;
}
public void commit(StreamInfoItemExtractor extractor) throws ParsingException {
try {
StreamInfoItem resultItem = new StreamInfoItem();
// importand information
resultItem.service_id = getServiceId();
resultItem.webpage_url = extractor.getWebPageUrl();
if (getUrlIdHandler() == null) {
throw new ParsingException("Error: UrlIdHandler not set");
} else if(!resultItem.webpage_url.isEmpty()) {
resultItem.id = NewPipe.getService(getServiceId())
.getUrlIdHandlerInstance()
.getId(resultItem.webpage_url);
}
resultItem.title = extractor.getTitle();
resultItem.stream_type = extractor.getStreamType();
// optional information
try {
resultItem.duration = extractor.getDuration();
} catch (Exception e) {
addError(e);
}
try {
resultItem.uploader = extractor.getUploader();
} catch (Exception e) {
addError(e);
}
try {
resultItem.upload_date = extractor.getUploadDate();
} catch (Exception e) {
addError(e);
}
try {
resultItem.view_count = extractor.getViewCount();
} catch (Exception e) {
addError(e);
}
try {
resultItem.thumbnail_url = extractor.getThumbnailUrl();
} catch (Exception e) {
addError(e);
}
addItem(resultItem);
addItem(extract(extractor));
} catch(FoundAdException ae) {
System.out.println("AD_WARNING: " + ae.getMessage());
} catch (Exception e) {

View File

@ -82,6 +82,8 @@ public class InfoItemBuilder {
}
public void buildByHolder(InfoItemHolder holder, final InfoItem i) {
if(i.infoType() != holder.infoType())
return;
switch(i.infoType()) {
case STREAM:
buildStreamInfoItem((StreamInfoItemHolder) holder, (StreamInfoItem) i);

View File

@ -71,16 +71,32 @@ public class InfoListAdapter extends RecyclerView.Adapter<InfoItemHolder> {
return infoItemList.size();
}
// don't ask why we have to do that this way... it's android accept it -.-
@Override
public InfoItemHolder onCreateViewHolder(ViewGroup parent, int i) {
switch(infoItemList.get(i).infoType()) {
public int getItemViewType(int position) {
switch(infoItemList.get(position).infoType()) {
case STREAM:
return 0;
case CHANNEL:
return 1;
case PLAYLIST:
return 2;
default:
Log.e(TAG, "Trollolo");
return -1;
}
}
@Override
public InfoItemHolder onCreateViewHolder(ViewGroup parent, int type) {
switch(type) {
case 0:
return new StreamInfoItemHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.stream_item, parent, false));
case CHANNEL:
case 1:
return new ChannelInfoItemHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.channel_item, parent, false));
case PLAYLIST:
case 2:
Log.e(TAG, "Playlist is not yet implemented");
return null;
default:

View File

@ -60,6 +60,9 @@ public class SearchInfoItemFragment extends Fragment {
private static final String TAG = SearchInfoItemFragment.class.toString();
private EnumSet<SearchEngine.Filter> filter =
EnumSet.of(SearchEngine.Filter.CHANNEL, SearchEngine.Filter.VIDEO);
/**
* Listener for search queries
*/
@ -293,6 +296,32 @@ public class SearchInfoItemFragment extends Fragment {
setupSearchView(searchView);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_filter_all:
changeFilter(item, EnumSet.of(SearchEngine.Filter.VIDEO, SearchEngine.Filter.CHANNEL));
return true;
case R.id.menu_filter_video:
changeFilter(item, EnumSet.of(SearchEngine.Filter.VIDEO));
return true;
case R.id.menu_filter_channel:
changeFilter(item, EnumSet.of(SearchEngine.Filter.CHANNEL));
return true;
default:
return false;
}
}
private void changeFilter(MenuItem item, EnumSet<SearchEngine.Filter> filter) {
this.filter = filter;
item.setChecked(true);
if(searchQuery != null && !searchQuery.isEmpty()) {
Log.d(TAG, "Fuck+ " + searchQuery);
search(searchQuery);
}
}
private void setupSearchView(SearchView searchView) {
suggestionListAdapter = new SuggestionListAdapter(getActivity());
searchView.setSuggestionsAdapter(suggestionListAdapter);
@ -320,7 +349,7 @@ public class SearchInfoItemFragment extends Fragment {
query,
page,
getActivity(),
EnumSet.of(SearchEngine.Filter.CHANNEL));
filter);
}
private void setDoneLoading() {

View File

@ -1,11 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_settings"
app:showAsAction="never"
android:title="@string/settings"/>
<item android:id="@+id/action_show_downloads"
app:showAsAction="never"
android:title="@string/downloads" />
<item android:id="@+id/action_settings"
app:showAsAction="never"
android:title="@string/settings"/>
</menu>

View File

@ -6,4 +6,15 @@
app:showAsAction="ifRoom"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView" />
<group android:id="@+id/search_filter_group"
android:checkableBehavior="single">
<item android:id="@+id/menu_filter_all"
android:title = "@string/all"
android:checked = "true"/>
<item android:id="@+id/menu_filter_video"
android:title = "@string/video"/>
<item android:id="@+id/menu_filter_channel"
android:title = "@string/channel"/>
</group>
</menu>

View File

@ -1,21 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="ifRoom"
android:title="@string/search"
app:actionViewClass="android.support.v7.widget.SearchView" />
<item android:id="@+id/action_settings"
app:showAsAction="never"
android:title="@string/settings"/>
<item android:id="@+id/action_show_downloads"
app:showAsAction="never"
android:title="@string/downloads" />
<item android:id="@+id/action_report_error"
app:showAsAction="never"
android:title="@string/report_error" />
</menu>

View File

@ -84,6 +84,8 @@
<string name="downloads_title">Downloads</string>
<string name="settings_title">Settings</string>
<string name="error_report_title">Error report</string>
<string name="all">All</string>
<string name="channel">Channel</string>
<!-- error strings -->
<string name="general_error">Error</string>