2016-07-26 13:50:52 +02:00
|
|
|
package org.schabi.newpipe;
|
|
|
|
|
2017-02-15 12:59:36 +01:00
|
|
|
import android.annotation.TargetApi;
|
|
|
|
import android.content.Context;
|
2016-08-01 01:56:19 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
2017-02-15 12:59:36 +01:00
|
|
|
import android.os.Build;
|
2016-07-26 13:50:52 +02:00
|
|
|
import android.os.Bundle;
|
2016-08-01 01:56:19 +02:00
|
|
|
import android.os.Handler;
|
2016-12-30 00:49:39 +05:30
|
|
|
import android.preference.PreferenceManager;
|
2016-08-01 01:56:19 +02:00
|
|
|
import android.support.design.widget.CollapsingToolbarLayout;
|
2016-07-26 13:50:52 +02:00
|
|
|
import android.support.design.widget.FloatingActionButton;
|
2017-02-15 12:59:36 +01:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2016-07-26 13:50:52 +02:00
|
|
|
import android.support.v7.app.AppCompatActivity;
|
2016-08-03 13:09:48 +02:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
2016-07-26 13:50:52 +02:00
|
|
|
import android.support.v7.widget.Toolbar;
|
2016-08-01 01:56:19 +02:00
|
|
|
import android.util.Log;
|
2016-07-26 13:50:52 +02:00
|
|
|
import android.view.View;
|
2017-02-15 12:59:36 +01:00
|
|
|
import android.view.Window;
|
|
|
|
import android.view.WindowManager;
|
2016-08-01 01:56:19 +02:00
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
|
|
|
|
2016-08-03 13:09:48 +02:00
|
|
|
import org.schabi.newpipe.detail.VideoItemDetailActivity;
|
|
|
|
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;
|
2016-08-03 13:09:48 +02:00
|
|
|
import org.schabi.newpipe.info_list.InfoListAdapter;
|
2016-09-13 23:39:32 +02:00
|
|
|
import org.schabi.newpipe.report.ErrorActivity;
|
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;
|
2016-12-30 00:49:39 +05:30
|
|
|
import java.util.Objects;
|
2016-07-26 13:50:52 +02:00
|
|
|
|
2017-02-15 12:59:36 +01:00
|
|
|
import static android.os.Build.VERSION.SDK;
|
|
|
|
import static android.os.Build.VERSION.SDK_INT;
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2016-07-26 13:50:52 +02:00
|
|
|
public class ChannelActivity extends AppCompatActivity {
|
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 = "";
|
2016-08-03 15:32:25 +02:00
|
|
|
private int pageNumber = 0;
|
2016-09-10 16:26:21 +02:00
|
|
|
private boolean hasNextPage = true;
|
2016-08-03 15:32:25 +02:00
|
|
|
private boolean isLoading = false;
|
2016-08-01 01:56:19 +02:00
|
|
|
|
|
|
|
private ImageLoader imageLoader = ImageLoader.getInstance();
|
2016-08-03 13:09:48 +02:00
|
|
|
private InfoListAdapter infoListAdapter = null;
|
2016-08-01 01:56:19 +02:00
|
|
|
|
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);
|
|
|
|
|
|
|
|
//since we set themeing we have to set translucent statusBar by hand
|
2017-01-26 12:07:41 +05:30
|
|
|
if (PreferenceManager.getDefaultSharedPreferences(this)
|
|
|
|
.getString("theme", getResources().getString(R.string.light_theme_title)).
|
|
|
|
equals(getResources().getString(R.string.dark_theme_title))) {
|
2016-12-30 00:49:39 +05:30
|
|
|
setTheme(R.style.DarkTheme_NoActionBar);
|
|
|
|
}
|
2017-02-18 21:59:48 +01:00
|
|
|
setTranslucentStatusBar(getWindow());
|
|
|
|
|
2016-07-26 13:50:52 +02:00
|
|
|
setContentView(R.layout.activity_channel);
|
|
|
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
2016-08-01 01:56:19 +02:00
|
|
|
rootView = findViewById(R.id.rootView);
|
2016-07-26 13:50:52 +02:00
|
|
|
setSupportActionBar(toolbar);
|
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
|
|
|
|
2016-08-03 13:09:48 +02:00
|
|
|
infoListAdapter = new InfoListAdapter(this, rootView);
|
|
|
|
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.channel_streams_view);
|
2016-08-03 15:32:25 +02:00
|
|
|
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
|
|
|
recyclerView.setLayoutManager(layoutManager);
|
2016-08-03 13:09:48 +02:00
|
|
|
recyclerView.setAdapter(infoListAdapter);
|
2017-02-15 12:59:36 +01:00
|
|
|
infoListAdapter.setOnStreamInfoItemSelectedListener(
|
2017-02-13 00:55:05 +01:00
|
|
|
new InfoItemBuilder.OnInfoItemSelectedListener() {
|
2016-08-03 13:09:48 +02:00
|
|
|
@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);
|
2016-08-03 13:09:48 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-08-03 15:32:25 +02:00
|
|
|
// detect if list has ben scrolled to the bottom
|
|
|
|
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
|
2016-08-01 01:56:19 +02:00
|
|
|
@Override
|
2016-08-03 15:32:25 +02:00
|
|
|
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();
|
|
|
|
|
2016-09-10 16:26:21 +02:00
|
|
|
if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount
|
|
|
|
&& !isLoading
|
|
|
|
&& hasNextPage)
|
2016-08-03 15:32:25 +02:00
|
|
|
{
|
|
|
|
pageNumber++;
|
2016-09-10 16:26:21 +02:00
|
|
|
requestData(true);
|
2016-08-01 21:50:41 +02:00
|
|
|
}
|
2016-08-01 01:56:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-10 16:26:21 +02:00
|
|
|
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) {
|
|
|
|
CollapsingToolbarLayout ctl = (CollapsingToolbarLayout) findViewById(R.id.channel_toolbar_layout);
|
|
|
|
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
|
|
|
|
ImageView channelBanner = (ImageView) findViewById(R.id.channel_banner_image);
|
2016-12-30 00:49:39 +05:30
|
|
|
final FloatingActionButton feedButton = (FloatingActionButton) findViewById(R.id.channel_rss_fab);
|
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);
|
2016-08-01 01:56:19 +02:00
|
|
|
|
|
|
|
progressBar.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
if(info.channel_name != null && !info.channel_name.isEmpty()) {
|
|
|
|
ctl.setTitle(info.channel_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(info.banner_url != null && !info.banner_url.isEmpty()) {
|
|
|
|
imageLoader.displayImage(info.banner_url, channelBanner,
|
2016-08-02 00:58:52 +02: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
|
|
|
}
|
|
|
|
|
2016-08-01 01:56:19 +02:00
|
|
|
if(info.feed_url != null && !info.feed_url.isEmpty()) {
|
|
|
|
feedButton.setOnClickListener(new View.OnClickListener() {
|
|
|
|
@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 {
|
|
|
|
feedButton.setVisibility(View.GONE);
|
|
|
|
}
|
2016-08-01 21:50:41 +02:00
|
|
|
}
|
|
|
|
|
2016-09-10 16:26:21 +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
|
|
|
|
2016-09-10 16:26:21 +02:00
|
|
|
private void requestData(final boolean onlyVideos) {
|
2016-08-03 15:32:25 +02:00
|
|
|
// 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;
|
2016-08-03 15:32:25 +02:00
|
|
|
try {
|
2016-09-27 22:59:04 +02:00
|
|
|
service = NewPipe.getService(serviceId);
|
2016-08-03 15:32:25 +02:00
|
|
|
ChannelExtractor extractor = service.getChannelExtractorInstance(
|
2016-09-27 22:59:04 +02:00
|
|
|
channelUrl, pageNumber);
|
2016-08-03 15:32:25 +02:00
|
|
|
|
2016-09-28 17:13:15 +02:00
|
|
|
final ChannelInfo info = ChannelInfo.getInfo(extractor);
|
2016-08-03 15:32:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
h.post(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2016-09-10 16:26:21 +02:00
|
|
|
isLoading = false;
|
|
|
|
if(!onlyVideos) {
|
|
|
|
updateUi(info);
|
|
|
|
}
|
|
|
|
hasNextPage = info.hasNextPage;
|
|
|
|
addVideos(info);
|
2016-08-03 15:32:25 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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 */));
|
2016-08-03 15:32:25 +02:00
|
|
|
}
|
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
});
|
2016-08-03 15:32:25 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
2016-08-03 15:32:25 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
2016-08-03 15:32:25 +02:00
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
channelExtractorThread.start();
|
|
|
|
}
|
2016-08-01 01:56:19 +02:00
|
|
|
|
2017-02-15 12:59:36 +01:00
|
|
|
|
|
|
|
// fix transparent statusbar fuckup (fuck google why can't they just leave something that worked
|
|
|
|
// as it is, and everyone gets happy)
|
|
|
|
public static void setTranslucentStatusBar(Window window) {
|
|
|
|
if (window == null) return;
|
|
|
|
int sdkInt = Build.VERSION.SDK_INT;
|
|
|
|
if (sdkInt >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
setTranslucentStatusBarLollipop(window);
|
|
|
|
} else if (sdkInt >= Build.VERSION_CODES.KITKAT) {
|
|
|
|
setTranslucentStatusBarKiKat(window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
|
|
private static void setTranslucentStatusBarLollipop(Window window) {
|
|
|
|
window.setStatusBarColor(
|
|
|
|
ContextCompat.getColor(window.getContext(), android.R.color.transparent));
|
|
|
|
}
|
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.KITKAT)
|
|
|
|
private static void setTranslucentStatusBarKiKat(Window window) {
|
|
|
|
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
|
|
|
|
}
|
|
|
|
|
2017-02-18 21:59:48 +01:00
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
try {
|
|
|
|
NavStack.getInstance()
|
|
|
|
.navBack(this);
|
|
|
|
} catch (Exception e) {
|
|
|
|
ErrorActivity.reportUiError(this, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 13:50:52 +02:00
|
|
|
}
|