Do not toggle USB ID during MTP startup if not needed

In most cases MTP is enabled so it is better to start our init.rc
with MTP enabled and try not to toggle USB IDs during TWRP boot so
that we can keep adb running to make debugging easier.

Change-Id: Idf122c5ad4deeef7e1ed775d495989c502ddfb19
diff --git a/partitionmanager.cpp b/partitionmanager.cpp
index ea61d57..5d0fb01 100644
--- a/partitionmanager.cpp
+++ b/partitionmanager.cpp
@@ -1903,8 +1903,6 @@
 	}
 	//Launch MTP Responder
 	LOGINFO("Starting MTP\n");
-	char vendor[PROPERTY_VALUE_MAX];
-	char product[PROPERTY_VALUE_MAX];
 	int count = 0;
 
 	int mtppipe[2];
@@ -1914,14 +1912,20 @@
 		return false;
 	}
 
-	property_set("sys.usb.config", "none");
-	property_get("usb.vendor", vendor, "18D1");
-	property_get("usb.product.mtpadb", product, "4EE2");
-	string vendorstr = vendor;
-	string productstr = product;
-	TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
-	TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
-	property_set("sys.usb.config", "mtp,adb");
+	char old_value[PROPERTY_VALUE_MAX];
+	property_get("sys.usb.config", old_value, "error");
+	if (strcmp(old_value, "error") != 0 && strcmp(old_value, "mtp,adb") != 0) {
+		char vendor[PROPERTY_VALUE_MAX];
+		char product[PROPERTY_VALUE_MAX];
+		property_set("sys.usb.config", "none");
+		property_get("usb.vendor", vendor, "18D1");
+		property_get("usb.product.mtpadb", product, "4EE2");
+		string vendorstr = vendor;
+		string productstr = product;
+		TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
+		TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
+		property_set("sys.usb.config", "mtp,adb");
+	}
 	std::vector<TWPartition*>::iterator iter;
 	/* To enable MTP debug, use the twrp command line feature to
 	 * twrp set tw_mtp_debug 1
@@ -1960,17 +1964,21 @@
 }
 
 bool TWPartitionManager::Disable_MTP(void) {
+	char old_value[PROPERTY_VALUE_MAX];
+	property_get("sys.usb.config", old_value, "error");
+	if (strcmp(old_value, "adb") != 0) {
+		char vendor[PROPERTY_VALUE_MAX];
+		char product[PROPERTY_VALUE_MAX];
+		property_set("sys.usb.config", "none");
+		property_get("usb.vendor", vendor, "18D1");
+		property_get("usb.product.adb", product, "D002");
+		string vendorstr = vendor;
+		string productstr = product;
+		TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
+		TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
+		usleep(2000);
+	}
 #ifdef TW_HAS_MTP
-	char vendor[PROPERTY_VALUE_MAX];
-	char product[PROPERTY_VALUE_MAX];
-	property_set("sys.usb.config", "none");
-	property_get("usb.vendor", vendor, "18D1");
-	property_get("usb.product.adb", product, "D002");
-	string vendorstr = vendor;
-	string productstr = product;
-	TWFunc::write_file("/sys/class/android_usb/android0/idVendor", vendorstr);
-	TWFunc::write_file("/sys/class/android_usb/android0/idProduct", productstr);
-	usleep(2000);
 	if (mtppid) {
 		LOGINFO("Disabling MTP\n");
 		int status;
@@ -1981,14 +1989,13 @@
 		close(mtp_write_fd);
 		mtp_write_fd = -1;
 	}
+#endif
 	property_set("sys.usb.config", "adb");
+#ifdef TW_HAS_MTP
 	DataManager::SetValue("tw_mtp_enabled", 0);
 	return true;
-#else
-	LOGERR("MTP support not included\n");
-	DataManager::SetValue("tw_mtp_enabled", 0);
-	return false;
 #endif
+	return false;
 }
 
 TWPartition* TWPartitionManager::Find_Partition_By_MTP_Storage_ID(unsigned int Storage_ID) {