Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.gallery3d.ui; |
| 18 | |
Ray Chen | 52a460c | 2011-10-05 12:31:24 +0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.location.Address; |
| 21 | import android.os.Handler; |
| 22 | import android.os.Looper; |
| 23 | |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 24 | import com.android.gallery3d.app.GalleryActivity; |
| 25 | import com.android.gallery3d.data.MediaDetails; |
| 26 | import com.android.gallery3d.util.Future; |
| 27 | import com.android.gallery3d.util.FutureListener; |
| 28 | import com.android.gallery3d.util.GalleryUtils; |
| 29 | import com.android.gallery3d.util.ReverseGeocoder; |
| 30 | import com.android.gallery3d.util.ThreadPool.Job; |
| 31 | import com.android.gallery3d.util.ThreadPool.JobContext; |
| 32 | |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 33 | public class DetailsAddressResolver { |
| 34 | private AddressResolvingListener mListener; |
Ray Chen | 52a460c | 2011-10-05 12:31:24 +0800 | [diff] [blame] | 35 | private final GalleryActivity mContext; |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 36 | private Future<Address> mAddressLookupJob; |
Ray Chen | 52a460c | 2011-10-05 12:31:24 +0800 | [diff] [blame] | 37 | private final Handler mHandler; |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 38 | |
| 39 | private class AddressLookupJob implements Job<Address> { |
Ray Chen | 52a460c | 2011-10-05 12:31:24 +0800 | [diff] [blame] | 40 | private double[] mLatlng; |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 41 | |
| 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 Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 62 | 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 Chen | 52a460c | 2011-10-05 12:31:24 +0800 | [diff] [blame] | 77 | return GalleryUtils.formatLatitudeLongitude("(%f,%f)", latlng[0], latlng[1]); |
Ray Chen | 327eeb8 | 2011-08-24 11:40:04 +0800 | [diff] [blame] | 78 | } |
| 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 | } |