[android] messages in SMS inbox

private void loadSmsList()
{
  Log.i("MainActivity.java | loadSmsList", "|" + "###############" + "|");
  Uri uriSMSURI = Uri.parse("content://sms/inbox");
  Cursor cur = getContentResolver().query(uriSMSURI, null, null, null, "date desc limit 10");
  
  Log.i("MainActivity.java | loadSmsList", "|count:" + cur.getCount() + "|column count :" + cur.getColumnCount() + "|name index:" + cur.getColumnIndex("name"));
  if (cur.getCount() == 0)
    return;
  
  cur.moveToFirst();
  
  while (!cur.isAfterLast())
  {
    long date = cur.getLong(cur.getColumnIndex("date"));
    String body = cur.getString(cur.getColumnIndex("body"));
    String address = cur.getString(cur.getColumnIndex("address"));
    
    String name = null;
    int nameIndex = cur.getColumnIndex("name");
    if (nameIndex == -1)
      name = displayName(getApplicationContext(), address);
    else
      name = cur.getString(cur.getColumnIndex("person"));
    
    if (TextUtils.isEmpty(name))
      name = address;
    
    Log.i("MainActivity.java | loadSmsList", "|" + dateFormat(date, "yyyy-MM-dd hh:mm:ss") + "|" + name + "|" + address + "|" + body);
    cur.moveToNext();
  }
  cur.close();
}


private String displayName(Context $context, String $number)
{
  String result = null;
  
  result = displayNameByNumber($context, Uri.encode($number));
  
  if (TextUtils.isEmpty(result))
    result = displayNameByNumber($context, Uri.encode(PhoneNumberUtils.formatNumber($number)));
  
  return result;
}


private String displayNameByNumber(Context $context, String $number)
{
  String result = null;
  
  Uri uriLookup = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, $number);
  Cursor cursor = $context.getContentResolver().query(uriLookup, new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }, null, null, null);
  
  if (cursor.getCount() > 0)
  {
    cursor.moveToFirst();
    result = cursor.getString(0);
  }
  
  cursor.close();
  
  return result;
}


public static String dateFormat(long $timeMillis, String $pattern)
{
  SimpleDateFormat format = new SimpleDateFormat($pattern, Locale.getDefault());
  return format.format($timeMillis);
}