package course.labs.asynctasklab;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;

public class DownloaderTaskFragment extends Fragment {

	private DownloadFinishedListener mCallback;
	private Context mContext;
	
	@SuppressWarnings ("unused")
	private static final String TAG = "Lab-Threads";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Preserve across reconfigurations
		setRetainInstance(true);
		
		// TODO: Create new DownloaderTask that "downloads" data

        
		
		// TODO: Retrieve arguments from DownloaderTaskFragment
		// Prepare them for use with DownloaderTask. 

        
        
        
		// TODO: Start the DownloaderTask 
		
        

	}

	// Assign current hosting Activity to mCallback
	// Store application context for use by downloadTweets()
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		mContext = activity.getApplicationContext(); 

		// Make sure that the hosting activity has implemented
		// the correct callback interface.
		try {
			mCallback = (DownloadFinishedListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement DownloadFinishedListener");
		}
	}

	// Null out mCallback
	@Override
	public void onDetach() {
		super.onDetach();
		mCallback = null;
	}

	// TODO: Implement an AsyncTask subclass called DownLoaderTask.
	// This class must use the downloadTweets method (currently commented
	// out). Ultimately, it must also pass newly available data back to 
	// the hosting Activity using the DownloadFinishedListener interface.

//	public class DownloaderTask extends ... {
	

    
    
    
    
    
    
    
        // TODO: Uncomment this helper method
		// Simulates downloading Twitter data from the network

        /*
         private String[] downloadTweets(Integer resourceIDS[]) {
			final int simulatedDelay = 2000;
			String[] feeds = new String[resourceIDS.length];
			try {
				for (int idx = 0; idx < resourceIDS.length; idx++) {
					InputStream inputStream;
					BufferedReader in;
					try {
						// Pretend downloading takes a long time
						Thread.sleep(simulatedDelay);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}

					inputStream = mContext.getResources().openRawResource(
							resourceIDS[idx]);
					in = new BufferedReader(new InputStreamReader(inputStream));

					String readLine;
					StringBuffer buf = new StringBuffer();

					while ((readLine = in.readLine()) != null) {
						buf.append(readLine);
					}

					feeds[idx] = buf.toString();

					if (null != in) {
						in.close();
					}
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			return feeds;
		}
         */


    
    
    
    
    
    

}
@@ -763,16 +763,59 @@
 s%22 data%0A
+%09%09DownloaderTask dt = new DownloaderTask();
 %0A       
@@ -925,34 +925,220 @@
 rTask. %0A
-%0A   
+%09%09Bundle bundle = getArguments();%0A%0A
      
-%0A
    
-     
+ArrayList%3CInteger%3E list = bundle.getIntegerArrayList(MainActivity.TAG_FRIEND_RES_IDS);%0A%09%09Integer%5B%5D integers = new Integer%5Blist.size()%5D;%0A%09%09list.toArray(integers);
 %0A       
@@ -1176,18 +1176,16 @@
 erTask %0A
-%09%09
 %0A       
@@ -1185,16 +1185,37 @@
         
+dt.execute(integers);
 %0A%0A%09%7D%0A%0A%09/
@@ -2134,18 +2134,16 @@
 rface.%0A%0A
-//
 %09public 
@@ -2175,26 +2175,166 @@
 nds 
-... %7B%0A%09%0A%0A    %0A    
+AsyncTask%3CInteger, Integer, String%5B%5D%3E %7B%0A%09%0A%0A    %0A    %09@Override%0A%09%09protected  void onPostExecute(String%5B%5D result)%7B%0A%09%09%09mCallback.notifyDataRefreshed(result);%0A%09%09%7D
 %0A   
@@ -2463,18 +2463,8 @@
 rk%0A%0A
-        /*
 %0A   
@@ -3383,50 +3383,121 @@
 %09%09%7D%0A
-         */%0A%0A%0A    %0A    %0A    %0A    %0A    %0A    %0A
+%0A%0A%09%09@Override%0A%09%09protected String%5B%5D doInBackground(Integer... integers) %7B%0A%09%09%09return downloadTweets(integers);%0A%09%09%7D%0A%09%7D
 %0A%7D

Complete assignment.