package edu.cmu.sv.trailscribe.view;

import java.util.ArrayList;


import edu.cmu.sv.trailscribe.R;
import edu.cmu.sv.trailscribe.controller.SynchronizationCenterController;
import edu.cmu.sv.trailscribe.model.AsyncTaskCompleteListener;
import edu.cmu.sv.trailscribe.model.Decompressor;
import edu.cmu.sv.trailscribe.model.Downloader;
import edu.cmu.sv.trailscribe.model.Map;
import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.util.Log;

public class SynchronizationCenterActivity 
    extends BaseActivity implements AsyncTaskCompleteListener {

	public static final ActivityTheme ACTIVITY_THEME = 
            new ActivityTheme("SyncCenter", "Synchronizes TrailScribe", R.color.red);
    
		private ListView mListView;
		private SynchronizationCenterController mController;
		private ArrayList<Map> mMaps;
		private Map mCurrentMap;
		private ProgressDialog mSyncProgressDialog;
		private ProgressDialog mDownloadProgressDialog;
		private ProgressDialog mUnzippingProgressDialog; 
		private ArrayAdapter<Map> mAdapter;
    private static final String LOG_TAG = "SynchronizationCenterActivity";
	  
    @Override
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mSyncProgressDialog = ProgressDialog.show(this, "", getResources().getString(R.string.synchronizing), true, true);
      	  mDownloadProgressDialog = new ProgressDialog(SynchronizationCenterActivity.this);
      	  mUnzippingProgressDialog = new ProgressDialog(SynchronizationCenterActivity.this);
          mController = new SynchronizationCenterController(this, this);
          mController.execute();
      }
	  
	  @Override
	  protected void onPause(){
		  super.onPause();
	  }
	  
	  @Override
	  protected void onResume(){
		  super.onResume();
	  }
	  
	@SuppressWarnings("unchecked")
	@Override
	public void onTaskCompleted(Object result) {
		if (result instanceof ArrayList<?>){
			mMaps = (ArrayList<Map>) result;
			
			if (mSyncProgressDialog != null){
                            try {
	            mSyncProgressDialog.dismiss();
                            } catch (Exception e) {
                                Log.w(LOG_TAG, "Activity ended before task completed");
                            }
			}
			
			setContentView(R.layout.activity_sync_center);
			mListView = (ListView) findViewById(R.id.sync_list);
	
            setActionBar(getResources().getString(ACTIVITY_THEME.getActivityColor()));
			mAdapter = new ArrayAdapter<Map>(SynchronizationCenterActivity.this,
	           android.R.layout.simple_list_item_1, android.R.id.text1, mMaps);
	 
	         // Assign adapter to ListView
	         mListView.setAdapter(mAdapter); 
	         
	         // ListView Item Click Listener
	         mListView.setOnItemClickListener(new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					mCurrentMap = mMaps.get(arg2);
					new Downloader(mCurrentMap, SynchronizationCenterActivity.this, mDownloadProgressDialog, SynchronizationCenterActivity.this).execute();
				}
	         }); 
		}
		else if(result instanceof Integer){
			int downloadResult = (Integer) result;
			if(downloadResult == DownloadManager.STATUS_SUCCESSFUL){
	            	new UnzipTask().execute();
	            }
		}
	}
	
	private class UnzipTask extends AsyncTask<Void, Integer, Void>{

		@Override
		protected void onPreExecute() 
		{
			mUnzippingProgressDialog = ProgressDialog.show(SynchronizationCenterActivity.this, "", getResources().getString(R.string.uncompressing), true, true);
		}
		
		@Override
		protected Void doInBackground(Void... params) 
		{
			//Get the current thread's token
			synchronized (this) 
			{
				int subStringIndex = mCurrentMap.getFilename().lastIndexOf("/") +1;
            	String mapFileName = mCurrentMap.getFilename().substring(subStringIndex);
            	Decompressor decompressor = new Decompressor(Environment.getExternalStorageDirectory() + "/trailscribe/" + mapFileName, Environment.getExternalStorageDirectory() + "/trailscribe/");
            	decompressor.unzip();
			}
			return null;
		}

		//after executing the code in the thread
		@Override
		protected void onPostExecute(Void result) 
		{
			//close the progress dialog
			if(mUnzippingProgressDialog!= null){
				mUnzippingProgressDialog.dismiss();
			}
			mAdapter.remove(mCurrentMap);
			mAdapter.notifyDataSetChanged();
		}
	}
}

@@ -1336,74 +1336,345 @@
 ate 
-static final String LOG_TAG = %22SynchronizationCenterActivity%22;%0A%09  
+boolean mapsFetched = false;%0A    private Runnable mapsFetchedCallback = null;%0A    private static final String LOG_TAG = %22SynchronizationCenterActivity%22;%0A%09  %0A%0A    public void setMapsFetchedCallback(Runnable callback) %7B%0A        mapsFetchedCallback = callback;%0A    %7D%0A%0A    public boolean areMapsFetched() %7B%0A        return mapsFetched;%0A    %7D%0A
 %0A   
@@ -3872,24 +3872,177 @@
        %7D%0A%09%09%7D
+%0A%0A                mapsFetched = true;%0A                if (mapsFetchedCallback != null) %7B%0A                    mapsFetchedCallback.run();%0A                %7D
 %0A%09%7D%0A%09%0A%09priva

Add callback to be able to test map fetching