• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License
15  */
16 
17 package com.android.providers.contacts;
18 
19 import android.content.Context;
20 import android.location.Country;
21 import android.location.CountryDetector;
22 import android.location.CountryListener;
23 import android.os.Looper;
24 
25 /**
26  * This class monitors the change of country.
27  * <p>
28  * {@link #getCountryIso()} is used to get the ISO 3166-1 two letters country
29  * code of current country.
30  */
31 public class CountryMonitor {
32     private String mCurrentCountryIso;
33     private Context mContext;
34 
CountryMonitor(Context context)35     public CountryMonitor(Context context) {
36         mContext = context;
37     }
38 
39     /**
40      * Get the current country code
41      *
42      * @return the ISO 3166-1 two letters country code of current country.
43      */
getCountryIso()44     public synchronized String getCountryIso() {
45         if (mCurrentCountryIso == null) {
46             final CountryDetector countryDetector =
47                     (CountryDetector) mContext.getSystemService(Context.COUNTRY_DETECTOR);
48             mCurrentCountryIso = countryDetector.detectCountry().getCountryIso();
49             countryDetector.addCountryListener(new CountryListener() {
50                 public void onCountryDetected(Country country) {
51                     mCurrentCountryIso = country.getCountryIso();
52                 }
53             }, Looper.getMainLooper());
54         }
55         return mCurrentCountryIso;
56     }
57 }
58