LocationBasedCountryDetector.java revision a550bdc84af70babb48091197bfb1b93d3671664
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) {
117a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                if (bestLocation == null || bestLocation.getTime() < lastKnownLocation.getTime()) {
118a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    bestLocation = lastKnownLocation;
119a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                }
120a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            }
121a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
122a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        return bestLocation;
123a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
124a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
125a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
126a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * @return the timeout for querying the location.
127a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
128a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    protected long getQueryLocationTimeout() {
129a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        return QUERY_LOCATION_TIMEOUT;
130a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
131a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
132a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa    protected List<String> getEnabledProviders() {
133a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mEnabledProviders == null) {
134a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mEnabledProviders = mLocationManager.getProviders(true);
135a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
136a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa        return mEnabledProviders;
137a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
138a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
139a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
140a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Start detecting the country.
141a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * <p>
142a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Queries the location from all location providers, then starts a thread to query the
143a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * country from GeoCoder.
144a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
145a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    @Override
146a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    public synchronized Country detectCountry() {
147a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mLocationListeners  != null) {
148a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            throw new IllegalStateException();
149a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
150a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        // Request the location from all enabled providers.
151a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa        List<String> enabledProviders = getEnabledProviders();
152a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa        int totalProviders = enabledProviders.size();
153a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (totalProviders > 0) {
154a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mLocationListeners = new ArrayList<LocationListener>(totalProviders);
155a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            for (int i = 0; i < totalProviders; i++) {
156a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                String provider = enabledProviders.get(i);
157a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                if (isAcceptableProvider(provider)) {
158a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                    LocationListener listener = new LocationListener () {
159a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        @Override
160a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        public void onLocationChanged(Location location) {
161a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                            if (location != null) {
162a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                                LocationBasedCountryDetector.this.stop();
163a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                                queryCountryCode(location);
164a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                            }
165a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                        }
166a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        @Override
167a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        public void onProviderDisabled(String provider) {
168a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        }
169a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        @Override
170a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        public void onProviderEnabled(String provider) {
171a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        }
172a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        @Override
173a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        public void onStatusChanged(String provider, int status, Bundle extras) {
174a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                        }
175a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                    };
176a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                    mLocationListeners.add(listener);
177a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                    registerListener(provider, listener);
178a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                }
179a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            }
180a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa
181a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mTimer = new Timer();
182a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mTimer.schedule(new TimerTask() {
183a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                @Override
184a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                public void run() {
185a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    mTimer = null;
186a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    LocationBasedCountryDetector.this.stop();
187a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    // Looks like no provider could provide the location, let's try the last
188a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    // known location.
189a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    queryCountryCode(getLastKnownLocation());
190a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                }
191a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            }, getQueryLocationTimeout());
192a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        } else {
193a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            // There is no provider enabled.
194a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            queryCountryCode(getLastKnownLocation());
195a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
196a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        return mDetectedCountry;
197a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
198a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
199a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
200a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Stop the current query without notifying the listener.
201a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
202a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    @Override
203a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    public synchronized void stop() {
204a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mLocationListeners != null) {
205a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa            for (LocationListener listener : mLocationListeners) {
206a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa                unregisterListener(listener);
207a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa            }
208a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mLocationListeners = null;
209a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
210a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mTimer != null) {
211a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mTimer.cancel();
212a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            mTimer = null;
213a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
214a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
215a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao
216a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    /**
217a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     * Start a new thread to query the country from Geocoder.
218a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao     */
219a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    private synchronized void queryCountryCode(final Location location) {
220a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (location == null) {
221a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            notifyListener(null);
222a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            return;
223a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        }
224a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        if (mQueryThread != null) return;
225a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        mQueryThread = new Thread(new Runnable() {
226a550bdc84af70babb48091197bfb1b93d3671664Daisuke Miyakawa            @Override
227a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            public void run() {
228a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                String countryIso = null;
229a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                if (location != null) {
230a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    countryIso = getCountryFromLocation(location);
231a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                }
232a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                if (countryIso != null) {
233a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    mDetectedCountry = new Country(countryIso, Country.COUNTRY_SOURCE_LOCATION);
234a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                } else {
235a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                    mDetectedCountry = null;
236a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                }
237a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                notifyListener(mDetectedCountry);
238a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao                mQueryThread = null;
239a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao            }
240a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        });
241a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao        mQueryThread.start();
242a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao    }
243a58a8751b4c2ce457f0082a0baaee61312d56195Bai Tao}
244