1package com.android.contacts.common.location;
2
3import android.app.IntentService;
4import android.content.Context;
5import android.content.Intent;
6import android.content.SharedPreferences;
7import android.content.SharedPreferences.Editor;
8import android.location.Address;
9import android.location.Geocoder;
10import android.location.Location;
11import android.preference.PreferenceManager;
12import android.util.Log;
13
14import java.io.IOException;
15import 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 */
21public 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
28    public UpdateCountryService() {
29        super(TAG);
30    }
31
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
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     */
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