package com.navercorp.utilset.storage;

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

import android.os.Environment;
import android.os.StatFs;

class MicroSdDetector {
	private static final int GIGABYTE = 1024 * 1024 * 1024;
//	private static final int JELLY_BEAN_MR2 = 18;

	public static String getMicroSDCardDirectory() {
		List<String> mMounts = readMountsFile();
		List<String> mVold = readVoldFile();

		for (int i = 0; i < mMounts.size(); i++) {
			String mount = mMounts.get(i);

			if (!mVold.contains(mount)) {
				mMounts.remove(i--);
				continue;
			}

			File root = new File(mount);
			if (!root.exists() || !root.isDirectory()) {
				mMounts.remove(i--);
				continue;
			}

			if (!isAvailableFileSystem(mount)) {
				mMounts.remove(i--);
				continue;
			}

			if (!checkMicroSDCard(mount)) {
				mMounts.remove(i--);
			}
		}

		if (mMounts.size() == 1) {
			return mMounts.get(0);
		}

		return null;
	}

	private static List<String> readMountsFile() {
		/**
		 * Scan the /proc/mounts file and look for lines like this:
		 * /dev/block/vold/179:1 /mnt/sdcard vfat
		 * rw,dirsync,nosuid,nodev,noexec,
		 * relatime,uid=1000,gid=1015,fmask=0602,dmask
		 * =0602,allow_utime=0020,codepage
		 * =cp437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
		 * 
		 * When one is found, split it into its elements and then pull out the
		 * path to the that mount point and add it to the arraylist
		 */
		List<String> mMounts = new ArrayList<String>();

		try {
			Scanner scanner = new Scanner(new File("/proc/mounts"));

			while (scanner.hasNext()) {
				String line = scanner.nextLine();

				if (line.startsWith("/dev/block/vold/")) {
					String[] lineElements = line.split("[ \t]+");
					String element = lineElements[1];

					mMounts.add(element);
				}
			}
		} catch (Exception e) {
			// Auto-generated catch block
			e.printStackTrace();
		}

		return mMounts;
	}

	private static List<String> readVoldFile() {
		/**
		 * Scan the /system/etc/vold.fstab file and look for lines like this:
		 * dev_mount sdcard /mnt/sdcard 1
		 * /devices/platform/s3c-sdhci.0/mmc_host/mmc0
		 * 
		 * When one is found, split it into its elements and then pull out the
		 * path to the that mount point and add it to the arraylist
		 */

		List<String> mVold = new ArrayList<String>();

		try {
			Scanner scanner = new Scanner(new File("/system/etc/vold.fstab"));

			while (scanner.hasNext()) {
				String line = scanner.nextLine();

				if (line.startsWith("dev_mount")) {
					String[] lineElements = line.split("[ \t]+");
					String element = lineElements[2];

					if (element.contains(":")) {
						element = element.substring(0, element.indexOf(":"));
					}

					mVold.add(element);
				}
			}
		} catch (Exception e) {
			// Auto-generated catch block
			e.printStackTrace();
		}

		return mVold;
	}

	@SuppressWarnings("deprecation")
	private static boolean checkMicroSDCard(String fileSystemName) {
		StatFs statFs = new StatFs(fileSystemName);

//		long totalSize = 0; 
//		
//		if (Build.VERSION.SDK_INT >= JELLY_BEAN_MR2) {
//			totalSize = (long) statFs.getBlockSizeLong() * statFs.getBlockCountLong();
//		}
//		else {
//			totalSize = (long) statFs.getBlockSize() * statFs.getBlockCount();
//		}
		
		long totalSize = (long) statFs.getBlockSize() * statFs.getBlockCount();

		if (totalSize < GIGABYTE) {
			return false;
		}

		return true;
	}

	private static boolean isAvailableFileSystem(String fileSystemName) {
		final String[] unAvailableFileSystemList = { "/dev", "/mnt/asec",
				"/mnt/obb", "/system", "/data", "/cache", "/efs", "/firmware" };

		for (String name : unAvailableFileSystemList) {
			if (fileSystemName.contains(name) == true) {
				return false;
			}
		}

		if (Environment.getExternalStorageDirectory().getAbsolutePath()
				.equals(fileSystemName) == true) {
			return false;
		}

		return true;
	}
}

@@ -3035,267 +3035,8 @@
 );%0A%0A
-//%09%09long totalSize = 0; %0A//%09%09%0A//%09%09if (Build.VERSION.SDK_INT %3E= JELLY_BEAN_MR2) %7B%0A//%09%09%09totalSize = (long) statFs.getBlockSizeLong() * statFs.getBlockCountLong();%0A//%09%09%7D%0A//%09%09else %7B%0A//%09%09%09totalSize = (long) statFs.getBlockSize() * statFs.getBlockCount();%0A//%09%09%7D%0A%09%09%0A
 %09%09lo

Remove commented lines in MicroSdDetector.checkMciroSDCard