Search in sources :

Example 6 with RawContact

use of com.android.contacts.common.model.RawContact in project packages_apps_Contacts by AOKP.

the class QuickContactActivity method copyToCard.

private void copyToCard(final int sub) {
    final int MSG_COPY_DONE = 0;
    final int MSG_COPY_FAILURE = 1;
    final int MSG_CARD_NO_SPACE = 2;
    final int MSG_NO_EMPTY_EMAIL = 3;
    if (mHandler == null) {
        mHandler = new Handler() {

            public void handleMessage(Message msg) {
                switch(msg.what) {
                    case MSG_COPY_DONE:
                        Toast.makeText(QuickContactActivity.this, R.string.copy_done, Toast.LENGTH_SHORT).show();
                        break;
                    case MSG_COPY_FAILURE:
                        Toast.makeText(QuickContactActivity.this, R.string.copy_failure, Toast.LENGTH_SHORT).show();
                        break;
                    case MSG_CARD_NO_SPACE:
                        Toast.makeText(QuickContactActivity.this, R.string.card_no_space, Toast.LENGTH_SHORT).show();
                        break;
                    case MSG_NO_EMPTY_EMAIL:
                        Toast.makeText(QuickContactActivity.this, R.string.no_empty_email_in_usim, Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };
    }
    new Thread(new Runnable() {

        public void run() {
            synchronized (this) {
                int adnCountInSimContact = 1;
                int anrCountInSimContact = 0;
                int emailCountInSimContact = 0;
                Cursor cr = null;
                // call query first, otherwise the count queries will fail
                try {
                    int subId = MoreContactUtils.getActiveSubId(QuickContactActivity.this, sub);
                    if (subId >= 0 && tm.getPhoneCount() > 1) {
                        cr = getContentResolver().query(Uri.parse(SimContactsConstants.SIM_SUB_URI + subId), null, null, null, null);
                    } else {
                        cr = getContentResolver().query(Uri.parse(SimContactsConstants.SIM_URI), null, null, null, null);
                    }
                } catch (NullPointerException e) {
                    Log.e(TAG, "Exception:" + e);
                } finally {
                    if (cr != null) {
                        cr.close();
                    }
                }
                if (MoreContactUtils.canSaveAnr(QuickContactActivity.this, sub)) {
                    anrCountInSimContact = MoreContactUtils.getOneSimAnrCount(QuickContactActivity.this, sub);
                }
                if (MoreContactUtils.canSaveEmail(QuickContactActivity.this, sub)) {
                    emailCountInSimContact = MoreContactUtils.getOneSimEmailCount(QuickContactActivity.this, sub);
                }
                int totalEmptyAdn = MoreContactUtils.getSimFreeCount(QuickContactActivity.this, sub);
                int totalEmptyAnr = MoreContactUtils.getSpareAnrCount(QuickContactActivity.this, sub);
                int totalEmptyEmail = MoreContactUtils.getSpareEmailCount(QuickContactActivity.this, sub);
                Message msg = Message.obtain();
                if (totalEmptyAdn <= 0) {
                    msg.what = MSG_CARD_NO_SPACE;
                    mHandler.sendMessage(msg);
                    return;
                }
                // to indiacate how many number in one ADN can saved to SIM card,
                // 1 means can only save one number,2,3 ... means can save anr
                int numEntitySize = adnCountInSimContact + anrCountInSimContact;
                // empty number is equals to the sum of adn and anr
                int emptyNumTotal = totalEmptyAdn + totalEmptyAnr;
                // Get name string
                String strName = mContactData.getDisplayName();
                ArrayList<String> arrayNumber = new ArrayList<String>();
                ArrayList<String> arrayEmail = new ArrayList<String>();
                for (RawContact rawContact : mContactData.getRawContacts()) {
                    for (DataItem dataItem : rawContact.getDataItems()) {
                        if (dataItem.getMimeType() == null) {
                            continue;
                        }
                        if (dataItem instanceof PhoneDataItem) {
                            // Get phone string
                            PhoneDataItem phoneNum = (PhoneDataItem) dataItem;
                            final String number = phoneNum.getNumber();
                            if (!TextUtils.isEmpty(number) && emptyNumTotal-- > 0) {
                                arrayNumber.add(number);
                            }
                        } else if (dataItem instanceof EmailDataItem) {
                            // Get email string
                            EmailDataItem emailData = (EmailDataItem) dataItem;
                            final String address = emailData.getData();
                            if (!TextUtils.isEmpty(address) && totalEmptyEmail-- > 0) {
                                arrayEmail.add(address);
                            }
                        }
                    }
                }
                // calculate how many ADN needed according to the number,name,phone,email,
                // then uses the max of them
                int nameCount = (strName != null && !strName.equals("")) ? 1 : 0;
                int groupNumCount = (arrayNumber.size() % numEntitySize) != 0 ? (arrayNumber.size() / numEntitySize + 1) : (arrayNumber.size() / numEntitySize);
                int groupEmailCount = emailCountInSimContact == 0 ? 0 : ((arrayEmail.size() % emailCountInSimContact) != 0 ? (arrayEmail.size() / emailCountInSimContact + 1) : (arrayEmail.size() / emailCountInSimContact));
                int groupCount = Math.max(groupEmailCount, Math.max(nameCount, groupNumCount));
                ArrayList<UsimEntity> results = new ArrayList<UsimEntity>();
                for (int i = 0; i < groupCount; i++) {
                    results.add(new UsimEntity());
                }
                UsimEntity value;
                // get the phone number for each ADN from arrayNumber,put them in UsimEntity
                for (int i = 0; i < groupNumCount; i++) {
                    value = results.get(i);
                    ArrayList<String> numberItem = new ArrayList<String>();
                    for (int j = 0; j < numEntitySize; j++) {
                        if ((i * numEntitySize + j) < arrayNumber.size()) {
                            numberItem.add(arrayNumber.get(i * numEntitySize + j));
                        }
                    }
                    value.putNumberList(numberItem);
                }
                for (int i = 0; i < groupEmailCount; i++) {
                    value = results.get(i);
                    ArrayList<String> emailItem = new ArrayList<String>();
                    for (int j = 0; j < emailCountInSimContact; j++) {
                        if ((i * emailCountInSimContact + j) < arrayEmail.size()) {
                            emailItem.add(arrayEmail.get(i * emailCountInSimContact + j));
                        }
                    }
                    value.putEmailList(emailItem);
                }
                ArrayList<String> emptyList = new ArrayList<String>();
                Uri itemUri = null;
                if (totalEmptyEmail < 0 && MoreContactUtils.canSaveEmail(QuickContactActivity.this, sub)) {
                    Message e_msg = Message.obtain();
                    e_msg.what = MSG_NO_EMPTY_EMAIL;
                    mHandler.sendMessage(e_msg);
                }
                // get phone number from UsimEntity,then insert to SIM card
                for (int i = 0; i < groupCount; i++) {
                    value = results.get(i);
                    if (value.containsNumber()) {
                        arrayNumber = (ArrayList<String>) value.getNumberList();
                    } else {
                        arrayNumber = emptyList;
                    }
                    if (value.containsEmail()) {
                        arrayEmail = (ArrayList<String>) value.getEmailList();
                    } else {
                        arrayEmail = emptyList;
                    }
                    String strNum = arrayNumber.size() > 0 ? arrayNumber.get(0) : null;
                    StringBuilder strAnrNum = new StringBuilder();
                    for (int j = 1; j < arrayNumber.size(); j++) {
                        String s = arrayNumber.get(j);
                        strAnrNum.append(s);
                        strAnrNum.append(SimContactsConstants.ANR_SEP);
                    }
                    StringBuilder strEmail = new StringBuilder();
                    for (int j = 0; j < arrayEmail.size(); j++) {
                        String s = arrayEmail.get(j);
                        strEmail.append(s);
                        strEmail.append(SimContactsConstants.EMAIL_SEP);
                    }
                    itemUri = MoreContactUtils.insertToCard(QuickContactActivity.this, strName, strNum, strEmail.toString(), strAnrNum.toString(), sub);
                }
                if (itemUri != null) {
                    msg.what = MSG_COPY_DONE;
                    mHandler.sendMessage(msg);
                } else {
                    msg.what = MSG_COPY_FAILURE;
                    mHandler.sendMessage(msg);
                }
            }
        }
    }).start();
}
Also used : Message(android.os.Message) PhoneDataItem(com.android.contacts.common.model.dataitem.PhoneDataItem) EventDataItem(com.android.contacts.common.model.dataitem.EventDataItem) OrganizationDataItem(com.android.contacts.common.model.dataitem.OrganizationDataItem) RelationDataItem(com.android.contacts.common.model.dataitem.RelationDataItem) StructuredNameDataItem(com.android.contacts.common.model.dataitem.StructuredNameDataItem) NoteDataItem(com.android.contacts.common.model.dataitem.NoteDataItem) DataItem(com.android.contacts.common.model.dataitem.DataItem) SipAddressDataItem(com.android.contacts.common.model.dataitem.SipAddressDataItem) EmailDataItem(com.android.contacts.common.model.dataitem.EmailDataItem) NicknameDataItem(com.android.contacts.common.model.dataitem.NicknameDataItem) StructuredPostalDataItem(com.android.contacts.common.model.dataitem.StructuredPostalDataItem) WebsiteDataItem(com.android.contacts.common.model.dataitem.WebsiteDataItem) GroupMembershipDataItem(com.android.contacts.common.model.dataitem.GroupMembershipDataItem) ImDataItem(com.android.contacts.common.model.dataitem.ImDataItem) PhoneDataItem(com.android.contacts.common.model.dataitem.PhoneDataItem) ArrayList(java.util.ArrayList) Handler(android.os.Handler) NfcHandler(com.android.contacts.NfcHandler) SpannableString(android.text.SpannableString) EmailDataItem(com.android.contacts.common.model.dataitem.EmailDataItem) Cursor(android.database.Cursor) Uri(android.net.Uri) RawContact(com.android.contacts.common.model.RawContact)

Example 7 with RawContact

use of com.android.contacts.common.model.RawContact in project packages_apps_Contacts by AOKP.

the class ConfirmAddDetailActivity method addEditableRawContact.

/**
 * 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();
    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;
}
Also used : ValuesDelta(com.android.contacts.common.model.ValuesDelta) AccountWithDataSet(com.android.contacts.common.model.account.AccountWithDataSet) RawContact(com.android.contacts.common.model.RawContact) AccountTypeManager(com.android.contacts.common.model.AccountTypeManager) AccountType(com.android.contacts.common.model.account.AccountType) RawContactDelta(com.android.contacts.common.model.RawContactDelta)

Example 8 with RawContact

use of com.android.contacts.common.model.RawContact in project packages_apps_Contacts by AOKP.

the class ContactDisplayUtils method getCompany.

/**
 * Returns the organization of the contact. If several organizations are given,
 * the first one is used. Returns null if not applicable.
 */
public static String getCompany(Context context, Contact contactData) {
    final boolean displayNameIsOrganization = contactData.getDisplayNameSource() == DisplayNameSources.ORGANIZATION;
    for (RawContact rawContact : contactData.getRawContacts()) {
        for (DataItem dataItem : Iterables.filter(rawContact.getDataItems(), OrganizationDataItem.class)) {
            OrganizationDataItem organization = (OrganizationDataItem) dataItem;
            final String company = organization.getCompany();
            final String title = organization.getTitle();
            final String combined;
            // is empty title). Make sure we don't show what's already shown as DisplayName
            if (TextUtils.isEmpty(company)) {
                combined = displayNameIsOrganization ? null : title;
            } else {
                if (TextUtils.isEmpty(title)) {
                    combined = displayNameIsOrganization ? null : company;
                } else {
                    if (displayNameIsOrganization) {
                        combined = title;
                    } else {
                        combined = context.getString(R.string.organization_company_and_title, company, title);
                    }
                }
            }
            if (!TextUtils.isEmpty(combined)) {
                return combined;
            }
        }
    }
    return null;
}
Also used : DataItem(com.android.contacts.common.model.dataitem.DataItem) OrganizationDataItem(com.android.contacts.common.model.dataitem.OrganizationDataItem) RawContact(com.android.contacts.common.model.RawContact) OrganizationDataItem(com.android.contacts.common.model.dataitem.OrganizationDataItem)

Example 9 with RawContact

use of com.android.contacts.common.model.RawContact in project packages_apps_Contacts by AOKP.

the class InvisibleContactUtil method isInvisibleAndAddable.

public static boolean isInvisibleAndAddable(Contact contactData, Context context) {
    // Only local contacts
    if (contactData == null || contactData.isDirectoryEntry())
        return false;
    // User profile cannot be added to contacts
    if (contactData.isUserProfile())
        return false;
    // Only if exactly one raw contact
    if (contactData.getRawContacts().size() != 1)
        return false;
    // test if the default group is assigned
    final List<GroupMetaData> groups = contactData.getGroupMetaData();
    // For accounts without group support, groups is null
    if (groups == null)
        return false;
    // remember the default group id. no default group? bail out early
    final long defaultGroupId = getDefaultGroupId(groups);
    if (defaultGroupId == -1)
        return false;
    final RawContact rawContact = (RawContact) contactData.getRawContacts().get(0);
    final AccountType type = rawContact.getAccountType(context);
    // Offline or non-writeable account? Nothing to fix
    if (type == null || !type.areContactsWritable())
        return false;
    // Check whether the contact is in the default group
    boolean isInDefaultGroup = false;
    for (DataItem dataItem : Iterables.filter(rawContact.getDataItems(), GroupMembershipDataItem.class)) {
        GroupMembershipDataItem groupMembership = (GroupMembershipDataItem) dataItem;
        final Long groupId = groupMembership.getGroupRowId();
        if (groupId != null && groupId == defaultGroupId) {
            isInDefaultGroup = true;
            break;
        }
    }
    return !isInDefaultGroup;
}
Also used : DataItem(com.android.contacts.common.model.dataitem.DataItem) GroupMembershipDataItem(com.android.contacts.common.model.dataitem.GroupMembershipDataItem) RawContact(com.android.contacts.common.model.RawContact) GroupMetaData(com.android.contacts.common.GroupMetaData) GroupMembershipDataItem(com.android.contacts.common.model.dataitem.GroupMembershipDataItem) AccountType(com.android.contacts.common.model.account.AccountType)

Example 10 with RawContact

use of com.android.contacts.common.model.RawContact in project packages_apps_Contacts by AOKP.

the class QuickContactActivity method generateDataModelFromContact.

/**
 * Builds the {@link DataItem}s Map out of the Contact.
 * @param data The contact to build the data from.
 * @return A pair containing a list of data items sorted within mimetype and sorted
 *  amongst mimetype. The map goes from mimetype string to the sorted list of data items within
 *  mimetype
 */
private Cp2DataCardModel generateDataModelFromContact(Contact data) {
    Trace.beginSection("Build data items map");
    final Map<String, List<DataItem>> dataItemsMap = new HashMap<>();
    final ResolveCache cache = ResolveCache.getInstance(this);
    for (RawContact rawContact : data.getRawContacts()) {
        for (DataItem dataItem : rawContact.getDataItems()) {
            dataItem.setRawContactId(rawContact.getId());
            final String mimeType = dataItem.getMimeType();
            if (mimeType == null)
                continue;
            final AccountType accountType = rawContact.getAccountType(this);
            final DataKind dataKind = AccountTypeManager.getInstance(this).getKindOrFallback(accountType, mimeType);
            if (dataKind == null)
                continue;
            dataItem.setDataKind(dataKind);
            final boolean hasData = !TextUtils.isEmpty(dataItem.buildDataString(this, dataKind));
            if (isMimeExcluded(mimeType) || !hasData)
                continue;
            List<DataItem> dataItemListByType = dataItemsMap.get(mimeType);
            if (dataItemListByType == null) {
                dataItemListByType = new ArrayList<>();
                dataItemsMap.put(mimeType, dataItemListByType);
            }
            dataItemListByType.add(dataItem);
        }
    }
    Trace.endSection();
    Trace.beginSection("sort within mimetypes");
    /*
         * Sorting is a multi part step. The end result is to a have a sorted list of the most
         * used data items, one per mimetype. Then, within each mimetype, the list of data items
         * for that type is also sorted, based off of {super primary, primary, times used} in that
         * order.
         */
    final List<List<DataItem>> dataItemsList = new ArrayList<>();
    for (List<DataItem> mimeTypeDataItems : dataItemsMap.values()) {
        // Remove duplicate data items
        Collapser.collapseList(mimeTypeDataItems, this);
        // Sort within mimetype
        Collections.sort(mimeTypeDataItems, mWithinMimeTypeDataItemComparator);
        // Add to the list of data item lists
        dataItemsList.add(mimeTypeDataItems);
    }
    Trace.endSection();
    Trace.beginSection("sort amongst mimetypes");
    // Sort amongst mimetypes to bubble up the top data items for the contact card
    Collections.sort(dataItemsList, mAmongstMimeTypeDataItemComparator);
    Trace.endSection();
    Trace.beginSection("cp2 data items to entries");
    final List<List<Entry>> contactCardEntries = new ArrayList<>();
    final List<List<Entry>> aboutCardEntries = buildAboutCardEntries(dataItemsMap);
    final MutableString aboutCardName = new MutableString();
    for (int i = 0; i < dataItemsList.size(); ++i) {
        final List<DataItem> dataItemsByMimeType = dataItemsList.get(i);
        final DataItem topDataItem = dataItemsByMimeType.get(0);
        if (SORTED_ABOUT_CARD_MIMETYPES.contains(topDataItem.getMimeType())) {
            // About card mimetypes are built in buildAboutCardEntries, skip here
            continue;
        } else {
            List<Entry> contactEntries = dataItemsToEntries(dataItemsList.get(i), aboutCardName);
            if (contactEntries.size() > 0) {
                contactCardEntries.add(contactEntries);
            }
        }
    }
    Trace.endSection();
    final Cp2DataCardModel dataModel = new Cp2DataCardModel();
    dataModel.customAboutCardName = aboutCardName.value;
    dataModel.aboutCardEntries = aboutCardEntries;
    dataModel.contactCardEntries = contactCardEntries;
    dataModel.dataItemsMap = dataItemsMap;
    return dataModel;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) PhoneDataItem(com.android.contacts.common.model.dataitem.PhoneDataItem) EventDataItem(com.android.contacts.common.model.dataitem.EventDataItem) OrganizationDataItem(com.android.contacts.common.model.dataitem.OrganizationDataItem) RelationDataItem(com.android.contacts.common.model.dataitem.RelationDataItem) StructuredNameDataItem(com.android.contacts.common.model.dataitem.StructuredNameDataItem) NoteDataItem(com.android.contacts.common.model.dataitem.NoteDataItem) DataItem(com.android.contacts.common.model.dataitem.DataItem) SipAddressDataItem(com.android.contacts.common.model.dataitem.SipAddressDataItem) EmailDataItem(com.android.contacts.common.model.dataitem.EmailDataItem) NicknameDataItem(com.android.contacts.common.model.dataitem.NicknameDataItem) StructuredPostalDataItem(com.android.contacts.common.model.dataitem.StructuredPostalDataItem) WebsiteDataItem(com.android.contacts.common.model.dataitem.WebsiteDataItem) GroupMembershipDataItem(com.android.contacts.common.model.dataitem.GroupMembershipDataItem) ImDataItem(com.android.contacts.common.model.dataitem.ImDataItem) DataKind(com.android.contacts.common.model.dataitem.DataKind) ArrayList(java.util.ArrayList) SpannableString(android.text.SpannableString) AccountType(com.android.contacts.common.model.account.AccountType) Entry(com.android.contacts.quickcontact.ExpandingEntryCardView.Entry) RawContact(com.android.contacts.common.model.RawContact) ColorStateList(android.content.res.ColorStateList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Aggregations

RawContact (com.android.contacts.common.model.RawContact)13 DataItem (com.android.contacts.common.model.dataitem.DataItem)5 SpannableString (android.text.SpannableString)4 RawContactDelta (com.android.contacts.common.model.RawContactDelta)4 AccountType (com.android.contacts.common.model.account.AccountType)4 GroupMembershipDataItem (com.android.contacts.common.model.dataitem.GroupMembershipDataItem)4 OrganizationDataItem (com.android.contacts.common.model.dataitem.OrganizationDataItem)4 EmailDataItem (com.android.contacts.common.model.dataitem.EmailDataItem)3 EventDataItem (com.android.contacts.common.model.dataitem.EventDataItem)3 ImDataItem (com.android.contacts.common.model.dataitem.ImDataItem)3 NicknameDataItem (com.android.contacts.common.model.dataitem.NicknameDataItem)3 NoteDataItem (com.android.contacts.common.model.dataitem.NoteDataItem)3 PhoneDataItem (com.android.contacts.common.model.dataitem.PhoneDataItem)3 RelationDataItem (com.android.contacts.common.model.dataitem.RelationDataItem)3 SipAddressDataItem (com.android.contacts.common.model.dataitem.SipAddressDataItem)3 StructuredNameDataItem (com.android.contacts.common.model.dataitem.StructuredNameDataItem)3 StructuredPostalDataItem (com.android.contacts.common.model.dataitem.StructuredPostalDataItem)3 WebsiteDataItem (com.android.contacts.common.model.dataitem.WebsiteDataItem)3 AccountWithDataSet (com.android.contacts.common.model.account.AccountWithDataSet)2 ArrayList (java.util.ArrayList)2