package com.blundell.tut.ui.phone;
 
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
 
import com.blundell.tut.R;
import com.blundell.tut.domain.Library;
import com.blundell.tut.domain.Video;
import com.blundell.tut.service.task.GetYouTubeUserVideosTask;
import com.blundell.tut.ui.VideoClickListener;
import com.blundell.tut.ui.widget.VideosListView;
 
/**
 * The Activity can retrieve Videos for a specific username from YouTube</br>
 * It then displays them into a list including the Thumbnail preview and the title</br>
 * There is a reference to each video on YouTube as well but this isn't used in this tutorial</br>
 * </br>
 * <b>Note<b/> orientation change isn't covered in this tutorial, you will want to override
 * onSaveInstanceState() and onRestoreInstanceState() when you come to this
 * </br>
 * @author paul.blundell
 */
public class MainActivity extends Activity implements VideoClickListener {
    
    private VideosListView listView;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        listView = (VideosListView) findViewById(R.id.videosListView);
        
        
        
        listView.setOnVideoClickListener(this);
    }
 
    public void getUserYouTubeFeed(View v){
        new Thread(new GetYouTubeUserVideosTask(responseHandler, "blundellp")).start();
    }
    
    Handler responseHandler = new Handler() {
        public void handleMessage(Message msg) {
            populateListWithVideos(msg);
        };
    };
 
    private void populateListWithVideos(Message msg) {
        Library lib = (Library) msg.getData().get(GetYouTubeUserVideosTask.LIBRARY);
        listView.setVideos(lib.getVideos());
    }
     
    @Override
    protected void onStop() {
        responseHandler = null;
        super.onStop();
    }
 
    
    
    @Override
    public void onVideoClicked(Video video) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(video.getUrl()));
        startActivity(intent);
    }
}
 
 
No comments:
Post a Comment