diff --git a/README.md b/README.md index 5bfc36560535..e18da3d9265a 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,27 @@ # WordPress-Utils-Android -Part of the [WordPress-Android] project. +Collection of utility methods for Android and WordPress. -[WordPress-Android]: https://github.com/wordpress-mobile/WordPress-Android +## Use the library + +* In your build.gradle: +```groovy +dependencies { + // use the latest 1.x version + compile 'org.wordpress:utils:1.+' +} +``` + +## Publish it to bintray + +```shell +$ ./gradlew assemble publishToMavenLocal bintrayUpload -PbintrayUser=FIXME -PbintrayKey=FIXME -PdryRun=false +``` + +## Apps that use this library +- [WordPress for Android][1] + +## License +Dual licensed under MIT, and GPL. + +[1]: https://github.com/wordpress-mobile/WordPress-Android diff --git a/WordPressUtils/build.gradle b/WordPressUtils/build.gradle index 4882ea606ee2..88be314b733d 100644 --- a/WordPressUtils/build.gradle +++ b/WordPressUtils/build.gradle @@ -3,13 +3,13 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.1.0' + classpath 'com.android.tools.build:gradle:2.1.2' + classpath 'com.novoda:bintray-release:0.3.4' } } apply plugin: 'com.android.library' -apply plugin: 'maven' -apply plugin: 'signing' +apply plugin: 'com.novoda.bintray-release' repositories { jcenter() @@ -20,7 +20,7 @@ dependencies { exclude group: 'commons-logging' } compile 'com.mcxiaoke.volley:library:1.0.18' - compile 'com.android.support:support-v13:23.1.1' + compile 'com.android.support:support-v13:23.4.0' } android { @@ -32,68 +32,13 @@ android { buildToolsVersion "23.0.3" defaultConfig { - versionName "1.10.0" + versionName "1.12.0" minSdkVersion 14 targetSdkVersion 23 } } -version android.defaultConfig.versionName -group = "org.wordpress" -archivesBaseName = "utils" - -signing { - required { - has("release") && project.properties.containsKey("signing.keyId") && project.properties.containsKey("signing.secretKeyRingFile") - } - sign configurations.archives -} - -uploadArchives { - repositories { - mavenDeployer { - beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } - - repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { - authentication(userName: project.properties.ossrhUsername, password: project.properties.ossrhPassword) - } - - snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { - authentication(userName: project.properties.ossrhUsername, password: project.properties.ossrhPassword) - } - - pom.project { - name 'WordPress-Utils-Android' - packaging 'aar' - description 'Utils lib for WordPress-Android' - url 'https://github.com/wordpress-mobile/WordPress-Utils-Android' - scm { - connection 'scm:git:https://github.com/wordpress-mobile/WordPress-Utils-Android.git' - developerConnection 'scm:git:https://github.com/wordpress-mobile/WordPress-Utils-Android.git' - url 'https://github.com/wordpress-mobile/WordPress-Utils-Android' - } - - licenses { - license { - name 'The MIT License (MIT)' - url 'http://opensource.org/licenses/MIT' - } - } - - developers { - developer { - id 'maxme' - name 'Maxime Biais' - email 'maxime@automattic.com' - } - } - } - } - } -} - android.libraryVariants.all { variant -> - task("generate${variant.name}Javadoc", type: Javadoc) { description "Generates Javadoc for $variant.name." source = variant.javaCompile.source @@ -105,3 +50,16 @@ android.libraryVariants.all { variant -> exclude '**/R.java' } } + +publish { + artifactId = 'utils' + userOrg = 'wordpress-mobile' + groupId = 'org.wordpress' + uploadName = 'utils' + description = 'Utils library for Android' + publishVersion = android.defaultConfig.versionName + licences = ['MIT', 'GPL'] + website = 'https://github.com/wordpress-mobile/WordPress-Utils-Android/' + dryRun = 'false' + autoPublish = 'true' +} diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/AppLog.java b/WordPressUtils/src/main/java/org/wordpress/android/util/AppLog.java index 433809317525..be60e748e6c9 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/AppLog.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/AppLog.java @@ -16,7 +16,7 @@ public class AppLog { // T for Tag public enum T {READER, EDITOR, MEDIA, NUX, API, STATS, UTILS, NOTIFS, DB, POSTS, COMMENTS, THEMES, TESTS, PROFILING, - SIMPERIUM, SUGGESTION, MAIN, SETTINGS, PLANS} + SIMPERIUM, SUGGESTION, MAIN, SETTINGS, PLANS, PEOPLE} public static final String TAG = "WordPress"; public static final int HEADER_LINE_COUNT = 2; @@ -269,4 +269,4 @@ public static String toPlainText(Context context) { } return sb.toString(); } -} \ No newline at end of file +} diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/GeocoderUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/GeocoderUtils.java index e861a88b8a88..372473e15b1d 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/GeocoderUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/GeocoderUtils.java @@ -22,7 +22,7 @@ public static Geocoder getGeocoder(Context context) { Geocoder gcd; try { - gcd = new Geocoder(context, Locale.getDefault()); + gcd = new Geocoder(context, LanguageUtils.getCurrentDeviceLanguage(context)); } catch (NullPointerException cannotIstantiateEx) { AppLog.e(AppLog.T.UTILS, "Cannot instantiate Geocoder", cannotIstantiateEx); return null; diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/LanguageUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/LanguageUtils.java new file mode 100644 index 000000000000..515b044b331a --- /dev/null +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/LanguageUtils.java @@ -0,0 +1,52 @@ +package org.wordpress.android.util; + +import android.content.Context; + +import java.util.Locale; + +/** + * Methods for dealing with i18n messages + */ +public class LanguageUtils { + + public static Locale getCurrentDeviceLanguage(Context context) { + //better use getConfiguration as it has the latest locale configuration change. + //Otherwise Locale.getDefault().getLanguage() gets + //the config upon application launch. + Locale deviceLocale = context != null ? context.getResources().getConfiguration().locale : Locale.getDefault(); + return deviceLocale; + } + + public static String getCurrentDeviceLanguageCode(Context context) { + String deviceLanguageCode = getCurrentDeviceLanguage(context).toString(); + return deviceLanguageCode; + } + + public static String getPatchedCurrentDeviceLanguage(Context context) { + return patchDeviceLanguageCode(getCurrentDeviceLanguageCode(context)); + } + + /** + * Patches a deviceLanguageCode if any of deprecated values iw, id, or yi + */ + public static String patchDeviceLanguageCode(String deviceLanguageCode){ + String patchedCode = deviceLanguageCode; + /* +

