Search in sources :

Example 11 with DirectoryRefreshJob

use of org.thoughtcrime.securesms.jobs.DirectoryRefreshJob in project Signal-Android by signalapp.

the class RecipientIdJobMigrationTest method migrate_directoryRefreshJob_nonNull.

@Test
public void migrate_directoryRefreshJob_nonNull() throws Exception {
    JobData testData = new JobData("DirectoryRefreshJob", "DirectoryRefreshJob", new Data.Builder().putString("address", "+16101234567").putBoolean("notify_of_new_users", true).build());
    mockRecipientResolve("+16101234567", 1);
    RecipientIdJobMigration subject = new RecipientIdJobMigration(mock(Application.class));
    JobData converted = subject.migrate(testData);
    assertEquals("DirectoryRefreshJob", converted.getFactoryKey());
    assertEquals("DirectoryRefreshJob", converted.getQueueKey());
    assertTrue(converted.getData().getBoolean("notify_of_new_users"));
    assertEquals("1", converted.getData().getString("recipient"));
    assertFalse(converted.getData().hasString("address"));
    new DirectoryRefreshJob.Factory().create(mock(Job.Parameters.class), converted.getData());
}
Also used : DirectoryRefreshJob(org.thoughtcrime.securesms.jobs.DirectoryRefreshJob) JobData(org.thoughtcrime.securesms.jobmanager.JobMigration.JobData) Application(android.app.Application) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with DirectoryRefreshJob

use of org.thoughtcrime.securesms.jobs.DirectoryRefreshJob in project Signal-Android by signalapp.

the class ContactsSyncAdapter method onPerformSync.

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
    Log.i(TAG, "onPerformSync(" + authority + ")");
    Context context = getContext();
    if (SignalStore.account().getE164() == null) {
        Log.i(TAG, "No local number set, skipping all sync operations.");
        return;
    }
    if (!SignalStore.account().isRegistered()) {
        Log.i(TAG, "Not push registered. Just syncing contact info.");
        DirectoryHelper.syncRecipientInfoWithSystemContacts(context);
        return;
    }
    Set<String> allSystemNumbers = ContactAccessor.getInstance().getAllContactsWithNumbers(context);
    Set<String> knownSystemNumbers = SignalDatabase.recipients().getAllPhoneNumbers();
    Set<String> unknownSystemNumbers = SetUtil.difference(allSystemNumbers, knownSystemNumbers);
    if (unknownSystemNumbers.size() > FULL_SYNC_THRESHOLD) {
        Log.i(TAG, "There are " + unknownSystemNumbers.size() + " unknown contacts. Doing a full sync.");
        try {
            DirectoryHelper.refreshDirectory(context, true);
        } catch (IOException e) {
            Log.w(TAG, e);
        }
    } else if (unknownSystemNumbers.size() > 0) {
        Log.i(TAG, "There are " + unknownSystemNumbers.size() + " unknown contacts. Doing an individual sync.");
        List<Recipient> recipients = Stream.of(unknownSystemNumbers).filter(s -> s.startsWith("+")).map(s -> Recipient.external(getContext(), s)).toList();
        try {
            DirectoryHelper.refreshDirectoryFor(context, recipients, true);
        } catch (IOException e) {
            Log.w(TAG, "Failed to refresh! Scheduling for later.", e);
            ApplicationDependencies.getJobManager().add(new DirectoryRefreshJob(true));
        }
    } else {
        Log.i(TAG, "No new contacts. Just syncing system contact data.");
        DirectoryHelper.syncRecipientInfoWithSystemContacts(context);
    }
}
Also used : Context(android.content.Context) SignalStore(org.thoughtcrime.securesms.keyvalue.SignalStore) Context(android.content.Context) SyncResult(android.content.SyncResult) SignalDatabase(org.thoughtcrime.securesms.database.SignalDatabase) Bundle(android.os.Bundle) DirectoryRefreshJob(org.thoughtcrime.securesms.jobs.DirectoryRefreshJob) Stream(com.annimon.stream.Stream) DirectoryHelper(org.thoughtcrime.securesms.contacts.sync.DirectoryHelper) Account(android.accounts.Account) ApplicationDependencies(org.thoughtcrime.securesms.dependencies.ApplicationDependencies) AbstractThreadedSyncAdapter(android.content.AbstractThreadedSyncAdapter) Set(java.util.Set) IOException(java.io.IOException) SetUtil(org.thoughtcrime.securesms.util.SetUtil) Log(org.signal.core.util.logging.Log) List(java.util.List) ContentProviderClient(android.content.ContentProviderClient) Recipient(org.thoughtcrime.securesms.recipients.Recipient) DirectoryRefreshJob(org.thoughtcrime.securesms.jobs.DirectoryRefreshJob) List(java.util.List) IOException(java.io.IOException)

Example 13 with DirectoryRefreshJob

use of org.thoughtcrime.securesms.jobs.DirectoryRefreshJob in project Signal-Android by signalapp.

the class LegacyMigrationJob method performMigration.

