1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.camera; 18 19 import android.location.Address; 20 import android.location.Geocoder; 21 import android.os.AsyncTask; 22 import android.util.Log; 23 24 import java.io.IOException; 25 import java.util.List; 26 27 // Reverse geocoding may take a long time to return so we put it in AsyncTask. 28 public class ReverseGeocoderTask extends AsyncTask<Void, Void, String> { 29 private static final String TAG = "ReverseGeocoder"; 30 31 public static interface Callback { onComplete(String location)32 public void onComplete(String location); 33 } 34 35 private Geocoder mGeocoder; 36 private float mLat; 37 private float mLng; 38 private Callback mCallback; 39 ReverseGeocoderTask(Geocoder geocoder, float[] latlng, Callback callback)40 public ReverseGeocoderTask(Geocoder geocoder, float[] latlng, 41 Callback callback) { 42 mGeocoder = geocoder; 43 mLat = latlng[0]; 44 mLng = latlng[1]; 45 mCallback = callback; 46 } 47 48 @Override doInBackground(Void... params)49 protected String doInBackground(Void... params) { 50 String value = MenuHelper.EMPTY_STRING; 51 try { 52 List<Address> address = 53 mGeocoder.getFromLocation(mLat, mLng, 1); 54 StringBuilder sb = new StringBuilder(); 55 for (Address addr : address) { 56 int index = addr.getMaxAddressLineIndex(); 57 sb.append(addr.getAddressLine(index)); 58 } 59 value = sb.toString(); 60 } catch (IOException ex) { 61 value = MenuHelper.EMPTY_STRING; 62 Log.e(TAG, "Geocoder exception: ", ex); 63 } catch (RuntimeException ex) { 64 value = MenuHelper.EMPTY_STRING; 65 Log.e(TAG, "Geocoder exception: ", ex); 66 } 67 return value; 68 } 69 70 @Override onPostExecute(String location)71 protected void onPostExecute(String location) { 72 mCallback.onComplete(location); 73 } 74 } 75 76