Search in sources :

Example 6 with Selection

use of com.android.dialer.common.database.Selection in project android_packages_apps_Dialer by LineageOS.

the class SystemBlockedNumberPhoneLookup method queryNumbers.

@WorkerThread
private ImmutableMap<DialerPhoneNumber, SystemBlockedNumberInfo> queryNumbers(ImmutableSet<DialerPhoneNumber> numbers) {
    Assert.isWorkerThread();
    PartitionedNumbers partitionedNumbers = new PartitionedNumbers(numbers);
    Set<DialerPhoneNumber> blockedNumbers = new ArraySet<>();
    Selection normalizedSelection = Selection.column(BlockedNumbers.COLUMN_E164_NUMBER).in(partitionedNumbers.validE164Numbers());
    try (Cursor cursor = appContext.getContentResolver().query(BlockedNumbers.CONTENT_URI, new String[] { BlockedNumbers.COLUMN_E164_NUMBER }, normalizedSelection.getSelection(), normalizedSelection.getSelectionArgs(), null)) {
        while (cursor != null && cursor.moveToNext()) {
            blockedNumbers.addAll(partitionedNumbers.dialerPhoneNumbersForValidE164(cursor.getString(0)));
        }
    }
    Selection rawSelection = Selection.column(BlockedNumbers.COLUMN_ORIGINAL_NUMBER).in(partitionedNumbers.invalidNumbers());
    try (Cursor cursor = appContext.getContentResolver().query(BlockedNumbers.CONTENT_URI, new String[] { BlockedNumbers.COLUMN_ORIGINAL_NUMBER }, rawSelection.getSelection(), rawSelection.getSelectionArgs(), null)) {
        while (cursor != null && cursor.moveToNext()) {
            blockedNumbers.addAll(partitionedNumbers.dialerPhoneNumbersForInvalid(cursor.getString(0)));
        }
    }
    ImmutableMap.Builder<DialerPhoneNumber, SystemBlockedNumberInfo> result = ImmutableMap.builder();
    for (DialerPhoneNumber number : numbers) {
        result.put(number, SystemBlockedNumberInfo.newBuilder().setBlockedState(blockedNumbers.contains(number) ? BlockedState.BLOCKED : BlockedState.NOT_BLOCKED).build());
    }
    return result.build();
}
Also used : SystemBlockedNumberInfo(com.android.dialer.phonelookup.PhoneLookupInfo.SystemBlockedNumberInfo) PartitionedNumbers(com.android.dialer.phonenumberproto.PartitionedNumbers) ArraySet(android.util.ArraySet) Selection(com.android.dialer.common.database.Selection) DialerPhoneNumber(com.android.dialer.DialerPhoneNumber) Cursor(android.database.Cursor) ImmutableMap(com.google.common.collect.ImmutableMap) WorkerThread(android.support.annotation.WorkerThread)

Example 7 with Selection

use of com.android.dialer.common.database.Selection in project android_packages_apps_Dialer by LineageOS.

the class CnapPhoneLookup method lookup.

/**
 * CNAP info cannot be retrieved when all we have is a number. The best we can do is returning the
 * existing info in {@link PhoneLookupHistory}.
 */
@Override
public ListenableFuture<CnapInfo> lookup(DialerPhoneNumber dialerPhoneNumber) {
    return backgroundExecutorService.submit(() -> {
        Selection selection = Selection.builder().and(Selection.column(PhoneLookupHistory.NORMALIZED_NUMBER).is("=", dialerPhoneNumber.getNormalizedNumber())).build();
        try (Cursor cursor = appContext.getContentResolver().query(PhoneLookupHistory.CONTENT_URI, new String[] { PhoneLookupHistory.PHONE_LOOKUP_INFO }, selection.getSelection(), selection.getSelectionArgs(), /* sortOrder = */
        null)) {
            if (cursor == null) {
                LogUtil.e("CnapPhoneLookup.lookup", "null cursor");
                return CnapInfo.getDefaultInstance();
            }
            if (!cursor.moveToFirst()) {
                LogUtil.i("CnapPhoneLookup.lookup", "empty cursor");
                return CnapInfo.getDefaultInstance();
            }
            // At ths point, we expect only one row in the cursor as
            // PhoneLookupHistory.NORMALIZED_NUMBER is the primary key of table PhoneLookupHistory.
            Assert.checkState(cursor.getCount() == 1);
            int phoneLookupInfoColumn = cursor.getColumnIndexOrThrow(PhoneLookupHistory.PHONE_LOOKUP_INFO);
            PhoneLookupInfo phoneLookupInfo;
            try {
                phoneLookupInfo = PhoneLookupInfo.parseFrom(cursor.getBlob(phoneLookupInfoColumn));
            } catch (InvalidProtocolBufferException e) {
                throw new IllegalStateException(e);
            }
            return phoneLookupInfo.getCnapInfo();
        }
    });
}
Also used : PhoneLookupInfo(com.android.dialer.phonelookup.PhoneLookupInfo) Selection(com.android.dialer.common.database.Selection) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Cursor(android.database.Cursor)

