Skip to content

Instantly share code, notes, and snippets.

@ohjongin
Created December 16, 2013 12:41
Show Gist options
  • Save ohjongin/7986386 to your computer and use it in GitHub Desktop.
Save ohjongin/7986386 to your computer and use it in GitHub Desktop.
Get google account profile information
public static boolean hasPermission(Context context, String permission) {
int res = context.checkCallingOrSelfPermission(permission);
return (res == PackageManager.PERMISSION_GRANTED);
}
@SuppressLint("NewApi")
public static String getUserDisplayName(Context context) {
if (Build.VERSION.SDK_INT < 14) return "";
if (!hasPermission(context, "android.permission.READ_PROFILE")) return "";
// Sets the columns to retrieve for the user profile
String[] projections = new String[] {
ContactsContract.Profile._ID,
ContactsContract.Profile.DISPLAY_NAME_PRIMARY,
ContactsContract.Profile.LOOKUP_KEY,
ContactsContract.Profile.PHOTO_THUMBNAIL_URI,
ContactsContract.Profile.IS_USER_PROFILE
};
// Retrieves the profile from the Contacts Provider
Cursor c = context.getContentResolver().query(
ContactsContract.Profile.CONTENT_URI,
projections ,
null,
null,
null);
String name = "";
if (c != null) {
int count = c.getCount();
c.moveToFirst();
do {
int is_user_profile = c.getInt(c.getColumnIndex(ContactsContract.Profile.IS_USER_PROFILE));
if (count == 1 || (count > 1 && is_user_profile == 1)) {
name = c.getString(c.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME_PRIMARY));
}
if (DEBUG_LOG) Log.e("[" + is_user_profile + "] - " + name);
break;
} while (c.moveToNext());
c.close();
}
return name;
}
@SuppressLint("NewApi")
public static Bitmap getUserProfile(Context context) {
if (Build.VERSION.SDK_INT < 14) return null;
if (!hasPermission(context, "android.permission.READ_PROFILE")) return null;
// Sets the columns to retrieve for the user profile
String[] projections = new String[] {
ContactsContract.Profile._ID,
ContactsContract.Profile.DISPLAY_NAME_PRIMARY,
ContactsContract.Profile.LOOKUP_KEY,
ContactsContract.Profile.PHOTO_THUMBNAIL_URI,
ContactsContract.Profile.IS_USER_PROFILE
};
// Retrieves the profile from the Contacts Provider
Cursor c = context.getContentResolver().query(
ContactsContract.Profile.CONTENT_URI,
projections ,
null,
null,
null);
Uri photoUri = null;
InputStream in = null;
if (c != null) {
int count = c.getCount();
c.moveToFirst();
do {
int is_user_profile = c.getInt(c.getColumnIndex(ContactsContract.Profile.IS_USER_PROFILE));
if (DEBUG_LOG) Log.e("[" + is_user_profile + "]" + c.getString(c.getColumnIndex(ContactsContract.Profile.DISPLAY_NAME_PRIMARY)) + ", " + c.getString(c.getColumnIndex(ContactsContract.Profile.PHOTO_THUMBNAIL_URI)));
if (count == 1 || (count > 1 && is_user_profile == 1)) {
photoUri = Uri.parse(c.getString(c.getColumnIndex(ContactsContract.Profile.PHOTO_THUMBNAIL_URI)));
}
} while (c.moveToNext());
c.close();
}
if (photoUri != null) {
try{
in = context.getContentResolver().openInputStream(photoUri);
}catch(FileNotFoundException e){
e.printStackTrace();
}
}
return in == null ? null : BitmapFactory.decodeStream(in);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment