LastPipeBender/app/src/main/java/org/schabi/newpipe/ChannelActivity.java

360 lines
14 KiB
Java
Raw Normal View History

2016-07-26 13:50:52 +02:00
package org.schabi.newpipe;
2016-08-01 01:56:19 +02:00
import android.content.Intent;
import android.net.Uri;
2016-07-26 13:50:52 +02:00
import android.os.Bundle;
2016-08-01 01:56:19 +02:00
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
2016-08-01 01:56:19 +02:00
import android.util.Log;
2017-02-27 16:38:01 +01:00
import android.view.Menu;
import android.view.MenuItem;
2016-07-26 13:50:52 +02:00
import android.view.View;
2017-02-27 15:58:09 +01:00
import android.widget.Button;
2016-08-01 01:56:19 +02:00
import android.widget.ImageView;
import android.widget.ProgressBar;
2017-02-27 15:58:09 +01:00
import android.widget.TextView;
2016-08-01 01:56:19 +02:00
import android.widget.Toast;
import com.nostra13.universalimageloader.core.ImageLoader;
import org.schabi.newpipe.detail.VideoItemDetailFragment;
2016-09-27 22:59:04 +02:00
import org.schabi.newpipe.extractor.NewPipe;
2016-08-01 01:56:19 +02:00
import org.schabi.newpipe.extractor.StreamingService;
2016-09-28 17:13:15 +02:00
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
import org.schabi.newpipe.extractor.channel.ChannelInfo;
import org.schabi.newpipe.extractor.exceptions.ExtractionException;
import org.schabi.newpipe.extractor.exceptions.ParsingException;
2016-09-26 17:02:55 +02:00
import org.schabi.newpipe.info_list.InfoItemBuilder;
import org.schabi.newpipe.info_list.InfoListAdapter;
2016-09-13 23:39:32 +02:00
import org.schabi.newpipe.report.ErrorActivity;
2017-02-27 16:38:01 +01:00
import org.schabi.newpipe.settings.SettingsActivity;
2017-02-18 21:59:48 +01:00
import org.schabi.newpipe.util.NavStack;
2016-08-01 01:56:19 +02:00
import java.io.IOException;
2017-02-15 12:59:36 +01:00
2016-09-12 00:33:11 +02:00
/**
* Copyright (C) Christian Schabesberger 2016 <chris.schabesberger@mailbox.org>
* ChannelActivity.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/>.
*/
2017-02-27 15:58:09 +01:00
public class ChannelActivity extends ThemableActivity {
2016-08-01 01:56:19 +02:00
private static final String TAG = ChannelActivity.class.toString();
private View rootView = null;
private int serviceId = -1;
private String channelUrl = "";
private int pageNumber = 0;
private boolean hasNextPage = true;
private boolean isLoading = false;
2016-08-01 01:56:19 +02:00
private ImageLoader imageLoader = ImageLoader.getInstance();
private InfoListAdapter infoListAdapter = null;
2016-08-01 01:56:19 +02:00
2017-02-27 15:58:09 +01:00
private String subS = "";
2016-07-26 13:50:52 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
2017-02-18 21:59:48 +01:00
super.onCreate(savedInstanceState);
2016-07-26 13:50:52 +02:00
setContentView(R.layout.activity_channel);
2017-02-27 15:58:09 +01:00
rootView = findViewById(android.R.id.content);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
2017-02-19 16:07:45 +01:00
if(savedInstanceState == null) {
Intent i = getIntent();
channelUrl = i.getStringExtra(NavStack.URL);
serviceId = i.getIntExtra(NavStack.SERVICE_ID, -1);
} else {
channelUrl = savedInstanceState.getString(NavStack.URL);
serviceId = savedInstanceState.getInt(NavStack.SERVICE_ID);
NavStack.getInstance()
.restoreSavedInstanceState(savedInstanceState);
}
2016-08-01 01:56:19 +02:00
2017-02-15 12:59:36 +01:00
infoListAdapter = new InfoListAdapter(this, rootView);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.channel_streams_view);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
2017-02-27 15:58:09 +01:00
infoListAdapter.setHeader(getLayoutInflater().inflate(R.layout.channel_header, recyclerView, false));
recyclerView.setAdapter(infoListAdapter);
2017-02-15 12:59:36 +01:00
infoListAdapter.setOnStreamInfoItemSelectedListener(
2017-02-13 00:55:05 +01:00
new InfoItemBuilder.OnInfoItemSelectedListener() {
@Override
2017-02-15 12:59:36 +01:00
public void selected(String url, int serviceId) {
2017-02-18 21:59:48 +01:00
NavStack.getInstance()
.openDetailActivity(ChannelActivity.this, url, serviceId);
}
});
// detect if list has ben scrolled to the bottom
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
2016-08-01 01:56:19 +02:00
@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 = layoutManager.getChildCount();
totalItemCount = layoutManager.getItemCount();
pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount
&& !isLoading
&& hasNextPage)
{
pageNumber++;
requestData(true);
2016-08-01 21:50:41 +02:00
}
2016-08-01 01:56:19 +02:00
}
}
});
2017-02-27 15:58:09 +01:00
subS = getString(R.string.subscriber);
requestData(false);
2016-08-01 01:56:19 +02:00
}
2017-02-19 16:07:45 +01:00
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(NavStack.URL, channelUrl);
outState.putInt(NavStack.SERVICE_ID, serviceId);
NavStack.getInstance()
.onSaveInstanceState(outState);
}
2016-08-01 01:56:19 +02:00
private void updateUi(final ChannelInfo info) {
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
ImageView channelBanner = (ImageView) findViewById(R.id.channel_banner_image);
2016-08-01 11:48:52 +02:00
ImageView avatarView = (ImageView) findViewById(R.id.channel_avatar_view);
ImageView haloView = (ImageView) findViewById(R.id.channel_avatar_halo);
2017-02-27 15:58:09 +01:00
TextView titleView = (TextView) findViewById(R.id.channel_title_view);
TextView subscirberView = (TextView) findViewById(R.id.channel_subscriber_view);
Button subscriberButton = (Button) findViewById(R.id.channel_subscribe_button);
2016-08-01 01:56:19 +02:00
progressBar.setVisibility(View.GONE);
if(info.channel_name != null && !info.channel_name.isEmpty()) {
2017-02-27 15:58:09 +01:00
getSupportActionBar().setTitle(info.channel_name);
titleView.setText(info.channel_name);
2016-08-01 01:56:19 +02:00
}
if(info.banner_url != null && !info.banner_url.isEmpty()) {
imageLoader.displayImage(info.banner_url, channelBanner,
2017-02-27 15:58:09 +01:00
new ImageErrorLoadingListener(this, rootView ,info.service_id));
2016-08-01 01:56:19 +02:00
}
2016-08-01 11:48:52 +02:00
if(info.avatar_url != null && !info.avatar_url.isEmpty()) {
avatarView.setVisibility(View.VISIBLE);
haloView.setVisibility(View.VISIBLE);
imageLoader.displayImage(info.avatar_url, avatarView,
2016-08-02 00:58:52 +02:00
new ImageErrorLoadingListener(this, rootView ,info.service_id));
2016-08-01 11:48:52 +02:00
}
2017-02-27 15:58:09 +01:00
if(info.subscriberCount != -1) {
subscirberView.setText(buildSubscriberString(info.subscriberCount));
}
if((info.feed_url != null && !info.feed_url.isEmpty()) ||
(info.subscriberCount != -1)) {
findViewById(R.id.channel_subscriber_layout).setVisibility(View.VISIBLE);
}
2016-08-01 01:56:19 +02:00
if(info.feed_url != null && !info.feed_url.isEmpty()) {
2017-02-27 15:58:09 +01:00
subscriberButton.setOnClickListener(new View.OnClickListener() {
2016-08-01 01:56:19 +02:00
@Override
public void onClick(View view) {
Log.d(TAG, info.feed_url);
2016-08-01 02:10:38 +02:00
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(info.feed_url));
2016-08-01 01:56:19 +02:00
startActivity(i);
}
});
} else {
2017-02-27 15:58:09 +01:00
subscriberButton.setVisibility(View.GONE);
2016-08-01 01:56:19 +02:00
}
2017-02-27 15:58:09 +01:00
2016-08-01 21:50:41 +02:00
}
private void addVideos(final ChannelInfo info) {
2017-02-13 00:55:05 +01:00
infoListAdapter.addInfoItemList(info.related_streams);
2016-08-01 01:56:19 +02:00
}
private void postNewErrorToast(Handler h, final int stringResource) {
h.post(new Runnable() {
2016-07-26 13:50:52 +02:00
@Override
2016-08-01 01:56:19 +02:00
public void run() {
Toast.makeText(ChannelActivity.this,
stringResource, Toast.LENGTH_LONG).show();
2016-07-26 13:50:52 +02:00
}
});
}
2016-08-01 01:56:19 +02:00
private void requestData(final boolean onlyVideos) {
// start processing
isLoading = true;
Thread channelExtractorThread = new Thread(new Runnable() {
Handler h = new Handler();
@Override
public void run() {
2016-09-11 23:15:22 +02:00
StreamingService service = null;
try {
2016-09-27 22:59:04 +02:00
service = NewPipe.getService(serviceId);
ChannelExtractor extractor = service.getChannelExtractorInstance(
2016-09-27 22:59:04 +02:00
channelUrl, pageNumber);
2016-09-28 17:13:15 +02:00
final ChannelInfo info = ChannelInfo.getInfo(extractor);
h.post(new Runnable() {
@Override
public void run() {
isLoading = false;
if(!onlyVideos) {
updateUi(info);
}
hasNextPage = info.hasNextPage;
addVideos(info);
}
});
// look for non critical errors during extraction
if(info != null &&
!info.errors.isEmpty()) {
Log.e(TAG, "OCCURRED ERRORS DURING EXTRACTION:");
for (Throwable e : info.errors) {
e.printStackTrace();
Log.e(TAG, "------");
}
2016-09-11 23:15:22 +02:00
View rootView = findViewById(android.R.id.content);
ErrorActivity.reportError(h, ChannelActivity.this,
info.errors, null, rootView,
2016-09-14 00:22:55 +02:00
ErrorActivity.ErrorInfo.make(ErrorActivity.REQUESTED_CHANNEL,
2016-09-11 23:15:22 +02:00
service.getServiceInfo().name, channelUrl, 0 /* no message for the user */));
}
} catch(IOException ioe) {
postNewErrorToast(h, R.string.network_error);
ioe.printStackTrace();
} catch(ParsingException pe) {
2016-09-11 23:15:22 +02:00
ErrorActivity.reportError(h, ChannelActivity.this, pe, VideoItemDetailFragment.class, null,
2016-09-14 00:22:55 +02:00
ErrorActivity.ErrorInfo.make(ErrorActivity.REQUESTED_CHANNEL,
2016-09-11 23:15:22 +02:00
service.getServiceInfo().name, channelUrl, R.string.parsing_error));
h.post(new Runnable() {
@Override
public void run() {
ChannelActivity.this.finish();
}
});
pe.printStackTrace();
} catch(ExtractionException ex) {
2017-02-15 12:59:36 +01:00
String name = "none";
if(service != null) {
name = service.getServiceInfo().name;
}
2016-09-11 23:15:22 +02:00
ErrorActivity.reportError(h, ChannelActivity.this, ex, VideoItemDetailFragment.class, null,
2016-09-14 00:22:55 +02:00
ErrorActivity.ErrorInfo.make(ErrorActivity.REQUESTED_CHANNEL,
2017-02-15 12:59:36 +01:00
name, channelUrl, R.string.parsing_error));
2016-09-11 23:15:22 +02:00
h.post(new Runnable() {
@Override
public void run() {
ChannelActivity.this.finish();
}
});
ex.printStackTrace();
} catch(Exception e) {
2016-09-11 23:15:22 +02:00
ErrorActivity.reportError(h, ChannelActivity.this, e, VideoItemDetailFragment.class, null,
2016-09-14 00:22:55 +02:00
ErrorActivity.ErrorInfo.make(ErrorActivity.REQUESTED_CHANNEL,
2016-09-11 23:15:22 +02:00
service.getServiceInfo().name, channelUrl, R.string.general_error));
h.post(new Runnable() {
@Override
public void run() {
ChannelActivity.this.finish();
}
});
e.printStackTrace();
}
}
});
channelExtractorThread.start();
}
2016-08-01 01:56:19 +02:00
2017-02-18 21:59:48 +01:00
@Override
public void onBackPressed() {
try {
NavStack.getInstance()
.navBack(this);
} catch (Exception e) {
ErrorActivity.reportUiError(this, e);
}
}
2017-02-27 16:38:01 +01:00
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu_channel, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()) {
case R.id.action_settings: {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
case R.id.menu_item_openInBrowser: {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(channelUrl));
startActivity(Intent.createChooser(intent, getString(R.string.choose_browser)));
}
case R.id.menu_item_share:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, channelUrl);
intent.setType("text/plain");
startActivity(Intent.createChooser(intent, getString(R.string.share_dialog_title)));
case android.R.id.home:
NavStack.getInstance().openMainActivity(this);
default:
return super.onOptionsItemSelected(item);
}
}
2017-02-27 15:58:09 +01:00
private String buildSubscriberString(long count) {
String out = "";
if(count >= 1000000000){
out += Long.toString((count/1000000000)%1000)+".";
}
if(count>=1000000){
out += Long.toString((count/1000000)%1000) + ".";
}
if(count>=1000){
out += Long.toString((count/1000)%1000)+".";
}
out += Long.toString(count%1000) + " " + subS;
return out;
}
2016-07-26 13:50:52 +02:00
}