package com.michaldabski.utils;

import java.io.File;
import java.util.List;

import com.michaldabski.fileexplorer.MainActivity;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;

public class IntentUtils
{
	
	public static Intent createFileOpenIntent(File file)
	{
		Intent intent = new Intent(Intent.ACTION_VIEW);		
		intent.setDataAndType(Uri.fromFile(file), FileUtils.getFileMimeType(file));
		return intent;
	}
	
	public static void createShortcut(Context context, File file)
	{
		final Intent shortcutIntent;
		if (file.isDirectory())
		{
			shortcutIntent = new Intent(context, MainActivity.class);
			shortcutIntent.putExtra(MainActivity.EXTRA_DIR, file.getAbsolutePath());
		}
		else 
		{
			shortcutIntent = createFileOpenIntent(file);
		}
		
		Intent addIntent = new Intent();
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, file.getName());
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
				Intent.ShortcutIconResource.fromContext(context, FileUtils.getFileIconResource(file)));
		addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
		context.sendBroadcast(addIntent);
	}
	
	public static List<ResolveInfo> getAppsThatHandleFile(File file, Context context)
	{
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setType(FileUtils.getFileMimeType(file));
		intent.setData(Uri.fromFile(file));
		return getAppsThatHandleIntent(intent, context);
	}
	
	public static List<ResolveInfo> getAppsThatHandleIntent(Intent intent, Context context)
	{
		PackageManager packageManager = context.getPackageManager();
		return packageManager.queryIntentActivities(intent, 0);
	}
	
	public static Drawable getAppIconForFile(File file, Context context)
	{
		List<ResolveInfo> infos = getAppsThatHandleFile(file, context);
		PackageManager packageManager = context.getPackageManager();
		for (ResolveInfo info : infos)
		{
			Drawable drawable = info.loadIcon(packageManager);
			if (drawable != null)
				return drawable;
		}
		return null;
	}
}

@@ -1430,147 +1430,8 @@
 %0A%09%7B%0A
-%09%09Intent intent = new Intent(Intent.ACTION_VIEW);%0A%09%09intent.setType(FileUtils.getFileMimeType(file));%0A%09%09intent.setData(Uri.fromFile(file));%0A
 %09%09re
@@ -1459,22 +1459,42 @@
 eIntent(
-intent
+createFileOpenIntent(file)
 , contex

Use existing method to create file open intent