From 37402446cd74da1a346469c03e09900d99673b8a Mon Sep 17 00:00:00 2001 From: Keith Dart Date: Mon, 2 Oct 2017 19:49:41 -0700 Subject: Make linter happy. (#81) * Add `presubmit` action to run formatter and lint. --- README.md | 16 ++++++ build.gradle | 34 +++++++++++-- src/main/AndroidManifest.xml | 4 +- .../mobly/snippet/bundled/AccountSnippet.java | 2 + .../android/mobly/snippet/bundled/FileSnippet.java | 13 ++--- .../mobly/snippet/bundled/NetworkingSnippet.java | 57 ++++++++++++++-------- .../mobly/snippet/bundled/WifiManagerSnippet.java | 8 +-- .../android/mobly/snippet/bundled/utils/Utils.java | 12 ++--- 8 files changed, 103 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 0dfcfbb..0c8825f 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,22 @@ Note: this is not an official Google product. self.ad.api.wifiEnable() ``` +## Develop + +If you want to contribute, use the usual github method of forking and sending +a pull request. + +Before sending a pull request, run the `presubmit` target to format and run +lint over the code. Fix any issues it indicates. When complete, send the pull +request. + +```shell +./gradlew presubmit +``` + +This target will reformat the code with +[googleJavaFormat](https://github.com/sherter/google-java-format-gradle-plugin) +and run lint. The lint report should open in your default browser. ## Other resources diff --git a/build.gradle b/build.gradle index 5283f0e..114e5ba 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:2.3.3' - classpath 'me.tatarka:gradle-retrolambda:3.5.0' + classpath 'me.tatarka:gradle-retrolambda:3.7.0' // NOTE: Do not place your application dependencies here. } @@ -31,7 +31,7 @@ allprojects { android { compileSdkVersion 24 - buildToolsVersion '25.0.0' + buildToolsVersion '25.0.3' defaultConfig { applicationId "com.google.android.mobly.snippet.bundled" @@ -50,6 +50,7 @@ android { abortOnError true checkAllWarnings true warningsAsErrors true + disable 'HardwareIds','MissingApplicationIcon','GoogleAppIndexingWarning','InvalidPackage','OldTargetApi' } } @@ -72,13 +73,36 @@ artifacts { dependencies { compile 'com.android.support.test:runner:0.5' compile 'com.google.android.mobly:mobly-snippet-lib:1.2.0' - compile 'com.google.code.gson:gson:2.6.2' - compile 'com.google.guava:guava:20.0' + compile 'com.google.code.gson:gson:2.8.2' + compile 'com.google.guava:guava:22.0-android' - testCompile 'com.google.truth:truth:0.32' + testCompile 'com.google.truth:truth:0.36' testCompile 'junit:junit:4.12' } googleJavaFormat { options style: 'AOSP' } + +// Open lint's HTML report in your default browser or viewer. +task openLintReport(type: Exec) { + def lint_report = "build/reports/lint-results.html" + def cmd = "cat" + def platform = System.getProperty('os.name').toLowerCase(Locale.ROOT) + if (platform.contains("linux")) { + cmd = "xdg-open" + } else if (platform.contains("darwin")) { + cmd = "open" + } else if (platform.contains("windows")) { + cmd = "launch" + } + commandLine cmd, lint_report +} + +task presubmit { + dependsOn { ['googleJavaFormat', 'lint', 'openLintReport'] } + doLast { + println "Fix any lint issues you see. When it looks good, submit the pull request." + } +} + diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index dc5d0af..0a0dad3 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -3,6 +3,8 @@ xmlns:android="http://schemas.android.com/apk/res/android" package="com.google.android.mobly.snippet.bundled"> + + @@ -25,7 +27,7 @@ - + pathsegments = uri.getPathSegments(); if (pathsegments.size() < 1) { - throw new IllegalArgumentException(String.format("The Uri %s does not have a path.", uri.toString())); + throw new IllegalArgumentException( + String.format(Locale.US, "The Uri %s does not have a path.", uri.toString())); } DownloadManager.Request request = new DownloadManager.Request(uri); - request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, - pathsegments.get(pathsegments.size() - 1)); + request.setDestinationInExternalPublicDir( + Environment.DIRECTORY_DOWNLOADS, pathsegments.get(pathsegments.size() - 1)); mIsDownloadComplete = false; mReqid = 0; IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); @@ -96,9 +101,16 @@ public class NetworkingSnippet implements Snippet { mContext.registerReceiver(receiver, filter); try { mReqid = mDownloadManager.enqueue(request); - Log.d(String.format("networkHttpDownload download of %s with id %d", url, mReqid)); + Log.d( + String.format( + Locale.US, + "networkHttpDownload download of %s with id %d", + url, + mReqid)); if (!Utils.waitUntil(() -> mIsDownloadComplete, 30)) { - Log.d(String.format("networkHttpDownload timed out waiting for completion")); + Log.d( + String.format( + Locale.US, "networkHttpDownload timed out waiting for completion")); throw new NetworkingSnippetException("networkHttpDownload timed out."); } } finally { @@ -106,11 +118,15 @@ public class NetworkingSnippet implements Snippet { } Uri resp = mDownloadManager.getUriForDownloadedFile(mReqid); if (resp != null) { - Log.d(String.format("networkHttpDownload completed to %s", resp.toString())); + Log.d(String.format(Locale.US, "networkHttpDownload completed to %s", resp.toString())); mReqid = 0; return resp.toString(); } else { - Log.d(String.format("networkHttpDownload Failed to download %s", uri.toString())); + Log.d( + String.format( + Locale.US, + "networkHttpDownload Failed to download %s", + uri.toString())); throw new NetworkingSnippetException("networkHttpDownload didn't get downloaded file."); } } @@ -121,8 +137,7 @@ public class NetworkingSnippet implements Snippet { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); long gotid = (long) intent.getExtras().get("extra_download_id"); - if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action) - && gotid == mReqid) { + if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action) && gotid == mReqid) { mIsDownloadComplete = true; } } diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java b/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java index 6614904..83a42f6 100644 --- a/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java +++ b/src/main/java/com/google/android/mobly/snippet/bundled/WifiManagerSnippet.java @@ -57,7 +57,9 @@ public class WifiManagerSnippet implements Snippet { public WifiManagerSnippet() { mContext = InstrumentationRegistry.getContext(); - mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); + mWifiManager = + (WifiManager) + mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE); } @Rpc( @@ -196,8 +198,8 @@ public class WifiManagerSnippet implements Snippet { String SSID = wifiConfig.SSID; // Return directly if network is already connected. WifiInfo connectionInfo = mWifiManager.getConnectionInfo(); - if (connectionInfo.getNetworkId() != -1 && connectionInfo.getSSID().equals(wifiConfig.SSID)) - { + if (connectionInfo.getNetworkId() != -1 + && connectionInfo.getSSID().equals(wifiConfig.SSID)) { Log.d("Network " + connectionInfo.getSSID() + " is already connected."); return; } diff --git a/src/main/java/com/google/android/mobly/snippet/bundled/utils/Utils.java b/src/main/java/com/google/android/mobly/snippet/bundled/utils/Utils.java index e4c0dbd..376bcb5 100644 --- a/src/main/java/com/google/android/mobly/snippet/bundled/utils/Utils.java +++ b/src/main/java/com/google/android/mobly/snippet/bundled/utils/Utils.java @@ -30,7 +30,7 @@ import java.util.concurrent.TimeoutException; public final class Utils { - private final static char[] hexArray = "0123456789abcdef".toCharArray(); + private static final char[] hexArray = "0123456789abcdef".toCharArray(); private Utils() {} @@ -194,22 +194,20 @@ public final class Utils { } /** - * Convert a byte array (binary data) to a hexadecimal string (ASCII) - * representation. - - * [\x01\x02] -> "0102" + * Convert a byte array (binary data) to a hexadecimal string (ASCII) representation. + * + *

[\x01\x02] -> "0102" * * @param bytes The array of byte to convert. * @return a String with the ASCII hex representation. */ public static String bytesToHexString(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; - for ( int j = 0; j < bytes.length; j++ ) { + for (int j = 0; j < bytes.length; j++) { int v = bytes[j] & 0xFF; hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2 + 1] = hexArray[v & 0x0F]; } return new String(hexChars); } - } -- cgit v1.2.3