Hi plusminus!
I wrote a more powerful regular expression for geotags and moved it's compilation out from a do-while loop.
I expect geotag in following format:
- Code: Select all
geo: +/-lat (-90 to 90), +/-lng (-180 to 180) [, +/-alt]
(based on
http://tools.ietf.org/html/draft-mayrhofer-geo-uri-00 ) and regexp catches lat, lng and alt in groups 1, 5 and 13 respectively.
So no more # in geotag is required but it's ok if it'l stay there
I think that static function with a regexp parser will be more convinient, but anyway, here is my version of a refreshFriendsList ():
Using java Syntax Highlighting
private void refreshFriendsList(){
Cursor c = getContentResolver().query(People.CONTENT_URI,
null, null, null, People.NAME + " ASC");
/* This method allows the activity to take
* care of managing the given Cursor's lifecycle
* for you based on the activity's lifecycle. */
startManagingCursor(c);
int notesColumn = c.getColumnIndex(People.NOTES);
int nameColumn = c.getColumnIndex(People.NAME);
// Moves the cursor to the first row
// and returns true if there is sth. to get
if (c.first()) {
// Pattern for extracting geo-ContentURIs from the notes.
final String geoPattern = "\\bgeo:\\s*([-+]?(90(\\.0*)?|[0-8]?[0-9](\\.[0-9]*)?))\\s*," + "\\s*([-+]?(1(80(\\.0*)?|[0-7]?[0-9](\\.[0-9]*)?)|([0-9]{1,2}(\\.[0-9]*)?)))(\\s*,\\s*([-+]?[0-9]*(\\.[0-9]*)?))?\\b";
// Compile and use regular expression
Pattern pattern = Pattern.compile(geoPattern);
do {
String notesString = c.getString(notesColumn);
Location friendLocation = null;
if (notesString != null) {
CharSequence inputStr = notesString;
Matcher matcher = pattern.matcher(inputStr);
if (matcher.find()) {
Double latid = Double.parseDouble(matcher.group(1));
Double longit = Double.parseDouble(matcher.group(5));
friendLocation = new Location();
friendLocation.setLatitude(latid.doubleValue());
friendLocation.setLongitude(longit.doubleValue());
}
}
String friendName = c.getString(nameColumn);
allFriends.add(new Friend(friendLocation, friendName));
} while (c.next());
}
}
Parsed in 0.036 seconds, using
GeSHi 1.0.8.4