Example 8 with Selection

use of com.android.dialer.common.database.Selection in project android_packages_apps_Dialer by LineageOS.

the class SpeedDialUiItemMutator method getStarredContacts.

@WorkerThread
private List<SpeedDialUiItem> getStarredContacts() {
    Trace.beginSection("getStrequentContacts");
    Assert.isWorkerThread();
    Set<String> contactIds = new ArraySet<>();
    // Fetch the contact ids of all starred contacts
    Uri strequentUri = Contacts.CONTENT_STREQUENT_URI.buildUpon().appendQueryParameter(ContactsContract.STREQUENT_PHONE_ONLY, "true").build();
    Selection selection = Selection.column(Phone.STARRED).is("=", 1);
    try (Cursor cursor = appContext.getContentResolver().query(strequentUri, new String[] { Phone.CONTACT_ID }, selection.getSelection(), selection.getSelectionArgs(), null)) {
        if (cursor == null) {
            LogUtil.e("SpeedDialUiItemMutator.getStarredContacts", "null cursor");
            Trace.endSection();
            return new ArrayList<>();
        }
        if (cursor.getCount() == 0) {
            Trace.endSection();
            return new ArrayList<>();
        }
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            contactIds.add(Long.toString(cursor.getLong(0)));
        }
    }
    // Build SpeedDialUiItems from those contact ids
    selection = Selection.builder().and(Selection.column(Phone.CONTACT_ID).in(contactIds)).build();
    try (Cursor cursor = appContext.getContentResolver().query(Phone.CONTENT_URI, SpeedDialUiItem.getPhoneProjection(isPrimaryDisplayNameOrder()), selection.getSelection(), selection.getSelectionArgs(), null)) {
        List<SpeedDialUiItem> contacts = new ArrayList<>();
        if (cursor == null) {
            LogUtil.e("SpeedDialUiItemMutator.getStrequentContacts", "null cursor");
            Trace.endSection();
            return new ArrayList<>();
        }
        if (cursor.getCount() == 0) {
            Trace.endSection();
            return contacts;
        }
        for (cursor.moveToFirst(); !cursor.isAfterLast(); ) /* Iterate in the loop */
        {
            contacts.add(SpeedDialUiItem.fromCursor(appContext.getResources(), cursor, CallUtil.isVideoEnabled(appContext)));
        }
        Trace.endSection();
        return contacts;
    }
}
Also used : ArraySet(android.util.ArraySet) Selection(com.android.dialer.common.database.Selection) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) Uri(android.net.Uri) WorkerThread(android.support.annotation.WorkerThread)

Example 9 with Selection

use of com.android.dialer.common.database.Selection in project android_packages_apps_Dialer by LineageOS.

the class SpeedDialUiItemMutator method getSpeedDialUiItemsFromEntries.

/**
 * Returns a map of SpeedDialEntries to their corresponding SpeedDialUiItems. Mappings to null
 * elements imply that the contact was deleted.
 */
