Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix for issue #16: Test runner uses APIs only available in newer Andr…

…oid versions. Added reflection/fallback around getExternalFilesDir. Also tweaked the example to work on the emulator (the hardcoded path assumes the mount point of external storage).
  • Loading branch information...
commit 75646b47a7f98615e918b56fa17ba86ab267f683 1 parent d550792
Jason Sankey authored
4 example/README
View
@@ -40,7 +40,9 @@ can also try a target with custom arguments:
which fetches a report to the same directory, but with the test suite
name included. This latter example requires the device to have external
-storage mounted.
+storage mounted. It also assumes that such storage is mounted at
+/sdcard, which is not always true (you can simply edit the path in the
+custom rules file to match your device if necessary).
More Information
----------------
2  example/tests/custom_rules.xml
View
@@ -49,7 +49,7 @@
<exec executable="${adb}" failonerror="true">
<arg line="${adb.device.arg}"/>
<arg value="pull"/>
- <arg value="/mnt/sdcard/Android/data/${tested.project.app.package}/files/my-reports/junit-report-com.zutubi.android.junitreport.example.AJRExampleActivityTest.xml"/>
+ <arg value="/sdcard/Android/data/${tested.project.app.package}/files/my-reports/junit-report-com.zutubi.android.junitreport.example.AJRExampleActivityTest.xml"/>
<arg value="${reports.dir}" />
</exec>
</target>
61 src/com/zutubi/android/junitreport/Compatibility.java
View
@@ -0,0 +1,61 @@
+package com.zutubi.android.junitreport;
+
+import java.io.File;
+import java.lang.reflect.Method;
+
+import android.content.Context;
+import android.os.Environment;
+import android.util.Log;
+
+/**
+ * Utilities for backwards-compatibility with early Android versions.
+ */
+public final class Compatibility {
+ private static final String LOG_TAG = Compatibility.class.getSimpleName();
+
+ private static final Method METHOD_GET_EXTERNAL_FILES_DIR;
+ static {
+ Method method = null;
+ try {
+ method = Context.class.getMethod("getExternalFilesDir", String.class);
+ } catch (Exception e) {
+ // Expected for API 7 and below. Fall back will be engaged.
+ }
+ METHOD_GET_EXTERNAL_FILES_DIR = method;
+ }
+
+ /**
+ * Do not instantiate.
+ */
+ private Compatibility() {
+ }
+
+ /**
+ * A backwards-compatible version of {@link Context#getExternalFilesDir(String)}
+ * which falls back to using {@link Environment#getExternalStorageDirectory()}
+ * on API 7 and below.
+ *
+ * @param context context to get the external files directory for
+ * @param type the type of files directory to return (may be null)
+ * @return the path of the directory holding application files on external
+ * storage, or null if external storage cannot be accessed
+ */
+ public static File getExternalFilesDir(final Context context, final String type) {
+ if (METHOD_GET_EXTERNAL_FILES_DIR == null) {
+ final File externalRoot = Environment.getExternalStorageDirectory();
+ if (externalRoot == null) {
+ return null;
+ }
+
+ final String packageName = context.getApplicationContext().getPackageName();
+ return new File(externalRoot, "Android/data/" + packageName + "/files");
+ } else {
+ try {
+ return (File) METHOD_GET_EXTERNAL_FILES_DIR.invoke(context, type);
+ } catch (Exception e) {
+ Log.e(LOG_TAG, "Could not invoke getExternalFilesDir: " + e.getMessage(), e);
+ return null;
+ }
+ }
+ }
+}
2  src/com/zutubi/android/junitreport/JUnitReportListener.java
View
@@ -204,7 +204,7 @@ private FileOutputStream openOutputStream(String fileName) throws IOException {
return mTargetContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
} else {
if (mReportDir.contains(TOKEN_EXTERNAL)) {
- File externalDir = mTargetContext.getExternalFilesDir(null);
+ File externalDir = Compatibility.getExternalFilesDir(mTargetContext, null);
if (externalDir == null) {
Log.e(LOG_TAG, "reportDir references external storage, but external storage is not available (check mounting and permissions)");
throw new IOException("Cannot access external storage");
Please sign in to comment.
Something went wrong with that request. Please try again.