diff options
author | Amith Yamasani <yamasani@google.com> | 2012-10-24 13:26:38 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2012-10-24 13:26:39 -0700 |
commit | 5749ce0903cad3160a5cf907af3a9a4cbc4b1d4e (patch) | |
tree | 8a19e4874afdcf06c9340decb1a59e750ff1783a /src/com | |
parent | e508a26e037c1b0a813575f1154da0ede7644979 (diff) | |
parent | 8d40fac706d206355f9a4be58744fb1a8cc3417d (diff) | |
download | packages_apps_Settings-5749ce0903cad3160a5cf907af3a9a4cbc4b1d4e.zip packages_apps_Settings-5749ce0903cad3160a5cf907af3a9a4cbc4b1d4e.tar.gz packages_apps_Settings-5749ce0903cad3160a5cf907af3a9a4cbc4b1d4e.tar.bz2 |
Merge "Use given name instead of full name for the initial population of User name." into jb-mr1-dev
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/android/settings/Utils.java | 81 | ||||
-rw-r--r-- | src/com/android/settings/users/ProfileUpdateReceiver.java | 2 | ||||
-rw-r--r-- | src/com/android/settings/users/UserSettings.java | 2 |
3 files changed, 73 insertions, 12 deletions
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java index 07f6716..6d76bed 100644 --- a/src/com/android/settings/Utils.java +++ b/src/com/android/settings/Utils.java @@ -20,6 +20,7 @@ import android.app.Activity; import android.app.ActivityManager; import android.app.AlertDialog; import android.app.Dialog; +import android.content.ContentResolver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -46,9 +47,13 @@ import android.preference.Preference; import android.preference.PreferenceActivity.Header; import android.preference.PreferenceFrameLayout; import android.preference.PreferenceGroup; +import android.provider.ContactsContract.CommonDataKinds; import android.provider.ContactsContract.CommonDataKinds.Phone; +import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.Contacts; +import android.provider.ContactsContract.Data; import android.provider.ContactsContract.Profile; +import android.provider.ContactsContract.RawContacts; import android.telephony.TelephonyManager; import android.text.TextUtils; import android.util.Log; @@ -487,22 +492,78 @@ public class Utils { return true; } - public static String getMeProfileName(Context context) { - Cursor cursor = context.getContentResolver().query( - Profile.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME}, - null, null, null); - if (cursor == null) { - return null; + public static String getMeProfileName(Context context, boolean full) { + if (full) { + return getProfileDisplayName(context); + } else { + return getShorterNameIfPossible(context); + } + } + + private static String getShorterNameIfPossible(Context context) { + final String given = getLocalProfileGivenName(context); + return !TextUtils.isEmpty(given) ? given : getProfileDisplayName(context); + } + + private static String getLocalProfileGivenName(Context context) { + final ContentResolver cr = context.getContentResolver(); + + // Find the raw contact ID for the local ME profile raw contact. + final long localRowProfileId; + final Cursor localRawProfile = cr.query( + Profile.CONTENT_RAW_CONTACTS_URI, + new String[] {RawContacts._ID}, + RawContacts.ACCOUNT_TYPE + " IS NULL AND " + + RawContacts.ACCOUNT_NAME + " IS NULL", + null, null); + if (localRawProfile == null) return null; + + try { + if (!localRawProfile.moveToFirst()) { + return null; + } + localRowProfileId = localRawProfile.getLong(0); + } finally { + localRawProfile.close(); + } + + // Find the structured name for the raw contact. + final Cursor structuredName = cr.query( + Profile.CONTENT_URI.buildUpon().appendPath(Contacts.Data.CONTENT_DIRECTORY).build(), + new String[] {CommonDataKinds.StructuredName.GIVEN_NAME, + CommonDataKinds.StructuredName.FAMILY_NAME}, + Data.RAW_CONTACT_ID + "=" + localRowProfileId, + null, null); + if (structuredName == null) return null; + + try { + if (!structuredName.moveToFirst()) { + return null; + } + String partialName = structuredName.getString(0); + if (TextUtils.isEmpty(partialName)) { + partialName = structuredName.getString(1); + } + return partialName; + } finally { + structuredName.close(); } + } + + private static final String getProfileDisplayName(Context context) { + final ContentResolver cr = context.getContentResolver(); + final Cursor profile = cr.query(Profile.CONTENT_URI, + new String[] {Profile.DISPLAY_NAME}, null, null, null); + if (profile == null) return null; try { - if (cursor.moveToFirst()) { - return cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME)); + if (!profile.moveToFirst()) { + return null; } + return profile.getString(0); } finally { - cursor.close(); + profile.close(); } - return null; } /** Not global warming, it's global change warning. */ diff --git a/src/com/android/settings/users/ProfileUpdateReceiver.java b/src/com/android/settings/users/ProfileUpdateReceiver.java index 88daa58..955ee4d 100644 --- a/src/com/android/settings/users/ProfileUpdateReceiver.java +++ b/src/com/android/settings/users/ProfileUpdateReceiver.java @@ -53,7 +53,7 @@ public class ProfileUpdateReceiver extends BroadcastReceiver { int userId = UserHandle.myUserId(); UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE); - String profileName = Utils.getMeProfileName(context); + String profileName = Utils.getMeProfileName(context, false /* partial name */); if (profileName != null && profileName.length() > 0) { um.setUserName(userId, profileName); // Flag that we've written the profile one time at least. No need to do it in the future. diff --git a/src/com/android/settings/users/UserSettings.java b/src/com/android/settings/users/UserSettings.java index 984ac08..632dfe5 100644 --- a/src/com/android/settings/users/UserSettings.java +++ b/src/com/android/settings/users/UserSettings.java @@ -503,7 +503,7 @@ public class UserSettings extends SettingsPreferenceFragment } private String getProfileName() { - String name = Utils.getMeProfileName(getActivity()); + String name = Utils.getMeProfileName(getActivity(), true); if (name != null) { mProfileExists = true; } |