Attempt to automatically identify data/media devices

Change-Id: Ia3007fbced9ce3bc94efdb3dacc582a5e68a49a4
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index af89e40..0063ff3 100644
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -56,6 +56,8 @@
 	#endif
 #endif
 
+extern bool datamedia;
+
 TWPartitionManager::TWPartitionManager(void) {
 	mtpid = 100;
 	mtp_was_enabled = false;
@@ -100,6 +102,16 @@
 		}
 	}
 	fclose(fstabFile);
+	if (!datamedia && !settings_partition && Find_Partition_By_Path("/sdcard") == NULL && Find_Partition_By_Path("/internal_sd") == NULL && Find_Partition_By_Path("/internal_sdcard") == NULL && Find_Partition_By_Path("/emmc") == NULL) {
+		// Attempt to automatically identify /data/media emulated storage devices
+		TWPartition* Dat = Find_Partition_By_Path("/data");
+		if (Dat) {
+			LOGINFO("Using automatic handling for /data/media emulated storage device.\n");
+			datamedia = true;
+			Dat->Setup_Data_Media(++mtpid);
+			settings_partition = Dat;
+		}
+	}
 	if (!settings_partition) {
 		std::vector<TWPartition*>::iterator iter;
 		for (iter = Partitions.begin(); iter != Partitions.end(); iter++) {
@@ -168,10 +180,8 @@
 void TWPartitionManager::Setup_Android_Secure_Location(TWPartition* Part) {
 	if (Part->Has_Android_Secure)
 		Part->Setup_AndSec();
-#ifndef RECOVERY_SDCARD_ON_DATA
-	else
+	else if (!datamedia)
 		Part->Setup_AndSec();
-#endif
 }
 
 void TWPartitionManager::Output_Partition_Logging(void) {
@@ -270,9 +280,12 @@
 	if (!Part->MTD_Name.empty())
 		printf("   MTD_Name: %s\n", Part->MTD_Name.c_str());
 	string back_meth = Part->Backup_Method_By_Name();
-	printf("   Backup_Method: %s\n\n", back_meth.c_str());
+	printf("   Backup_Method: %s\n", back_meth.c_str());
 	if (Part->Mount_Flags || !Part->Mount_Options.empty())
 		printf("   Mount_Flags=0x%8x, Mount_Options=%s\n", Part->Mount_Flags, Part->Mount_Options.c_str());
+	if (Part->mtpid)
+		printf("   MTP Storage ID: %i\n", Part->mtpid);
+	printf("\n");
 }
 
 int TWPartitionManager::Mount_By_Path(string Path, bool Display_Error) {
@@ -1660,15 +1673,13 @@
 
 			// Sleep for a bit so that the device will be ready
 			sleep(1);
-#ifdef RECOVERY_SDCARD_ON_DATA
-			if (dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
+			if (dat->Has_Data_Media && dat->Mount(false) && TWFunc::Path_Exists("/data/media/0")) {
 				dat->Storage_Path = "/data/media/0";
 				dat->Symlink_Path = dat->Storage_Path;
 				DataManager::SetValue("tw_storage_path", "/data/media/0");
 				dat->UnMount(false);
 				Output_Partition(dat);
 			}
-#endif
 			Update_System_Details();
 			UnMount_Main_Partitions();
 		} else
@@ -1843,9 +1854,9 @@
 	TWPartition* Boot_Partition = Find_Partition_By_Path("/boot");
 
 	UnMount_By_Path("/system", true);
-#ifndef RECOVERY_SDCARD_ON_DATA
-	UnMount_By_Path("/data", true);
-#endif
+	if (!datamedia)
+		UnMount_By_Path("/data", true);
+
 	if (Boot_Partition != NULL && Boot_Partition->Can_Be_Mounted)
 		Boot_Partition->UnMount(true);
 }