Note that Java uses several deprecated two-letter codes. The Hebrew ("he") language + * code is rewritten as "iw", Indonesian ("id") as "in", and Yiddish ("yi") as "ji". This + * rewriting happens even if you construct your own {@code Locale} object, not just for + * instances returned by the various lookup methods. + */ + if (deviceLanguageCode != null) { + if (deviceLanguageCode.startsWith("iw")) + patchedCode = deviceLanguageCode.replace("iw", "he"); + else if (deviceLanguageCode.startsWith("in")) + patchedCode = deviceLanguageCode.replace("in", "id"); + else if (deviceLanguageCode.startsWith("ji")) + patchedCode = deviceLanguageCode.replace("ji", "yi"); + } + + return patchedCode; + } + +} diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/MapUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/MapUtils.java index 981e537d257a..c6e72dc5b7c2 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/MapUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/MapUtils.java @@ -47,12 +47,40 @@ public static long getMapLong(final Map map, final String key, long defaul } } + /* + * float version of above + */ + public static float getMapFloat(final Map map, final String key) { + return getMapFloat(map, key, 0); + } + public static float getMapFloat(final Map map, final String key, float defaultValue) { + try { + return Float.parseFloat(getMapStr(map, key)); + } catch (NumberFormatException e) { + return defaultValue; + } + } + + /* + * double version of above + */ + public static double getMapDouble(final Map map, final String key) { + return getMapDouble(map, key, 0); + } + public static double getMapDouble(final Map map, final String key, double defaultValue) { + try { + return Double.parseDouble(getMapStr(map, key)); + } catch (NumberFormatException e) { + return defaultValue; + } + } + /* * returns a date object from the passed key in the passed map * returns null if key doesn't exist or isn't a date */ public static Date getMapDate(final Map map, final String key) { - if (map==null || key==null || !map.containsKey(key)) + if (map == null || key == null || !map.containsKey(key)) return null; try { return (Date) map.get(key); diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java index c398ee079cb9..b15e8d824fa1 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/StringUtils.java @@ -225,10 +225,10 @@ public static String replaceUnicodeSurrogateBlocksWithHTMLEntities(final String * Used to convert a language code ([lc]_[rc] where lc is language code (en, fr, es, etc...) * and rc is region code (zh-CN, zh-HK, zh-TW, etc...) to a displayable string with the languages * name. - *

+ * * The input string must be between 2 and 6 characters, inclusive. An empty string is returned * if that is not the case. - *

+ * * If the input string is recognized by {@link Locale} the result of this method is the given * * @return non-null diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index dc40faa78c10..5e7158a61136 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Wed Jul 09 11:48:51 CEST 2014 +#Tue Sep 06 11:08:13 CEST 2016 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.11-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip