diff options
author | Tor Norbye <tnorbye@google.com> | 2012-11-07 11:00:01 -0800 |
---|---|---|
committer | Tor Norbye <tnorbye@google.com> | 2012-11-07 11:03:14 -0800 |
commit | 1f2fbfb23f307554728730eb427f3b23e61d8e31 (patch) | |
tree | 58a2f2c8fba371068ec0704241b47275e141fcb9 /lint/libs | |
parent | ad66ec63987fcab69b98bc1e6f816dd18fbbb0cb (diff) | |
download | sdk-1f2fbfb23f307554728730eb427f3b23e61d8e31.zip sdk-1f2fbfb23f307554728730eb427f3b23e61d8e31.tar.gz sdk-1f2fbfb23f307554728730eb427f3b23e61d8e31.tar.bz2 |
Make /sdcard detector also flag /data/data/ references
Change-Id: If08b99370a32eaa256e9cb63939428480d563bfe
Diffstat (limited to 'lint/libs')
3 files changed, 39 insertions, 9 deletions
diff --git a/lint/libs/lint_checks/src/com/android/tools/lint/checks/SdCardDetector.java b/lint/libs/lint_checks/src/com/android/tools/lint/checks/SdCardDetector.java index e7545a1..3a155e3 100644 --- a/lint/libs/lint_checks/src/com/android/tools/lint/checks/SdCardDetector.java +++ b/lint/libs/lint_checks/src/com/android/tools/lint/checks/SdCardDetector.java @@ -46,7 +46,11 @@ public class SdCardDetector extends Detector implements Detector.JavaScanner { "Looks for hardcoded references to /sdcard", "Your code should not reference the `/sdcard` path directly; instead use " + - "`Environment.getExternalStorageDirectory().getPath()`", + "`Environment.getExternalStorageDirectory().getPath()`.\n" + + "\n" + + "Similarly, do not reference the `/data/data/` path directly; it can vary " + + "in multi-user scenarios. Instead, use " + + "`Context.getFilesDir().getPath()`.", Category.CORRECTNESS, 6, @@ -91,16 +95,29 @@ public class SdCardDetector extends Detector implements Detector.JavaScanner { @Override public boolean visitStringLiteral(StringLiteral node) { String s = node.astValue(); - // Other potential String prefixes to check for: - // /mnt/sdcard/ - // /system/media/sdcard - // file://sdcard - // file:///sdcard - if (s.startsWith("/sdcard")) { //$NON-NLS-1$ + if (s.isEmpty()) { + return false; + } + char c = s.charAt(0); + if (c != '/' && c != 'f') { + return false; + } + + if (s.startsWith("/sdcard") //$NON-NLS-1$ + || s.startsWith("/mnt/sdcard/") //$NON-NLS-1$ + || s.startsWith("/system/media/sdcard") //$NON-NLS-1$ + || s.startsWith("file://sdcard/") //$NON-NLS-1$ + || s.startsWith("file:///sdcard/")) { //$NON-NLS-1$ String message = "Do not hardcode \"/sdcard/\"; " + "use Environment.getExternalStorageDirectory().getPath() instead"; Location location = mContext.getLocation(node); mContext.report(ISSUE, node, location, message, s); + } else if (s.startsWith("/data/data/") //$NON-NLS-1$ + || s.startsWith("/data/user/")) { //$NON-NLS-1$ + String message = "Do not hardcode \"/data/\"; " + + "use Context.getFilesDir().getPath() instead"; + Location location = mContext.getLocation(node); + mContext.report(ISSUE, node, location, message, s); } return false; diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/SdCardDetectorTest.java b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/SdCardDetectorTest.java index 1acbb87..53272c8 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/SdCardDetectorTest.java +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/SdCardDetectorTest.java @@ -48,14 +48,24 @@ public class SdCardDetectorTest extends AbstractCheckTest { "src/test/pkg/SdCardTest.java:24: Warning: Do not hardcode \"/sdcard/\"; use Environment.getExternalStorageDirectory().getPath() instead [SdCardPath]\n" + " String FilePath = \"/sdcard/\" + new File(\"test\");\n" + " ~~~~~~~~~~\n" + + "src/test/pkg/SdCardTest.java:29: Warning: Do not hardcode \"/sdcard/\"; use Environment.getExternalStorageDirectory().getPath() instead [SdCardPath]\n" + + " intent.setDataAndType(Uri.parse(\"file://sdcard/foo.json\"), \"application/bar-json\");\n" + + " ~~~~~~~~~~~~~~~~~~~~~~~~\n" + "src/test/pkg/SdCardTest.java:30: Warning: Do not hardcode \"/sdcard/\"; use Environment.getExternalStorageDirectory().getPath() instead [SdCardPath]\n" + " intent.putExtra(\"path-filter\", \"/sdcard(/.+)*\");\n" + " ~~~~~~~~~~~~~~~\n" + "src/test/pkg/SdCardTest.java:31: Warning: Do not hardcode \"/sdcard/\"; use Environment.getExternalStorageDirectory().getPath() instead [SdCardPath]\n" + " intent.putExtra(\"start-dir\", \"/sdcard\");\n" + " ~~~~~~~~~\n" + - "0 errors, 9 warnings\n" + - "", + "src/test/pkg/SdCardTest.java:32: Warning: Do not hardcode \"/data/\"; use Context.getFilesDir().getPath() instead [SdCardPath]\n" + + " String mypath = \"/data/data/foo\";\n" + + " ~~~~~~~~~~~~~~~~\n" + + "src/test/pkg/SdCardTest.java:33: Warning: Do not hardcode \"/data/\"; use Context.getFilesDir().getPath() instead [SdCardPath]\n" + + " String base = \"/data/data/foo.bar/test-profiling\";\n" + + " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + + "src/test/pkg/SdCardTest.java:34: Warning: Do not hardcode \"/sdcard/\"; use Environment.getExternalStorageDirectory().getPath() instead [SdCardPath]\n" + + " String s = \"file://sdcard/foo\";\n" + + " ~~~~~~~~~~~~~~~~~~~\n", lintProject("src/test/pkg/SdCardTest.java.txt=>src/test/pkg/SdCardTest.java")); } diff --git a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/SdCardTest.java.txt b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/SdCardTest.java.txt index e2719e9..2087857 100644 --- a/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/SdCardTest.java.txt +++ b/lint/libs/lint_checks/tests/src/com/android/tools/lint/checks/data/src/test/pkg/SdCardTest.java.txt @@ -29,5 +29,8 @@ public class SdCardTest { intent.setDataAndType(Uri.parse("file://sdcard/foo.json"), "application/bar-json"); intent.putExtra("path-filter", "/sdcard(/.+)*"); intent.putExtra("start-dir", "/sdcard"); + String mypath = "/data/data/foo"; + String base = "/data/data/foo.bar/test-profiling"; + String s = "file://sdcard/foo"; } } |