That fuck shit the fascists are using
at master 102 lines 3.8 kB view raw
1package org.tm.archive.contacts; 2 3import android.accounts.Account; 4import android.content.AbstractThreadedSyncAdapter; 5import android.content.ContentProviderClient; 6import android.content.Context; 7import android.content.SyncResult; 8import android.os.Bundle; 9 10import com.annimon.stream.Stream; 11 12import org.signal.contacts.SystemContactsRepository; 13import org.signal.core.util.logging.Log; 14import org.tm.archive.contacts.sync.ContactDiscovery; 15import org.tm.archive.database.SignalDatabase; 16import org.tm.archive.dependencies.ApplicationDependencies; 17import org.tm.archive.jobs.DirectoryRefreshJob; 18import org.tm.archive.keyvalue.SignalStore; 19import org.tm.archive.phonenumbers.PhoneNumberFormatter; 20import org.tm.archive.recipients.Recipient; 21import org.signal.core.util.SetUtil; 22import org.tm.archive.util.Util; 23 24import java.io.IOException; 25import java.util.List; 26import java.util.Set; 27import java.util.stream.Collectors; 28 29public class ContactsSyncAdapter extends AbstractThreadedSyncAdapter { 30 31 private static final String TAG = Log.tag(ContactsSyncAdapter.class); 32 33 private static final int FULL_SYNC_THRESHOLD = 10; 34 35 public ContactsSyncAdapter(Context context, boolean autoInitialize) { 36 super(context, autoInitialize); 37 } 38 39 @Override 40 public void onPerformSync(Account account, Bundle extras, String authority, 41 ContentProviderClient provider, SyncResult syncResult) 42 { 43 Log.i(TAG, "onPerformSync(" + authority +")"); 44 45 Context context = getContext(); 46 47 if (SignalStore.account().getE164() == null) { 48 Log.i(TAG, "No local number set, skipping all sync operations."); 49 return; 50 } 51 52 if (!SignalStore.account().isRegistered()) { 53 Log.i(TAG, "Not push registered. Just syncing contact info."); 54 ContactDiscovery.syncRecipientInfoWithSystemContacts(context); 55 return; 56 } 57 58 Set<String> allSystemE164s = SystemContactsRepository.getAllDisplayNumbers(context) 59 .stream() 60 .map(number -> PhoneNumberFormatter.get(context).format(number)) 61 .collect(Collectors.toSet()); 62 Set<String> knownSystemE164s = SignalDatabase.recipients().getAllE164s(); 63 Set<String> unknownSystemE164s = SetUtil.difference(allSystemE164s, knownSystemE164s); 64 65 if (unknownSystemE164s.size() > FULL_SYNC_THRESHOLD) { 66 Log.i(TAG, "There are " + unknownSystemE164s.size() + " unknown contacts. Doing a full sync."); 67 try { 68 ContactDiscovery.refreshAll(context, true); 69 } catch (IOException e) { 70 Log.w(TAG, e); 71 } 72 } else if (unknownSystemE164s.size() > 0) { 73 List<Recipient> recipients = Stream.of(unknownSystemE164s) 74 .filter(s -> s.startsWith("+")) 75 .map(s -> Recipient.external(getContext(), s)) 76 .toList(); 77 78 Log.i(TAG, "There are " + unknownSystemE164s.size() + " unknown E164s, which are now " + recipients.size() + " recipients. Only syncing these specific contacts."); 79 80 try { 81 ContactDiscovery.refresh(context, recipients, true); 82 } catch (IOException e) { 83 Log.w(TAG, "Failed to refresh! Scheduling for later.", e); 84 ApplicationDependencies.getJobManager().add(new DirectoryRefreshJob(true)); 85 } 86 } else { 87 Log.i(TAG, "No new contacts. Just syncing system contact data."); 88 ContactDiscovery.syncRecipientInfoWithSystemContacts(context); 89 } 90 } 91 92 @Override 93 public void onSyncCanceled() { 94 Log.w(TAG, "onSyncCanceled()"); 95 } 96 97 @Override 98 public void onSyncCanceled(Thread thread) { 99 Log.w(TAG, "onSyncCanceled(" + thread + ")"); 100 } 101 102}