@Override
    protected Integer doInBackground(RawContactDeltaList... params) {
      final Context context = activityTarget;
      final ContentResolver resolver = context.getContentResolver();

      RawContactDeltaList state = params[0];

      if (state == null) {
        return RESULT_FAILURE;
      }

      // Trim any empty fields, and RawContacts, before persisting
      RawContactModifier.trimEmpty(state, mAccountTypeManager);

      // Attempt to persist changes
      int tries = 0;
      Integer result = RESULT_FAILURE;
      while (tries++ < PERSIST_TRIES) {
        try {
          // Build operations and try applying
          // Note: In case we've created a new raw_contact because the selected contact
          // is read-only, buildDiff() will create aggregation exceptions to join
          // the new one to the existing contact.
          final ArrayList<ContentProviderOperation> diff = state.buildDiff();
          ContentProviderResult[] results = null;
          if (!diff.isEmpty()) {
            results = resolver.applyBatch(ContactsContract.AUTHORITY, diff);
          }

          result = (diff.size() > 0) ? RESULT_SUCCESS : RESULT_UNCHANGED;
          break;

        } catch (RemoteException e) {
          // Something went wrong, bail without success
          Log.e(TAG, "Problem persisting user edits", e);
          break;

        } catch (OperationApplicationException e) {
          // Version consistency failed, bail without success
          Log.e(TAG, "Version consistency failed", e);
          break;
        }
      }

      return result;
    }
    @Override
    protected void onPostExecute(RawContactDeltaList entityList) {
      if (activityTarget.isFinishing()) {
        return;
      }
      if ((entityList == null) || (entityList.size() == 0)) {
        Log.e(TAG, "Contact not found.");
        activityTarget.finish();
        return;
      }

      activityTarget.setEntityDeltaList(entityList);
    }
  private void setEntityDeltaList(RawContactDeltaList entityList) {
    if (entityList == null) {
      throw new IllegalStateException();
    }
    if (VERBOSE_LOGGING) {
      Log.v(TAG, "setEntityDeltaList: " + entityList);
    }

    mEntityDeltaList = entityList;

    // Find the editable raw_contact.
    mRawContactDelta = mEntityDeltaList.getFirstWritableRawContact(this);

    // If no editable raw_contacts are found, create one.
    if (mRawContactDelta == null) {
      mRawContactDelta = addEditableRawContact(this, mEntityDeltaList);

      if ((mRawContactDelta != null) && VERBOSE_LOGGING) {
        Log.v(TAG, "setEntityDeltaList: created editable raw_contact " + entityList);
      }
    }

    if (mRawContactDelta == null) {
      // Selected contact is read-only, and there's no editable account.
      mIsReadOnly = true;
      mEditableAccountType = null;
    } else {
      mIsReadOnly = false;

      mEditableAccountType = mRawContactDelta.getRawContactAccountType(this);

      // Handle any incoming values that should be inserted
      final Bundle extras = getIntent().getExtras();
      if (extras != null && extras.size() > 0) {
        // If there are any intent extras, add them as additional fields in the
        // RawContactDelta.
        RawContactModifier.parseExtras(this, mEditableAccountType, mRawContactDelta, extras);
      }
    }

    bindEditor();
  }
  /**
   * Create an {@link RawContactDelta} for a raw_contact on the first editable account found, and
   * add to the list. Also copy the structured name from an existing (read-only) raw_contact to the
   * new one, if any of the read-only contacts has a name.
   */
  private static RawContactDelta addEditableRawContact(
      Context context, RawContactDeltaList entityDeltaList) {
    // First, see if there's an editable account.
    final AccountTypeManager accounts = AccountTypeManager.getInstance(context);
    final List<AccountWithDataSet> editableAccounts = accounts.getAccounts(true);
    if (editableAccounts.size() == 0) {
      // No editable account type found.  The dialog will be read-only mode.
      return null;
    }
    final AccountWithDataSet editableAccount = editableAccounts.get(0);
    final AccountType accountType =
        accounts.getAccountType(editableAccount.type, editableAccount.dataSet);

    // Create a new RawContactDelta for the new raw_contact.
    final RawContact rawContact = new RawContact(context);
    rawContact.setAccount(editableAccount);

    final RawContactDelta entityDelta =
        new RawContactDelta(ValuesDelta.fromAfter(rawContact.getValues()));

    // Then, copy the structure name from an existing (read-only) raw_contact.
    for (RawContactDelta entity : entityDeltaList) {
      final ArrayList<ValuesDelta> readOnlyNames =
          entity.getMimeEntries(StructuredName.CONTENT_ITEM_TYPE);
      if ((readOnlyNames != null) && (readOnlyNames.size() > 0)) {
        final ValuesDelta readOnlyName = readOnlyNames.get(0);
        final ValuesDelta newName =
            RawContactModifier.ensureKindExists(
                entityDelta, accountType, StructuredName.CONTENT_ITEM_TYPE);

        // Copy all the data fields.
        newName.copyStructuredNameFieldsFrom(readOnlyName);
        break;
      }
    }

    // Add the new RawContactDelta to the list.
    entityDeltaList.add(entityDelta);

    return entityDelta;
  }
    @Override
    protected RawContactDeltaList doInBackground(Intent... params) {

      final Intent intent = params[0];

      final ContentResolver resolver = activityTarget.getContentResolver();

      // Handle both legacy and new authorities
      final Uri data = intent.getData();
      final String authority = data.getAuthority();
      final String mimeType = intent.resolveType(resolver);

      mSelection = "0";
      String selectionArg = null;
      if (ContactsContract.AUTHORITY.equals(authority)) {
        if (Contacts.CONTENT_ITEM_TYPE.equals(mimeType)) {
          // Handle selected aggregate
          final long contactId = ContentUris.parseId(data);
          selectionArg = String.valueOf(contactId);
          mSelection = RawContacts.CONTACT_ID + "=?";
        } else if (RawContacts.CONTENT_ITEM_TYPE.equals(mimeType)) {
          final long rawContactId = ContentUris.parseId(data);
          final long contactId = queryForContactId(resolver, rawContactId);
          selectionArg = String.valueOf(contactId);
          mSelection = RawContacts.CONTACT_ID + "=?";
        }
      } else if (android.provider.Contacts.AUTHORITY.equals(authority)) {
        final long rawContactId = ContentUris.parseId(data);
        selectionArg = String.valueOf(rawContactId);
        mSelection = Data.RAW_CONTACT_ID + "=?";
      }

      // Note that this query does not need to concern itself with whether the contact is
      // the user's profile, since the profile does not show up in the picker.
      return RawContactDeltaList.fromQuery(
          RawContactsEntity.CONTENT_URI,
          activityTarget.getContentResolver(),
          mSelection,
          new String[] {selectionArg},
          null);
    }