Skip to content

Commit 043c5f8

Browse files
Justin HoAndroid (Google) Code Review
authored andcommitted
Merge "Cherry-picking Id45abeba and Ia065dec6 for MR1" into ics-mr1
2 parents 6469ee8 + d73b79b commit 043c5f8

File tree

4 files changed

+204
-42
lines changed

4 files changed

+204
-42
lines changed

location/java/android/location/Country.java

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import android.os.Parcel;
2020
import android.os.Parcelable;
21+
import android.os.SystemClock;
2122

2223
import java.util.Locale;
2324

@@ -58,8 +59,14 @@ public class Country implements Parcelable {
5859
private final int mSource;
5960

6061
private int mHashCode;
62+
63+
/**
64+
* Time that this object was created (which we assume to be the time that the source was
65+
* consulted). This time is in milliseconds since boot up.
66+
*/
67+
private final long mTimestamp;
68+
6169
/**
62-
*
6370
* @param countryIso the ISO 3166-1 two letters country code.
6471
* @param source where the countryIso came from, could be one of below
6572
* values
@@ -78,11 +85,23 @@ public Country(final String countryIso, final int source) {
7885
}
7986
mCountryIso = countryIso.toUpperCase(Locale.US);
8087
mSource = source;
88+
mTimestamp = SystemClock.elapsedRealtime();
89+
}
90+
91+
private Country(final String countryIso, final int source, long timestamp) {
92+
if (countryIso == null || source < COUNTRY_SOURCE_NETWORK
93+
|| source > COUNTRY_SOURCE_LOCALE) {
94+
throw new IllegalArgumentException();
95+
}
96+
mCountryIso = countryIso.toUpperCase(Locale.US);
97+
mSource = source;
98+
mTimestamp = timestamp;
8199
}
82100

83101
public Country(Country country) {
84102
mCountryIso = country.mCountryIso;
85103
mSource = country.mSource;
104+
mTimestamp = country.mTimestamp;
86105
}
87106

88107
/**
@@ -106,9 +125,17 @@ public final int getSource() {
106125
return mSource;
107126
}
108127

128+
/**
129+
* Returns the time that this object was created (which we assume to be the time that the source
130+
* was consulted).
131+
*/
132+
public final long getTimestamp() {
133+
return mTimestamp;
134+
}
135+
109136
public static final Parcelable.Creator<Country> CREATOR = new Parcelable.Creator<Country>() {
110137
public Country createFromParcel(Parcel in) {
111-
return new Country(in.readString(), in.readInt());
138+
return new Country(in.readString(), in.readInt(), in.readLong());
112139
}
113140

114141
public Country[] newArray(int size) {
@@ -123,15 +150,22 @@ public int describeContents() {
123150
public void writeToParcel(Parcel parcel, int flags) {
124151
parcel.writeString(mCountryIso);
125152
parcel.writeInt(mSource);
153+
parcel.writeLong(mTimestamp);
126154
}
127155

156+
/**
157+
* Returns true if this {@link Country} is equivalent to the given object. This ignores
158+
* the timestamp value and just checks for equivalence of countryIso and source values.
159+
* Returns false otherwise.
160+
*/
128161
@Override
129162
public boolean equals(Object object) {
130163
if (object == this) {
131164
return true;
132165
}
133166
if (object instanceof Country) {
134167
Country c = (Country) object;
168+
// No need to check the equivalence of the timestamp
135169
return mCountryIso.equals(c.getCountryIso()) && mSource == c.getSource();
136170
}
137171
return false;
@@ -150,8 +184,8 @@ public int hashCode() {
150184
}
151185

152186
/**
153-
* Compare the specified country to this country object ignoring the mSource
154-
* field, return true if the countryIso fields are equal
187+
* Compare the specified country to this country object ignoring the source
188+
* and timestamp fields, return true if the countryIso fields are equal
155189
*
156190
* @param country the country to compare
157191
* @return true if the specified country's countryIso field is equal to this
@@ -160,4 +194,9 @@ public int hashCode() {
160194
public boolean equalsIgnoreSource(Country country) {
161195
return country != null && mCountryIso.equals(country.getCountryIso());
162196
}
197+
198+
@Override
199+
public String toString() {
200+
return "Country {ISO=" + mCountryIso + ", source=" + mSource + ", time=" + mTimestamp + "}";
201+
}
163202
}

services/java/com/android/server/CountryDetectorService.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.android.server;
1818

19+
import java.io.FileDescriptor;
20+
import java.io.PrintWriter;
1921
import java.util.HashMap;
2022

2123
import com.android.server.location.ComprehensiveCountryDetector;
@@ -30,6 +32,8 @@
3032
import android.os.Looper;
3133
import android.os.Process;
3234
import android.os.RemoteException;
35+
import android.util.PrintWriterPrinter;
36+
import android.util.Printer;
3337
import android.util.Slog;
3438

3539
/**
@@ -75,7 +79,7 @@ public ICountryListener getListener() {
7579
}
7680
}
7781

78-
private final static String TAG = "CountryDetectorService";
82+
private final static String TAG = "CountryDetector";
7983

8084
private final HashMap<IBinder, Receiver> mReceivers;
8185
private final Context mContext;
@@ -201,4 +205,20 @@ public void run() {
201205
boolean isSystemReady() {
202206
return mSystemReady;
203207
}
208+
209+
@Override
210+
protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
211+
try {
212+
final Printer p = new PrintWriterPrinter(fout);
213+
p.println("CountryDetectorService state:");
214+
p.println(" Number of listeners=" + mReceivers.keySet().size());
215+
if (mCountryDetector == null) {
216+
p.println(" ComprehensiveCountryDetector not initialized");
217+
} else {
218+
p.println(" " + mCountryDetector.toString());
219+
}
220+
} catch (Exception e) {
221+
Slog.e(TAG, "Failed to dump CountryDetectorService: ", e);
222+
}
223+
}
204224
}

0 commit comments

Comments
 (0)