package com.eegeo.photos;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import com.eegeo.helpers.IActivityIntentResultHandler;
import com.eegeo.mobileexampleapp.MainActivity;

// This class handles the dispatching of intents related to fetching an image via photo or gallery selection.
public class PhotoIntentDispatcher 
{
	public static final int REQUEST_IMAGE_CAPTURE = 1;
	public static final int SELECT_PHOTO_FROM_GALLERY = 2;
	
	private MainActivity m_activity = null;
	private List<IActivityIntentResultHandler> m_activityCallbacks = new ArrayList<IActivityIntentResultHandler>();
	private Uri m_currentPhotoPath;
	
	public PhotoIntentDispatcher(MainActivity activity)
	{
		m_activity = activity;
	}
	
	public void takePhoto()
	{
		Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		if(takePictureIntent.resolveActivity(m_activity.getPackageManager()) != null)
		{
			String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
			String filePath = timeStamp + ".jpg";
			File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), filePath);
			m_currentPhotoPath = Uri.fromFile(file);
			takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, m_currentPhotoPath);
			m_activity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
		}
	}
	
	public void selectPhotoFromGallery()
	{
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		m_activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PHOTO_FROM_GALLERY);
	}
	
	public void addActivityIntentResultHandler(IActivityIntentResultHandler handler)
	{
		if(!m_activityCallbacks.contains(handler))
		{
			m_activityCallbacks.add(handler);
		}
	}
	
	public void removeActivityIntentResultHandler(IActivityIntentResultHandler handler)
	{
		if(m_activityCallbacks.contains(handler))
		{
			m_activityCallbacks.remove(handler);
		}
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if(requestCode != REQUEST_IMAGE_CAPTURE && requestCode != SELECT_PHOTO_FROM_GALLERY)
			return;
		
		for(IActivityIntentResultHandler handler : m_activityCallbacks)
		{
			handler.onActivityResult(requestCode, resultCode, data);
		}
	}

	public Uri getCurrentPhotoPath() {
		return m_currentPhotoPath;
	}
}

@@ -1663,84 +1663,85 @@
 ent(
-);%0A%09%09intent.setType(%22image/*%22);%0A%09%09intent.setAction(Intent.ACTION_GET
+Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL
 _CONTENT
 );%0A%09
@@ -1736,16 +1736,20 @@
 _CONTENT
+_URI
 );%0A%09%09m_a
@@ -1783,54 +1783,14 @@
 ult(
-Intent.createChooser(intent, %22Select Picture%22)
+intent
 , SE

Fix for MPLY-5193. Using ACTION_PICK intent with URI for primary image storage instead of ACTION_GET_CONTENT with specified content type. This stops apps like Dropbox, Google Drive and Root Explorer from being options to select a POI image from. Buddy: Malc