Search in sources :

Example 46 with WorkerThread

use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class PaymentDatabase method markPaymentSeen.

@WorkerThread
public void markPaymentSeen(@NonNull UUID uuid) {
    SQLiteDatabase database = databaseHelper.getSignalWritableDatabase();
    ContentValues values = new ContentValues(1);
    String where = PAYMENT_UUID + " = ?";
    String[] args = { uuid.toString() };
    values.put(SEEN, 1);
    int updated = database.update(TABLE_NAME, values, where, args);
    if (updated > 0) {
        notifyChanged(uuid);
    }
}
Also used : ContentValues(android.content.ContentValues) WorkerThread(androidx.annotation.WorkerThread)

Example 47 with WorkerThread

use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class PaymentDatabase method getUnseenPayments.

@WorkerThread
@NonNull
public List<PaymentTransaction> getUnseenPayments() {
    SQLiteDatabase db = databaseHelper.getSignalReadableDatabase();
    String query = SEEN + " = 0 AND " + STATE + " = " + State.SUCCESSFUL.serialize();
    List<PaymentTransaction> results = new LinkedList<>();
    try (Cursor cursor = db.query(TABLE_NAME, null, query, null, null, null, null)) {
        while (cursor.moveToNext()) {
            results.add(readPayment(cursor));
        }
    }
    return results;
}
Also used : Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Example 48 with WorkerThread

use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class PaymentDatabase method markAllSeen.

@WorkerThread
public void markAllSeen() {
    SQLiteDatabase database = databaseHelper.getSignalWritableDatabase();
    ContentValues values = new ContentValues(1);
    List<UUID> unseenIds = new LinkedList<>();
    String[] unseenProjection = SqlUtil.buildArgs(PAYMENT_UUID);
    String unseenWhile = SEEN + " != ?";
    String[] unseenArgs = SqlUtil.buildArgs("1");
    int updated = -1;
    values.put(SEEN, 1);
    try {
        database.beginTransaction();
        try (Cursor cursor = database.query(TABLE_NAME, unseenProjection, unseenWhile, unseenArgs, null, null, null)) {
            while (cursor != null && cursor.moveToNext()) {
                unseenIds.add(UUID.fromString(CursorUtil.requireString(cursor, PAYMENT_UUID)));
            }
        }
        if (!unseenIds.isEmpty()) {
            updated = database.update(TABLE_NAME, values, null, null);
        }
        database.setTransactionSuccessful();
    } finally {
        database.endTransaction();
    }
    if (updated > 0) {
        for (final UUID unseenId : unseenIds) {
            notifyUuidChanged(unseenId);
        }
        notifyChanged();
    }
}
Also used : ContentValues(android.content.ContentValues) UUID(java.util.UUID) Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) WorkerThread(androidx.annotation.WorkerThread)

Example 49 with WorkerThread

use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class PaymentDatabase method getAll.

@WorkerThread
@NonNull
public List<PaymentTransaction> getAll() {
    SQLiteDatabase database = databaseHelper.getSignalReadableDatabase();
    List<PaymentTransaction> result = new LinkedList<>();
    try (Cursor cursor = database.query(TABLE_NAME, null, null, null, null, null, TIMESTAMP + " DESC")) {
        while (cursor.moveToNext()) {
            result.add(readPayment(cursor));
        }
    }
    return result;
}
Also used : Cursor(android.database.Cursor) LinkedList(java.util.LinkedList) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Example 50 with WorkerThread

use of androidx.annotation.WorkerThread in project Signal-Android by WhisperSystems.

the class LedgerReconcile method reconcile.

