Home | History | Annotate | Download | only in ui
      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 
     19 import android.content.Context;
     20 import android.location.Address;
     21 import android.os.Handler;
     22 import android.os.Looper;
     23 
     24 import com.android.gallery3d.app.AbstractGalleryActivity;
     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 
     33 public class DetailsAddressResolver {
     34     private AddressResolvingListener mListener;
     35     private final AbstractGalleryActivity mContext;
     36     private Future<Address> mAddressLookupJob;
     37     private final Handler mHandler;
     38 
     39     private class AddressLookupJob implements Job<Address> {
     40         private double[] mLatlng;
     41 
     42         protected AddressLookupJob(double[] latlng) {
     43             mLatlng = latlng;
     44         }
     45 
     46         @Override
     47         public Address run(JobContext jc) {
     48             ReverseGeocoder geocoder = new ReverseGeocoder(mContext.getAndroidContext());
     49             return geocoder.lookupAddress(mLatlng[0], mLatlng[1], true);
     50         }
     51     }
     52 
     53     public interface AddressResolvingListener {
     54         public void onAddressAvailable(String address);
     55     }
     56 
     57     public DetailsAddressResolver(AbstractGalleryActivity context) {
     58         mContext = context;
     59         mHandler = new Handler(Looper.getMainLooper());
     60     }
     61 
     62     public String resolveAddress(double[] latlng, AddressResolvingListener listener) {
     63         mListener = listener;
     64         mAddressLookupJob = mContext.getThreadPool().submit(
     65                 new AddressLookupJob(latlng),
     66                 new FutureListener<Address>() {
     67                     @Override
     68                     public void onFutureDone(final Future<Address> future) {
     69                         mAddressLookupJob = null;
     70                         if (!future.isCancelled()) {
     71                             mHandler.post(new Runnable() {
     72                                 @Override
     73                                 public void run() {
     74                                     updateLocation(future.get());
     75                                 }
     76                             });
     77                         }
     78                     }
     79                 });
     80         return GalleryUtils.formatLatitudeLongitude("(%f,%f)", latlng[0], latlng[1]);
     81     }
     82 
     83     private void updateLocation(Address address) {
     84         if (address != null) {
     85             Context context = mContext.getAndroidContext();
     86             String parts[] = {
     87                 address.getAdminArea(),
     88                 address.getSubAdminArea(),
     89                 address.getLocality(),
     90                 address.getSubLocality(),
     91                 address.getThoroughfare(),
     92                 address.getSubThoroughfare(),
     93                 address.getPremises(),
     94                 address.getPostalCode(),
     95                 address.getCountryName()
     96             };
     97 
     98             String addressText = "";
     99             for (int i = 0; i < parts.length; i++) {
    100                 if (parts[i] == null || parts[i].isEmpty()) continue;
    101                 if (!addressText.isEmpty()) {
    102                     addressText += ", ";
    103                 }
    104                 addressText += parts[i];
    105             }
    106             String text = String.format("%s : %s", DetailsHelper.getDetailsName(
    107                     context, MediaDetails.INDEX_LOCATION), addressText);
    108             mListener.onAddressAvailable(text);
    109         }
    110     }
    111 
    112     public void cancel() {
    113         if (mAddressLookupJob != null) {
    114             mAddressLookupJob.cancel();
    115             mAddressLookupJob = null;
    116         }
    117     }
    118 }
    119