@@ -7,7 +7,7 @@ import java.util.Locale
77import androidx.annotation.RestrictTo
88
99@RestrictTo(RestrictTo .Scope .LIBRARY_GROUP )
10- class CountryInfo (val locale : Locale , val countryCode : Int ) : Comparable<CountryInfo>, Parcelable {
10+ class CountryInfo (val locale : Locale ? , val countryCode : Int ) : Comparable<CountryInfo>, Parcelable {
1111
1212 // Use a collator initialized to the default locale.
1313 private val collator: Collator = Collator .getInstance(Locale .getDefault()).apply {
@@ -21,7 +21,8 @@ class CountryInfo(val locale: Locale, val countryCode: Int) : Comparable<Country
2121 override fun newArray (size : Int ): Array <CountryInfo ?> = arrayOfNulls(size)
2222 }
2323
24- fun localeToEmoji (locale : Locale ): String {
24+ fun localeToEmoji (locale : Locale ? ): String {
25+ if (locale == null ) return " "
2526 val countryCode = locale.country
2627 // 0x41 is Letter A, 0x1F1E6 is Regional Indicator Symbol Letter A.
2728 // For example, for "US": 'U' => (0x55 - 0x41) + 0x1F1E6, 'S' => (0x53 - 0x41) + 0x1F1E6.
@@ -33,7 +34,7 @@ class CountryInfo(val locale: Locale, val countryCode: Int) : Comparable<Country
3334
3435 // Secondary constructor to recreate from a Parcel.
3536 constructor (parcel: Parcel ) : this (
36- parcel.readSerializable() as Locale ,
37+ parcel.readSerializable() as ? Locale ,
3738 parcel.readInt()
3839 )
3940
@@ -44,13 +45,14 @@ class CountryInfo(val locale: Locale, val countryCode: Int) : Comparable<Country
4445 }
4546
4647 override fun hashCode (): Int {
48+ if (locale == null ) return 1
4749 var result = locale.hashCode()
4850 result = 31 * result + countryCode
4951 return result
5052 }
5153
5254 override fun toString (): String {
53- return " ${localeToEmoji(locale)} ${locale.displayCountry} +$countryCode "
55+ return " ${localeToEmoji(locale)} ${locale? .displayCountry ? : " " } +$countryCode "
5456 }
5557
5658 fun toShortString (): String {
@@ -60,8 +62,8 @@ class CountryInfo(val locale: Locale, val countryCode: Int) : Comparable<Country
6062 override fun compareTo (other : CountryInfo ): Int {
6163 val defaultLocale = Locale .getDefault()
6264 return collator.compare(
63- locale.displayCountry.uppercase(defaultLocale),
64- other.locale.displayCountry.uppercase(defaultLocale)
65+ locale? .displayCountry? .uppercase(defaultLocale) ? : " " ,
66+ other.locale? .displayCountry? .uppercase(defaultLocale) ? : " "
6567 )
6668 }
6769
0 commit comments