blob: 97f5978cdc7b6c51200f61c19357b5db63d23cff [file] [log] [blame]
repo syncaa7075c2009-06-24 14:59:48 +08001/*
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
17package com.android.camera;
18
19import android.location.Address;
20import android.location.Geocoder;
21import android.os.AsyncTask;
22import android.util.Log;
23
24import java.io.IOException;
25import java.util.List;
26
27// Reverse geocoding may take a long time to return so we put it in AsyncTask.
28public class ReverseGeocoderTask extends AsyncTask<Void, Void, String> {
29 private static final String TAG = "ReverseGeocoder";
30
31 public static interface Callback {
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
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
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
71 protected void onPostExecute(String location) {
72 mCallback.onComplete(location);
73 }
74}
75