Android Open Source - callerid-for-android Geocoder Async Task






From Project

Back to project page callerid-for-android.

License

The source code is released under:

GNU General Public License

If you think the Android project callerid-for-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.integralblue.callerid;
//w ww.j a va 2  s. c  om
import java.util.List;

import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;

import com.google.inject.Inject;
import com.integralblue.callerid.geocoder.Geocoder;

import roboguice.util.Ln;
import roboguice.util.RoboAsyncTask;
import android.content.Context;
import android.location.Address;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class GeocoderAsyncTask extends RoboAsyncTask<Address> {

  final String locationName;
  final ViewGroup layout;
  
  @Inject
  Geocoder geocoder;

  public GeocoderAsyncTask(Context context, final String locationName, final ViewGroup layout) {
    super(context);
    this.locationName = locationName;
    this.layout = layout;
  }
  
  /**
     * Returns <tt>true</tt> if this task was cancelled before it completed
     * normally.
     *
     * Request to add this to Roboguice: https://code.google.com/p/roboguice/issues/detail?id=210
     *
     * @return <tt>true</tt> if task was cancelled before it completed
     *
     * @see #cancel(boolean)
     */
  public boolean isCancelled(){
    return future == null ? false : future.isCancelled();
  }

  public Address call() throws Exception {
    List<Address> addresses = geocoder.getFromLocationName(locationName, 1);
    if(addresses.size()==1){
      return addresses.get(0);
    }else{
      return null;
    }
  }

  @Override
  protected void onSuccess(final Address address)
      throws Exception {
    if(isCancelled()) return; //don't do any UI things if the task was cancelled
    MapView mapView = (MapView) layout.findViewById(R.id.map_view);
    if(address == null){
      if(mapView!=null) mapView.setVisibility(View.GONE);
    }else{
      if(mapView == null){
        LayoutInflater.from(getContext()).inflate(R.layout.map, layout, true);
        mapView = (MapView) layout.findViewById(R.id.map_view);
        mapView.setBuiltInZoomControls(true);
      }
          mapView.getController().setZoom(16);
      mapView.getController().setCenter(new GeoPoint(address.getLatitude(),address.getLongitude()));
      mapView.setVisibility(View.VISIBLE);
    }
  }

  @Override
  protected void onException(final Exception e) throws RuntimeException {
    Ln.e(e);
    if(layout.findViewById(R.id.map_view)!=null) layout.findViewById(R.id.map_view).setVisibility(View.GONE);
  }

  @Override
  protected void onInterrupted(final Exception e) {
    // intentionally not calling the super, as that calls onException(e), and that's not what we want
    // super.onInterrupted(e);
    if(layout.findViewById(R.id.map_view)!=null) layout.findViewById(R.id.map_view).setVisibility(View.GONE);
  }
}




Java Source Code List

com.blundell.tut.LoaderImageView.java
com.integralblue.callerid.CallerIDApplication.java
com.integralblue.callerid.CallerIDBroadcastReceiver.java
com.integralblue.callerid.CallerIDLookup.java
com.integralblue.callerid.CallerIDResult.java
com.integralblue.callerid.CallerIDService.java
com.integralblue.callerid.GeocoderAsyncTask.java
com.integralblue.callerid.HttpCallerIDLookup.java
com.integralblue.callerid.LookupAsyncTask.java
com.integralblue.callerid.LookupFragment.java
com.integralblue.callerid.MainActivity.java
com.integralblue.callerid.PreferencesActivity.java
com.integralblue.callerid.RecentCallsFragment.java
com.integralblue.callerid.SpecialPhoneNumbers.java
com.integralblue.callerid.TabsAdapter.java
com.integralblue.callerid.contacts.ContactsHelper.java
com.integralblue.callerid.contacts.NewContactsHelper.java
com.integralblue.callerid.contacts.OldContactsHelper.java
com.integralblue.callerid.geocoder.AndroidGeocoder.java
com.integralblue.callerid.geocoder.Geocoder.java
com.integralblue.callerid.geocoder.NominatimGeocoder.java
com.integralblue.callerid.inject.CallerIDModule.java
com.integralblue.callerid.inject.ContactsHelperProvider.java
com.integralblue.callerid.inject.CountryDetector.java
com.integralblue.callerid.inject.GeocoderHelperProvider.java
com.integralblue.callerid.inject.PreferencesNameProvider.java
com.integralblue.callerid.inject.RestTemplateProvider.java
com.integralblue.callerid.inject.TextToSpeechHelper.java
com.integralblue.callerid.inject.VersionInformationHelper.java
com.integralblue.callerid.widget.DontPressWithParentImageView.java
com.squareup.okhttp.OkHttpURLStreamHandlerFactory.java
com.squareup.okhttp.internal.OkHttpClientHandler.java