diff options
author | Tor Norbye <tnorbye@google.com> | 2012-11-07 11:27:49 -0800 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2012-11-07 11:27:50 -0800 |
commit | 088f24a417cf97799a5873016ad939f1a3096161 (patch) | |
tree | d01c3b28c299512add273d219b84163aead4d07a /lint/libs | |
parent | 1186e06da584dede79c9e72ba98213946fbe5d78 (diff) | |
parent | 1f2fbfb23f307554728730eb427f3b23e61d8e31 (diff) | |
download | sdk-088f24a417cf97799a5873016ad939f1a3096161.zip sdk-088f24a417cf97799a5873016ad939f1a3096161.tar.gz sdk-088f24a417cf97799a5873016ad939f1a3096161.tar.bz2 |
Merge "Make /sdcard detector also flag /data/data/ references"
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"; } } |