use of com.android.contacts.common.model.RawContactDeltaList in project packages_apps_Contacts by AOKP.
the class CompactRawContactsEditorView method getRawContactDeltaListForSelector.
private RawContactDeltaList getRawContactDeltaListForSelector(RawContactDeltaList rawContactDeltas) {
// Sort raw contacts so google accounts come first
Collections.sort(rawContactDeltas, new RawContactDeltaComparator(getContext()));
final RawContactDeltaList result = new RawContactDeltaList();
for (RawContactDelta rawContactDelta : rawContactDeltas) {
if (rawContactDelta.isVisible() && rawContactDelta.getRawContactId() > 0) {
// Only add raw contacts that can be opened in the editor
result.add(rawContactDelta);
}
}
// in the compact editor in the full editor
if (result.size() == 1 && result.get(0).getRawContactAccountType(getContext()).areContactsWritable()) {
result.clear();
return result;
}
return result;
}
use of com.android.contacts.common.model.RawContactDeltaList in project packages_apps_Contacts by AOKP.
the class ContactEditorBaseFragment method onSaveCompleted.
@Override
public void onSaveCompleted(boolean hadChanges, int saveMode, boolean saveSucceeded, Uri contactLookupUri, Long joinContactId, int result) {
Log.d(TAG, "onSaveCompleted(" + saveMode + ", " + contactLookupUri + ", saveResult:" + result);
if (hadChanges) {
if (saveSucceeded) {
switch(saveMode) {
case SaveMode.JOIN:
break;
case SaveMode.SPLIT:
Toast.makeText(mContext, R.string.contactUnlinkedToast, Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(mContext, R.string.contactSavedToast, Toast.LENGTH_SHORT).show();
}
} else {
switch(result) {
case ContactSaveService.RESULT_NUMBER_INVALID:
{
Toast.makeText(mContext, R.string.invalid_phone_number, Toast.LENGTH_LONG).show();
break;
}
case ContactSaveService.RESULT_SIM_FAILURE:
{
Toast.makeText(mContext, R.string.contactSavedToSimCardError, Toast.LENGTH_LONG).show();
break;
}
case ContactSaveService.RESULT_NUMBER_ANR_FAILURE:
{
Toast.makeText(mContext, R.string.number_anr_too_long, Toast.LENGTH_LONG).show();
break;
}
case ContactSaveService.RESULT_EMAIL_FAILURE:
{
Toast.makeText(mContext, R.string.email_address_too_long, Toast.LENGTH_LONG).show();
break;
}
case ContactSaveService.RESULT_SIM_FULL_FAILURE:
{
Toast.makeText(mContext, R.string.sim_card_full, Toast.LENGTH_LONG).show();
break;
}
case ContactSaveService.RESULT_TAG_FAILURE:
{
Toast.makeText(mContext, R.string.tag_too_long, Toast.LENGTH_SHORT).show();
break;
}
case ContactSaveService.RESULT_NO_NUMBER_AND_EMAIL:
{
Toast.makeText(mContext, R.string.no_phone_number_or_email, Toast.LENGTH_SHORT).show();
break;
}
default:
{
Toast.makeText(mContext, R.string.contactSavedErrorToast, Toast.LENGTH_LONG).show();
break;
}
}
}
}
switch(saveMode) {
case SaveMode.CLOSE:
{
final Intent resultIntent;
if (saveSucceeded && contactLookupUri != null) {
final Uri lookupUri = maybeConvertToLegacyLookupUri(mContext, contactLookupUri, mLookupUri);
resultIntent = ImplicitIntentsUtil.composeQuickContactIntent(lookupUri, QuickContactActivity.MODE_FULLY_EXPANDED);
resultIntent.putExtra(QuickContactActivity.EXTRA_PREVIOUS_SCREEN_TYPE, ScreenType.EDITOR);
} else {
resultIntent = null;
}
// It is already saved, so prevent it from being saved again
mStatus = Status.CLOSING;
if (mListener != null)
mListener.onSaveFinished(resultIntent);
break;
}
case SaveMode.COMPACT:
{
// It is already saved, so prevent it from being saved again
mStatus = Status.CLOSING;
if (mListener != null)
mListener.onSaveFinished(/* resultIntent= */
null);
break;
}
case SaveMode.JOIN:
if (saveSucceeded && contactLookupUri != null && joinContactId != null) {
joinAggregate(joinContactId);
}
break;
case SaveMode.RELOAD:
if (saveSucceeded && contactLookupUri != null) {
// If this was in INSERT, we are changing into an EDIT now.
// If it already was an EDIT, we are changing to the new Uri now
mState = new RawContactDeltaList();
load(Intent.ACTION_EDIT, contactLookupUri, null);
mStatus = Status.LOADING;
getLoaderManager().restartLoader(LOADER_CONTACT, null, mContactLoaderListener);
}
break;
case SaveMode.SPLIT:
mStatus = Status.CLOSING;
if (mListener != null) {
mListener.onContactSplit(contactLookupUri);
} else {
Log.d(TAG, "No listener registered, can not call onSplitFinished");
}
break;
}
}
use of com.android.contacts.common.model.RawContactDeltaList in project packages_apps_Contacts by AOKP.
the class ContactEditorBaseFragment method rebindEditorsForNewContact.
/**
* Removes a current editor ({@link #mState}) and rebinds new editor for a new account.
* Some of old data are reused with new restriction enforced by the new account.
*
* @param oldState Old data being edited.
* @param oldAccount Old account associated with oldState.
* @param newAccount New account to be used.
*/
protected void rebindEditorsForNewContact(RawContactDelta oldState, AccountWithDataSet oldAccount, AccountWithDataSet newAccount) {
AccountTypeManager accountTypes = AccountTypeManager.getInstance(mContext);
AccountType oldAccountType = accountTypes.getAccountTypeForAccount(oldAccount);
AccountType newAccountType = accountTypes.getAccountTypeForAccount(newAccount);
if (newAccountType.getCreateContactActivityClassName() != null) {
Log.w(TAG, "external activity called in rebind situation");
if (mListener != null) {
mListener.onCustomCreateContactActivityRequested(newAccount, mIntentExtras);
}
} else {
mExistingContactDataReady = false;
mNewContactDataReady = false;
mState = new RawContactDeltaList();
setStateForNewContact(newAccount, newAccountType, oldState, oldAccountType, isEditingUserProfile());
if (mIsEdit) {
setStateForExistingContact(mReadOnlyDisplayName, isEditingUserProfile(), mRawContacts);
}
}
}
use of com.android.contacts.common.model.RawContactDeltaList in project packages_apps_Contacts by AOKP.
the class InvisibleContactUtil method addToDefaultGroup.
public static void addToDefaultGroup(Contact contactData, Context context) {
final long defaultGroupId = getDefaultGroupId(contactData.getGroupMetaData());
// but let's be safe here
if (defaultGroupId == -1)
return;
// add the group membership to the current state
final RawContactDeltaList contactDeltaList = contactData.createRawContactDeltaList();
final RawContactDelta rawContactEntityDelta = contactDeltaList.get(0);
final AccountTypeManager accountTypes = AccountTypeManager.getInstance(context);
final AccountType type = rawContactEntityDelta.getAccountType(accountTypes);
final DataKind groupMembershipKind = type.getKindForMimetype(GroupMembership.CONTENT_ITEM_TYPE);
final ValuesDelta entry = RawContactModifier.insertChild(rawContactEntityDelta, groupMembershipKind);
if (entry == null)
return;
entry.setGroupRowId(defaultGroupId);
// and fire off the intent. we don't need a callback, as the database listener
// should update the ui
final Intent intent = ContactSaveService.createSaveContactIntent(context, contactDeltaList, "", 0, false, QuickContactActivity.class, Intent.ACTION_VIEW, null, /* joinContactIdExtraKey =*/
null, /* joinContactId =*/
null);
ContactSaveService.startService(context, intent);
}
use of com.android.contacts.common.model.RawContactDeltaList in project packages_apps_Contacts by AOKP.
the class ContactSaveService method saveContact.
private void saveContact(Intent intent) {
RawContactDeltaList state = intent.getParcelableExtra(EXTRA_CONTACT_STATE);
boolean isProfile = intent.getBooleanExtra(EXTRA_SAVE_IS_PROFILE, false);
Bundle updatedPhotos = intent.getParcelableExtra(EXTRA_UPDATED_PHOTOS);
if (state == null) {
Log.e(TAG, "Invalid arguments for saveContact request");
return;
}
int saveMode = intent.getIntExtra(EXTRA_SAVE_MODE, -1);
// Trim any empty fields, and RawContacts, before persisting
final AccountTypeManager accountTypes = AccountTypeManager.getInstance(this);
RawContactModifier.trimEmpty(state, accountTypes);
Uri lookupUri = null;
final ContentResolver resolver = getContentResolver();
boolean succeeded = false;
// Keep track of the id of a newly raw-contact (if any... there can be at most one).
long insertedRawContactId = -1;
// Attempt to persist changes
Integer result = RESULT_FAILURE;
ArrayList<Long> rawContactsList = new ArrayList<Long>();
boolean isCardOperation = false;
for (int i = 0; i < state.size(); i++) {
final RawContactDelta entity = state.get(i);
final String accountType = entity.getValues().getAsString(RawContacts.ACCOUNT_TYPE);
final String accountName = entity.getValues().getAsString(RawContacts.ACCOUNT_NAME);
rawContactsList.add(entity.getRawContactId());
if (accountType != null && ExchangeAccountType.isExchangeType(accountType))
removeDisplayName(entity);
final int subscription = MoreContactUtils.getSubscription(accountType, accountName);
isCardOperation = (subscription != SubscriptionManager.INVALID_SUBSCRIPTION_ID) ? true : false;
if (isCardOperation) {
result = doSaveToSimCard(entity, resolver, subscription);
Log.d(TAG, "doSaveToSimCard result is " + result);
}
}
int tries = 0;
while (tries++ < PERSIST_TRIES) {
if (result == RESULT_SUCCESS || result == RESULT_FAILURE) {
try {
// Build operations and try applying
final ArrayList<CPOWrapper> diffWrapper = state.buildDiffWrapper();
final ArrayList<ContentProviderOperation> diff = Lists.newArrayList();
for (CPOWrapper cpoWrapper : diffWrapper) {
diff.add(cpoWrapper.getOperation());
}
if (DEBUG) {
Log.v(TAG, "Content Provider Operations:");
for (ContentProviderOperation operation : diff) {
Log.v(TAG, operation.toString());
}
}
int numberProcessed = 0;
boolean batchFailed = false;
final ContentProviderResult[] results = new ContentProviderResult[diff.size()];
while (numberProcessed < diff.size()) {
final int subsetCount = applyDiffSubset(diff, numberProcessed, results, resolver);
if (subsetCount == -1) {
Log.w(TAG, "Resolver.applyBatch failed in saveContacts");
batchFailed = true;
break;
} else {
numberProcessed += subsetCount;
}
}
if (batchFailed) {
// Retry save
continue;
}
final long rawContactId = getRawContactId(state, diffWrapper, results);
if (rawContactId == -1) {
throw new IllegalStateException("Could not determine RawContact ID after save");
}
// We don't have to check to see if the value is still -1. If we reach here,
// the previous loop iteration didn't succeed, so any ID that we obtained is bogus.
insertedRawContactId = getInsertedRawContactId(diffWrapper, results);
if (isProfile) {
// Since the profile supports local raw contacts, which may have been completely
// removed if all information was removed, we need to do a special query to
// get the lookup URI for the profile contact (if it still exists).
Cursor c = resolver.query(Profile.CONTENT_URI, new String[] { Contacts._ID, Contacts.LOOKUP_KEY }, null, null, null);
if (c == null) {
continue;
}
try {
if (c.moveToFirst()) {
final long contactId = c.getLong(0);
final String lookupKey = c.getString(1);
lookupUri = Contacts.getLookupUri(contactId, lookupKey);
}
} finally {
c.close();
}
} else {
final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
lookupUri = RawContacts.getContactLookupUri(resolver, rawContactUri);
}
if (lookupUri != null) {
Log.v(TAG, "Saved contact. New URI: " + lookupUri);
}
// We can change this back to false later, if we fail to save the contact photo.
succeeded = true;
break;
} catch (RemoteException e) {
// Something went wrong, bail without success
Log.e(TAG, "Problem persisting user edits", e);
break;
} catch (IllegalArgumentException e) {
// This is thrown by applyBatch on malformed requests
Log.e(TAG, "Problem persisting user edits", e);
showToast(R.string.contactSavedErrorToast);
break;
} catch (OperationApplicationException e) {
// Version consistency failed, re-parent change and try again
Log.w(TAG, "Version consistency failed, re-parenting: " + e.toString());
final StringBuilder sb = new StringBuilder(RawContacts._ID + " IN(");
boolean first = true;
final int count = state.size();
for (int i = 0; i < count; i++) {
Long rawContactId = state.getRawContactId(i);
if (rawContactId != null && rawContactId != -1) {
if (!first) {
sb.append(',');
}
sb.append(rawContactId);
first = false;
}
}
sb.append(")");
if (first) {
throw new IllegalStateException("Version consistency failed for a new contact", e);
}
final RawContactDeltaList newState = RawContactDeltaList.fromQuery(isProfile ? RawContactsEntity.PROFILE_CONTENT_URI : RawContactsEntity.CONTENT_URI, resolver, sb.toString(), null, null);
state = RawContactDeltaList.mergeAfter(newState, state);
// Update the new state to use profile URIs if appropriate.
if (isProfile) {
for (RawContactDelta delta : state) {
delta.setProfileQueryUri();
}
}
}
}
}
// the ContactProvider already knows about newly-created contacts.
if (updatedPhotos != null) {
for (String key : updatedPhotos.keySet()) {
Uri photoUri = updatedPhotos.getParcelable(key);
long rawContactId = Long.parseLong(key);
// replace the bogus ID with the new one that we actually saved the contact at.
if (rawContactId < 0) {
rawContactId = insertedRawContactId;
}
// If the save failed, insertedRawContactId will be -1
if (rawContactId < 0 || !saveUpdatedPhoto(rawContactId, photoUri, saveMode)) {
succeeded = false;
}
}
}
Intent callbackIntent = intent.getParcelableExtra(EXTRA_CALLBACK_INTENT);
if (callbackIntent != null) {
if (succeeded) {
// Mark the intent to indicate that the save was successful (even if the lookup URI
// is now null). For local contacts or the local profile, it's possible that the
// save triggered removal of the contact, so no lookup URI would exist..
callbackIntent.putExtra(EXTRA_SAVE_SUCCEEDED, true);
}
callbackIntent.setData(lookupUri);
deliverCallback(callbackIntent);
}
}
Aggregations