Merge "Allow factory reset when bluetooth is off (2/2)"
diff --git a/btif/src/btif_config.c b/btif/src/btif_config.c
index 0aa310d..82c6069 100644
--- a/btif/src/btif_config.c
+++ b/btif/src/btif_config.c
@@ -40,6 +40,15 @@
 #include "osi/include/log.h"
 #include "osi/include/osi.h"
 
+/**
+ * TODO(apanicke): cutils/properties.h is only being used to pull-in runtime
+ * settings on Android. Remove this conditional include once we have a generic
+ * way to obtain system properties.
+ */
+#if !defined(OS_GENERIC)
+#include <cutils/properties.h>
+#endif  /* !defined(OS_GENERIC) */
+
 // TODO(armansito): Find a better way than searching by a hardcoded path.
 #if defined(OS_GENERIC)
 static const char *CONFIG_FILE_PATH = "bt_config.conf";
@@ -53,6 +62,8 @@
 static void timer_config_save_cb(void *data);
 static void btif_config_write(UINT16 event, char *p_param);
 static void btif_config_devcache_cleanup(void);
+static bool is_factory_reset(void);
+static void delete_config_files(void);
 
 static enum ConfigSource {
   NOT_LOADED,
@@ -109,6 +120,10 @@
 
 static future_t *init(void) {
   pthread_mutex_init(&lock, NULL);
+
+  if (is_factory_reset())
+    delete_config_files();
+
   config = config_new(CONFIG_FILE_PATH);
   btif_config_source = ORIGINAL;
   if (!config) {
@@ -381,7 +396,7 @@
   pthread_mutex_unlock(&lock);
 }
 
-bool btif_config_clear(void){
+bool btif_config_clear(void) {
   assert(config != NULL);
   assert(config_timer != NULL);
 
@@ -482,3 +497,15 @@
 
     pthread_mutex_unlock(&lock);
 }
+
+static bool is_factory_reset(void) {
+  char factory_reset[PROPERTY_VALUE_MAX] = {0};
+  property_get("persist.bluetooth.factoryreset", factory_reset, "false");
+  return strncmp(factory_reset, "true", 4) == 0;
+}
+
+static void delete_config_files(void) {
+  remove(CONFIG_FILE_PATH);
+  remove(CONFIG_BACKUP_PATH);
+  property_set("persist.bluetooth.factoryreset", "false");
+}