Use given name instead of full name for the initial population of User name.
Bug: 7406217 Change-Id: I5006039ef146369f569445d0fcbaf82fd163343e
This commit is contained in:
@@ -20,6 +20,7 @@ import android.app.Activity;
|
|||||||
import android.app.ActivityManager;
|
import android.app.ActivityManager;
|
||||||
import android.app.AlertDialog;
|
import android.app.AlertDialog;
|
||||||
import android.app.Dialog;
|
import android.app.Dialog;
|
||||||
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -46,9 +47,13 @@ import android.preference.Preference;
|
|||||||
import android.preference.PreferenceActivity.Header;
|
import android.preference.PreferenceActivity.Header;
|
||||||
import android.preference.PreferenceFrameLayout;
|
import android.preference.PreferenceFrameLayout;
|
||||||
import android.preference.PreferenceGroup;
|
import android.preference.PreferenceGroup;
|
||||||
|
import android.provider.ContactsContract.CommonDataKinds;
|
||||||
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
import android.provider.ContactsContract.CommonDataKinds.Phone;
|
||||||
|
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
|
||||||
import android.provider.ContactsContract.Contacts;
|
import android.provider.ContactsContract.Contacts;
|
||||||
|
import android.provider.ContactsContract.Data;
|
||||||
import android.provider.ContactsContract.Profile;
|
import android.provider.ContactsContract.Profile;
|
||||||
|
import android.provider.ContactsContract.RawContacts;
|
||||||
import android.telephony.TelephonyManager;
|
import android.telephony.TelephonyManager;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
@@ -487,23 +492,79 @@ public class Utils {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getMeProfileName(Context context) {
|
public static String getMeProfileName(Context context, boolean full) {
|
||||||
Cursor cursor = context.getContentResolver().query(
|
if (full) {
|
||||||
Profile.CONTENT_URI, new String[] {Phone._ID, Phone.DISPLAY_NAME},
|
return getProfileDisplayName(context);
|
||||||
null, null, null);
|
} else {
|
||||||
if (cursor == null) {
|
return getShorterNameIfPossible(context);
|
||||||
return null;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
try {
|
||||||
if (cursor.moveToFirst()) {
|
if (!localRawProfile.moveToFirst()) {
|
||||||
return cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
return null;
|
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 (!profile.moveToFirst()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return profile.getString(0);
|
||||||
|
} finally {
|
||||||
|
profile.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** Not global warming, it's global change warning. */
|
/** Not global warming, it's global change warning. */
|
||||||
public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId,
|
public static Dialog buildGlobalChangeWarningDialog(final Context context, int titleResId,
|
||||||
|
@@ -53,7 +53,7 @@ public class ProfileUpdateReceiver extends BroadcastReceiver {
|
|||||||
|
|
||||||
int userId = UserHandle.myUserId();
|
int userId = UserHandle.myUserId();
|
||||||
UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
|
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) {
|
if (profileName != null && profileName.length() > 0) {
|
||||||
um.setUserName(userId, profileName);
|
um.setUserName(userId, profileName);
|
||||||
// Flag that we've written the profile one time at least. No need to do it in the future.
|
// Flag that we've written the profile one time at least. No need to do it in the future.
|
||||||
|
@@ -503,7 +503,7 @@ public class UserSettings extends SettingsPreferenceFragment
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String getProfileName() {
|
private String getProfileName() {
|
||||||
String name = Utils.getMeProfileName(getActivity());
|
String name = Utils.getMeProfileName(getActivity(), true);
|
||||||
if (name != null) {
|
if (name != null) {
|
||||||
mProfileExists = true;
|
mProfileExists = true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user