add static to cache phone->name mapping

This commit is contained in:
Eric House 2012-04-21 22:16:47 -07:00
parent 3af20f1d5a
commit a72dbdd8dd

View file

@ -41,6 +41,7 @@ import android.widget.CheckBox;
import android.widget.EditText; import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import java.util.HashMap;
import junit.framework.Assert; import junit.framework.Assert;
import org.eehouse.android.xw4.jni.*; import org.eehouse.android.xw4.jni.*;
@ -52,6 +53,8 @@ public class Utils {
private static Boolean s_isFirstBootThisVersion = null; private static Boolean s_isFirstBootThisVersion = null;
private static Boolean s_isFirstBootEver = null; private static Boolean s_isFirstBootEver = null;
private static HashMap<String,String> s_phonesHash =
new HashMap<String,String>();
private Utils() {} private Utils() {}
@ -140,7 +143,14 @@ public class Utils {
// http://stackoverflow.com/questions/2174048/how-to-look-up-a-contacts-name-from-their-phone-number-on-android // http://stackoverflow.com/questions/2174048/how-to-look-up-a-contacts-name-from-their-phone-number-on-android
public static String phoneToContact( Context context, String phone ) public static String phoneToContact( Context context, String phone )
{ {
String name = null; // I'm assuming that since context is passed this needn't
// worry about synchronization -- will always be called from
// UI thread.
String name;
if ( s_phonesHash.containsKey( phone ) ) {
name = s_phonesHash.get( phone );
} else {
name = null;
ContentResolver contentResolver = context.getContentResolver(); ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = Cursor cursor =
contentResolver contentResolver
@ -153,6 +163,9 @@ public class Utils {
name = cursor.getString( indx ); name = cursor.getString( indx );
} }
cursor.close(); cursor.close();
s_phonesHash.put( phone, name );
}
return name; return name;
} }