@WorkerThread
private Map<SpeedDialEntry, SpeedDialUiItem> getSpeedDialUiItemsFromEntries(List<SpeedDialEntry> entries) {
    Trace.beginSection("getSpeedDialUiItemsFromEntries");
    Assert.isWorkerThread();
    // Fetch the contact ids from the SpeedDialEntries
    Set<String> contactIds = new ArraySet<>();
    entries.forEach(entry -> contactIds.add(Long.toString(entry.contactId())));
    if (contactIds.isEmpty()) {
        Trace.endSection();
        return new ArrayMap<>();
    }
    // Build SpeedDialUiItems from those contact ids and map them to their entries
    Selection selection = Selection.builder().and(Selection.column(Phone.CONTACT_ID).in(contactIds)).build();
    try (Cursor cursor = appContext.getContentResolver().query(Phone.CONTENT_URI, SpeedDialUiItem.getPhoneProjection(isPrimaryDisplayNameOrder()), selection.getSelection(), selection.getSelectionArgs(), null)) {
        Map<SpeedDialEntry, SpeedDialUiItem> map = new ArrayMap<>();
        for (cursor.moveToFirst(); !cursor.isAfterLast(); ) /* Iterate in the loop */
        {
            SpeedDialUiItem item = SpeedDialUiItem.fromCursor(appContext.getResources(), cursor, CallUtil.isVideoEnabled(appContext));
            for (SpeedDialEntry entry : entries) {
                if (entry.contactId() == item.contactId()) {
                    // Update the id and pinned position to match it's corresponding SpeedDialEntry.
                    SpeedDialUiItem.Builder entrySpeedDialItem = item.toBuilder().setSpeedDialEntryId(entry.id()).setPinnedPosition(entry.pinnedPosition());
                    // Preserve the default channel if it didn't change/still exists
                    Channel defaultChannel = entry.defaultChannel();
                    if (defaultChannel != null) {
                        if (item.channels().contains(defaultChannel) || isValidDuoDefaultChannel(item.channels(), defaultChannel)) {
                            entrySpeedDialItem.setDefaultChannel(defaultChannel);
                        }
                    }
                    // It's impossible for two contacts to exist with the same contact id, so if this entry
                    // was previously matched to a SpeedDialUiItem and is being matched again, something
                    // went horribly wrong.
                    Assert.checkArgument(map.put(entry, entrySpeedDialItem.build()) == null, "Each SpeedDialEntry only has one correct SpeedDialUiItem");
                }
            }
        }
        // Contact must have been deleted
        for (SpeedDialEntry entry : entries) {
            map.putIfAbsent(entry, null);
        }
        Trace.endSection();
        return map;
    }
}
Also used : ArraySet(android.util.ArraySet) Selection(com.android.dialer.common.database.Selection) SpeedDialEntry(com.android.dialer.speeddial.database.SpeedDialEntry) Channel(com.android.dialer.speeddial.database.SpeedDialEntry.Channel) ArrayMap(android.util.ArrayMap) Cursor(android.database.Cursor) WorkerThread(android.support.annotation.WorkerThread)

Example 10 with Selection

use of com.android.dialer.common.database.Selection in project android_packages_apps_Dialer by LineageOS.

the class SpeedDialEntryDatabaseHelper method delete.

private void delete(SQLiteDatabase writeableDatabase, ImmutableList<Long> ids) {
    List<String> idStrings = new ArrayList<>();
    for (Long id : ids) {
        idStrings.add(Long.toString(id));
    }
    Selection selection = Selection.builder().and(Selection.column(ID).in(idStrings)).build();
    int count = writeableDatabase.delete(TABLE_NAME, selection.getSelection(), selection.getSelectionArgs());
    if (count != ids.size()) {
        throw Assert.createUnsupportedOperationFailException("Attempted to delete an undetermined number of rows: " + count);
    }
}
Also used : Selection(com.android.dialer.common.database.Selection) ArrayList(java.util.ArrayList)

Aggregations

Selection (com.android.dialer.common.database.Selection)12 Cursor (android.database.Cursor)9 ArrayList (java.util.ArrayList)5 WorkerThread (android.support.annotation.WorkerThread)4 ArraySet (android.util.ArraySet)3 ArrayMap (android.util.ArrayMap)2 PhoneLookupInfo (com.android.dialer.phonelookup.PhoneLookupInfo)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 SuppressLint (android.annotation.SuppressLint)1 ContentProviderOperation (android.content.ContentProviderOperation)1 ContentValues (android.content.ContentValues)1 Uri (android.net.Uri)1 DialerPhoneNumber (com.android.dialer.DialerPhoneNumber)1 SystemBlockedNumberInfo (com.android.dialer.phonelookup.PhoneLookupInfo.SystemBlockedNumberInfo)1 PartitionedNumbers (com.android.dialer.phonenumberproto.PartitionedNumbers)1 SpeedDialEntry (com.android.dialer.speeddial.database.SpeedDialEntry)1 Channel (com.android.dialer.speeddial.database.SpeedDialEntry.Channel)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1