• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.contacts.common.location;
2 
3 import android.app.IntentService;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.SharedPreferences;
7 import android.content.SharedPreferences.Editor;
8 import android.location.Address;
9 import android.location.Geocoder;
10 import android.location.Location;
11 import android.preference.PreferenceManager;
12 import android.util.Log;
13 
14 import java.io.IOException;
15 import java.util.List;
16 
17 /**
18  * Service used to perform asynchronous geocoding from within a broadcast receiver. Given a
19  * {@link Location}, convert it into a country code, and save it in shared preferences.
20  */
21 public class UpdateCountryService extends IntentService {
22     private static final String TAG = UpdateCountryService.class.getSimpleName();
23 
24     private static final String ACTION_UPDATE_COUNTRY = "saveCountry";
25 
26     private static final String KEY_INTENT_LOCATION = "location";
27 
UpdateCountryService()28     public UpdateCountryService() {
29         super(TAG);
30     }
31 
updateCountry(Context context, Location location)32     public static void updateCountry(Context context, Location location) {
33         final Intent serviceIntent = new Intent(context, UpdateCountryService.class);
34         serviceIntent.setAction(ACTION_UPDATE_COUNTRY);
35         serviceIntent.putExtra(UpdateCountryService.KEY_INTENT_LOCATION, location);
36         context.startService(serviceIntent);
37     }
38 
39     @Override
onHandleIntent(Intent intent)40     protected void onHandleIntent(Intent intent) {
41         if (ACTION_UPDATE_COUNTRY.equals(intent.getAction())) {
42             final Location location = (Location) intent.getParcelableExtra(KEY_INTENT_LOCATION);
43             final String country = getCountryFromLocation(getApplicationContext(), location);
44 
45             if (country == null) {
46                 return;
47             }
48 
49             final SharedPreferences prefs =
50                     PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
51 
52             final Editor editor = prefs.edit();
53             editor.putLong(CountryDetector.KEY_PREFERENCE_TIME_UPDATED,
54                     System.currentTimeMillis());
55             editor.putString(CountryDetector.KEY_PREFERENCE_CURRENT_COUNTRY, country);
56             editor.commit();
57         }
58     }
59 
60     /**
61      * Given a {@link Location}, return a country code.
62      *
63      * @return the ISO 3166-1 two letter country code
64      */
getCountryFromLocation(Context context, Location location)65     private String getCountryFromLocation(Context context, Location location) {
66         final Geocoder geocoder = new Geocoder(context);
67         String country = null;
68         try {
69             final List<Address> addresses = geocoder.getFromLocation(
70                     location.getLatitude(), location.getLongitude(), 1);
71             if (addresses != null && addresses.size() > 0) {
72                 country = addresses.get(0).getCountryCode();
73             }
74         } catch (IOException e) {
75             Log.w(TAG, "Exception occurred when getting geocoded country from location");
76         }
77         return country;
78     }
79 }
80