Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolved issue where "NewApi" build error occurred in API version 21 … #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

implementation 'org.apache.commons:commons-lang3:3.6'
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.miguelruivo.flutter.plugin.countrycodes.country_codes

import android.R
import android.os.Build
import java.util.Locale
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin
Expand All @@ -9,6 +10,7 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.Registrar
import org.apache.commons.lang3.LocaleUtils

/** CountryCodesPlugin */
public class CountryCodesPlugin: FlutterPlugin, MethodCallHandler {
Expand Down Expand Up @@ -47,16 +49,75 @@ public class CountryCodesPlugin: FlutterPlugin, MethodCallHandler {
private fun getLocalizedCountryNames(localeTag: String?) : HashMap<String, String> {
var localizedCountries: HashMap<String,String> = HashMap()

val deviceCountry: String = Locale.getDefault().toLanguageTag();
val deviceCountry: String = Locale.getDefault().getLanguageTag();

for (countryCode in Locale.getISOCountries()) {
val locale = Locale(localeTag ?: deviceCountry,countryCode)
var countryName: String? = locale.getDisplayCountry(Locale.forLanguageTag(localeTag ?: deviceCountry))
var countryName: String? = locale.getDisplayCountry(initLocaleForTag(localeTag ?: deviceCountry))
localizedCountries[countryCode.toUpperCase()] = countryName ?: "";
}
return localizedCountries
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
}

private fun initLocaleForTag(languageTag: String): Locale {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return Locale.forLanguageTag(languageTag)
}

return LocaleUtils.toLocale(languageTag)
}
}

private fun Locale.getLanguageTag(): String {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return toLanguageTag()
}

// we will use a dash as per BCP 47
val SEP = '-'
var language: String = language
var region: String = country
var variant: String = variant

// special case for Norwegian Nynorsk since "NY" cannot be a variant as per BCP 47
// this goes before the string matching since "NY" wont pass the variant checks
if (language == "no" && region == "NO" && variant == "NY") {
language = "nn"
region = "NO"
variant = ""
}

if (language.isEmpty() || !language.matches(Regex("\\p{Alpha}{2,8}"))) {
language = "und" // Follow the Locale#toLanguageTag() implementation
// which says to return "und" for Undetermined
} else if (language.equals("iw")) {
language = "he" // correct deprecated "Hebrew"
} else if (language.equals("in")) {
language = "id" // correct deprecated "Indonesian"
} else if (language.equals("ji")) {
language = "yi" // correct deprecated "Yiddish"
}

// ensure valid country code, if not well formed, it's omitted
if (!region.matches(Regex("\\p{Alpha}{2}|\\p{Digit}{3}"))) {
region = ""
}

// variant subtags that begin with a letter must be at least 5 characters long
if (!variant.matches(Regex("\\p{Alnum}{5,8}|\\p{Digit}\\p{Alnum}{3}"))) {
variant = ""
}

val bcp47Tag = StringBuilder(language)
if (region.isNotEmpty()) {
bcp47Tag.append(SEP).append(region)
}
if (variant.isNotEmpty()) {
bcp47Tag.append(SEP).append(variant)
}

return bcp47Tag.toString()
}