@Override
void performMigration() throws RetryLaterException {
    Log.i(TAG, "Running background upgrade..");
    int lastSeenVersion = VersionTracker.getLastSeenVersion(context);
    MasterSecret masterSecret = KeyCachingService.getMasterSecret(context);
    if (lastSeenVersion < SQLCIPHER && masterSecret != null) {
        SignalDatabase.onApplicationLevelUpgrade(context, masterSecret, lastSeenVersion, (progress, total) -> {
            Log.i(TAG, "onApplicationLevelUpgrade: " + progress + "/" + total);
        });
    } else if (lastSeenVersion < SQLCIPHER) {
        throw new RetryLaterException();
    }
    if (lastSeenVersion < NO_V1_VERSION) {
        File v1sessions = new File(context.getFilesDir(), "sessions");
        if (v1sessions.exists() && v1sessions.isDirectory()) {
            File[] contents = v1sessions.listFiles();
            if (contents != null) {
                for (File session : contents) {
                    session.delete();
                }
            }
            v1sessions.delete();
        }
    }
    if (lastSeenVersion < SIGNED_PREKEY_VERSION) {
        CreateSignedPreKeyJob.enqueueIfNeeded();
    }
    if (lastSeenVersion < NO_DECRYPT_QUEUE_VERSION) {
        scheduleMessagesInPushDatabase(context);
    }
    if (lastSeenVersion < PUSH_DECRYPT_SERIAL_ID_VERSION) {
        scheduleMessagesInPushDatabase(context);
    }
    if (lastSeenVersion < MIGRATE_SESSION_PLAINTEXT) {
        // new TextSecureSessionStore(context, masterSecret).migrateSessions();
        // new TextSecurePreKeyStore(context, masterSecret).migrateRecords();
        scheduleMessagesInPushDatabase(context);
        ;
    }
    if (lastSeenVersion < CONTACTS_ACCOUNT_VERSION) {
        ApplicationDependencies.getJobManager().add(new DirectoryRefreshJob(false));
    }
    if (lastSeenVersion < MEDIA_DOWNLOAD_CONTROLS_VERSION) {
        schedulePendingIncomingParts(context);
    }
    if (lastSeenVersion < REDPHONE_SUPPORT_VERSION) {
        ApplicationDependencies.getJobManager().add(new RefreshAttributesJob());
        ApplicationDependencies.getJobManager().add(new DirectoryRefreshJob(false));
    }
    if (lastSeenVersion < PROFILES) {
        ApplicationDependencies.getJobManager().add(new DirectoryRefreshJob(false));
    }
    if (lastSeenVersion < SCREENSHOTS) {
        boolean screenSecurity = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(TextSecurePreferences.SCREEN_SECURITY_PREF, true);
        TextSecurePreferences.setScreenSecurityEnabled(context, screenSecurity);
    }
    if (lastSeenVersion < PERSISTENT_BLOBS) {
        File externalDir = context.getExternalFilesDir(null);
        if (externalDir != null && externalDir.isDirectory() && externalDir.exists()) {
            for (File blob : externalDir.listFiles()) {
                if (blob.exists() && blob.isFile())
                    blob.delete();
            }
        }
    }
    if (lastSeenVersion < INTERNALIZE_CONTACTS) {
        if (SignalStore.account().isRegistered()) {
            TextSecurePreferences.setHasSuccessfullyRetrievedDirectory(context, true);
        }
    }
    if (lastSeenVersion < SQLCIPHER) {
        scheduleMessagesInPushDatabase(context);
    }
    if (lastSeenVersion < SQLCIPHER_COMPLETE) {
        File file = context.getDatabasePath("messages.db");
        if (file != null && file.exists())
            file.delete();
    }
    if (lastSeenVersion < REMOVE_JOURNAL) {
        File file = context.getDatabasePath("messages.db-journal");
        if (file != null && file.exists())
            file.delete();
    }
    if (lastSeenVersion < REMOVE_CACHE) {
        FileUtils.deleteDirectoryContents(context.getCacheDir());
    }
    if (lastSeenVersion < IMAGE_CACHE_CLEANUP) {
        FileUtils.deleteDirectoryContents(context.getExternalCacheDir());
        GlideApp.get(context).clearDiskCache();
    }
    if (lastSeenVersion < COLOR_MIGRATION) {
        long startTime = System.currentTimeMillis();
        // noinspection deprecation
        SignalDatabase.recipients().updateSystemContactColors();
        Log.i(TAG, "Color migration took " + (System.currentTimeMillis() - startTime) + " ms");
    }
    if (lastSeenVersion < UNIDENTIFIED_DELIVERY) {
        Log.i(TAG, "Scheduling UD attributes refresh.");
        ApplicationDependencies.getJobManager().add(new RefreshAttributesJob());
    }
    if (lastSeenVersion < SIGNALING_KEY_DEPRECATION) {
        Log.i(TAG, "Scheduling a RefreshAttributesJob to remove the signaling key remotely.");
        ApplicationDependencies.getJobManager().add(new RefreshAttributesJob());
    }
}
Also used : MasterSecret(org.thoughtcrime.securesms.crypto.MasterSecret) RefreshAttributesJob(org.thoughtcrime.securesms.jobs.RefreshAttributesJob) DirectoryRefreshJob(org.thoughtcrime.securesms.jobs.DirectoryRefreshJob) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) File(java.io.File)

Aggregations

DirectoryRefreshJob (org.thoughtcrime.securesms.jobs.DirectoryRefreshJob)13 Application (android.app.Application)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 JobData (org.thoughtcrime.securesms.jobmanager.JobMigration.JobData)4 Account (android.accounts.Account)2 AbstractThreadedSyncAdapter (android.content.AbstractThreadedSyncAdapter)2 ContentProviderClient (android.content.ContentProviderClient)2 Context (android.content.Context)2 SyncResult (android.content.SyncResult)2 Bundle (android.os.Bundle)2 Stream (com.annimon.stream.Stream)2 File (java.io.File)2 IOException (java.io.IOException)2 List (java.util.List)2 Set (java.util.Set)2 Log (org.signal.core.util.logging.Log)2 DirectoryHelper (org.thoughtcrime.securesms.contacts.sync.DirectoryHelper)2 MasterSecret (org.thoughtcrime.securesms.crypto.MasterSecret)2 SignalDatabase (org.thoughtcrime.securesms.database.SignalDatabase)2