コード例 #1
0
  /**
   * Retrieves the user profile information in a manner supported by Ice Cream Sandwich devices.
   *
   * @param context the context from which to retrieve the user's email address and name
   * @return a list of the possible user's email address and name
   */
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  private static UserProfile getUserProfileOnIcsDevice(Context context) throws Exception {
    final ContentResolver content = context.getContentResolver();
    final Cursor cursor =
        content.query(
            // Retrieves data rows for the device user's 'profile' contact
            Uri.withAppendedPath(
                ContactsContract.Profile.CONTENT_URI,
                ContactsContract.Contacts.Data.CONTENT_DIRECTORY),
            ProfileQuery.PROJECTION,

            // Selects only email addresses or names
            ContactsContract.Contacts.Data.MIMETYPE
                + "=? OR "
                + ContactsContract.Contacts.Data.MIMETYPE
                + "=? OR "
                + ContactsContract.Contacts.Data.MIMETYPE
                + "=?"
                + " OR "
                + ContactsContract.Contacts.Data.MIMETYPE
                + "=?",
            new String[] {
              ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
              ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE,
              ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE,
              ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
            },

            // Show primary rows first. Note that there won't be a primary
            // email address if the
            // user hasn't specified one.
            ContactsContract.Contacts.Data.IS_PRIMARY + " DESC");

    UserProfile userProfile = getUserProfileOnGingerbreadDevice(context);
    String mime_type;
    while (cursor != null && cursor.moveToNext()) {
      mime_type = cursor.getString(ProfileQuery.MIME_TYPE);
      if (mime_type.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE))
        userProfile.addPossibleEmail(
            cursor.getString(ProfileQuery.EMAIL), cursor.getInt(ProfileQuery.IS_PRIMARY_EMAIL) > 0);
      else if (mime_type.equals(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE))
        userProfile.addPossibleName(
            cursor.getString(ProfileQuery.GIVEN_NAME)
                + " "
                + cursor.getString(ProfileQuery.FAMILY_NAME));
      else if (mime_type.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE))
        userProfile.addPossiblePhoneNumber(
            cursor.getString(ProfileQuery.PHONE_NUMBER),
            cursor.getInt(ProfileQuery.IS_PRIMARY_PHONE_NUMBER) > 0);
      else if (mime_type.equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE))
        userProfile.addPossiblePhoto(Uri.parse(cursor.getString(ProfileQuery.PHOTO)));
    }

    if (cursor != null) {
      cursor.close();
    }

    return userProfile;
  }
コード例 #2
0
  /**
   * Retrieves the user profile information in a manner supported by Gingerbread devices.
   *
   * @param context the context from which to retrieve the user's email address and name
   * @return a list of the possible user's email address and name
   */
  private static UserProfile getUserProfileOnGingerbreadDevice(Context context) {
    // Other that using Patterns (API level 8) this works on devices down to
    // API level 5
    final Matcher valid_email_address = Patterns.EMAIL_ADDRESS.matcher("");
    final Account[] accounts =
        AccountManager.get(context).getAccountsByType(GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
    UserProfile user_profile = new UserProfile();
    // As far as I can tell, there is no way to get the real name or phone
    // number from the Google account
    for (Account account : accounts) {
      if (valid_email_address.reset(account.name).matches())
        user_profile.addPossibleEmail(account.name);
    }
    // Gets the phone number of the device is the device has one
    if (context.getPackageManager().hasSystemFeature(Context.TELEPHONY_SERVICE)) {
      final TelephonyManager telephony =
          (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
      user_profile.addPossiblePhoneNumber(telephony.getLine1Number());
    }

    return user_profile;
  }