PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/src/com/radioactiveyak/wamf/FriendLocationLookup.java

http://wherearemyfriends.googlecode.com/
Java | 102 lines | 54 code | 17 blank | 31 comment | 5 complexity | 0365d46d714b2fa0f168d30d6b1a473e MD5 | raw file
  1package com.radioactiveyak.wamf;
  2
  3import java.io.IOException;
  4import java.util.HashMap;
  5import java.util.List;
  6import java.util.Locale;
  7import android.content.Context;
  8import android.database.Cursor;
  9import android.location.Address;
 10import android.location.Geocoder;
 11import android.location.Location;
 12import android.provider.Contacts;
 13import android.provider.Contacts.People;
 14import android.util.Log;
 15
 16/**
 17 * Static class used to find the location of each contact
 18 * in the Contact Content Provider by reverse-geocoding their
 19 * home address.
 20 * 
 21 * @author Reto Meier
 22 * Author of Professional Android Application Development
 23 * http://www.amazon.com/gp/product/0470344717?tag=interventione-20
 24 *
 25 */
 26public class FriendLocationLookup {
 27 
 28  /**
 29   * Contact list query result cursor.
 30   */
 31  public static Cursor cursor;
 32  
 33  /**
 34   * Contact's home address query result cursor.
 35   */
 36  public static Cursor addressCursor;  
 37  
 38  /**
 39   * Return a hash of contact names and the
 40   * reverse-geocoded location of their home addresses.
 41   * 
 42   * @param context Calling application's context
 43   * @return Hash of contact names with their physical locations
 44   */
 45  public static HashMap<String, Location> GetFriendLocations(Context context) {
 46    HashMap<String, Location> result = new HashMap<String, Location>();
 47
 48    // Return a query result of all the peope in the contact list.
 49    cursor = context.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
 50
 51    // Use the convenience properties to get the index of the columns
 52    int nameIdx = cursor.getColumnIndexOrThrow(People.NAME);
 53    int personID = cursor.getColumnIndexOrThrow(People._ID);    
 54    
 55    if (cursor.moveToFirst())
 56    do {
 57      // Extract the name.
 58      String name = cursor.getString(nameIdx);
 59      String id = cursor.getString(personID);
 60        
 61      // Extract the address.
 62      String where = Contacts.ContactMethods.PERSON_ID + " == " + id + 
 63                     " AND " +
 64                     Contacts.ContactMethods.KIND + " == " + Contacts.KIND_POSTAL;
 65
 66      addressCursor = context.getContentResolver().query(Contacts.ContactMethods.CONTENT_URI, null, where, null, null);
 67
 68      // Extract the postal address from the cursor
 69      int postalAddress = addressCursor.getColumnIndexOrThrow(Contacts.ContactMethodsColumns.DATA);
 70      String address = "";
 71      if (addressCursor.moveToFirst())
 72        address = addressCursor.getString(postalAddress);
 73      addressCursor.close();
 74      
 75      // Reverse geocode the postal address to get a location.
 76      Location friendLocation = new Location("reverseGeocoded");
 77      
 78      if (address != null) {
 79        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
 80        try {
 81          List<Address> addressResult = geocoder.getFromLocationName(address, 1);
 82          if (!addressResult.isEmpty()) {
 83            Address resultAddress = addressResult.get(0);
 84            friendLocation.setLatitude(resultAddress.getLatitude());
 85            friendLocation.setLongitude(resultAddress.getLongitude());
 86          }
 87        } catch (IOException e) {
 88         Log.d("Contact Location Lookup Failed", e.getMessage());
 89        }
 90      }      
 91      
 92      // Populate the result hash
 93      result.put(name, friendLocation);
 94      
 95    } while(cursor.moveToNext());
 96
 97    // Cleanup the cursor.
 98    cursor.close();
 99    
100    return result;
101  }
102}