@@ -7,7 +7,7 @@ import java.util.Locale
7
7
import androidx.annotation.RestrictTo
8
8
9
9
@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 {
11
11
12
12
// Use a collator initialized to the default locale.
13
13
private val collator: Collator = Collator .getInstance(Locale .getDefault()).apply {
@@ -21,7 +21,8 @@ class CountryInfo(val locale: Locale, val countryCode: Int) : Comparable<Country
21
21
override fun newArray (size : Int ): Array <CountryInfo ?> = arrayOfNulls(size)
22
22
}
23
23
24
- fun localeToEmoji (locale : Locale ): String {
24
+ fun localeToEmoji (locale : Locale ? ): String {
25
+ if (locale == null ) return " "
25
26
val countryCode = locale.country
26
27
// 0x41 is Letter A, 0x1F1E6 is Regional Indicator Symbol Letter A.
27
28
// 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
33
34
34
35
// Secondary constructor to recreate from a Parcel.
35
36
constructor (parcel: Parcel ) : this (
36
- parcel.readSerializable() as Locale ,
37
+ parcel.readSerializable() as ? Locale ,
37
38
parcel.readInt()
38
39
)
39
40
@@ -44,13 +45,14 @@ class CountryInfo(val locale: Locale, val countryCode: Int) : Comparable<Country
44
45
}
45
46
46
47
override fun hashCode (): Int {
48
+ if (locale == null ) return 1
47
49
var result = locale.hashCode()
48
50
result = 31 * result + countryCode
49
51
return result
50
52
}
51
53
52
54
override fun toString (): String {
53
- return " ${localeToEmoji(locale)} ${locale.displayCountry} +$countryCode "
55
+ return " ${localeToEmoji(locale)} ${locale? .displayCountry ? : " " } +$countryCode "
54
56
}
55
57
56
58
fun toShortString (): String {
@@ -60,8 +62,8 @@ class CountryInfo(val locale: Locale, val countryCode: Int) : Comparable<Country
60
62
override fun compareTo (other : CountryInfo ): Int {
61
63
val defaultLocale = Locale .getDefault()
62
64
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) ? : " "
65
67
)
66
68
}
67
69
0 commit comments