blob: f5439234d0964097ad37443e04ea7962c30522f6 [file] [log] [blame]
Ray Chen327eeb82011-08-24 11:40:04 +08001/*
2 * Copyright (C) 2011 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.gallery3d.ui;
18
Ray Chen52a460c2011-10-05 12:31:24 +080019import android.content.Context;
20import android.location.Address;
21import android.os.Handler;
22import android.os.Looper;
23
Ray Chen327eeb82011-08-24 11:40:04 +080024import com.android.gallery3d.app.GalleryActivity;
25import com.android.gallery3d.data.MediaDetails;
26import com.android.gallery3d.util.Future;
27import com.android.gallery3d.util.FutureListener;
28import com.android.gallery3d.util.GalleryUtils;
29import com.android.gallery3d.util.ReverseGeocoder;
30import com.android.gallery3d.util.ThreadPool.Job;
31import com.android.gallery3d.util.ThreadPool.JobContext;
32
Ray Chen327eeb82011-08-24 11:40:04 +080033public class DetailsAddressResolver {
34 private AddressResolvingListener mListener;
Ray Chen52a460c2011-10-05 12:31:24 +080035 private final GalleryActivity mContext;
Ray Chen327eeb82011-08-24 11:40:04 +080036 private Future<Address> mAddressLookupJob;
Ray Chen52a460c2011-10-05 12:31:24 +080037 private final Handler mHandler;
Ray Chen327eeb82011-08-24 11:40:04 +080038
39 private class AddressLookupJob implements Job<Address> {
Ray Chen52a460c2011-10-05 12:31:24 +080040 private double[] mLatlng;
Ray Chen327eeb82011-08-24 11:40:04 +080041
42 protected AddressLookupJob(double[] latlng) {
43 mLatlng = latlng;
44 }
45
46 public Address run(JobContext jc) {
47 ReverseGeocoder geocoder = new ReverseGeocoder(mContext.getAndroidContext());
48 return geocoder.lookupAddress(mLatlng[0], mLatlng[1], true);
49 }
50 }
51
52 public interface AddressResolvingListener {
53 public void onAddressAvailable(String address);
54 }
55
56 public DetailsAddressResolver(GalleryActivity context) {
57 mContext = context;
58 mHandler = new Handler(Looper.getMainLooper());
59 }
60
61 public String resolveAddress(double[] latlng, AddressResolvingListener listener) {
Ray Chen327eeb82011-08-24 11:40:04 +080062 mListener = listener;
63 mAddressLookupJob = mContext.getThreadPool().submit(
64 new AddressLookupJob(latlng),
65 new FutureListener<Address>() {
66 public void onFutureDone(final Future<Address> future) {
67 mAddressLookupJob = null;
68 if (!future.isCancelled()) {
69 mHandler.post(new Runnable() {
70 public void run() {
71 updateLocation(future.get());
72 }
73 });
74 }
75 }
76 });
Ray Chen52a460c2011-10-05 12:31:24 +080077 return GalleryUtils.formatLatitudeLongitude("(%f,%f)", latlng[0], latlng[1]);
Ray Chen327eeb82011-08-24 11:40:04 +080078 }
79
80 private void updateLocation(Address address) {
81 if (address != null) {
82 Context context = mContext.getAndroidContext();
83 String parts[] = {
84 address.getAdminArea(),
85 address.getSubAdminArea(),
86 address.getLocality(),
87 address.getSubLocality(),
88 address.getThoroughfare(),
89 address.getSubThoroughfare(),
90 address.getPremises(),
91 address.getPostalCode(),
92 address.getCountryName()
93 };
94
95 String addressText = "";
96 for (int i = 0; i < parts.length; i++) {
97 if (parts[i] == null || parts[i].isEmpty()) continue;
98 if (!addressText.isEmpty()) {
99 addressText += ", ";
100 }
101 addressText += parts[i];
102 }
103 String text = String.format("%s : %s", DetailsHelper.getDetailsName(
104 context, MediaDetails.INDEX_LOCATION), addressText);
105 mListener.onAddressAvailable(text);
106 }
107 }
108
109 public void cancel() {
110 if (mAddressLookupJob != null) {
111 mAddressLookupJob.cancel();
112 mAddressLookupJob = null;
113 }
114 }
115}