@WorkerThread
@NonNull
private static List<Payment> reconcile(@NonNull Collection<? extends Payment> allLocalPaymentTransactions, @NonNull List<MobileCoinLedgerWrapper.OwnedTxo> allTxOuts) {
    List<? extends Payment> nonFailedLocalPayments = Stream.of(allLocalPaymentTransactions).filter(i -> i.getState() != State.FAILED).toList();
    Set<ByteString> allKnownPublicKeys = new HashSet<>(nonFailedLocalPayments.size());
    Set<ByteString> allKnownKeyImages = new HashSet<>(nonFailedLocalPayments.size());
    for (Payment paymentTransaction : nonFailedLocalPayments) {
        PaymentMetaData.MobileCoinTxoIdentification txoIdentification = paymentTransaction.getPaymentMetaData().getMobileCoinTxoIdentification();
        allKnownPublicKeys.addAll(txoIdentification.getPublicKeyList());
        allKnownKeyImages.addAll(txoIdentification.getKeyImagesList());
    }
    Set<MobileCoinLedgerWrapper.OwnedTxo> knownTxosByKeyImage = Stream.of(allTxOuts).filter(t -> allKnownKeyImages.contains(t.getKeyImage())).collect(Collectors.toSet());
    Set<MobileCoinLedgerWrapper.OwnedTxo> knownTxosByPublicKeys = Stream.of(allTxOuts).filter(t -> allKnownPublicKeys.contains(t.getPublicKey())).collect(Collectors.toSet());
    // any TXO that we can't pair up the pub key for, we don't have detail for how it got into the account
    Set<MobileCoinLedgerWrapper.OwnedTxo> unknownTxOutsReceived = new HashSet<>(allTxOuts);
    unknownTxOutsReceived.removeAll(knownTxosByPublicKeys);
    // any TXO that we can't pair up the keyimage for, we don't have detail for how it got spent
    Set<MobileCoinLedgerWrapper.OwnedTxo> unknownTxOutsSpent = Stream.of(allTxOuts).filter(MobileCoinLedgerWrapper.OwnedTxo::isSpent).collect(Collectors.toSet());
    unknownTxOutsSpent.removeAll(knownTxosByKeyImage);
    if (unknownTxOutsReceived.isEmpty() && unknownTxOutsSpent.isEmpty()) {
        return Stream.of(allLocalPaymentTransactions).map(t -> (Payment) t).toList();
    }
    List<DetailedTransaction> detailedTransactions = reconstructAllTransactions(unknownTxOutsReceived, unknownTxOutsSpent);
    List<Payment> reconstructedPayments = new ArrayList<>(detailedTransactions.size());
    List<Payment> blockDecoratedLocalPayments = decoratePaymentsWithBlockIndexes(allLocalPaymentTransactions, allTxOuts);
    for (DetailedTransaction detailedTransaction : detailedTransactions) {
        reconstructedPayments.add(new ReconstructedPayment(detailedTransaction.blockDetail.getBlockIndex(), detailedTransaction.blockDetail.getBlockTimestampOrZero(), detailedTransaction.transaction.getDirection(), detailedTransaction.transaction.getValue()));
    }
    Collections.sort(reconstructedPayments, Payment.DESCENDING_BLOCK_INDEX);
    return ZipList.zipList(blockDecoratedLocalPayments, reconstructedPayments, Payment.DESCENDING_BLOCK_INDEX_UNKNOWN_FIRST);
}
Also used : PaymentMetaData(org.thoughtcrime.securesms.payments.proto.PaymentMetaData) PaymentDecorator(org.thoughtcrime.securesms.payments.PaymentDecorator) MapUtil(org.signal.core.util.MapUtil) Stream(com.annimon.stream.Stream) NonNull(androidx.annotation.NonNull) MobileCoinLedgerWrapper(org.thoughtcrime.securesms.payments.MobileCoinLedgerWrapper) WorkerThread(androidx.annotation.WorkerThread) HashMap(java.util.HashMap) ComparatorCompat(com.annimon.stream.ComparatorCompat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Locale(java.util.Locale) Map(java.util.Map) TransactionReconstruction(org.thoughtcrime.securesms.payments.history.TransactionReconstruction) Nullable(javax.annotation.Nullable) State(org.thoughtcrime.securesms.payments.State) Collectors(com.annimon.stream.Collectors) Collection(java.util.Collection) Set(java.util.Set) ByteString(com.google.protobuf.ByteString) Log(org.signal.core.util.logging.Log) List(java.util.List) ReconstructedPayment(org.thoughtcrime.securesms.payments.ReconstructedPayment) Payment(org.thoughtcrime.securesms.payments.Payment) Comparator(java.util.Comparator) Collections(java.util.Collections) Money(org.whispersystems.signalservice.api.payments.Money) PaymentMetaData(org.thoughtcrime.securesms.payments.proto.PaymentMetaData) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) ReconstructedPayment(org.thoughtcrime.securesms.payments.ReconstructedPayment) MobileCoinLedgerWrapper(org.thoughtcrime.securesms.payments.MobileCoinLedgerWrapper) ReconstructedPayment(org.thoughtcrime.securesms.payments.ReconstructedPayment) Payment(org.thoughtcrime.securesms.payments.Payment) HashSet(java.util.HashSet) WorkerThread(androidx.annotation.WorkerThread) NonNull(androidx.annotation.NonNull)

Aggregations

WorkerThread (androidx.annotation.WorkerThread)365 NonNull (androidx.annotation.NonNull)151 IOException (java.io.IOException)83 Recipient (org.thoughtcrime.securesms.recipients.Recipient)82 Nullable (androidx.annotation.Nullable)51 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)46 Cursor (android.database.Cursor)41 ArrayList (java.util.ArrayList)41 Context (android.content.Context)39 Uri (android.net.Uri)32 LinkedList (java.util.LinkedList)30 List (java.util.List)30 RecipientDatabase (org.thoughtcrime.securesms.database.RecipientDatabase)28 Stream (com.annimon.stream.Stream)26 HashMap (java.util.HashMap)26 Log (org.signal.core.util.logging.Log)26 HashSet (java.util.HashSet)24 Map (java.util.Map)21 Collections (java.util.Collections)20 ApplicationDependencies (org.thoughtcrime.securesms.dependencies.ApplicationDependencies)20