12530512f639c4979fd7371c7dd25dd67e8118124Bai Tao/*
22530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * Copyright (C) 2010 The Android Open Source Project
32530512f639c4979fd7371c7dd25dd67e8118124Bai Tao *
42530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * Licensed under the Apache License, Version 2.0 (the "License"); you may not
52530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * use this file except in compliance with the License. You may obtain a copy of
62530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * the License at
72530512f639c4979fd7371c7dd25dd67e8118124Bai Tao *
82530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * http://www.apache.org/licenses/LICENSE-2.0
92530512f639c4979fd7371c7dd25dd67e8118124Bai Tao *
102530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * Unless required by applicable law or agreed to in writing, software
112530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
122530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
132530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * License for the specific language governing permissions and limitations under
142530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * the License
152530512f639c4979fd7371c7dd25dd67e8118124Bai Tao */
162530512f639c4979fd7371c7dd25dd67e8118124Bai Tao
172530512f639c4979fd7371c7dd25dd67e8118124Bai Taopackage com.android.providers.contacts;
182530512f639c4979fd7371c7dd25dd67e8118124Bai Tao
192530512f639c4979fd7371c7dd25dd67e8118124Bai Taoimport android.content.Context;
202530512f639c4979fd7371c7dd25dd67e8118124Bai Taoimport android.location.Country;
212530512f639c4979fd7371c7dd25dd67e8118124Bai Taoimport android.location.CountryDetector;
222530512f639c4979fd7371c7dd25dd67e8118124Bai Taoimport android.location.CountryListener;
232530512f639c4979fd7371c7dd25dd67e8118124Bai Taoimport android.os.Looper;
242530512f639c4979fd7371c7dd25dd67e8118124Bai Tao
25d9b90fa203d65d962650574662611e5d7425163fYorke Leeimport java.util.Locale;
26d9b90fa203d65d962650574662611e5d7425163fYorke Lee
272530512f639c4979fd7371c7dd25dd67e8118124Bai Tao/**
282530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * This class monitors the change of country.
292530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * <p>
302530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * {@link #getCountryIso()} is used to get the ISO 3166-1 two letters country
312530512f639c4979fd7371c7dd25dd67e8118124Bai Tao * code of current country.
322530512f639c4979fd7371c7dd25dd67e8118124Bai Tao */
332530512f639c4979fd7371c7dd25dd67e8118124Bai Taopublic class CountryMonitor {
342530512f639c4979fd7371c7dd25dd67e8118124Bai Tao    private String mCurrentCountryIso;
352530512f639c4979fd7371c7dd25dd67e8118124Bai Tao    private Context mContext;
362530512f639c4979fd7371c7dd25dd67e8118124Bai Tao
3789b7c2b6e0003b17d08002f02d6aeec9f9788c8dDmitri Plotnikov    public CountryMonitor(Context context) {
382530512f639c4979fd7371c7dd25dd67e8118124Bai Tao        mContext = context;
392530512f639c4979fd7371c7dd25dd67e8118124Bai Tao    }
402530512f639c4979fd7371c7dd25dd67e8118124Bai Tao
412530512f639c4979fd7371c7dd25dd67e8118124Bai Tao    /**
422530512f639c4979fd7371c7dd25dd67e8118124Bai Tao     * Get the current country code
432530512f639c4979fd7371c7dd25dd67e8118124Bai Tao     *
442530512f639c4979fd7371c7dd25dd67e8118124Bai Tao     * @return the ISO 3166-1 two letters country code of current country.
452530512f639c4979fd7371c7dd25dd67e8118124Bai Tao     */
462530512f639c4979fd7371c7dd25dd67e8118124Bai Tao    public synchronized String getCountryIso() {
472530512f639c4979fd7371c7dd25dd67e8118124Bai Tao        if (mCurrentCountryIso == null) {
482530512f639c4979fd7371c7dd25dd67e8118124Bai Tao            final CountryDetector countryDetector =
492530512f639c4979fd7371c7dd25dd67e8118124Bai Tao                    (CountryDetector) mContext.getSystemService(Context.COUNTRY_DETECTOR);
50d9b90fa203d65d962650574662611e5d7425163fYorke Lee            Country country = null;
51d9b90fa203d65d962650574662611e5d7425163fYorke Lee            if (countryDetector != null) country = countryDetector.detectCountry();
52d9b90fa203d65d962650574662611e5d7425163fYorke Lee
53d9b90fa203d65d962650574662611e5d7425163fYorke Lee            if (country == null) {
54d9b90fa203d65d962650574662611e5d7425163fYorke Lee                // Fallback to Locale if there are issues with CountryDetector
55d9b90fa203d65d962650574662611e5d7425163fYorke Lee                return Locale.getDefault().getCountry();
56d9b90fa203d65d962650574662611e5d7425163fYorke Lee            }
57d9b90fa203d65d962650574662611e5d7425163fYorke Lee
58d9b90fa203d65d962650574662611e5d7425163fYorke Lee            mCurrentCountryIso = country.getCountryIso();
59d9b90fa203d65d962650574662611e5d7425163fYorke Lee                countryDetector.addCountryListener(new CountryListener() {
60d9b90fa203d65d962650574662611e5d7425163fYorke Lee                    public void onCountryDetected(Country country) {
61d9b90fa203d65d962650574662611e5d7425163fYorke Lee                        mCurrentCountryIso = country.getCountryIso();
62d9b90fa203d65d962650574662611e5d7425163fYorke Lee                    }
63d9b90fa203d65d962650574662611e5d7425163fYorke Lee                }, Looper.getMainLooper());
642530512f639c4979fd7371c7dd25dd67e8118124Bai Tao        }
652530512f639c4979fd7371c7dd25dd67e8118124Bai Tao        return mCurrentCountryIso;
662530512f639c4979fd7371c7dd25dd67e8118124Bai Tao    }
672530512f639c4979fd7371c7dd25dd67e8118124Bai Tao}
68