1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package android.location; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 import java.util.Locale; 23 24 /** 25 * This class wraps the country information. 26 * 27 * @hide 28 */ 29 public class Country implements Parcelable { 30 /** 31 * The country code came from the mobile network 32 */ 33 public static final int COUNTRY_SOURCE_NETWORK = 0; 34 35 /** 36 * The country code came from the location service 37 */ 38 public static final int COUNTRY_SOURCE_LOCATION = 1; 39 40 /** 41 * The country code was read from the SIM card 42 */ 43 public static final int COUNTRY_SOURCE_SIM = 2; 44 45 /** 46 * The country code came from the system locale setting 47 */ 48 public static final int COUNTRY_SOURCE_LOCALE = 3; 49 50 /** 51 * The ISO 3166-1 two letters country code. 52 */ 53 private final String mCountryIso; 54 55 /** 56 * Where the country code came from. 57 */ 58 private final int mSource; 59 60 private int mHashCode; 61 /** 62 * 63 * @param countryIso the ISO 3166-1 two letters country code. 64 * @param source where the countryIso came from, could be one of below 65 * values 66 * <p> 67 * <ul> 68 * <li>{@link #COUNTRY_SOURCE_NETWORK}</li> 69 * <li>{@link #COUNTRY_SOURCE_LOCATION}</li> 70 * <li>{@link #COUNTRY_SOURCE_SIM}</li> 71 * <li>{@link #COUNTRY_SOURCE_LOCALE}</li> 72 * </ul> 73 */ 74 public Country(final String countryIso, final int source) { 75 if (countryIso == null || source < COUNTRY_SOURCE_NETWORK 76 || source > COUNTRY_SOURCE_LOCALE) { 77 throw new IllegalArgumentException(); 78 } 79 mCountryIso = countryIso.toUpperCase(Locale.US); 80 mSource = source; 81 } 82 83 public Country(Country country) { 84 mCountryIso = country.mCountryIso; 85 mSource = country.mSource; 86 } 87 88 /** 89 * @return the ISO 3166-1 two letters country code 90 */ 91 public final String getCountryIso() { 92 return mCountryIso; 93 } 94 95 /** 96 * @return where the country code came from, could be one of below values 97 * <p> 98 * <ul> 99 * <li>{@link #COUNTRY_SOURCE_NETWORK}</li> 100 * <li>{@link #COUNTRY_SOURCE_LOCATION}</li> 101 * <li>{@link #COUNTRY_SOURCE_SIM}</li> 102 * <li>{@link #COUNTRY_SOURCE_LOCALE}</li> 103 * </ul> 104 */ 105 public final int getSource() { 106 return mSource; 107 } 108 109 public static final Parcelable.Creator<Country> CREATOR = new Parcelable.Creator<Country>() { 110 public Country createFromParcel(Parcel in) { 111 return new Country(in.readString(), in.readInt()); 112 } 113 114 public Country[] newArray(int size) { 115 return new Country[size]; 116 } 117 }; 118 119 public int describeContents() { 120 return 0; 121 } 122 123 public void writeToParcel(Parcel parcel, int flags) { 124 parcel.writeString(mCountryIso); 125 parcel.writeInt(mSource); 126 } 127 128 @Override 129 public boolean equals(Object object) { 130 if (object == this) { 131 return true; 132 } 133 if (object instanceof Country) { 134 Country c = (Country) object; 135 return mCountryIso.equals(c.getCountryIso()) && mSource == c.getSource(); 136 } 137 return false; 138 } 139 140 @Override 141 public int hashCode() { 142 int hash = mHashCode; 143 if (hash == 0) { 144 hash = 17; 145 hash = hash * 13 + mCountryIso.hashCode(); 146 hash = hash * 13 + mSource; 147 mHashCode = hash; 148 } 149 return mHashCode; 150 } 151 152 /** 153 * Compare the specified country to this country object ignoring the mSource 154 * field, return true if the countryIso fields are equal 155 * 156 * @param country the country to compare 157 * @return true if the specified country's countryIso field is equal to this 158 * country's, false otherwise. 159 */ 160 public boolean equalsIgnoreSource(Country country) { 161 return country != null && mCountryIso.equals(country.getCountryIso()); 162 } 163 } 164