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

208 lines
7.6 KiB
Java
Raw Normal View History

2016-07-26 11:50:52 +00:00
package org.schabi.newpipe;
2016-07-31 23:56:19 +00:00
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
2016-07-26 11:50:52 +00:00
import android.os.Bundle;
2016-07-31 23:56:19 +00:00
import android.os.Handler;
import android.support.design.widget.CollapsingToolbarLayout;
2016-07-26 11:50:52 +00:00
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
2016-07-31 23:56:19 +00:00
import android.util.Log;
2016-08-01 19:50:41 +00:00
import android.view.LayoutInflater;
2016-07-26 11:50:52 +00:00
import android.view.View;
2016-07-31 23:56:19 +00:00
import android.widget.ImageView;
2016-08-01 19:50:41 +00:00
import android.widget.LinearLayout;
2016-07-31 23:56:19 +00:00
import android.widget.ProgressBar;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import org.schabi.newpipe.extractor.ChannelExtractor;
import org.schabi.newpipe.extractor.ChannelInfo;
import org.schabi.newpipe.extractor.ExtractionException;
import org.schabi.newpipe.extractor.ParsingException;
import org.schabi.newpipe.extractor.ServiceList;
2016-08-01 19:50:41 +00:00
import org.schabi.newpipe.extractor.StreamPreviewInfo;
2016-07-31 23:56:19 +00:00
import org.schabi.newpipe.extractor.StreamingService;
import java.io.IOException;
2016-08-01 19:50:41 +00:00
import java.util.ArrayList;
2016-07-26 11:50:52 +00:00
public class ChannelActivity extends AppCompatActivity {
2016-07-31 23:56:19 +00:00
private static final String TAG = ChannelActivity.class.toString();
private View rootView = null;
class FailedThumbnailListener implements ImageLoadingListener {
int serviceId = -1;
public FailedThumbnailListener(int serviceId) {
this.serviceId= serviceId;
}
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
ErrorActivity.reportError(ChannelActivity.this,
failReason.getCause(), null, rootView,
ErrorActivity.ErrorInfo.make(ErrorActivity.LOAD_IMAGE,
ServiceList.getNameOfService(serviceId), imageUri,
R.string.could_not_load_image));
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
}
// intent const
public static final String CHANNEL_URL = "channel_url";
public static final String SERVICE_ID = "service_id";
private int serviceId = -1;
private String channelUrl = "";
private ImageLoader imageLoader = ImageLoader.getInstance();
2016-07-26 11:50:52 +00:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_channel);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
2016-07-31 23:56:19 +00:00
rootView = findViewById(R.id.rootView);
2016-07-26 11:50:52 +00:00
setSupportActionBar(toolbar);
2016-07-31 23:56:19 +00:00
Intent i = getIntent();
channelUrl = i.getStringExtra(CHANNEL_URL);
serviceId = i.getIntExtra(SERVICE_ID, -1);
// start processing
Thread channelExtractorThread = new Thread(new Runnable() {
Handler h = new Handler();
@Override
public void run() {
try {
StreamingService service = ServiceList.getService(serviceId);
ChannelExtractor extractor = service.getChannelExtractorInstance(
channelUrl, new Downloader());
final ChannelInfo info = ChannelInfo.getInfo(extractor, new Downloader());
h.post(new Runnable() {
@Override
public void run() {
updateUi(info);
}
});
2016-08-01 19:50:41 +00: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-07-31 23:56:19 +00:00
} catch(IOException ioe) {
postNewErrorToast(h, R.string.network_error);
ioe.printStackTrace();
} catch(ParsingException pe) {
pe.printStackTrace();
} catch(ExtractionException ex) {
ex.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
});
channelExtractorThread.start();
}
2016-07-26 11:50:52 +00:00
2016-07-31 23:56:19 +00:00
private void updateUi(final ChannelInfo info) {
2016-08-01 19:50:41 +00:00
VideoInfoItemViewCreator viCreator =
new VideoInfoItemViewCreator(LayoutInflater.from(this), this, rootView);
2016-07-31 23:56:19 +00:00
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-08-01 19:50:41 +00:00
View channelContentView = findViewById(R.id.channel_content_view);
2016-07-31 23:56:19 +00:00
FloatingActionButton feedButton = (FloatingActionButton) findViewById(R.id.channel_rss_fab);
2016-08-01 09:48:52 +00:00
ImageView avatarView = (ImageView) findViewById(R.id.channel_avatar_view);
ImageView haloView = (ImageView) findViewById(R.id.channel_avatar_halo);
2016-07-31 23:56:19 +00:00
progressBar.setVisibility(View.GONE);
channelContentView.setVisibility(View.VISIBLE);
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,
new FailedThumbnailListener(info.service_id));
}
2016-08-01 09:48:52 +00:00
if(info.avatar_url != null && !info.avatar_url.isEmpty()) {
avatarView.setVisibility(View.VISIBLE);
haloView.setVisibility(View.VISIBLE);
imageLoader.displayImage(info.avatar_url, avatarView,
new FailedThumbnailListener(info.service_id));
}
2016-07-31 23:56:19 +00: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 00:10:38 +00:00
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(info.feed_url));
2016-07-31 23:56:19 +00:00
startActivity(i);
}
});
} else {
feedButton.setVisibility(View.GONE);
}
2016-08-01 19:50:41 +00:00
initVideos(info, viCreator);
}
private void initVideos(final ChannelInfo info, VideoInfoItemViewCreator viCreator) {
LinearLayout streamLayout = (LinearLayout) findViewById(R.id.channel_streams_view);
ArrayList<StreamPreviewInfo> streamsList = new ArrayList<>(info.related_streams);
for(final StreamPreviewInfo streamInfo : streamsList) {
View itemView = viCreator.getViewFromVideoInfoItem(null, streamLayout, streamInfo);
itemView = viCreator.setupView(itemView, streamInfo);
streamLayout.addView(itemView);
}
2016-07-31 23:56:19 +00:00
}
private void postNewErrorToast(Handler h, final int stringResource) {
h.post(new Runnable() {
2016-07-26 11:50:52 +00:00
@Override
2016-07-31 23:56:19 +00:00
public void run() {
Toast.makeText(ChannelActivity.this,
stringResource, Toast.LENGTH_LONG).show();
2016-07-26 11:50:52 +00:00
}
});
}
2016-07-31 23:56:19 +00:00
2016-07-26 11:50:52 +00:00
}