package org.smerty.zooborns;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import android.R;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.ImageView;
import android.widget.Toast;
public class FullscreenImage extends Activity {


	private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    
	private GestureDetector gestureDetector;
	
	private FullscreenImage that;
	

	class SwipeDetector extends SimpleOnGestureListener {
	   
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {

			try {
				if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
					return false;
				if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
						&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
					if (position < imageUriList.size() - 1) {
						position++;
						setImage();
					}
				} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
						&& Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
					if (position > 0) {
						position--;
						setImage();
					}
				}
			} catch (Exception e) {
				// nothing
			}
			return false;
		}
	}

	
	@Override
	public boolean onTouchEvent(MotionEvent event) {		
		gestureDetector.onTouchEvent(event);
		return true;
	}

	private ArrayList<Uri> imageUriList;
	private int position = -1;
	private ImageView fsimgview;
	
	@SuppressWarnings("unchecked")
	public void setImage() {
		if (fsimgview == null) {
			fsimgview = new ImageView(this);
			setContentView(fsimgview);
		}
		
		
		if (fsimgview != null) {
			if (position < 0) {
				position = this.getIntent().getIntExtra("currentImageIndex", 0);
				imageUriList = (ArrayList<Uri>) this.getIntent().getSerializableExtra("imageUriList");
			}
			Uri imgUri = imageUriList.get(position);
			if (imgUri != null) {
				Drawable image;
				try {
					image = Drawable.createFromStream(new FileInputStream(imgUri.getPath()), "src");
					fsimgview.setImageDrawable(image);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					fsimgview.setImageResource(R.drawable.ic_menu_close_clear_cancel);
				}
			}
			else {
				fsimgview.setImageResource(R.drawable.ic_menu_help);
			}
			fsimgview.invalidate();
		}
		else {
			Log.d("onCreate", "fsimgview was null?");
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		if(that == null) {
			that = this;
		}
		
		gestureDetector = new GestureDetector( new SwipeDetector() );
		
		setImage();
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	  super.onConfigurationChanged(newConfig);
	  if (fsimgview != null) {
		  setContentView(fsimgview);
	  }
	}
	
    public static final int MENU_SEND = 10;

    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_SEND, 0, "Send");
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_SEND:
			Intent i=new Intent(android.content.Intent.ACTION_SEND);
			i.setType("image/jpg");
			i.putExtra(Intent.EXTRA_SUBJECT, "ZooBorns");
			i.putExtra(Intent.EXTRA_STREAM, imageUriList.get(position));
			startActivity(Intent.createChooser(i, "Share Photo Using..."));
            return true;
        }
        return false;
    }

}

@@ -89,24 +89,52 @@
 dException;%0A
+import java.io.IOException;%0A
 import java.
@@ -612,37 +612,8 @@
 ew;%0A
-import android.widget.Toast;%0A
 publ
@@ -3300,16 +3300,65 @@
 ND = 10;
+%0A    public static final int MENU_WALLPAPER = 11;
 %0A%0A    pu
@@ -3439,19 +3439,83 @@
 D, 0, %22S
-end
+hare...%22);%0A        menu.add(0, MENU_WALLPAPER, 0, %22Set as Wallpaper
 %22);%0A    
@@ -3923,16 +3923,16 @@
 ...%22));%0A
-
         
@@ -3944,24 +3944,397 @@
 eturn true;%0A
+        case MENU_WALLPAPER:%0A        %09try %7B%0A%09%09%09%09that.getApplicationContext().setWallpaper(new FileInputStream(imageUriList.get(position).getPath()));%0A%09%09%09%7D catch (FileNotFoundException e) %7B%0A%09%09%09%09// TODO Auto-generated catch block%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09// TODO Auto-generated catch block%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A        %09return true;%0A
         %7D%0A  

add set as wallpaper and change send to share