1a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao/*
2a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * Copyright (C) 2010 The Android Open Source Project
3a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao *
4a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * Licensed under the Apache License, Version 2.0 (the "License");
5a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * you may not use this file except in compliance with the License.
6a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * You may obtain a copy of the License at
7a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao *
8a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao *      http://www.apache.org/licenses/LICENSE-2.0
9a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao *
10a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * Unless required by applicable law or agreed to in writing, software
11a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * distributed under the License is distributed on an "AS IS" BASIS,
12a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * See the License for the specific language governing permissions and
14a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * limitations under the License
15a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao */
16a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
17a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taopackage com.android.server.location;
18a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
19a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.content.Context;
20a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.location.Address;
21a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.location.Country;
22a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.location.Geocoder;
23a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.location.Location;
24a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.location.LocationListener;
25a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.location.LocationManager;
26a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.os.Bundle;
27a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taoimport android.util.Slog;
28a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
29a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawaimport java.io.IOException;
30a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawaimport java.util.ArrayList;
31a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawaimport java.util.List;
32a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawaimport java.util.Timer;
33a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawaimport java.util.TimerTask;
34a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa
35a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao/**
36a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * This class detects which country the user currently is in through the enabled
37a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * location providers and the GeoCoder
38a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * <p>
39a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * Use {@link #detectCountry} to start querying. If the location can not be
40a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * resolved within the given time, the last known location will be used to get
41a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * the user country through the GeoCoder. The IllegalStateException will be
42a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * thrown if there is a ongoing query.
43a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * <p>
44a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * The current query can be stopped by {@link #stop()}
45a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao *
46a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao * @hide
47a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao */
48a58a8751b4c2ce457f0082a0baaee61312d56195Bai Taopublic class LocationBasedCountryDetector extends CountryDetectorBase {
49a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    private final static String TAG = "LocationBasedCountryDetector";
50a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    private final static long QUERY_LOCATION_TIMEOUT = 1000 * 60 * 5; // 5 mins
51a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
52a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
53a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Used for canceling location query
54a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
55a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected Timer mTimer;
56a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
57a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
58a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * The thread to query the country from the GeoCoder.
59a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
60a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected Thread mQueryThread;
61a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected List<LocationListener> mLocationListeners;
62a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
63a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    private LocationManager mLocationManager;
64a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    private List<String> mEnabledProviders;
65a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
66a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    public LocationBasedCountryDetector(Context ctx) {
67a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        super(ctx);
68a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        mLocationManager = (LocationManager) ctx.getSystemService(Context.LOCATION_SERVICE);
69a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
70a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
71a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
72a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * @return the ISO 3166-1 two letters country code from the location
73a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
74a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected String getCountryFromLocation(Location location) {
75a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        String country = null;
76a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        Geocoder geoCoder = new Geocoder(mContext);
77a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        try {
78a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            List<Address> addresses = geoCoder.getFromLocation(
79a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    location.getLatitude(), location.getLongitude(), 1);
80a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            if (addresses != null && addresses.size() > 0) {
81a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                country = addresses.get(0).getCountryCode();
82a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            }
83a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        } catch (IOException e) {
84a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            Slog.w(TAG, "Exception occurs when getting country from location");
85a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
86a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        return country;
87a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
88a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
89a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa    protected boolean isAcceptableProvider(String provider) {
90a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa        // We don't want to actively initiate a location fix here (with gps or network providers).
91a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa        return LocationManager.PASSIVE_PROVIDER.equals(provider);
92a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa    }
93a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa
94a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
95a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa     * Register a listener with a provider name
96a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
97a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa    protected void registerListener(String provider, LocationListener listener) {
98a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa        mLocationManager.requestLocationUpdates(provider, 0, 0, listener);
99a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
100a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
101a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
102a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa     * Unregister an already registered listener
103a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
104a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa    protected void unregisterListener(LocationListener listener) {
105a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa        mLocationManager.removeUpdates(listener);
106a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
107a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
108a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
109a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * @return the last known location from all providers
110a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
111a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected Location getLastKnownLocation() {
112a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        List<String> providers = mLocationManager.getAllProviders();
113a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        Location bestLocation = null;
114a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        for (String provider : providers) {
115a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            Location lastKnownLocation = mLocationManager.getLastKnownLocation(provider);
116a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            if (lastKnownLocation != null) {
1172eeeec248a38ff33999c83f4b8d5bab7d50e79d2Nick Pelly                if (bestLocation == null ||
1184118012da9a22694b3353040a485f8cdc27e2f17Philip Milne                        bestLocation.getElapsedRealtimeNanos() <
1194118012da9a22694b3353040a485f8cdc27e2f17Philip Milne                        lastKnownLocation.getElapsedRealtimeNanos()) {
120a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    bestLocation = lastKnownLocation;
121a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                }
122a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            }
123a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
124a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        return bestLocation;
125a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
126a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
127a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
128a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * @return the timeout for querying the location.
129a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
130a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected long getQueryLocationTimeout() {
131a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        return QUERY_LOCATION_TIMEOUT;
132a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
133a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
134a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa    protected List<String> getEnabledProviders() {
135a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mEnabledProviders == null) {
136a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mEnabledProviders = mLocationManager.getProviders(true);
137a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
138a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa        return mEnabledProviders;
139a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
140a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
141a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
142a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Start detecting the country.
143a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * <p>
144a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Queries the location from all location providers, then starts a thread to query the
145a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * country from GeoCoder.
146a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
147a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    @Override
148a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    public synchronized Country detectCountry() {
149a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mLocationListeners  != null) {
150a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            throw new IllegalStateException();
151a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
152a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        // Request the location from all enabled providers.
153a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa        List<String> enabledProviders = getEnabledProviders();
154a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa        int totalProviders = enabledProviders.size();
155a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (totalProviders > 0) {
156a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mLocationListeners = new ArrayList<LocationListener>(totalProviders);
157a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            for (int i = 0; i < totalProviders; i++) {
158a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                String provider = enabledProviders.get(i);
159a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                if (isAcceptableProvider(provider)) {
160a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                    LocationListener listener = new LocationListener () {
161a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        @Override
162a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        public void onLocationChanged(Location location) {
163a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                            if (location != null) {
164a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                                LocationBasedCountryDetector.this.stop();
165a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                                queryCountryCode(location);
166a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                            }
167a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                        }
168a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        @Override
169a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        public void onProviderDisabled(String provider) {
170a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        }
171a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        @Override
172a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        public void onProviderEnabled(String provider) {
173a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        }
174a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        @Override
175a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        public void onStatusChanged(String provider, int status, Bundle extras) {
176a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        }
177a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                    };
178a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                    mLocationListeners.add(listener);
179a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                    registerListener(provider, listener);
180a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                }
181a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            }
182a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa
183a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mTimer = new Timer();
184a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mTimer.schedule(new TimerTask() {
185a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                @Override
186a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                public void run() {
187a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    mTimer = null;
188a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    LocationBasedCountryDetector.this.stop();
189a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    // Looks like no provider could provide the location, let's try the last
190a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    // known location.
191a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    queryCountryCode(getLastKnownLocation());
192a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                }
193a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            }, getQueryLocationTimeout());
194a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        } else {
195a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            // There is no provider enabled.
196a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            queryCountryCode(getLastKnownLocation());
197a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
198a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        return mDetectedCountry;
199a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
200a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
201a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
202a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Stop the current query without notifying the listener.
203a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
204a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    @Override
205a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    public synchronized void stop() {
206a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mLocationListeners != null) {
207a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa            for (LocationListener listener : mLocationListeners) {
208a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                unregisterListener(listener);
209a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa            }
210a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mLocationListeners = null;
211a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
212a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mTimer != null) {
213a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mTimer.cancel();
214a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mTimer = null;
215a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
216a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
217a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
218a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
219a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Start a new thread to query the country from Geocoder.
220a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
221a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    private synchronized void queryCountryCode(final Location location) {
222a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (location == null) {
223a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            notifyListener(null);
224a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            return;
225a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
226a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mQueryThread != null) return;
227a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        mQueryThread = new Thread(new Runnable() {
228a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa            @Override
229a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            public void run() {
230a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                String countryIso = null;
231a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                if (location != null) {
232a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    countryIso = getCountryFromLocation(location);
233a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                }
234a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                if (countryIso != null) {
235a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    mDetectedCountry = new Country(countryIso, Country.COUNTRY_SOURCE_LOCATION);
236a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                } else {
237a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    mDetectedCountry = null;
238a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                }
239a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                notifyListener(mDetectedCountry);
240a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                mQueryThread = null;
241a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            }
242a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        });
243a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        mQueryThread.start();
244a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
245a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao}
246