Use $EXTERNAL_STORAGE instead of /sdcard.

The multi-user changes affect apps launched from the shell (like gtest), so
we need to grab the appropriate location dynamically, rather than continuing
to hard-code it.

Bug: 7121917

Change-Id: I51a58379eff713e63d34a507dc318a138faccb53
diff --git a/src/gtest-port.cc b/src/gtest-port.cc
index f4a8def..01c5e1e 100644
--- a/src/gtest-port.cc
+++ b/src/gtest-port.cc
@@ -507,9 +507,20 @@
     filename_ = temp_file_path;
 // ANDROID
 #elif GTEST_OS_LINUX_ANDROID
-    char name_template[] = "/sdcard/captured_stderr.XXXXXX";
+    // Get $EXTERNAL_STORAGE from the environment, since this can change
+    // for shell users (fallback is still /sdcard, but this probably will
+    // never work properly again).
+    ::std::string external_storage = "/sdcard";
+    char *sdcard_path = getenv("EXTERNAL_STORAGE");
+    if (sdcard_path) {
+      external_storage = sdcard_path;
+    }
+    external_storage += "/captured_stderr.XXXXXX";
+    char *name_template = strdup(external_storage.c_str());
     const int captured_fd = mkstemp(name_template);
     filename_ = name_template;
+    free(name_template);
+    name_template = NULL;
 // END ANDROID
 # else
     // There's no guarantee that a test has write access to the