盛康基办.apk(点击下载) / ContactsController.java


package im.guobwnxjuc.messenger;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.Build;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.SparseArray;
import androidx.exifinterface.media.ExifInterface;
import com.bjz.comm.net.premission.PermissionUtils;
import im.guobwnxjuc.messenger.support.SparseLongArray;
import im.guobwnxjuc.phoneformat.PhoneFormat;
import im.guobwnxjuc.tgnet.ConnectionsManager;
import im.guobwnxjuc.tgnet.RequestDelegate;
import im.guobwnxjuc.tgnet.TLObject;
import im.guobwnxjuc.tgnet.TLRPC;
import im.guobwnxjuc.tgnet.TLRPCContacts;
import im.guobwnxjuc.ui.hui.CharacterParser;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Marker;

public class ContactsController extends BaseController {
    private static volatile ContactsController[] Instance = new ContactsController[3];
    public static final int PRIVACY_RULES_TYPE_ADDED_BY_PHONE = 7;
    public static final int PRIVACY_RULES_TYPE_CALLS = 2;
    public static final int PRIVACY_RULES_TYPE_COUNT = 9;
    public static final int PRIVACY_RULES_TYPE_FORWARDS = 5;
    public static final int PRIVACY_RULES_TYPE_INVITE = 1;
    public static final int PRIVACY_RULES_TYPE_LASTSEEN = 0;
    public static final int PRIVACY_RULES_TYPE_MOMENT = 8;
    public static final int PRIVACY_RULES_TYPE_P2P = 3;
    public static final int PRIVACY_RULES_TYPE_PHONE = 6;
    public static final int PRIVACY_RULES_TYPE_PHOTO = 4;
    private ArrayList<TLRPC.PrivacyRule> addedByPhonePrivacyRules;
    private ArrayList<TLRPC.PrivacyRule> callPrivacyRules;
    private int completedRequestsCount;
    public ArrayList<TLRPC.Contact> contacts = new ArrayList<>();
    public HashMap<String, Contact> contactsBook = new HashMap<>();
    private boolean contactsBookLoaded;
    public HashMap<String, Contact> contactsBookSPhones = new HashMap<>();
    public HashMap<String, TLRPC.Contact> contactsByPhone = new HashMap<>();
    public HashMap<String, TLRPC.Contact> contactsByShortPhone = new HashMap<>();
    public ConcurrentHashMap<Integer, TLRPC.Contact> contactsDict = new ConcurrentHashMap<>(20, 1.0f, 2);
    public boolean contactsLoaded;
    private boolean contactsSyncInProgress;
    private ArrayList<Integer> delayedContactsUpdate = new ArrayList<>();
    private int deleteAccountTTL;
    private ArrayList<TLRPC.PrivacyRule> forwardsPrivacyRules;
    private ArrayList<TLRPC.PrivacyRule> groupPrivacyRules;
    private boolean ignoreChanges;
    private String inviteLink;
    private String lastContactsVersions = "";
    private ArrayList<TLRPC.PrivacyRule> lastseenPrivacyRules;
    private final Object loadContactsSync = new Object();
    private boolean loadingContacts;
    private int loadingDeleteInfo;
    private int[] loadingPrivacyInfo = new int[9];
    private boolean migratingContacts;
    private ArrayList<TLRPC.PrivacyRule> momentPrivacyRules;
    private final Object observerLock = new Object();
    private ArrayList<TLRPC.PrivacyRule> p2pPrivacyRules;
    public ArrayList<Contact> phoneBookContacts = new ArrayList<>();
    public ArrayList<String> phoneBookSectionsArray = new ArrayList<>();
    public HashMap<String, ArrayList<Object>> phoneBookSectionsDict = new HashMap<>();
    private ArrayList<TLRPC.PrivacyRule> phonePrivacyRules;
    private ArrayList<TLRPC.PrivacyRule> profilePhotoPrivacyRules;
    private String[] projectionNames = {"lookup", "data2", "data3", "data5"};
    private String[] projectionPhones = {"lookup", "data1", "data2", "data3", "display_name", "account_type"};
    private HashMap<String, String> sectionsToReplace = new HashMap<>();
    public ArrayList<String> sortedUsersMutualSectionsArray = new ArrayList<>();
    public ArrayList<String> sortedUsersSectionsArray = new ArrayList<>();
    private Account systemAccount;
    private boolean updatingInviteLink;
    public HashMap<String, ArrayList<TLRPC.Contact>> usersMutualSectionsDict = new HashMap<>();
    public HashMap<String, ArrayList<TLRPC.Contact>> usersSectionsDict = new HashMap<>();

    static /* synthetic */ void lambda$resetImportedContacts$9(TLObject tLObject, TLRPC.TL_error tL_error) {
    }

    /* access modifiers changed from: private */
    public class MyContentObserver extends ContentObserver {
        private Runnable checkRunnable = $$Lambda$ContactsController$MyContentObserver$26Ns6nuWTDXTZDMynglbhqmgijU.INSTANCE;

        public boolean deliverSelfNotifications() {
            return false;
        }

        static /* synthetic */ void lambda$new$0() {
            for (int i = 0; i < 3; i++) {
                if (UserConfig.getInstance(i).isClientActivated()) {
                    ConnectionsManager.getInstance(i).resumeNetworkMaybe();
                }
            }
        }

        public MyContentObserver() {
            super(null);
        }

        public void onChange(boolean z) {
            super.onChange(z);
            synchronized (ContactsController.this.observerLock) {
                if (!ContactsController.this.ignoreChanges) {
                    Utilities.globalQueue.cancelRunnable(this.checkRunnable);
                    Utilities.globalQueue.postRunnable(this.checkRunnable, 500);
                }
            }
        }
    }

    public static class Contact {
        public int contact_id;
        public String first_name;
        public int imported;
        public boolean isGoodProvider;
        public String key;
        public String last_name;
        public boolean namesFilled;
        public ArrayList<Integer> phoneDeleted = new ArrayList<>(4);
        public ArrayList<String> phoneTypes = new ArrayList<>(4);
        public ArrayList<String> phones = new ArrayList<>(4);
        public String provider;
        public ArrayList<String> shortPhones = new ArrayList<>(4);
        public TLRPC.User user;

        public String getLetter() {
            return getLetter(this.first_name, this.last_name);
        }

        public static String getLetter(String str, String str2) {
            if (!TextUtils.isEmpty(str)) {
                return str.substring(0, 1);
            }
            return !TextUtils.isEmpty(str2) ? str2.substring(0, 1) : "#";
        }
    }

    public static ContactsController getInstance(int i) {
        ContactsController contactsController = Instance[i];
        if (contactsController == null) {
            synchronized (ContactsController.class) {
                contactsController = Instance[i];
                if (contactsController == null) {
                    ContactsController[] contactsControllerArr = Instance;
                    ContactsController contactsController2 = new ContactsController(i);
                    contactsControllerArr[i] = contactsController2;
                    contactsController = contactsController2;
                }
            }
        }
        return contactsController;
    }

    public ContactsController(int i) {
        super(i);
        if (MessagesController.getMainSettings(this.currentAccount).getBoolean("needGetStatuses", false)) {
            reloadContactsStatuses();
        }
        this.sectionsToReplace.put("À", ExifInterface.GPS_MEASUREMENT_IN_PROGRESS);
        this.sectionsToReplace.put("Á", ExifInterface.GPS_MEASUREMENT_IN_PROGRESS);
        this.sectionsToReplace.put("Ä", ExifInterface.GPS_MEASUREMENT_IN_PROGRESS);
        this.sectionsToReplace.put("Ù", "U");
        this.sectionsToReplace.put("Ú", "U");
        this.sectionsToReplace.put("Ü", "U");
        this.sectionsToReplace.put("Ì", "I");
        this.sectionsToReplace.put("Í", "I");
        this.sectionsToReplace.put("Ï", "I");
        this.sectionsToReplace.put("È", ExifInterface.LONGITUDE_EAST);
        this.sectionsToReplace.put("É", ExifInterface.LONGITUDE_EAST);
        this.sectionsToReplace.put("Ê", ExifInterface.LONGITUDE_EAST);
        this.sectionsToReplace.put("Ë", ExifInterface.LONGITUDE_EAST);
        this.sectionsToReplace.put("Ò", "O");
        this.sectionsToReplace.put("Ó", "O");
        this.sectionsToReplace.put("Ö", "O");
        this.sectionsToReplace.put("Ç", "C");
        this.sectionsToReplace.put("Ñ", "N");
        this.sectionsToReplace.put("Ÿ", "Y");
        this.sectionsToReplace.put("Ý", "Y");
        this.sectionsToReplace.put("Ţ", "Y");
        if (i == 0) {
            Utilities.globalQueue.postRunnable(new Runnable() {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$M3m2w4tOxjlebr9Zrvkyb0QiTkw */

                public final void run() {
                    ContactsController.this.lambda$new$0$ContactsController();
                }
            });
        }
    }

    public /* synthetic */ void lambda$new$0$ContactsController() {
        try {
            if (hasContactsPermission()) {
                ApplicationLoader.applicationContext.getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, new MyContentObserver());
            }
        } catch (Throwable unused) {
        }
    }

    public void cleanup() {
        this.contactsBook.clear();
        this.contactsBookSPhones.clear();
        this.phoneBookContacts.clear();
        this.contacts.clear();
        this.contactsDict.clear();
        this.usersSectionsDict.clear();
        this.usersMutualSectionsDict.clear();
        this.sortedUsersSectionsArray.clear();
        this.sortedUsersMutualSectionsArray.clear();
        this.delayedContactsUpdate.clear();
        this.contactsByPhone.clear();
        this.contactsByShortPhone.clear();
        this.phoneBookSectionsDict.clear();
        this.phoneBookSectionsArray.clear();
        this.loadingContacts = false;
        this.contactsSyncInProgress = false;
        this.contactsLoaded = false;
        this.contactsBookLoaded = false;
        this.lastContactsVersions = "";
        this.loadingDeleteInfo = 0;
        this.deleteAccountTTL = 0;
        int i = 0;
        while (true) {
            int[] iArr = this.loadingPrivacyInfo;
            if (i < iArr.length) {
                iArr[i] = 0;
                i++;
            } else {
                this.lastseenPrivacyRules = null;
                this.groupPrivacyRules = null;
                this.callPrivacyRules = null;
                this.p2pPrivacyRules = null;
                this.profilePhotoPrivacyRules = null;
                this.forwardsPrivacyRules = null;
                this.phonePrivacyRules = null;
                Utilities.globalQueue.postRunnable(new Runnable() {
                    /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$IL0az8w5bDVT_3q821r68jQo1q8 */

                    public final void run() {
                        ContactsController.this.lambda$cleanup$1$ContactsController();
                    }
                });
                return;
            }
        }
    }

    public /* synthetic */ void lambda$cleanup$1$ContactsController() {
        this.migratingContacts = false;
        this.completedRequestsCount = 0;
    }

    public void checkInviteText() {
        SharedPreferences mainSettings = MessagesController.getMainSettings(this.currentAccount);
        this.inviteLink = mainSettings.getString("invitelink", null);
        int i = mainSettings.getInt("invitelinktime", 0);
        if (this.updatingInviteLink) {
            return;
        }
        if (this.inviteLink == null || Math.abs((System.currentTimeMillis() / 1000) - ((long) i)) >= 86400) {
            this.updatingInviteLink = true;
            getConnectionsManager().sendRequest(new TLRPC.TL_help_getInviteText(), new RequestDelegate() {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$SR5DLfvcYQqJEXCIbl9NdwNQO74 */

                @Override // im.guobwnxjuc.tgnet.RequestDelegate
                public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                    ContactsController.this.lambda$checkInviteText$3$ContactsController(tLObject, tL_error);
                }
            }, 2);
        }
    }

    public /* synthetic */ void lambda$checkInviteText$3$ContactsController(TLObject tLObject, TLRPC.TL_error tL_error) {
        if (tLObject != null) {
            TLRPC.TL_help_inviteText tL_help_inviteText = (TLRPC.TL_help_inviteText) tLObject;
            if (tL_help_inviteText.message.length() != 0) {
                AndroidUtilities.runOnUIThread(new Runnable(tL_help_inviteText) {
                    /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$dyoMayZ6H2KxiCP8ujmDUJJSgow */
                    private final /* synthetic */ TLRPC.TL_help_inviteText f$1;

                    {
                        this.f$1 = r2;
                    }

                    public final void run() {
                        ContactsController.this.lambda$null$2$ContactsController(this.f$1);
                    }
                });
            }
        }
    }

    public /* synthetic */ void lambda$null$2$ContactsController(TLRPC.TL_help_inviteText tL_help_inviteText) {
        this.updatingInviteLink = false;
        SharedPreferences.Editor edit = MessagesController.getMainSettings(this.currentAccount).edit();
        String str = tL_help_inviteText.message;
        this.inviteLink = str;
        edit.putString("invitelink", str);
        edit.putInt("invitelinktime", (int) (System.currentTimeMillis() / 1000));
        edit.commit();
    }

    public String getInviteText(int i) {
        String str = this.inviteLink;
        if (str == null) {
            str = "https://m12345.cc/dl";
        }
        if (i <= 1) {
            return LocaleController.formatString("InviteText2", R.string.InviteText2, str);
        }
        try {
            return String.format(LocaleController.getPluralString("InviteTextNum", i), Integer.valueOf(i), str);
        } catch (Exception unused) {
            return LocaleController.formatString("InviteText2", R.string.InviteText2, str);
        }
    }

    public void checkAppAccount() {
        boolean z;
        AccountManager accountManager = AccountManager.get(ApplicationLoader.applicationContext);
        try {
            Account[] accountsByType = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID);
            this.systemAccount = null;
            for (int i = 0; i < accountsByType.length; i++) {
                Account account = accountsByType[i];
                int i2 = 0;
                while (true) {
                    if (i2 >= 3) {
                        z = false;
                        break;
                    }
                    TLRPC.User currentUser = UserConfig.getInstance(i2).getCurrentUser();
                    if (currentUser != null) {
                        String str = account.name;
                        if (str.equals("" + currentUser.id)) {
                            if (i2 == this.currentAccount) {
                                this.systemAccount = account;
                            }
                            z = true;
                        }
                    }
                    i2++;
                }
                if (!z) {
                    try {
                        accountManager.removeAccount(accountsByType[i], null, null);
                    } catch (Exception unused) {
                    }
                }
            }
        } catch (Throwable unused2) {
        }
        if (getUserConfig().isClientActivated()) {
            readContacts();
            if (this.systemAccount == null) {
                try {
                    Account account2 = new Account("" + getUserConfig().getClientUserId(), BuildConfig.APPLICATION_ID);
                    this.systemAccount = account2;
                    accountManager.addAccountExplicitly(account2, "", null);
                } catch (Exception unused3) {
                }
            }
        }
    }

    public void deleteUnknownAppAccounts() {
        boolean z;
        try {
            this.systemAccount = null;
            AccountManager accountManager = AccountManager.get(ApplicationLoader.applicationContext);
            Account[] accountsByType = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID);
            for (int i = 0; i < accountsByType.length; i++) {
                Account account = accountsByType[i];
                int i2 = 0;
                while (true) {
                    if (i2 >= 3) {
                        z = false;
                        break;
                    }
                    TLRPC.User currentUser = UserConfig.getInstance(i2).getCurrentUser();
                    if (currentUser != null) {
                        String str = account.name;
                        if (str.equals("" + currentUser.id)) {
                            z = true;
                            break;
                        }
                    }
                    i2++;
                }
                if (!z) {
                    try {
                        accountManager.removeAccount(accountsByType[i], null, null);
                    } catch (Exception unused) {
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void checkContacts() {
        Utilities.globalQueue.postRunnable(new Runnable() {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$Ah6k_XgF_94emvz0P2UAubN0GaM */

            public final void run() {
                ContactsController.this.lambda$checkContacts$4$ContactsController();
            }
        });
    }

    public /* synthetic */ void lambda$checkContacts$4$ContactsController() {
        if (checkContactsInternal()) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("detected contacts change");
            }
            performSyncPhoneBook(getContactsCopy(this.contactsBook), true, false, true, false, true, false);
        }
    }

    public void forceImportContacts() {
        Utilities.globalQueue.postRunnable(new Runnable() {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$9lK9DjHnEUEmumZaBwzPCh8njvo */

            public final void run() {
                ContactsController.this.lambda$forceImportContacts$5$ContactsController();
            }
        });
    }

    public /* synthetic */ void lambda$forceImportContacts$5$ContactsController() {
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("force import contacts");
        }
        performSyncPhoneBook(new HashMap<>(), true, true, true, true, false, false);
    }

    public void syncPhoneBookByAlert(HashMap<String, Contact> hashMap, boolean z, boolean z2, boolean z3) {
        Utilities.globalQueue.postRunnable(new Runnable(hashMap, z, z2, z3) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$6zTEqyqXMCSskXDGpOLsPKEl8U */
            private final /* synthetic */ HashMap f$1;
            private final /* synthetic */ boolean f$2;
            private final /* synthetic */ boolean f$3;
            private final /* synthetic */ boolean f$4;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
                this.f$4 = r5;
            }

            public final void run() {
                ContactsController.this.lambda$syncPhoneBookByAlert$6$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4);
            }
        });
    }

    public /* synthetic */ void lambda$syncPhoneBookByAlert$6$ContactsController(HashMap hashMap, boolean z, boolean z2, boolean z3) {
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("sync contacts by alert");
        }
        performSyncPhoneBook(hashMap, true, z, z2, false, false, z3);
    }

    public void deleteAllContacts(Runnable runnable) {
        resetImportedContacts();
        TLRPC.TL_contacts_deleteContacts tL_contacts_deleteContacts = new TLRPC.TL_contacts_deleteContacts();
        int size = this.contacts.size();
        for (int i = 0; i < size; i++) {
            tL_contacts_deleteContacts.id.add(getMessagesController().getInputUser(this.contacts.get(i).user_id));
        }
        getConnectionsManager().sendRequest(tL_contacts_deleteContacts, new RequestDelegate(runnable) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$dsB65AZLimivwKu8qrSlcoVbmeA */
            private final /* synthetic */ Runnable f$1;

            {
                this.f$1 = r2;
            }

            @Override // im.guobwnxjuc.tgnet.RequestDelegate
            public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                ContactsController.this.lambda$deleteAllContacts$8$ContactsController(this.f$1, tLObject, tL_error);
            }
        });
    }

    public /* synthetic */ void lambda$deleteAllContacts$8$ContactsController(Runnable runnable, TLObject tLObject, TLRPC.TL_error tL_error) {
        if (tL_error == null) {
            this.contactsBookSPhones.clear();
            this.contactsBook.clear();
            this.completedRequestsCount = 0;
            this.migratingContacts = false;
            this.contactsSyncInProgress = false;
            this.contactsLoaded = false;
            this.loadingContacts = false;
            this.contactsBookLoaded = false;
            this.lastContactsVersions = "";
            AndroidUtilities.runOnUIThread(new Runnable(runnable) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$M99c3xLnUDr4Of0hXkvR4wWJ_Tg */
                private final /* synthetic */ Runnable f$1;

                {
                    this.f$1 = r2;
                }

                public final void run() {
                    ContactsController.this.lambda$null$7$ContactsController(this.f$1);
                }
            });
            return;
        }
        AndroidUtilities.runOnUIThread(runnable);
    }

    /* JADX WARNING: Can't wrap try/catch for region: R(8:0|1|2|(4:5|(2:6|(1:20)(3:8|(2:10|(3:22|12|21)(1:24))(1:23)|13))|14|3)|15|16|17|19) */
    /* JADX WARNING: Failed to process nested try/catch */
    /* JADX WARNING: Missing exception handler attribute for start block: B:15:0x0049 */
    public /* synthetic */ void lambda$null$7$ContactsController(Runnable runnable) {
        AccountManager accountManager = AccountManager.get(ApplicationLoader.applicationContext);
        Account[] accountsByType = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID);
        this.systemAccount = null;
        for (Account account : accountsByType) {
            int i = 0;
            while (true) {
                if (i >= 3) {
                    break;
                }
                TLRPC.User currentUser = UserConfig.getInstance(i).getCurrentUser();
                if (currentUser != null) {
                    if (account.name.equals("" + currentUser.id)) {
                        accountManager.removeAccount(account, null, null);
                        break;
                    }
                }
                i++;
            }
        }
        Account account2 = new Account("" + getUserConfig().getClientUserId(), BuildConfig.APPLICATION_ID);
        this.systemAccount = account2;
        accountManager.addAccountExplicitly(account2, "", null);
        getMessagesStorage().putCachedPhoneBook(new HashMap<>(), false, true);
        getMessagesStorage().putContacts(new ArrayList<>(), true);
        this.phoneBookContacts.clear();
        this.contacts.clear();
        this.contactsDict.clear();
        this.usersSectionsDict.clear();
        this.usersMutualSectionsDict.clear();
        this.sortedUsersSectionsArray.clear();
        this.phoneBookSectionsDict.clear();
        this.phoneBookSectionsArray.clear();
        this.delayedContactsUpdate.clear();
        this.sortedUsersMutualSectionsArray.clear();
        this.contactsByPhone.clear();
        this.contactsByShortPhone.clear();
        getNotificationCenter().postNotificationName(NotificationCenter.contactsDidLoad, new Object[0]);
        loadContacts(false, 0);
        runnable.run();
    }

    public void resetImportedContacts() {
        getConnectionsManager().sendRequest(new TLRPC.TL_contacts_resetSaved(), $$Lambda$ContactsController$BOcxPhqMYruR9yyXG75jpPsYDJc.INSTANCE);
    }

    /* JADX WARNING: Code restructure failed: missing block: B:23:0x0051, code lost:
        r3 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:24:0x0052, code lost:
        if (r2 != null) goto L_0x0054;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:26:?, code lost:
        r2.close();
     */
    /* JADX WARNING: Code restructure failed: missing block: B:27:0x0058, code lost:
        r2 = move-exception;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:28:0x0059, code lost:
        r0.addSuppressed(r2);
     */
    /* JADX WARNING: Code restructure failed: missing block: B:29:0x005c, code lost:
        throw r3;
     */
    private boolean checkContactsInternal() {
        boolean z = false;
        try {
            if (!hasContactsPermission()) {
                return false;
            }
            try {
                Cursor query = ApplicationLoader.applicationContext.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI, new String[]{"version"}, null, null, null);
                if (query != null) {
                    StringBuilder sb = new StringBuilder();
                    while (query.moveToNext()) {
                        sb.append(query.getString(query.getColumnIndex("version")));
                    }
                    String sb2 = sb.toString();
                    if (this.lastContactsVersions.length() != 0 && !this.lastContactsVersions.equals(sb2)) {
                        z = true;
                    }
                    this.lastContactsVersions = sb2;
                }
                if (query != null) {
                    query.close();
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
            return z;
        } catch (Exception e2) {
            FileLog.e(e2);
        }
    }

    public void readContacts() {
        synchronized (this.loadContactsSync) {
            if (!this.loadingContacts) {
                this.loadingContacts = true;
                Utilities.stageQueue.postRunnable(new Runnable() {
                    /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$BT18uZi9e84pz7XWYj7eTe5RDco */

                    public final void run() {
                        ContactsController.this.lambda$readContacts$10$ContactsController();
                    }
                });
            }
        }
    }

    public /* synthetic */ void lambda$readContacts$10$ContactsController() {
        if (!this.contacts.isEmpty() || this.contactsLoaded) {
            synchronized (this.loadContactsSync) {
                this.loadingContacts = false;
            }
            return;
        }
        loadContacts(true, 0);
    }

    public void syncRemoteContacts() {
        synchronized (this.loadContactsSync) {
            if (!this.loadingContacts) {
                this.loadingContacts = true;
                loadContacts(false, getContactsHash(this.contacts));
            }
        }
    }

    private boolean isNotValidNameString(String str) {
        if (TextUtils.isEmpty(str)) {
            return true;
        }
        int length = str.length();
        int i = 0;
        for (int i2 = 0; i2 < length; i2++) {
            char charAt = str.charAt(i2);
            if (charAt >= '0' && charAt <= '9') {
                i++;
            }
        }
        if (i > 3) {
            return true;
        }
        return false;
    }

    /* JADX DEBUG: Failed to insert an additional move for type inference into block B:193:0x0061 */
    /* JADX DEBUG: Failed to insert an additional move for type inference into block B:195:0x0061 */
    /* JADX WARN: Type inference failed for: r9v1 */
    /* JADX WARN: Type inference failed for: r9v4, types: [boolean, int] */
    /* JADX WARN: Type inference failed for: r9v5 */
    /* JADX WARN: Type inference failed for: r9v6 */
    /* JADX WARN: Type inference failed for: r9v8 */
    /* JADX WARNING: Removed duplicated region for block: B:176:0x032c A[Catch:{ all -> 0x0344 }] */
    /* JADX WARNING: Removed duplicated region for block: B:178:0x0331 A[SYNTHETIC, Splitter:B:178:0x0331] */
    /* JADX WARNING: Removed duplicated region for block: B:184:0x033e  */
    /* JADX WARNING: Removed duplicated region for block: B:202:? A[RETURN, SYNTHETIC] */
    /* JADX WARNING: Unknown variable types count: 1 */
    private HashMap<String, Contact> readContactsFromPhoneBook() {
        HashMap<String, Contact> hashMap;
        Cursor cursor;
        HashMap<String, Contact> hashMap2;
        Throwable th;
        ContentResolver contentResolver;
        Cursor cursor2;
        Cursor cursor3;
        HashMap<String, Contact> hashMap3;
        String str;
        ContentResolver contentResolver2;
        StringBuilder sb;
        String str2;
        ContactsController contactsController = this;
        if (!getUserConfig().syncContacts) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("contacts sync disabled");
            }
            return new HashMap<>();
        } else if (!hasContactsPermission()) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("app has no contacts permissions");
            }
            return new HashMap<>();
        } else {
            try {
                StringBuilder sb2 = new StringBuilder();
                ContentResolver contentResolver3 = ApplicationLoader.applicationContext.getContentResolver();
                HashMap hashMap4 = new HashMap();
                ArrayList arrayList = new ArrayList();
                cursor = contentResolver3.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, contactsController.projectionPhones, null, null, null);
                int i = 0;
                ?? r9 = 1;
                if (cursor != null) {
                    try {
                        int count = cursor.getCount();
                        if (count > 0) {
                            hashMap3 = new HashMap<>(count);
                            int i2 = 1;
                            while (cursor.moveToNext()) {
                                try {
                                    int i3 = r9 == true ? 1 : 0;
                                    int i4 = r9 == true ? 1 : 0;
                                    int i5 = r9 == true ? 1 : 0;
                                    int i6 = r9 == true ? 1 : 0;
                                    int i7 = r9 == true ? 1 : 0;
                                    String string = cursor.getString(i3);
                                    String string2 = cursor.getString(5);
                                    if (string2 == null) {
                                        string2 = "";
                                    }
                                    boolean z = string2.indexOf(".sim") != 0;
                                    if (!TextUtils.isEmpty(string)) {
                                        String stripExceptNumbers = PhoneFormat.stripExceptNumbers(string, r9);
                                        if (!TextUtils.isEmpty(stripExceptNumbers)) {
                                            if (stripExceptNumbers.startsWith(Marker.ANY_NON_NULL_MARKER)) {
                                                try {
                                                    str = stripExceptNumbers.substring(r9);
                                                } catch (Throwable th2) {
                                                    th = th2;
                                                }
                                            } else {
                                                str = stripExceptNumbers;
                                            }
                                            String string3 = cursor.getString(i);
                                            sb2.setLength(i);
                                            DatabaseUtils.appendEscapedSQLString(sb2, string3);
                                            String sb3 = sb2.toString();
                                            Contact contact = (Contact) hashMap4.get(str);
                                            if (contact == null) {
                                                if (!arrayList.contains(sb3)) {
                                                    arrayList.add(sb3);
                                                }
                                                int i8 = cursor.getInt(2);
                                                Contact contact2 = hashMap3.get(string3);
                                                if (contact2 == null) {
                                                    contact2 = new Contact();
                                                    sb = sb2;
                                                    String string4 = cursor.getString(4);
                                                    if (string4 == null) {
                                                        str2 = "";
                                                    } else {
                                                        str2 = string4.trim();
                                                    }
                                                    if (contactsController.isNotValidNameString(str2)) {
                                                        contact2.first_name = str2;
                                                        contact2.last_name = "";
                                                        contentResolver2 = contentResolver3;
                                                    } else {
                                                        contentResolver2 = contentResolver3;
                                                        int lastIndexOf = str2.lastIndexOf(32);
                                                        if (lastIndexOf != -1) {
                                                            contact2.first_name = str2.substring(0, lastIndexOf).trim();
                                                            contact2.last_name = str2.substring(lastIndexOf + 1).trim();
                                                        } else {
                                                            contact2.first_name = str2;
                                                            contact2.last_name = "";
                                                        }
                                                    }
                                                    contact2.provider = string2;
                                                    contact2.isGoodProvider = z;
                                                    contact2.key = string3;
                                                    contact2.contact_id = i2;
                                                    hashMap3.put(string3, contact2);
                                                    i2++;
                                                } else {
                                                    sb = sb2;
                                                    contentResolver2 = contentResolver3;
                                                }
                                                contact2.shortPhones.add(str);
                                                contact2.phones.add(stripExceptNumbers);
                                                contact2.phoneDeleted.add(0);
                                                if (i8 == 0) {
                                                    String string5 = cursor.getString(3);
                                                    ArrayList<String> arrayList2 = contact2.phoneTypes;
                                                    if (string5 == null) {
                                                        string5 = LocaleController.getString("PhoneMobile", R.string.PhoneMobile);
                                                    }
                                                    arrayList2.add(string5);
                                                } else if (i8 == 1) {
                                                    contact2.phoneTypes.add(LocaleController.getString("PhoneHome", R.string.PhoneHome));
                                                } else if (i8 == 2) {
                                                    contact2.phoneTypes.add(LocaleController.getString("PhoneMobile", R.string.PhoneMobile));
                                                } else if (i8 == 3) {
                                                    contact2.phoneTypes.add(LocaleController.getString("PhoneWork", R.string.PhoneWork));
                                                } else if (i8 == 12) {
                                                    contact2.phoneTypes.add(LocaleController.getString("PhoneMain", R.string.PhoneMain));
                                                } else {
                                                    contact2.phoneTypes.add(LocaleController.getString("PhoneOther", R.string.PhoneOther));
                                                }
                                                hashMap4.put(str, contact2);
                                                sb2 = sb;
                                                contentResolver3 = contentResolver2;
                                                r9 = 1;
                                                i = 0;
                                                contactsController = this;
                                            } else if (!contact.isGoodProvider && !string2.equals(contact.provider)) {
                                                sb2.setLength(0);
                                                DatabaseUtils.appendEscapedSQLString(sb2, contact.key);
                                                arrayList.remove(sb2.toString());
                                                arrayList.add(sb3);
                                                contact.key = string3;
                                                contact.isGoodProvider = z;
                                                contact.provider = string2;
                                            }
                                        }
                                    }
                                    r9 = 1;
                                    i = 0;
                                } catch (Throwable th3) {
                                    th = th3;
                                    hashMap2 = hashMap3;
                                    try {
                                        FileLog.e(th);
                                        if (hashMap2 != null) {
                                        }
                                        hashMap = hashMap2;
                                        if (hashMap == null) {
                                        }
                                    } finally {
                                        if (cursor != null) {
                                            try {
                                                cursor.close();
                                            } catch (Exception e) {
                                                FileLog.e(e);
                                            }
                                        }
                                    }
                                }
                            }
                            contentResolver = contentResolver3;
                        } else {
                            contentResolver = contentResolver3;
                            hashMap3 = null;
                        }
                        try {
                            cursor.close();
                        } catch (Exception unused) {
                        }
                        hashMap = hashMap3;
                        cursor2 = null;
                    } catch (Throwable th4) {
                        th = th4;
                        hashMap2 = null;
                        FileLog.e(th);
                        if (hashMap2 != null) {
                        }
                        hashMap = hashMap2;
                        if (hashMap == null) {
                        }
                    }
                } else {
                    contentResolver = contentResolver3;
                    cursor2 = cursor;
                    hashMap = null;
                }
                try {
                    String join = TextUtils.join(",", arrayList);
                    Uri uri = ContactsContract.Data.CONTENT_URI;
                    try {
                        Cursor query = contentResolver.query(uri, this.projectionNames, "lookup IN (" + join + ") AND " + "mimetype" + " = '" + "vnd.android.cursor.item/name" + "'", null, null);
                        if (query != null) {
                            while (query.moveToNext()) {
                                String string6 = query.getString(0);
                                String string7 = query.getString(1);
                                String string8 = query.getString(2);
                                String string9 = query.getString(3);
                                Contact contact3 = hashMap.get(string6);
                                if (contact3 != null && !contact3.namesFilled) {
                                    if (contact3.isGoodProvider) {
                                        if (string7 != null) {
                                            contact3.first_name = string7;
                                        } else {
                                            contact3.first_name = "";
                                        }
                                        if (string8 != null) {
                                            contact3.last_name = string8;
                                        } else {
                                            contact3.last_name = "";
                                        }
                                        if (!TextUtils.isEmpty(string9)) {
                                            if (!TextUtils.isEmpty(contact3.first_name)) {
                                                contact3.first_name += " " + string9;
                                            } else {
                                                contact3.first_name = string9;
                                            }
                                        }
                                    } else if ((!isNotValidNameString(string7) && (contact3.first_name.contains(string7) || string7.contains(contact3.first_name))) || (!isNotValidNameString(string8) && (contact3.last_name.contains(string8) || string7.contains(contact3.last_name)))) {
                                        if (string7 != null) {
                                            contact3.first_name = string7;
                                        } else {
                                            contact3.first_name = "";
                                        }
                                        if (!TextUtils.isEmpty(string9)) {
                                            if (!TextUtils.isEmpty(contact3.first_name)) {
                                                contact3.first_name += " " + string9;
                                            } else {
                                                contact3.first_name = string9;
                                            }
                                        }
                                        if (string8 != null) {
                                            contact3.last_name = string8;
                                        } else {
                                            contact3.last_name = "";
                                        }
                                    }
                                    contact3.namesFilled = true;
                                }
                            }
                            try {
                                query.close();
                            } catch (Exception unused2) {
                            }
                            cursor3 = null;
                        } else {
                            cursor3 = query;
                        }
                        if (cursor3 != null) {
                            try {
                                cursor3.close();
                            } catch (Exception e2) {
                                FileLog.e(e2);
                            }
                        }
                    } catch (Throwable th5) {
                        th = th5;
                        cursor = cursor2;
                        hashMap2 = hashMap;
                        FileLog.e(th);
                        if (hashMap2 != null) {
                        }
                        hashMap = hashMap2;
                        if (hashMap == null) {
                        }
                    }
                } catch (Throwable th6) {
                    th = th6;
                    cursor = cursor2;
                    hashMap2 = hashMap;
                    FileLog.e(th);
                    if (hashMap2 != null) {
                    }
                    hashMap = hashMap2;
                    if (hashMap == null) {
                    }
                }
            } catch (Throwable th7) {
                th = th7;
                hashMap2 = null;
                cursor = null;
                FileLog.e(th);
                if (hashMap2 != null) {
                    hashMap2.clear();
                }
                hashMap = hashMap2;
                if (hashMap == null) {
                }
            }
            return hashMap == null ? hashMap : new HashMap<>();
        }
    }

    public HashMap<String, Contact> getContactsCopy(HashMap<String, Contact> hashMap) {
        HashMap<String, Contact> hashMap2 = new HashMap<>();
        for (Map.Entry<String, Contact> entry : hashMap.entrySet()) {
            Contact contact = new Contact();
            Contact value = entry.getValue();
            contact.phoneDeleted.addAll(value.phoneDeleted);
            contact.phones.addAll(value.phones);
            contact.phoneTypes.addAll(value.phoneTypes);
            contact.shortPhones.addAll(value.shortPhones);
            contact.first_name = value.first_name;
            contact.last_name = value.last_name;
            contact.contact_id = value.contact_id;
            contact.key = value.key;
            hashMap2.put(contact.key, contact);
        }
        return hashMap2;
    }

    /* access modifiers changed from: protected */
    public void migratePhoneBookToV7(SparseArray<Contact> sparseArray) {
        Utilities.globalQueue.postRunnable(new Runnable(sparseArray) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$tNY5zRWqt3AowOkS0GJ4_rXulkQ */
            private final /* synthetic */ SparseArray f$1;

            {
                this.f$1 = r2;
            }

            public final void run() {
                ContactsController.this.lambda$migratePhoneBookToV7$11$ContactsController(this.f$1);
            }
        });
    }

    public /* synthetic */ void lambda$migratePhoneBookToV7$11$ContactsController(SparseArray sparseArray) {
        if (!this.migratingContacts) {
            this.migratingContacts = true;
            HashMap<String, Contact> hashMap = new HashMap<>();
            HashMap<String, Contact> readContactsFromPhoneBook = readContactsFromPhoneBook();
            HashMap hashMap2 = new HashMap();
            Iterator<Map.Entry<String, Contact>> it = readContactsFromPhoneBook.entrySet().iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                Contact value = it.next().getValue();
                for (int i = 0; i < value.shortPhones.size(); i++) {
                    hashMap2.put(value.shortPhones.get(i), value.key);
                }
            }
            for (int i2 = 0; i2 < sparseArray.size(); i2++) {
                Contact contact = (Contact) sparseArray.valueAt(i2);
                int i3 = 0;
                while (true) {
                    if (i3 >= contact.shortPhones.size()) {
                        break;
                    }
                    String str = (String) hashMap2.get(contact.shortPhones.get(i3));
                    if (str != null) {
                        contact.key = str;
                        hashMap.put(str, contact);
                        break;
                    }
                    i3++;
                }
            }
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("migrated contacts " + hashMap.size() + " of " + sparseArray.size());
            }
            getMessagesStorage().putCachedPhoneBook(hashMap, true, false);
        }
    }

    public void checkPhonebookUsers() {
        Utilities.globalQueue.postRunnable(new Runnable() {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$yhxbIIH2CbzoX2JHyqgaX4muv8U */

            public final void run() {
                ContactsController.this.lambda$checkPhonebookUsers$14$ContactsController();
            }
        });
    }

    public /* synthetic */ void lambda$checkPhonebookUsers$14$ContactsController() {
        int i;
        HashMap<String, Contact> readContactsFromPhoneBook = readContactsFromPhoneBook();
        ArrayList arrayList = new ArrayList();
        HashMap hashMap = new HashMap();
        Iterator<Map.Entry<String, Contact>> it = readContactsFromPhoneBook.entrySet().iterator();
        while (true) {
            i = 0;
            if (!it.hasNext()) {
                break;
            }
            Map.Entry<String, Contact> next = it.next();
            Contact value = next.getValue();
            next.getKey();
            while (i < value.phones.size()) {
                TLRPC.TL_inputPhoneContact tL_inputPhoneContact = new TLRPC.TL_inputPhoneContact();
                tL_inputPhoneContact.client_id = (long) value.contact_id;
                tL_inputPhoneContact.client_id |= ((long) i) << 32;
                tL_inputPhoneContact.first_name = value.first_name;
                tL_inputPhoneContact.last_name = value.last_name;
                tL_inputPhoneContact.phone = value.phones.get(i);
                arrayList.add(tL_inputPhoneContact);
                hashMap.put(value.phones.get(i), tL_inputPhoneContact);
                i++;
            }
        }
        this.completedRequestsCount = 0;
        ArrayList arrayList2 = new ArrayList();
        double size = (double) arrayList.size();
        Double.isNaN(size);
        int ceil = (int) Math.ceil(size / 500.0d);
        while (i < ceil) {
            TLRPC.TL_contacts_importContacts tL_contacts_importContacts = new TLRPC.TL_contacts_importContacts();
            int i2 = i * 500;
            tL_contacts_importContacts.contacts = new ArrayList<>(arrayList.subList(i2, Math.min(i2 + 500, arrayList.size())));
            getConnectionsManager().sendRequest(tL_contacts_importContacts, new RequestDelegate(arrayList2, ceil, hashMap) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$_8hNMqPoh1m1wbzPubsYFXzjG8Y */
                private final /* synthetic */ ArrayList f$1;
                private final /* synthetic */ int f$2;
                private final /* synthetic */ HashMap f$3;

                {
                    this.f$1 = r2;
                    this.f$2 = r3;
                    this.f$3 = r4;
                }

                @Override // im.guobwnxjuc.tgnet.RequestDelegate
                public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                    ContactsController.this.lambda$null$13$ContactsController(this.f$1, this.f$2, this.f$3, tLObject, tL_error);
                }
            }, 6);
            i++;
        }
    }

    public /* synthetic */ void lambda$null$13$ContactsController(ArrayList arrayList, int i, HashMap hashMap, TLObject tLObject, TLRPC.TL_error tL_error) {
        this.completedRequestsCount++;
        if (tL_error == null) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("contacts imported");
            }
            TLRPC.TL_contacts_importedContacts tL_contacts_importedContacts = (TLRPC.TL_contacts_importedContacts) tLObject;
            if (tL_contacts_importedContacts.users != null && !tL_contacts_importedContacts.users.isEmpty()) {
                arrayList.addAll(tL_contacts_importedContacts.users);
            }
        }
        if (this.completedRequestsCount == i) {
            AndroidUtilities.runOnUIThread(new Runnable(arrayList, hashMap) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$KX0esa7NbgUq7TNjkW718iPjIyQ */
                private final /* synthetic */ ArrayList f$1;
                private final /* synthetic */ HashMap f$2;

                {
                    this.f$1 = r2;
                    this.f$2 = r3;
                }

                public final void run() {
                    ContactsController.this.lambda$null$12$ContactsController(this.f$1, this.f$2);
                }
            });
        }
    }

    public /* synthetic */ void lambda$null$12$ContactsController(ArrayList arrayList, HashMap hashMap) {
        getNotificationCenter().postNotificationName(NotificationCenter.contactAboutPhonebookLoaded, arrayList, hashMap);
    }

    /* access modifiers changed from: protected */
    public void performSyncPhoneBook(HashMap<String, Contact> hashMap, boolean z, boolean z2, boolean z3, boolean z4, boolean z5, boolean z6) {
        if (z2 || this.contactsBookLoaded) {
            Utilities.globalQueue.postRunnable(new Runnable(hashMap, z3, z, z2, z4, z5, z6) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$3lasTheRfWGxYAB8gob2CaWR_0 */
                private final /* synthetic */ HashMap f$1;
                private final /* synthetic */ boolean f$2;
                private final /* synthetic */ boolean f$3;
                private final /* synthetic */ boolean f$4;
                private final /* synthetic */ boolean f$5;
                private final /* synthetic */ boolean f$6;
                private final /* synthetic */ boolean f$7;

                {
                    this.f$1 = r2;
                    this.f$2 = r3;
                    this.f$3 = r4;
                    this.f$4 = r5;
                    this.f$5 = r6;
                    this.f$6 = r7;
                    this.f$7 = r8;
                }

                public final void run() {
                    ContactsController.this.lambda$performSyncPhoneBook$27$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4, this.f$5, this.f$6, this.f$7);
                }
            });
        }
    }

    /* JADX WARNING: Code restructure failed: missing block: B:121:0x02f9, code lost:
        if (r11.intValue() == 1) goto L_0x030b;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:215:0x04c6, code lost:
        if ((r13.contactsByPhone.size() - r0) > ((r13.contactsByPhone.size() / 3) * 2)) goto L_0x04ca;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:42:0x0133, code lost:
        if (r2.first_name.equals(r4.first_name) != false) goto L_0x0138;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:47:0x0148, code lost:
        if (r2.last_name.equals(r4.last_name) == false) goto L_0x014a;
     */
    /* JADX WARNING: Code restructure failed: missing block: B:48:0x014a, code lost:
        r0 = true;
     */
    /* JADX WARNING: Removed duplicated region for block: B:107:0x0271  */
    /* JADX WARNING: Removed duplicated region for block: B:116:0x02c3  */
    /* JADX WARNING: Removed duplicated region for block: B:134:0x0346  */
    /* JADX WARNING: Removed duplicated region for block: B:219:0x04ce  */
    /* JADX WARNING: Removed duplicated region for block: B:221:0x04fa  */
    /* JADX WARNING: Removed duplicated region for block: B:223:0x050c  */
    /* JADX WARNING: Removed duplicated region for block: B:246:0x0349 A[SYNTHETIC] */
    /* JADX WARNING: Removed duplicated region for block: B:37:0x011b  */
    /* JADX WARNING: Removed duplicated region for block: B:39:0x0121  */
    /* JADX WARNING: Removed duplicated region for block: B:49:0x014c  */
    /* JADX WARNING: Removed duplicated region for block: B:80:0x01e5  */
    public /* synthetic */ void lambda$performSyncPhoneBook$27$ContactsController(HashMap hashMap, boolean z, boolean z2, boolean z3, boolean z4, boolean z5, boolean z6) {
        int i;
        ArrayList arrayList;
        HashMap hashMap2;
        HashMap hashMap3;
        ContactsController contactsController;
        int i2;
        int i3;
        Contact contact;
        HashMap hashMap4;
        ArrayList arrayList2;
        HashMap hashMap5;
        String str;
        HashMap hashMap6;
        int i4;
        int i5;
        boolean z7;
        int i6;
        ArrayList arrayList3;
        int i7;
        boolean z8;
        int i8;
        ContactsController contactsController2 = this;
        HashMap hashMap7 = new HashMap();
        Iterator it = hashMap.entrySet().iterator();
        while (true) {
            i = 0;
            if (!it.hasNext()) {
                break;
            }
            Contact contact2 = (Contact) ((Map.Entry) it.next()).getValue();
            while (i < contact2.shortPhones.size()) {
                hashMap7.put(contact2.shortPhones.get(i), contact2);
                i++;
            }
        }
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("start read contacts from phone");
        }
        if (!z) {
            checkContactsInternal();
        }
        HashMap<String, Contact> readContactsFromPhoneBook = readContactsFromPhoneBook();
        HashMap hashMap8 = new HashMap();
        HashMap hashMap9 = new HashMap();
        ArrayList arrayList4 = new ArrayList();
        for (Map.Entry<String, Contact> entry : readContactsFromPhoneBook.entrySet()) {
            Contact value = entry.getValue();
            int size = value.shortPhones.size();
            for (int i9 = 0; i9 < size; i9++) {
                String str2 = value.shortPhones.get(i9);
                hashMap9.put(str2.substring(Math.max(0, str2.length() - 7)), value);
            }
            String letter = value.getLetter();
            ArrayList arrayList5 = (ArrayList) hashMap8.get(letter);
            if (arrayList5 == null) {
                arrayList5 = new ArrayList();
                hashMap8.put(letter, arrayList5);
                arrayList4.add(letter);
            }
            arrayList5.add(value);
        }
        HashMap hashMap10 = new HashMap();
        int size2 = hashMap.size();
        ArrayList arrayList6 = new ArrayList();
        String str3 = "";
        if (!hashMap.isEmpty()) {
            Iterator<Map.Entry<String, Contact>> it2 = readContactsFromPhoneBook.entrySet().iterator();
            i2 = 0;
            int i10 = 0;
            while (it2.hasNext()) {
                Map.Entry<String, Contact> next = it2.next();
                String key = next.getKey();
                Contact value2 = next.getValue();
                Contact contact3 = (Contact) hashMap.get(key);
                if (contact3 == null) {
                    while (true) {
                        if (i >= value2.shortPhones.size()) {
                            break;
                        }
                        contact = (Contact) hashMap7.get(value2.shortPhones.get(i));
                        if (contact != null) {
                            key = contact.key;
                            break;
                        }
                        i++;
                    }
                    if (contact != null) {
                        value2.imported = contact.imported;
                    }
                    if (contact == null) {
                        if (!TextUtils.isEmpty(value2.first_name)) {
                            hashMap4 = hashMap7;
                        } else {
                            hashMap4 = hashMap7;
                        }
                        if (!TextUtils.isEmpty(value2.last_name)) {
                        }
                    } else {
                        hashMap4 = hashMap7;
                    }
                    boolean z9 = false;
                    if (contact != null || z9) {
                        str = str3;
                        arrayList2 = arrayList4;
                        hashMap6 = hashMap9;
                        hashMap5 = hashMap8;
                        i5 = 0;
                        while (i5 < value2.phones.size()) {
                            String str4 = value2.shortPhones.get(i5);
                            str4.substring(Math.max(0, str4.length() - 7));
                            hashMap10.put(str4, value2);
                            if (contact != null) {
                                int indexOf = contact.shortPhones.indexOf(str4);
                                if (indexOf != -1) {
                                    Integer num = contact.phoneDeleted.get(indexOf);
                                    value2.phoneDeleted.set(i5, num);
                                }
                            }
                            if (z2) {
                                if (!z9) {
                                    if (contactsController2.contactsByPhone.containsKey(str4)) {
                                        i10++;
                                    } else {
                                        i2++;
                                    }
                                }
                                TLRPC.TL_inputPhoneContact tL_inputPhoneContact = new TLRPC.TL_inputPhoneContact();
                                tL_inputPhoneContact.client_id = (long) value2.contact_id;
                                z7 = z9;
                                i6 = size2;
                                tL_inputPhoneContact.client_id = (((long) i5) << 32) | tL_inputPhoneContact.client_id;
                                tL_inputPhoneContact.first_name = value2.first_name;
                                tL_inputPhoneContact.last_name = value2.last_name;
                                tL_inputPhoneContact.phone = value2.phones.get(i5);
                                arrayList6.add(tL_inputPhoneContact);
                                i5++;
                                size2 = i6;
                                z9 = z7;
                                contactsController2 = this;
                            }
                            z7 = z9;
                            i6 = size2;
                            i5++;
                            size2 = i6;
                            z9 = z7;
                            contactsController2 = this;
                        }
                        i4 = size2;
                        if (contact == null) {
                            hashMap.remove(key);
                        }
                    } else {
                        int i11 = 0;
                        while (i11 < value2.phones.size()) {
                            String str5 = value2.shortPhones.get(i11);
                            String substring = str5.substring(Math.max(0, str5.length() - 7));
                            hashMap10.put(str5, value2);
                            int indexOf2 = contact.shortPhones.indexOf(str5);
                            if (z2) {
                                i8 = indexOf2;
                                TLRPC.Contact contact4 = contactsController2.contactsByPhone.get(str5);
                                if (contact4 != null) {
                                    arrayList3 = arrayList4;
                                    TLRPC.User user = getMessagesController().getUser(Integer.valueOf(contact4.user_id));
                                    if (user != null) {
                                        i10++;
                                        if (TextUtils.isEmpty(user.first_name) && TextUtils.isEmpty(user.last_name) && (!TextUtils.isEmpty(value2.first_name) || !TextUtils.isEmpty(value2.last_name))) {
                                            z8 = true;
                                            i8 = -1;
                                            i7 = i8;
                                            if (i7 != -1) {
                                                value2.phoneDeleted.set(i11, contact.phoneDeleted.get(i7));
                                                contact.phones.remove(i7);
                                                contact.shortPhones.remove(i7);
                                                contact.phoneDeleted.remove(i7);
                                                contact.phoneTypes.remove(i7);
                                            } else if (z2) {
                                                if (!z8) {
                                                    TLRPC.Contact contact5 = contactsController2.contactsByPhone.get(str5);
                                                    if (contact5 != null) {
                                                        TLRPC.User user2 = getMessagesController().getUser(Integer.valueOf(contact5.user_id));
                                                        if (user2 != null) {
                                                            i10++;
                                                            String str6 = user2.first_name != null ? user2.first_name : str3;
                                                            String str7 = user2.last_name != null ? user2.last_name : str3;
                                                            if (str6.equals(value2.first_name)) {
                                                                if (str7.equals(value2.last_name)) {
                                                                }
                                                            }
                                                            if (TextUtils.isEmpty(value2.first_name) && TextUtils.isEmpty(value2.last_name)) {
                                                            }
                                                        } else {
                                                            i2++;
                                                        }
                                                    } else if (contactsController2.contactsByShortPhone.containsKey(substring)) {
                                                        i10++;
                                                    }
                                                }
                                                TLRPC.TL_inputPhoneContact tL_inputPhoneContact2 = new TLRPC.TL_inputPhoneContact();
                                                tL_inputPhoneContact2.client_id = (long) value2.contact_id;
                                                tL_inputPhoneContact2.client_id = (((long) i11) << 32) | tL_inputPhoneContact2.client_id;
                                                tL_inputPhoneContact2.first_name = value2.first_name;
                                                tL_inputPhoneContact2.last_name = value2.last_name;
                                                tL_inputPhoneContact2.phone = value2.phones.get(i11);
                                                arrayList6.add(tL_inputPhoneContact2);
                                                i2 = i2;
                                            }
                                            i11++;
                                            hashMap9 = hashMap9;
                                            str3 = str3;
                                            hashMap8 = hashMap8;
                                            arrayList4 = arrayList3;
                                        }
                                    }
                                    z8 = false;
                                    i7 = i8;
                                    if (i7 != -1) {
                                    }
                                    i11++;
                                    hashMap9 = hashMap9;
                                    str3 = str3;
                                    hashMap8 = hashMap8;
                                    arrayList4 = arrayList3;
                                } else {
                                    arrayList3 = arrayList4;
                                    if (contactsController2.contactsByShortPhone.containsKey(substring)) {
                                        i10++;
                                    }
                                }
                            } else {
                                arrayList3 = arrayList4;
                                i8 = indexOf2;
                            }
                            i7 = i8;
                            z8 = false;
                            if (i7 != -1) {
                            }
                            i11++;
                            hashMap9 = hashMap9;
                            str3 = str3;
                            hashMap8 = hashMap8;
                            arrayList4 = arrayList3;
                        }
                        str = str3;
                        arrayList2 = arrayList4;
                        hashMap6 = hashMap9;
                        hashMap5 = hashMap8;
                        if (contact.phones.isEmpty()) {
                            hashMap.remove(key);
                        }
                        i4 = size2;
                    }
                    it2 = it2;
                    size2 = i4;
                    hashMap7 = hashMap4;
                    hashMap9 = hashMap6;
                    str3 = str;
                    hashMap8 = hashMap5;
                    arrayList4 = arrayList2;
                    i = 0;
                    contactsController2 = this;
                }
                contact = contact3;
                if (contact != null) {
                }
                if (contact == null) {
                }
                boolean z92 = false;
                if (contact != null) {
                }
                str = str3;
                arrayList2 = arrayList4;
                hashMap6 = hashMap9;
                hashMap5 = hashMap8;
                i5 = 0;
                while (i5 < value2.phones.size()) {
                }
                i4 = size2;
                if (contact == null) {
                }
                it2 = it2;
                size2 = i4;
                hashMap7 = hashMap4;
                hashMap9 = hashMap6;
                str3 = str;
                hashMap8 = hashMap5;
                arrayList4 = arrayList2;
                i = 0;
                contactsController2 = this;
            }
            arrayList = arrayList4;
            hashMap3 = hashMap9;
            hashMap2 = hashMap8;
            if (z3 || !hashMap.isEmpty() || !arrayList6.isEmpty()) {
                size2 = size2;
            } else {
                size2 = size2;
                if (size2 == readContactsFromPhoneBook.size()) {
                    if (BuildVars.LOGS_ENABLED) {
                        FileLog.d("contacts not changed!");
                        return;
                    }
                    return;
                }
            }
            if (z2 && !hashMap.isEmpty() && !readContactsFromPhoneBook.isEmpty() && arrayList6.isEmpty()) {
                getMessagesStorage().putCachedPhoneBook(readContactsFromPhoneBook, false, false);
            }
            contactsController = this;
            i3 = i10;
        } else {
            arrayList = arrayList4;
            hashMap3 = hashMap9;
            hashMap2 = hashMap8;
            if (z2) {
                int i12 = 0;
                for (Map.Entry<String, Contact> entry2 : readContactsFromPhoneBook.entrySet()) {
                    Contact value3 = entry2.getValue();
                    entry2.getKey();
                    for (int i13 = 0; i13 < value3.phones.size(); i13++) {
                        if (!z4) {
                            String str8 = value3.shortPhones.get(i13);
                            String substring2 = str8.substring(Math.max(0, str8.length() - 7));
                            TLRPC.Contact contact6 = this.contactsByPhone.get(str8);
                            if (contact6 != null) {
                                TLRPC.User user3 = getMessagesController().getUser(Integer.valueOf(contact6.user_id));
                                if (user3 != null) {
                                    i12++;
                                    String str9 = user3.first_name != null ? user3.first_name : str3;
                                    String str10 = user3.last_name != null ? user3.last_name : str3;
                                    if ((str9.equals(value3.first_name) && str10.equals(value3.last_name)) || (TextUtils.isEmpty(value3.first_name) && TextUtils.isEmpty(value3.last_name))) {
                                    }
                                }
                            } else if (this.contactsByShortPhone.containsKey(substring2)) {
                                i12++;
                            }
                        }
                        TLRPC.TL_inputPhoneContact tL_inputPhoneContact3 = new TLRPC.TL_inputPhoneContact();
                        tL_inputPhoneContact3.client_id = (long) value3.contact_id;
                        tL_inputPhoneContact3.client_id |= ((long) i13) << 32;
                        tL_inputPhoneContact3.first_name = value3.first_name;
                        tL_inputPhoneContact3.last_name = value3.last_name;
                        tL_inputPhoneContact3.phone = value3.phones.get(i13);
                        arrayList6.add(tL_inputPhoneContact3);
                    }
                }
                contactsController = this;
                i3 = i12;
            } else {
                contactsController = this;
                i3 = 0;
            }
            i2 = 0;
        }
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("done processing contacts");
        }
        if (!z2) {
            Utilities.stageQueue.postRunnable(new Runnable(hashMap10, readContactsFromPhoneBook, z3, hashMap2, arrayList, hashMap3) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$GHecTqMf1wkvbIokHruMGFmR1Fo */
                private final /* synthetic */ HashMap f$1;
                private final /* synthetic */ HashMap f$2;
                private final /* synthetic */ boolean f$3;
                private final /* synthetic */ HashMap f$4;
                private final /* synthetic */ ArrayList f$5;
                private final /* synthetic */ HashMap f$6;

                {
                    this.f$1 = r2;
                    this.f$2 = r3;
                    this.f$3 = r4;
                    this.f$4 = r5;
                    this.f$5 = r6;
                    this.f$6 = r7;
                }

                public final void run() {
                    ContactsController.this.lambda$null$26$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4, this.f$5, this.f$6);
                }
            });
            if (!readContactsFromPhoneBook.isEmpty()) {
                getMessagesStorage().putCachedPhoneBook(readContactsFromPhoneBook, false, false);
            }
        } else if (!arrayList6.isEmpty()) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.e("start import contacts");
            }
            int i14 = 2;
            if (z5 && i2 != 0) {
                if (i2 >= 30) {
                    i14 = 1;
                } else if (z3) {
                    if (size2 == 0) {
                    }
                }
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.d("new phone book contacts " + i2 + " serverContactsInPhonebook " + i3 + " totalContacts " + contactsController.contactsByPhone.size());
                }
                if (i14 == 0) {
                    AndroidUtilities.runOnUIThread(new Runnable(i14, hashMap, z3, z) {
                        /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$5suKhc2OPzkWUv3AcnrBjmDj9ns */
                        private final /* synthetic */ int f$1;
                        private final /* synthetic */ HashMap f$2;
                        private final /* synthetic */ boolean f$3;
                        private final /* synthetic */ boolean f$4;

                        {
                            this.f$1 = r2;
                            this.f$2 = r3;
                            this.f$3 = r4;
                            this.f$4 = r5;
                        }

                        public final void run() {
                            ContactsController.this.lambda$null$16$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4);
                        }
                    });
                    return;
                } else if (z6) {
                    Utilities.stageQueue.postRunnable(new Runnable(hashMap10, readContactsFromPhoneBook, z3, hashMap2, arrayList, hashMap3) {
                        /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$FEu2JqrLN32KYfdqbeH60HeQkMc */
                        private final /* synthetic */ HashMap f$1;
                        private final /* synthetic */ HashMap f$2;
                        private final /* synthetic */ boolean f$3;
                        private final /* synthetic */ HashMap f$4;
                        private final /* synthetic */ ArrayList f$5;
                        private final /* synthetic */ HashMap f$6;

                        {
                            this.f$1 = r2;
                            this.f$2 = r3;
                            this.f$3 = r4;
                            this.f$4 = r5;
                            this.f$5 = r6;
                            this.f$6 = r7;
                        }

                        public final void run() {
                            ContactsController.this.lambda$null$18$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4, this.f$5, this.f$6);
                        }
                    });
                    return;
                } else {
                    boolean[] zArr = {false};
                    HashMap hashMap11 = new HashMap(readContactsFromPhoneBook);
                    SparseArray sparseArray = new SparseArray();
                    for (Map.Entry entry3 : hashMap11.entrySet()) {
                        Contact contact7 = (Contact) entry3.getValue();
                        sparseArray.put(contact7.contact_id, contact7.key);
                    }
                    contactsController.completedRequestsCount = 0;
                    double size3 = (double) arrayList6.size();
                    Double.isNaN(size3);
                    int ceil = (int) Math.ceil(size3 / 500.0d);
                    int i15 = 0;
                    while (i15 < ceil) {
                        TLRPC.TL_contacts_importContacts tL_contacts_importContacts = new TLRPC.TL_contacts_importContacts();
                        int i16 = i15 * 500;
                        tL_contacts_importContacts.contacts = new ArrayList<>(arrayList6.subList(i16, Math.min(i16 + 500, arrayList6.size())));
                        getConnectionsManager().sendRequest(tL_contacts_importContacts, new RequestDelegate(hashMap11, sparseArray, zArr, readContactsFromPhoneBook, tL_contacts_importContacts, ceil, hashMap10, z3, hashMap2, arrayList, hashMap3) {
                            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$0H7Ds_QS_F8zqO9Cr_Lotos0x2E */
                            private final /* synthetic */ HashMap f$1;
                            private final /* synthetic */ ArrayList f$10;
                            private final /* synthetic */ HashMap f$11;
                            private final /* synthetic */ SparseArray f$2;
                            private final /* synthetic */ boolean[] f$3;
                            private final /* synthetic */ HashMap f$4;
                            private final /* synthetic */ TLRPC.TL_contacts_importContacts f$5;
                            private final /* synthetic */ int f$6;
                            private final /* synthetic */ HashMap f$7;
                            private final /* synthetic */ boolean f$8;
                            private final /* synthetic */ HashMap f$9;

                            {
                                this.f$1 = r2;
                                this.f$2 = r3;
                                this.f$3 = r4;
                                this.f$4 = r5;
                                this.f$5 = r6;
                                this.f$6 = r7;
                                this.f$7 = r8;
                                this.f$8 = r9;
                                this.f$9 = r10;
                                this.f$10 = r11;
                                this.f$11 = r12;
                            }

                            @Override // im.guobwnxjuc.tgnet.RequestDelegate
                            public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                                ContactsController.this.lambda$null$22$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4, this.f$5, this.f$6, this.f$7, this.f$8, this.f$9, this.f$10, this.f$11, tLObject, tL_error);
                            }
                        }, 6);
                        i15++;
                        zArr = zArr;
                        ceil = ceil;
                        arrayList6 = arrayList6;
                        hashMap10 = hashMap10;
                        sparseArray = sparseArray;
                        hashMap11 = hashMap11;
                        arrayList = arrayList;
                    }
                    return;
                }
            }
            i14 = 0;
            if (BuildVars.LOGS_ENABLED) {
            }
            if (i14 == 0) {
            }
        } else {
            Utilities.stageQueue.postRunnable(new Runnable(hashMap10, readContactsFromPhoneBook, z3, hashMap2, arrayList, hashMap3) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$6fewdAFbElSeVhBqfxCGua8OJ4 */
                private final /* synthetic */ HashMap f$1;
                private final /* synthetic */ HashMap f$2;
                private final /* synthetic */ boolean f$3;
                private final /* synthetic */ HashMap f$4;
                private final /* synthetic */ ArrayList f$5;
                private final /* synthetic */ HashMap f$6;

                {
                    this.f$1 = r2;
                    this.f$2 = r3;
                    this.f$3 = r4;
                    this.f$4 = r5;
                    this.f$5 = r6;
                    this.f$6 = r7;
                }

                public final void run() {
                    ContactsController.this.lambda$null$24$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4, this.f$5, this.f$6);
                }
            });
        }
    }

    private /* synthetic */ void lambda$null$15(HashMap hashMap) {
        ArrayList<TLRPC.User> arrayList = new ArrayList<>();
        if (hashMap != null && !hashMap.isEmpty()) {
            try {
                HashMap hashMap2 = new HashMap();
                for (int i = 0; i < this.contacts.size(); i++) {
                    TLRPC.User user = getMessagesController().getUser(Integer.valueOf(this.contacts.get(i).user_id));
                    if (user != null) {
                        if (!TextUtils.isEmpty(user.phone)) {
                            hashMap2.put(user.phone, user);
                        }
                    }
                }
                for (Map.Entry entry : hashMap.entrySet()) {
                    Contact contact = (Contact) entry.getValue();
                    int i2 = 0;
                    boolean z = false;
                    while (i2 < contact.shortPhones.size()) {
                        TLRPC.User user2 = (TLRPC.User) hashMap2.get(contact.shortPhones.get(i2));
                        if (user2 != null) {
                            arrayList.add(user2);
                            contact.shortPhones.remove(i2);
                            i2--;
                            z = true;
                        }
                        i2++;
                    }
                    if (z) {
                        contact.shortPhones.size();
                    }
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
        if (!arrayList.isEmpty()) {
            deleteContact(arrayList);
        }
    }

    public /* synthetic */ void lambda$null$16$ContactsController(int i, HashMap hashMap, boolean z, boolean z2) {
        getNotificationCenter().postNotificationName(NotificationCenter.hasNewContactsToImport, Integer.valueOf(i), hashMap, Boolean.valueOf(z), Boolean.valueOf(z2));
    }

    public /* synthetic */ void lambda$null$18$ContactsController(HashMap hashMap, HashMap hashMap2, boolean z, HashMap hashMap3, ArrayList arrayList, HashMap hashMap4) {
        this.contactsBookSPhones = hashMap;
        this.contactsBook = hashMap2;
        this.contactsSyncInProgress = false;
        this.contactsBookLoaded = true;
        if (z) {
            this.contactsLoaded = true;
        }
        if (!this.delayedContactsUpdate.isEmpty() && this.contactsLoaded) {
            applyContactsUpdates(this.delayedContactsUpdate, null, null, null);
            this.delayedContactsUpdate.clear();
        }
        getMessagesStorage().putCachedPhoneBook(hashMap2, false, false);
        AndroidUtilities.runOnUIThread(new Runnable(hashMap3, arrayList, hashMap4) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$st1FRih_B_eWXFPfXqOlSzYxlQU */
            private final /* synthetic */ HashMap f$1;
            private final /* synthetic */ ArrayList f$2;
            private final /* synthetic */ HashMap f$3;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
            }

            public final void run() {
                ContactsController.this.lambda$null$17$ContactsController(this.f$1, this.f$2, this.f$3);
            }
        });
    }

    public /* synthetic */ void lambda$null$17$ContactsController(HashMap hashMap, ArrayList arrayList, HashMap hashMap2) {
        lambda$null$25$ContactsController(hashMap, arrayList, hashMap2);
        updateUnregisteredContacts();
        getNotificationCenter().postNotificationName(NotificationCenter.contactsDidLoad, new Object[0]);
        getNotificationCenter().postNotificationName(NotificationCenter.contactsImported, new Object[0]);
    }

    public /* synthetic */ void lambda$null$22$ContactsController(HashMap hashMap, SparseArray sparseArray, boolean[] zArr, HashMap hashMap2, TLRPC.TL_contacts_importContacts tL_contacts_importContacts, int i, HashMap hashMap3, boolean z, HashMap hashMap4, ArrayList arrayList, HashMap hashMap5, TLObject tLObject, TLRPC.TL_error tL_error) {
        this.completedRequestsCount++;
        if (tL_error == null) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("contacts imported");
            }
            TLRPC.TL_contacts_importedContacts tL_contacts_importedContacts = (TLRPC.TL_contacts_importedContacts) tLObject;
            if (!tL_contacts_importedContacts.retry_contacts.isEmpty()) {
                for (int i2 = 0; i2 < tL_contacts_importedContacts.retry_contacts.size(); i2++) {
                    hashMap.remove(sparseArray.get((int) tL_contacts_importedContacts.retry_contacts.get(i2).longValue()));
                }
                zArr[0] = true;
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.d("result has retry contacts");
                }
            }
            for (int i3 = 0; i3 < tL_contacts_importedContacts.popular_invites.size(); i3++) {
                TLRPC.TL_popularContact tL_popularContact = tL_contacts_importedContacts.popular_invites.get(i3);
                Contact contact = (Contact) hashMap2.get(sparseArray.get((int) tL_popularContact.client_id));
                if (contact != null) {
                    contact.imported = tL_popularContact.importers;
                }
            }
            getMessagesStorage().putUsersAndChats(tL_contacts_importedContacts.users, null, true, true);
            ArrayList<TLRPC.Contact> arrayList2 = new ArrayList<>();
            for (int i4 = 0; i4 < tL_contacts_importedContacts.imported.size(); i4++) {
                TLRPC.Contact contact2 = new TLRPC.Contact();
                contact2.user_id = tL_contacts_importedContacts.imported.get(i4).user_id;
                arrayList2.add(contact2);
            }
            processLoadedContacts(arrayList2, tL_contacts_importedContacts.users, 2);
        } else {
            for (int i5 = 0; i5 < tL_contacts_importContacts.contacts.size(); i5++) {
                hashMap.remove(sparseArray.get((int) tL_contacts_importContacts.contacts.get(i5).client_id));
            }
            zArr[0] = true;
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("import contacts error " + tL_error.text);
            }
        }
        if (this.completedRequestsCount == i) {
            if (!hashMap.isEmpty()) {
                getMessagesStorage().putCachedPhoneBook(hashMap, false, false);
            }
            Utilities.stageQueue.postRunnable(new Runnable(hashMap3, hashMap2, z, hashMap4, arrayList, hashMap5, zArr) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$C6zWXmX8bpPSRJ_d1OHYzE4yNI8 */
                private final /* synthetic */ HashMap f$1;
                private final /* synthetic */ HashMap f$2;
                private final /* synthetic */ boolean f$3;
                private final /* synthetic */ HashMap f$4;
                private final /* synthetic */ ArrayList f$5;
                private final /* synthetic */ HashMap f$6;
                private final /* synthetic */ boolean[] f$7;

                {
                    this.f$1 = r2;
                    this.f$2 = r3;
                    this.f$3 = r4;
                    this.f$4 = r5;
                    this.f$5 = r6;
                    this.f$6 = r7;
                    this.f$7 = r8;
                }

                public final void run() {
                    ContactsController.this.lambda$null$21$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4, this.f$5, this.f$6, this.f$7);
                }
            });
        }
    }

    public /* synthetic */ void lambda$null$21$ContactsController(HashMap hashMap, HashMap hashMap2, boolean z, HashMap hashMap3, ArrayList arrayList, HashMap hashMap4, boolean[] zArr) {
        this.contactsBookSPhones = hashMap;
        this.contactsBook = hashMap2;
        this.contactsSyncInProgress = false;
        this.contactsBookLoaded = true;
        if (z) {
            this.contactsLoaded = true;
        }
        if (!this.delayedContactsUpdate.isEmpty() && this.contactsLoaded) {
            applyContactsUpdates(this.delayedContactsUpdate, null, null, null);
            this.delayedContactsUpdate.clear();
        }
        AndroidUtilities.runOnUIThread(new Runnable(hashMap3, arrayList, hashMap4) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$5nCNafSczRbawvRVUqif7ZXP4 */
            private final /* synthetic */ HashMap f$1;
            private final /* synthetic */ ArrayList f$2;
            private final /* synthetic */ HashMap f$3;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
            }

            public final void run() {
                ContactsController.this.lambda$null$19$ContactsController(this.f$1, this.f$2, this.f$3);
            }
        });
        if (zArr[0]) {
            Utilities.globalQueue.postRunnable(new Runnable() {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$6knkXRlui5UNg54jL1HVXxf2_zs */

                public final void run() {
                    ContactsController.this.lambda$null$20$ContactsController();
                }
            }, 300000);
        }
    }

    public /* synthetic */ void lambda$null$19$ContactsController(HashMap hashMap, ArrayList arrayList, HashMap hashMap2) {
        lambda$null$25$ContactsController(hashMap, arrayList, hashMap2);
        getNotificationCenter().postNotificationName(NotificationCenter.contactsImported, new Object[0]);
    }

    public /* synthetic */ void lambda$null$20$ContactsController() {
        getMessagesStorage().getCachedPhoneBook(true);
    }

    public /* synthetic */ void lambda$null$24$ContactsController(HashMap hashMap, HashMap hashMap2, boolean z, HashMap hashMap3, ArrayList arrayList, HashMap hashMap4) {
        this.contactsBookSPhones = hashMap;
        this.contactsBook = hashMap2;
        this.contactsSyncInProgress = false;
        this.contactsBookLoaded = true;
        if (z) {
            this.contactsLoaded = true;
        }
        if (!this.delayedContactsUpdate.isEmpty() && this.contactsLoaded) {
            applyContactsUpdates(this.delayedContactsUpdate, null, null, null);
            this.delayedContactsUpdate.clear();
        }
        AndroidUtilities.runOnUIThread(new Runnable(hashMap3, arrayList, hashMap4) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$YIU4oedDP2Q3oTYZ29cbEcRBGC4 */
            private final /* synthetic */ HashMap f$1;
            private final /* synthetic */ ArrayList f$2;
            private final /* synthetic */ HashMap f$3;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
            }

            public final void run() {
                ContactsController.this.lambda$null$23$ContactsController(this.f$1, this.f$2, this.f$3);
            }
        });
    }

    public /* synthetic */ void lambda$null$23$ContactsController(HashMap hashMap, ArrayList arrayList, HashMap hashMap2) {
        lambda$null$25$ContactsController(hashMap, arrayList, hashMap2);
        updateUnregisteredContacts();
        getNotificationCenter().postNotificationName(NotificationCenter.contactsDidLoad, new Object[0]);
        getNotificationCenter().postNotificationName(NotificationCenter.contactsImported, new Object[0]);
    }

    public /* synthetic */ void lambda$null$26$ContactsController(HashMap hashMap, HashMap hashMap2, boolean z, HashMap hashMap3, ArrayList arrayList, HashMap hashMap4) {
        this.contactsBookSPhones = hashMap;
        this.contactsBook = hashMap2;
        this.contactsSyncInProgress = false;
        this.contactsBookLoaded = true;
        if (z) {
            this.contactsLoaded = true;
        }
        if (!this.delayedContactsUpdate.isEmpty() && this.contactsLoaded && this.contactsBookLoaded) {
            applyContactsUpdates(this.delayedContactsUpdate, null, null, null);
            this.delayedContactsUpdate.clear();
        }
        AndroidUtilities.runOnUIThread(new Runnable(hashMap3, arrayList, hashMap4) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$2nbTwYzzlgyS5L0wyxUrKIqala8 */
            private final /* synthetic */ HashMap f$1;
            private final /* synthetic */ ArrayList f$2;
            private final /* synthetic */ HashMap f$3;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
            }

            public final void run() {
                ContactsController.this.lambda$null$25$ContactsController(this.f$1, this.f$2, this.f$3);
            }
        });
    }

    public boolean isLoadingContacts() {
        boolean z;
        synchronized (this.loadContactsSync) {
            z = this.loadingContacts;
        }
        return z;
    }

    private int getContactsHash(ArrayList<TLRPC.Contact> arrayList) {
        int i;
        long j;
        ArrayList arrayList2 = new ArrayList(arrayList);
        Collections.sort(arrayList2, $$Lambda$ContactsController$13mgZSHaWjpAPdHj_JNmBs06bk.INSTANCE);
        int size = arrayList2.size();
        long j2 = 0;
        for (int i2 = -1; i2 < size; i2++) {
            if (i2 == -1) {
                j = (j2 * 20261) + 2147483648L;
                i = getUserConfig().contactsSavedCount;
            } else {
                j = (j2 * 20261) + 2147483648L;
                i = ((TLRPC.Contact) arrayList2.get(i2)).user_id;
            }
            j2 = (j + ((long) i)) % 2147483648L;
        }
        return (int) j2;
    }

    static /* synthetic */ int lambda$getContactsHash$28(TLRPC.Contact contact, TLRPC.Contact contact2) {
        if (contact.user_id > contact2.user_id) {
            return 1;
        }
        return contact.user_id < contact2.user_id ? -1 : 0;
    }

    public void loadContacts(boolean z, int i) {
        synchronized (this.loadContactsSync) {
            this.loadingContacts = true;
        }
        if (z) {
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("load contacts from cache");
            }
            getMessagesStorage().getContacts();
            return;
        }
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("load contacts from server");
        }
        TLRPCContacts.TL_getContactsV1 tL_getContactsV1 = new TLRPCContacts.TL_getContactsV1();
        tL_getContactsV1.hash = i;
        getConnectionsManager().sendRequest(tL_getContactsV1, new RequestDelegate(i) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$KbhZRki4vpShrx0rgiVPZ0wQnAI */
            private final /* synthetic */ int f$1;

            {
                this.f$1 = r2;
            }

            @Override // im.guobwnxjuc.tgnet.RequestDelegate
            public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                ContactsController.this.lambda$loadContacts$30$ContactsController(this.f$1, tLObject, tL_error);
            }
        });
    }

    public /* synthetic */ void lambda$loadContacts$30$ContactsController(int i, TLObject tLObject, TLRPC.TL_error tL_error) {
        if (tL_error == null) {
            TLRPC.contacts_Contacts contacts_contacts = (TLRPC.contacts_Contacts) tLObject;
            if (i == 0 || !(contacts_contacts instanceof TLRPC.TL_contacts_contactsNotModified)) {
                getUserConfig().contactsSavedCount = contacts_contacts.saved_count;
                getUserConfig().saveConfig(false);
                processLoadedContacts(contacts_contacts.contacts, contacts_contacts.users, 0);
                return;
            }
            this.contactsLoaded = true;
            if (!this.delayedContactsUpdate.isEmpty() && this.contactsBookLoaded) {
                applyContactsUpdates(this.delayedContactsUpdate, null, null, null);
                this.delayedContactsUpdate.clear();
            }
            getUserConfig().lastContactsSyncTime = (int) (System.currentTimeMillis() / 1000);
            getUserConfig().saveConfig(false);
            AndroidUtilities.runOnUIThread(new Runnable() {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$HrpijUB02fuDgSSpIm7g5RyJ0 */

                public final void run() {
                    ContactsController.this.lambda$null$29$ContactsController();
                }
            });
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("load contacts don't change");
            }
        }
    }

    public /* synthetic */ void lambda$null$29$ContactsController() {
        synchronized (this.loadContactsSync) {
            this.loadingContacts = false;
        }
        getNotificationCenter().postNotificationName(NotificationCenter.contactsDidLoad, new Object[0]);
    }

    public void processLoadedContacts(ArrayList<TLRPC.Contact> arrayList, ArrayList<TLRPC.User> arrayList2, int i) {
        AndroidUtilities.runOnUIThread(new Runnable(arrayList2, i, arrayList) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$g8sCPHTFUFRAOvLIeYUqbCJKmW0 */
            private final /* synthetic */ ArrayList f$1;
            private final /* synthetic */ int f$2;
            private final /* synthetic */ ArrayList f$3;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
            }

            public final void run() {
                ContactsController.this.lambda$processLoadedContacts$38$ContactsController(this.f$1, this.f$2, this.f$3);
            }
        });
    }

    public /* synthetic */ void lambda$processLoadedContacts$38$ContactsController(ArrayList arrayList, int i, ArrayList arrayList2) {
        getMessagesController().putUsers(arrayList, i == 1);
        SparseArray sparseArray = new SparseArray();
        ArrayList arrayList3 = new ArrayList();
        boolean isEmpty = arrayList2.isEmpty();
        if (!this.contacts.isEmpty()) {
            HashMap hashMap = new HashMap();
            int i2 = 0;
            while (i2 < arrayList2.size()) {
                TLRPC.Contact contact = (TLRPC.Contact) arrayList2.get(i2);
                hashMap.put(Integer.valueOf(contact.user_id), contact);
                if (this.contactsDict.get(Integer.valueOf(contact.user_id)) != null) {
                    arrayList2.remove(i2);
                    i2--;
                }
                i2++;
            }
            int i3 = 0;
            while (i3 < this.contacts.size()) {
                if (hashMap.get(Integer.valueOf(this.contacts.get(i3).user_id)) == null) {
                    this.contacts.remove(i3);
                    i3--;
                }
                i3++;
            }
            arrayList2.addAll(this.contacts);
        }
        for (int i4 = 0; i4 < arrayList2.size(); i4++) {
            TLRPC.User user = getMessagesController().getUser(Integer.valueOf(((TLRPC.Contact) arrayList2.get(i4)).user_id));
            if (user != null) {
                sparseArray.put(user.id, user);
                arrayList3.add(Integer.valueOf(user.id));
            }
        }
        Utilities.stageQueue.postRunnable(new Runnable(i, arrayList2, sparseArray, arrayList, isEmpty) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$6hRrSduvzXYuKNJHWBCdcLNKGMY */
            private final /* synthetic */ int f$1;
            private final /* synthetic */ ArrayList f$2;
            private final /* synthetic */ SparseArray f$3;
            private final /* synthetic */ ArrayList f$4;
            private final /* synthetic */ boolean f$5;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
                this.f$4 = r5;
                this.f$5 = r6;
            }

            public final void run() {
                ContactsController.this.lambda$null$37$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4, this.f$5);
            }
        });
    }

    public /* synthetic */ void lambda$null$37$ContactsController(int i, ArrayList arrayList, SparseArray sparseArray, ArrayList arrayList2, boolean z) {
        HashMap hashMap;
        HashMap hashMap2;
        int i2;
        String str;
        ArrayList arrayList3 = arrayList;
        SparseArray sparseArray2 = sparseArray;
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("done loading contactsfrom = " + i);
        }
        if (i == 1 && (arrayList.isEmpty() || Math.abs((System.currentTimeMillis() / 1000) - ((long) getUserConfig().lastContactsSyncTime)) >= 7200)) {
            loadContacts(false, getContactsHash(arrayList3));
            if (arrayList.isEmpty()) {
                return;
            }
        }
        if (i == 0) {
            getUserConfig().lastContactsSyncTime = (int) (System.currentTimeMillis() / 1000);
            getUserConfig().saveConfig(false);
        }
        for (int i3 = 0; i3 < arrayList.size(); i3++) {
            TLRPC.Contact contact = arrayList3.get(i3);
            if (sparseArray2.get(contact.user_id) == null && contact.user_id != getUserConfig().getClientUserId()) {
                loadContacts(false, 0);
                if (BuildVars.LOGS_ENABLED) {
                    FileLog.d("contacts are broken, load from server");
                    return;
                }
                return;
            }
        }
        if (i != 1) {
            getMessagesStorage().putUsersAndChats(arrayList2, null, true, true);
            getMessagesStorage().putContacts(arrayList3, i != 2);
        }
        Collections.sort(arrayList3, new Comparator(sparseArray2) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$VPiPGUWoBX5W5lVHC5PZ1RWVZw */
            private final /* synthetic */ SparseArray f$0;

            {
                this.f$0 = r1;
            }

            @Override // java.util.Comparator
            public final int compare(Object obj, Object obj2) {
                SparseArray sparseArray;
                return UserObject.getFirstName((TLRPC.User) sparseArray.get(((TLRPC.Contact) obj).user_id)).compareTo(UserObject.getFirstName((TLRPC.User) this.f$0.get(((TLRPC.Contact) obj2).user_id)));
            }
        });
        ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap(20, 1.0f, 2);
        HashMap hashMap3 = new HashMap();
        HashMap hashMap4 = new HashMap();
        ArrayList arrayList4 = new ArrayList();
        ArrayList arrayList5 = new ArrayList();
        if (!this.contactsBookLoaded) {
            HashMap hashMap5 = new HashMap();
            hashMap = new HashMap();
            hashMap2 = hashMap5;
        } else {
            hashMap2 = null;
            hashMap = null;
        }
        int i4 = 0;
        while (i4 < arrayList.size()) {
            TLRPC.Contact contact2 = arrayList3.get(i4);
            TLRPC.User user = (TLRPC.User) sparseArray2.get(contact2.user_id);
            if (user != null) {
                concurrentHashMap.put(Integer.valueOf(contact2.user_id), contact2);
                if (hashMap2 == null || TextUtils.isEmpty(user.phone)) {
                    i2 = 0;
                } else {
                    hashMap2.put(user.phone, contact2);
                    i2 = 0;
                    hashMap.put(user.phone.substring(Math.max(0, user.phone.length() - 7)), contact2);
                }
                String selling = CharacterParser.getInstance().getSelling(UserObject.getFirstName(user));
                if (selling.length() > 1) {
                    selling = selling.substring(i2, 1);
                }
                if (selling.length() == 0) {
                    str = "#";
                } else {
                    str = selling.toUpperCase();
                }
                String str2 = this.sectionsToReplace.get(str);
                if (str2 != null) {
                    str = str2;
                }
                ArrayList arrayList6 = (ArrayList) hashMap3.get(str);
                if (arrayList6 == null) {
                    arrayList6 = new ArrayList();
                    hashMap3.put(str, arrayList6);
                    arrayList4.add(str);
                }
                arrayList6.add(contact2);
                if (user.mutual_contact) {
                    ArrayList arrayList7 = (ArrayList) hashMap4.get(str);
                    if (arrayList7 == null) {
                        arrayList7 = new ArrayList();
                        hashMap4.put(str, arrayList7);
                        arrayList5.add(str);
                    }
                    arrayList7.add(contact2);
                }
            }
            i4++;
            arrayList3 = arrayList;
            sparseArray2 = sparseArray;
        }
        Collections.sort(arrayList4, $$Lambda$ContactsController$J2YCRNRyxFlS9WS5APJFnFTZbhk.INSTANCE);
        Collections.sort(arrayList5, $$Lambda$ContactsController$atAkN52K3bGaHCR2HnKoNlJSgHI.INSTANCE);
        AndroidUtilities.runOnUIThread(new Runnable(arrayList, concurrentHashMap, hashMap3, hashMap4, arrayList4, arrayList5, i, z) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$fvvPrBRHFOcPerwuCc8ko9a2fOE */
            private final /* synthetic */ ArrayList f$1;
            private final /* synthetic */ ConcurrentHashMap f$2;
            private final /* synthetic */ HashMap f$3;
            private final /* synthetic */ HashMap f$4;
            private final /* synthetic */ ArrayList f$5;
            private final /* synthetic */ ArrayList f$6;
            private final /* synthetic */ int f$7;
            private final /* synthetic */ boolean f$8;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
                this.f$4 = r5;
                this.f$5 = r6;
                this.f$6 = r7;
                this.f$7 = r8;
                this.f$8 = r9;
            }

            public final void run() {
                ContactsController.this.lambda$null$34$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4, this.f$5, this.f$6, this.f$7, this.f$8);
            }
        });
        if (!this.delayedContactsUpdate.isEmpty() && this.contactsLoaded && this.contactsBookLoaded) {
            applyContactsUpdates(this.delayedContactsUpdate, null, null, null);
            this.delayedContactsUpdate.clear();
        }
        if (hashMap2 != null) {
            AndroidUtilities.runOnUIThread(new Runnable(hashMap2, hashMap) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$EtXQ7aQTV1LpMwzMIhcEh0tx7Is */
                private final /* synthetic */ HashMap f$1;
                private final /* synthetic */ HashMap f$2;

                {
                    this.f$1 = r2;
                    this.f$2 = r3;
                }

                public final void run() {
                    ContactsController.this.lambda$null$36$ContactsController(this.f$1, this.f$2);
                }
            });
        } else {
            this.contactsLoaded = true;
        }
    }

    static /* synthetic */ int lambda$null$32(String str, String str2) {
        char charAt = str.charAt(0);
        char charAt2 = str2.charAt(0);
        if (charAt == '#') {
            return 1;
        }
        if (charAt2 == '#') {
            return -1;
        }
        return str.compareTo(str2);
    }

    static /* synthetic */ int lambda$null$33(String str, String str2) {
        char charAt = str.charAt(0);
        char charAt2 = str2.charAt(0);
        if (charAt == '#') {
            return 1;
        }
        if (charAt2 == '#') {
            return -1;
        }
        return str.compareTo(str2);
    }

    public /* synthetic */ void lambda$null$34$ContactsController(ArrayList arrayList, ConcurrentHashMap concurrentHashMap, HashMap hashMap, HashMap hashMap2, ArrayList arrayList2, ArrayList arrayList3, int i, boolean z) {
        this.contacts = arrayList;
        this.contactsDict = concurrentHashMap;
        this.usersSectionsDict = hashMap;
        this.usersMutualSectionsDict = hashMap2;
        this.sortedUsersSectionsArray = arrayList2;
        this.sortedUsersMutualSectionsArray = arrayList3;
        if (i != 2) {
            synchronized (this.loadContactsSync) {
                this.loadingContacts = false;
            }
        }
        performWriteContactsToPhoneBook();
        updateUnregisteredContacts();
        getNotificationCenter().postNotificationName(NotificationCenter.contactsDidLoad, new Object[0]);
        if (i == 1 || z) {
            reloadContactsStatusesMaybe();
        } else {
            saveContactsLoadTime();
        }
    }

    public /* synthetic */ void lambda$null$36$ContactsController(HashMap hashMap, HashMap hashMap2) {
        Utilities.globalQueue.postRunnable(new Runnable(hashMap, hashMap2) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$aYU1LLY709z1xm2nACji9spM */
            private final /* synthetic */ HashMap f$1;
            private final /* synthetic */ HashMap f$2;

            {
                this.f$1 = r2;
                this.f$2 = r3;
            }

            public final void run() {
                ContactsController.this.lambda$null$35$ContactsController(this.f$1, this.f$2);
            }
        });
        if (!this.contactsSyncInProgress) {
            this.contactsSyncInProgress = true;
            getMessagesStorage().getCachedPhoneBook(false);
        }
    }

    public /* synthetic */ void lambda$null$35$ContactsController(HashMap hashMap, HashMap hashMap2) {
        this.contactsByPhone = hashMap;
        this.contactsByShortPhone = hashMap2;
    }

    public boolean isContact(int i) {
        return this.contactsDict.get(Integer.valueOf(i)) != null;
    }

    private void reloadContactsStatusesMaybe() {
        try {
            if (MessagesController.getMainSettings(this.currentAccount).getLong("lastReloadStatusTime", 0) < System.currentTimeMillis() - 86400000) {
                reloadContactsStatuses();
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    private void saveContactsLoadTime() {
        try {
            MessagesController.getMainSettings(this.currentAccount).edit().putLong("lastReloadStatusTime", System.currentTimeMillis()).commit();
        } catch (Exception e) {
            FileLog.e(e);
        }
    }

    /* access modifiers changed from: private */
    /* renamed from: mergePhonebookAndAppContacts */
    public void lambda$null$25$ContactsController(HashMap<String, ArrayList<Object>> hashMap, ArrayList<String> arrayList, HashMap<String, Contact> hashMap2) {
        Utilities.globalQueue.postRunnable(new Runnable(new ArrayList(this.contacts), hashMap2, hashMap, arrayList) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$tUfS2vMt1dCg4XcHCQVBCKpJB8 */
            private final /* synthetic */ ArrayList f$1;
            private final /* synthetic */ HashMap f$2;
            private final /* synthetic */ HashMap f$3;
            private final /* synthetic */ ArrayList f$4;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
                this.f$4 = r5;
            }

            public final void run() {
                ContactsController.this.lambda$mergePhonebookAndAppContacts$42$ContactsController(this.f$1, this.f$2, this.f$3, this.f$4);
            }
        });
    }

    public /* synthetic */ void lambda$mergePhonebookAndAppContacts$42$ContactsController(ArrayList arrayList, HashMap hashMap, HashMap hashMap2, ArrayList arrayList2) {
        int size = arrayList.size();
        for (int i = 0; i < size; i++) {
            TLRPC.User user = getMessagesController().getUser(Integer.valueOf(((TLRPC.Contact) arrayList.get(i)).user_id));
            if (user != null && !TextUtils.isEmpty(user.phone)) {
                Contact contact = (Contact) hashMap.get(user.phone.substring(Math.max(0, user.phone.length() - 7)));
                if (contact == null) {
                    String letter = Contact.getLetter(user.first_name, user.last_name);
                    ArrayList arrayList3 = (ArrayList) hashMap2.get(letter);
                    if (arrayList3 == null) {
                        arrayList3 = new ArrayList();
                        hashMap2.put(letter, arrayList3);
                        arrayList2.add(letter);
                    }
                    arrayList3.add(user);
                } else if (contact.user == null) {
                    contact.user = user;
                }
            }
        }
        for (ArrayList arrayList4 : hashMap2.values()) {
            Collections.sort(arrayList4, $$Lambda$ContactsController$InrWdyaj8Rx83VwIdVGMvZnbC1k.INSTANCE);
        }
        Collections.sort(arrayList2, $$Lambda$ContactsController$RmZjM1OHOenoslSzl9CBWZXv5yY.INSTANCE);
        AndroidUtilities.runOnUIThread(new Runnable(arrayList2, hashMap2) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$GlknbixogTSHoSFayDEc_9Kf8Q */
            private final /* synthetic */ ArrayList f$1;
            private final /* synthetic */ HashMap f$2;

            {
                this.f$1 = r2;
                this.f$2 = r3;
            }

            public final void run() {
                ContactsController.this.lambda$null$41$ContactsController(this.f$1, this.f$2);
            }
        });
    }

    static /* synthetic */ int lambda$null$39(Object obj, Object obj2) {
        String str;
        String str2;
        String str3 = "";
        if (obj instanceof TLRPC.User) {
            TLRPC.User user = (TLRPC.User) obj;
            str = formatName(user.first_name, user.last_name);
        } else if (obj instanceof Contact) {
            Contact contact = (Contact) obj;
            str = contact.user != null ? formatName(contact.user.first_name, contact.user.last_name) : formatName(contact.first_name, contact.last_name);
        } else {
            str = str3;
        }
        if (obj2 instanceof TLRPC.User) {
            TLRPC.User user2 = (TLRPC.User) obj2;
            str3 = formatName(user2.first_name, user2.last_name);
        } else if (obj2 instanceof Contact) {
            Contact contact2 = (Contact) obj2;
            if (contact2.user != null) {
                str2 = formatName(contact2.user.first_name, contact2.user.last_name);
            } else {
                str2 = formatName(contact2.first_name, contact2.last_name);
            }
            str3 = str2;
        }
        return str.compareTo(str3);
    }

    static /* synthetic */ int lambda$null$40(String str, String str2) {
        char charAt = str.charAt(0);
        char charAt2 = str2.charAt(0);
        if (charAt == '#') {
            return 1;
        }
        if (charAt2 == '#') {
            return -1;
        }
        return str.compareTo(str2);
    }

    public /* synthetic */ void lambda$null$41$ContactsController(ArrayList arrayList, HashMap hashMap) {
        this.phoneBookSectionsArray = arrayList;
        this.phoneBookSectionsDict = hashMap;
    }

    private void updateUnregisteredContacts() {
        boolean z;
        HashMap hashMap = new HashMap();
        int size = this.contacts.size();
        for (int i = 0; i < size; i++) {
            TLRPC.Contact contact = this.contacts.get(i);
            TLRPC.User user = getMessagesController().getUser(Integer.valueOf(contact.user_id));
            if (user != null && !TextUtils.isEmpty(user.phone)) {
                hashMap.put(user.phone, contact);
            }
        }
        ArrayList<Contact> arrayList = new ArrayList<>();
        for (Map.Entry<String, Contact> entry : this.contactsBook.entrySet()) {
            Contact value = entry.getValue();
            int i2 = 0;
            while (true) {
                z = true;
                if (i2 >= value.phones.size()) {
                    z = false;
                    break;
                }
                if (hashMap.containsKey(value.shortPhones.get(i2)) || value.phoneDeleted.get(i2).intValue() == 1) {
                    break;
                }
                i2++;
            }
            if (!z) {
                arrayList.add(value);
            }
        }
        Collections.sort(arrayList, $$Lambda$ContactsController$7khn3ZNYGM7ac3LnoETU3mggUbc.INSTANCE);
        this.phoneBookContacts = arrayList;
    }

    static /* synthetic */ int lambda$updateUnregisteredContacts$43(Contact contact, Contact contact2) {
        String str = contact.first_name;
        if (str.length() == 0) {
            str = contact.last_name;
        }
        String str2 = contact2.first_name;
        if (str2.length() == 0) {
            str2 = contact2.last_name;
        }
        return str.compareTo(str2);
    }

    private void buildContactsSectionsArrays(boolean z) {
        String str;
        if (z) {
            Collections.sort(this.contacts, new Comparator() {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$0bx2ZxWzsUCczD4CEXf0QgUU9jU */

                @Override // java.util.Comparator
                public final int compare(Object obj, Object obj2) {
                    return ContactsController.this.lambda$buildContactsSectionsArrays$44$ContactsController((TLRPC.Contact) obj, (TLRPC.Contact) obj2);
                }
            });
        }
        HashMap<String, ArrayList<TLRPC.Contact>> hashMap = new HashMap<>();
        ArrayList<String> arrayList = new ArrayList<>();
        for (int i = 0; i < this.contacts.size(); i++) {
            TLRPC.Contact contact = this.contacts.get(i);
            TLRPC.User user = getMessagesController().getUser(Integer.valueOf(contact.user_id));
            if (user != null) {
                String selling = CharacterParser.getInstance().getSelling(UserObject.getFirstName(user));
                if (selling.length() > 1) {
                    selling = selling.substring(0, 1);
                }
                if (selling.length() == 0) {
                    str = "#";
                } else {
                    str = selling.toUpperCase();
                }
                String str2 = this.sectionsToReplace.get(str);
                if (str2 != null) {
                    str = str2;
                }
                ArrayList<TLRPC.Contact> arrayList2 = hashMap.get(str);
                if (arrayList2 == null) {
                    arrayList2 = new ArrayList<>();
                    hashMap.put(str, arrayList2);
                    arrayList.add(str);
                }
                arrayList2.add(contact);
            }
        }
        Collections.sort(arrayList, $$Lambda$ContactsController$yC3Ck9ZAO8RN9LkeBMPI4Dv9jDo.INSTANCE);
        this.usersSectionsDict = hashMap;
        this.sortedUsersSectionsArray = arrayList;
    }

    public /* synthetic */ int lambda$buildContactsSectionsArrays$44$ContactsController(TLRPC.Contact contact, TLRPC.Contact contact2) {
        return UserObject.getFirstName(getMessagesController().getUser(Integer.valueOf(contact.user_id))).compareTo(UserObject.getFirstName(getMessagesController().getUser(Integer.valueOf(contact2.user_id))));
    }

    static /* synthetic */ int lambda$buildContactsSectionsArrays$45(String str, String str2) {
        char charAt = str.charAt(0);
        char charAt2 = str2.charAt(0);
        if (charAt == '#') {
            return 1;
        }
        if (charAt2 == '#') {
            return -1;
        }
        return str.compareTo(str2);
    }

    private boolean hasContactsPermission() {
        if (Build.VERSION.SDK_INT < 23) {
            Cursor cursor = null;
            try {
                Cursor query = ApplicationLoader.applicationContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, this.projectionPhones, null, null, null);
                if (query == null || query.getCount() == 0) {
                    if (query != null) {
                        try {
                            query.close();
                        } catch (Exception e) {
                            FileLog.e(e);
                        }
                    }
                    return false;
                }
                if (query != null) {
                    try {
                        query.close();
                    } catch (Exception e2) {
                        FileLog.e(e2);
                    }
                }
                return true;
            } catch (Throwable th) {
                if (0 != 0) {
                    try {
                        cursor.close();
                    } catch (Exception e3) {
                        FileLog.e(e3);
                    }
                }
                throw th;
            }
        } else if (ApplicationLoader.applicationContext.checkSelfPermission(PermissionUtils.LINKMAIN) == 0) {
            return true;
        } else {
            return false;
        }
    }

    /* access modifiers changed from: private */
    /* JADX WARNING: Removed duplicated region for block: B:33:0x009b  */
    /* JADX WARNING: Removed duplicated region for block: B:41:? A[RETURN, SYNTHETIC] */
    /* renamed from: performWriteContactsToPhoneBookInternal */
    public void lambda$performWriteContactsToPhoneBook$46$ContactsController(ArrayList<TLRPC.Contact> arrayList) {
        Throwable th;
        Exception e;
        Cursor cursor = null;
        try {
            if (hasContactsPermission()) {
                Cursor query = ApplicationLoader.applicationContext.getContentResolver().query(ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter("account_name", this.systemAccount.name).appendQueryParameter("account_type", this.systemAccount.type).build(), new String[]{"_id", "sync2"}, null, null, null);
                try {
                    SparseLongArray sparseLongArray = new SparseLongArray();
                    if (query != null) {
                        while (query.moveToNext()) {
                            sparseLongArray.put(query.getInt(1), query.getLong(0));
                        }
                        query.close();
                        for (int i = 0; i < arrayList.size(); i++) {
                            TLRPC.Contact contact = arrayList.get(i);
                            if (sparseLongArray.indexOfKey(contact.user_id) < 0) {
                                addContactToPhoneBook(getMessagesController().getUser(Integer.valueOf(contact.user_id)), false);
                            }
                        }
                    } else {
                        cursor = query;
                    }
                    if (cursor == null) {
                        return;
                    }
                } catch (Exception e2) {
                    e = e2;
                    cursor = query;
                    try {
                        FileLog.e(e);
                        if (cursor == null) {
                        }
                        cursor.close();
                    } catch (Throwable th2) {
                        th = th2;
                        if (cursor != null) {
                        }
                        throw th;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    cursor = query;
                    if (cursor != null) {
                        cursor.close();
                    }
                    throw th;
                }
                cursor.close();
            }
        } catch (Exception e3) {
            e = e3;
            FileLog.e(e);
            if (cursor == null) {
                return;
            }
            cursor.close();
        }
    }

    private void performWriteContactsToPhoneBook() {
        Utilities.phoneBookQueue.postRunnable(new Runnable(new ArrayList(this.contacts)) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$jRwRbhT1OTRWidfiwYf9Wbo5U */
            private final /* synthetic */ ArrayList f$1;

            {
                this.f$1 = r2;
            }

            public final void run() {
                ContactsController.this.lambda$performWriteContactsToPhoneBook$46$ContactsController(this.f$1);
            }
        });
    }

    private void applyContactsUpdates(ArrayList<Integer> arrayList, ConcurrentHashMap<Integer, TLRPC.User> concurrentHashMap, ArrayList<TLRPC.Contact> arrayList2, ArrayList<Integer> arrayList3) {
        int indexOf;
        int indexOf2;
        if (arrayList2 == null || arrayList3 == null) {
            arrayList2 = new ArrayList<>();
            arrayList3 = new ArrayList<>();
            for (int i = 0; i < arrayList.size(); i++) {
                Integer num = arrayList.get(i);
                if (num.intValue() > 0) {
                    TLRPC.Contact contact = new TLRPC.Contact();
                    contact.user_id = num.intValue();
                    arrayList2.add(contact);
                } else if (num.intValue() < 0) {
                    arrayList3.add(Integer.valueOf(-num.intValue()));
                }
            }
        }
        if (BuildVars.LOGS_ENABLED) {
            FileLog.d("process update - contacts add = " + arrayList2.size() + " delete = " + arrayList3.size());
        }
        StringBuilder sb = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        int i2 = 0;
        boolean z = false;
        while (true) {
            TLRPC.User user = null;
            if (i2 >= arrayList2.size()) {
                break;
            }
            TLRPC.Contact contact2 = arrayList2.get(i2);
            if (concurrentHashMap != null) {
                user = concurrentHashMap.get(Integer.valueOf(contact2.user_id));
            }
            if (user == null) {
                user = getMessagesController().getUser(Integer.valueOf(contact2.user_id));
            } else {
                getMessagesController().putUser(user, true);
            }
            if (user == null || TextUtils.isEmpty(user.phone)) {
                z = true;
            } else {
                Contact contact3 = this.contactsBookSPhones.get(user.phone);
                if (!(contact3 == null || (indexOf2 = contact3.shortPhones.indexOf(user.phone)) == -1)) {
                    contact3.phoneDeleted.set(indexOf2, 0);
                }
                if (sb.length() != 0) {
                    sb.append(",");
                }
                sb.append(user.phone);
            }
            i2++;
        }
        for (int i3 = 0; i3 < arrayList3.size(); i3++) {
            Integer num2 = arrayList3.get(i3);
            Utilities.phoneBookQueue.postRunnable(new Runnable(num2) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$q_NVB3rldHCNNmU1nIdX1ebWPp4 */
                private final /* synthetic */ Integer f$1;

                {
                    this.f$1 = r2;
                }

                public final void run() {
                    ContactsController.this.lambda$applyContactsUpdates$47$ContactsController(this.f$1);
                }
            });
            TLRPC.User user2 = concurrentHashMap != null ? concurrentHashMap.get(num2) : null;
            if (user2 == null) {
                user2 = getMessagesController().getUser(num2);
            } else {
                getMessagesController().putUser(user2, true);
            }
            if (user2 == null) {
                z = true;
            } else if (!TextUtils.isEmpty(user2.phone)) {
                Contact contact4 = this.contactsBookSPhones.get(user2.phone);
                if (!(contact4 == null || (indexOf = contact4.shortPhones.indexOf(user2.phone)) == -1)) {
                    contact4.phoneDeleted.set(indexOf, 1);
                }
                if (sb2.length() != 0) {
                    sb2.append(",");
                }
                sb2.append(user2.phone);
            }
        }
        if (!(sb.length() == 0 && sb2.length() == 0)) {
            getMessagesStorage().applyPhoneBookUpdates(sb.toString(), sb2.toString());
        }
        if (z) {
            Utilities.stageQueue.postRunnable(new Runnable() {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$0qfqOLBG8miiYIFlGQb6CRHouJc */

                public final void run() {
                    ContactsController.this.lambda$applyContactsUpdates$48$ContactsController();
                }
            });
        } else {
            AndroidUtilities.runOnUIThread(new Runnable(arrayList2, arrayList3) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$eFnRQ4ZRdY7CMPYunyLBFBLXOoo */
                private final /* synthetic */ ArrayList f$1;
                private final /* synthetic */ ArrayList f$2;

                {
                    this.f$1 = r2;
                    this.f$2 = r3;
                }

                public final void run() {
                    ContactsController.this.lambda$applyContactsUpdates$49$ContactsController(this.f$1, this.f$2);
                }
            });
        }
    }

    public /* synthetic */ void lambda$applyContactsUpdates$47$ContactsController(Integer num) {
        deleteContactFromPhoneBook(num.intValue());
    }

    public /* synthetic */ void lambda$applyContactsUpdates$48$ContactsController() {
        loadContacts(false, 0);
    }

    public /* synthetic */ void lambda$applyContactsUpdates$49$ContactsController(ArrayList arrayList, ArrayList arrayList2) {
        for (int i = 0; i < arrayList.size(); i++) {
            TLRPC.Contact contact = (TLRPC.Contact) arrayList.get(i);
            if (this.contactsDict.get(Integer.valueOf(contact.user_id)) == null) {
                this.contacts.add(contact);
                this.contactsDict.put(Integer.valueOf(contact.user_id), contact);
            }
        }
        for (int i2 = 0; i2 < arrayList2.size(); i2++) {
            Integer num = (Integer) arrayList2.get(i2);
            TLRPC.Contact contact2 = this.contactsDict.get(num);
            if (contact2 != null) {
                this.contacts.remove(contact2);
                this.contactsDict.remove(num);
            }
        }
        if (!arrayList.isEmpty()) {
            updateUnregisteredContacts();
            performWriteContactsToPhoneBook();
        }
        performSyncPhoneBook(getContactsCopy(this.contactsBook), false, false, false, false, true, false);
        buildContactsSectionsArrays(!arrayList.isEmpty());
        getNotificationCenter().postNotificationName(NotificationCenter.contactsDidLoad, new Object[0]);
    }

    public void processContactsUpdates(ArrayList<Integer> arrayList, ConcurrentHashMap<Integer, TLRPC.User> concurrentHashMap) {
        int indexOf;
        int indexOf2;
        ArrayList<TLRPC.Contact> arrayList2 = new ArrayList<>();
        ArrayList<Integer> arrayList3 = new ArrayList<>();
        Iterator<Integer> it = arrayList.iterator();
        while (it.hasNext()) {
            Integer next = it.next();
            if (next.intValue() > 0) {
                TLRPC.Contact contact = new TLRPC.Contact();
                contact.user_id = next.intValue();
                arrayList2.add(contact);
                if (!this.delayedContactsUpdate.isEmpty() && (indexOf2 = this.delayedContactsUpdate.indexOf(Integer.valueOf(-next.intValue()))) != -1) {
                    this.delayedContactsUpdate.remove(indexOf2);
                }
            } else if (next.intValue() < 0) {
                arrayList3.add(Integer.valueOf(-next.intValue()));
                if (!this.delayedContactsUpdate.isEmpty() && (indexOf = this.delayedContactsUpdate.indexOf(Integer.valueOf(-next.intValue()))) != -1) {
                    this.delayedContactsUpdate.remove(indexOf);
                }
            }
        }
        arrayList3.isEmpty();
        if (!arrayList2.isEmpty()) {
            getMessagesStorage().putContacts(arrayList2, false);
        }
        if (!this.contactsLoaded || !this.contactsBookLoaded) {
            this.delayedContactsUpdate.addAll(arrayList);
            if (BuildVars.LOGS_ENABLED) {
                FileLog.d("delay update - contacts add = " + arrayList2.size() + " delete = " + arrayList3.size());
                return;
            }
            return;
        }
        applyContactsUpdates(arrayList, concurrentHashMap, arrayList2, arrayList3);
    }

    public long addContactToPhoneBook(TLRPC.User user, boolean z) {
        String str;
        long j = -1;
        if (this.systemAccount == null || user == null || !hasContactsPermission()) {
            return -1;
        }
        synchronized (this.observerLock) {
            this.ignoreChanges = true;
        }
        ContentResolver contentResolver = ApplicationLoader.applicationContext.getContentResolver();
        if (z) {
            try {
                contentResolver.delete(ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter("caller_is_syncadapter", "true").appendQueryParameter("account_name", this.systemAccount.name).appendQueryParameter("account_type", this.systemAccount.type).build(), "sync2 = " + user.id, null);
            } catch (Exception unused) {
            }
        }
        ArrayList<ContentProviderOperation> arrayList = new ArrayList<>();
        ContentProviderOperation.Builder newInsert = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
        newInsert.withValue("account_name", this.systemAccount.name);
        newInsert.withValue("account_type", this.systemAccount.type);
        newInsert.withValue("sync1", TextUtils.isEmpty(user.phone) ? "" : user.phone);
        newInsert.withValue("sync2", Integer.valueOf(user.id));
        arrayList.add(newInsert.build());
        ContentProviderOperation.Builder newInsert2 = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        newInsert2.withValueBackReference("raw_contact_id", 0);
        newInsert2.withValue("mimetype", "vnd.android.cursor.item/name");
        newInsert2.withValue("data2", user.first_name);
        newInsert2.withValue("data3", user.last_name);
        arrayList.add(newInsert2.build());
        ContentProviderOperation.Builder newInsert3 = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        newInsert3.withValueBackReference("raw_contact_id", 0);
        newInsert3.withValue("mimetype", "vnd.android.cursor.item/vnd.im.guobwnxjuc.messenger.android.profile");
        newInsert3.withValue("data1", Integer.valueOf(user.id));
        newInsert3.withValue("data2", LocaleController.getString(R.string.AppName) + " Profile");
        if (TextUtils.isEmpty(user.phone)) {
            str = formatName(user.first_name, user.last_name);
        } else {
            str = Marker.ANY_NON_NULL_MARKER + user.phone;
        }
        newInsert3.withValue("data3", str);
        newInsert3.withValue("data4", Integer.valueOf(user.id));
        arrayList.add(newInsert3.build());
        try {
            ContentProviderResult[] applyBatch = contentResolver.applyBatch("com.android.contacts", arrayList);
            if (!(applyBatch == null || applyBatch.length <= 0 || applyBatch[0].uri == null)) {
                j = Long.parseLong(applyBatch[0].uri.getLastPathSegment());
            }
        } catch (Exception unused2) {
        }
        synchronized (this.observerLock) {
            this.ignoreChanges = false;
        }
        return j;
    }

    private void deleteContactFromPhoneBook(int i) {
        if (hasContactsPermission()) {
            synchronized (this.observerLock) {
                this.ignoreChanges = true;
            }
            try {
                ContentResolver contentResolver = ApplicationLoader.applicationContext.getContentResolver();
                Uri build = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter("caller_is_syncadapter", "true").appendQueryParameter("account_name", this.systemAccount.name).appendQueryParameter("account_type", this.systemAccount.type).build();
                contentResolver.delete(build, "sync2 = " + i, null);
            } catch (Exception e) {
                FileLog.e(e);
            }
            synchronized (this.observerLock) {
                this.ignoreChanges = false;
            }
        }
    }

    /* access modifiers changed from: protected */
    public void markAsContacted(String str) {
        if (str != null) {
            Utilities.phoneBookQueue.postRunnable(new Runnable(str) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$vTazDC541YnKxz7SYzQ4x6tAHq8 */
                private final /* synthetic */ String f$0;

                {
                    this.f$0 = r1;
                }

                public final void run() {
                    ContactsController.lambda$markAsContacted$50(this.f$0);
                }
            });
        }
    }

    static /* synthetic */ void lambda$markAsContacted$50(String str) {
        Uri parse = Uri.parse(str);
        ContentValues contentValues = new ContentValues();
        contentValues.put("last_time_contacted", Long.valueOf(System.currentTimeMillis()));
        ApplicationLoader.applicationContext.getContentResolver().update(parse, contentValues, null, null);
    }

    public void addContact(TLRPC.User user, boolean z) {
        if (user != null) {
            TLRPC.TL_contacts_addContact tL_contacts_addContact = new TLRPC.TL_contacts_addContact();
            tL_contacts_addContact.id = getMessagesController().getInputUser(user);
            tL_contacts_addContact.first_name = user.first_name;
            tL_contacts_addContact.last_name = user.last_name;
            tL_contacts_addContact.phone = user.phone;
            tL_contacts_addContact.add_phone_privacy_exception = z;
            if (tL_contacts_addContact.phone == null) {
                tL_contacts_addContact.phone = "";
            } else if (tL_contacts_addContact.phone.length() > 0 && !tL_contacts_addContact.phone.startsWith(Marker.ANY_NON_NULL_MARKER)) {
                tL_contacts_addContact.phone = Marker.ANY_NON_NULL_MARKER + tL_contacts_addContact.phone;
            }
            getConnectionsManager().sendRequest(tL_contacts_addContact, new RequestDelegate(user) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$RINftb0JhA_PrcfsaqHyhOaJPQ0 */
                private final /* synthetic */ TLRPC.User f$1;

                {
                    this.f$1 = r2;
                }

                @Override // im.guobwnxjuc.tgnet.RequestDelegate
                public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                    ContactsController.this.lambda$addContact$53$ContactsController(this.f$1, tLObject, tL_error);
                }
            }, 6);
        }
    }

    public /* synthetic */ void lambda$addContact$53$ContactsController(TLRPC.User user, TLObject tLObject, TLRPC.TL_error tL_error) {
        int indexOf;
        if (tL_error == null) {
            TLRPC.Updates updates = (TLRPC.Updates) tLObject;
            getMessagesController().processUpdates(updates, false);
            for (int i = 0; i < updates.users.size(); i++) {
                TLRPC.User user2 = updates.users.get(i);
                if (user2.id == user.id) {
                    Utilities.phoneBookQueue.postRunnable(new Runnable(user2) {
                        /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$Qmywm9BfsLnfvnJDxB73ydc_s */
                        private final /* synthetic */ TLRPC.User f$1;

                        {
                            this.f$1 = r2;
                        }

                        public final void run() {
                            ContactsController.this.lambda$null$51$ContactsController(this.f$1);
                        }
                    });
                    TLRPC.Contact contact = new TLRPC.Contact();
                    contact.user_id = user2.id;
                    ArrayList<TLRPC.Contact> arrayList = new ArrayList<>();
                    arrayList.add(contact);
                    getMessagesStorage().putContacts(arrayList, false);
                    if (!TextUtils.isEmpty(user2.phone)) {
                        formatName(user2.first_name, user2.last_name);
                        getMessagesStorage().applyPhoneBookUpdates(user2.phone, "");
                        Contact contact2 = this.contactsBookSPhones.get(user2.phone);
                        if (!(contact2 == null || (indexOf = contact2.shortPhones.indexOf(user2.phone)) == -1)) {
                            contact2.phoneDeleted.set(indexOf, 0);
                        }
                    }
                }
            }
            AndroidUtilities.runOnUIThread(new Runnable(updates) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$Gyb8s_wEnae2t7tjZuYYiF1bsgk */
                private final /* synthetic */ TLRPC.Updates f$1;

                {
                    this.f$1 = r2;
                }

                public final void run() {
                    ContactsController.this.lambda$null$52$ContactsController(this.f$1);
                }
            });
        }
    }

    public /* synthetic */ void lambda$null$51$ContactsController(TLRPC.User user) {
        addContactToPhoneBook(user, true);
    }

    public /* synthetic */ void lambda$null$52$ContactsController(TLRPC.Updates updates) {
        for (int i = 0; i < updates.users.size(); i++) {
            TLRPC.User user = updates.users.get(i);
            if (user.contact && this.contactsDict.get(Integer.valueOf(user.id)) == null) {
                TLRPC.Contact contact = new TLRPC.Contact();
                contact.user_id = user.id;
                this.contacts.add(contact);
                this.contactsDict.put(Integer.valueOf(contact.user_id), contact);
            }
        }
        buildContactsSectionsArrays(true);
        getNotificationCenter().postNotificationName(NotificationCenter.contactsDidLoad, new Object[0]);
    }

    public void deleteContact(ArrayList<TLRPC.User> arrayList) {
        if (arrayList != null && !arrayList.isEmpty()) {
            TLRPC.TL_contacts_deleteContacts tL_contacts_deleteContacts = new TLRPC.TL_contacts_deleteContacts();
            ArrayList arrayList2 = new ArrayList();
            Iterator<TLRPC.User> it = arrayList.iterator();
            while (it.hasNext()) {
                TLRPC.User next = it.next();
                TLRPC.InputUser inputUser = getMessagesController().getInputUser(next);
                if (inputUser != null) {
                    next.contact = false;
                    arrayList2.add(Integer.valueOf(next.id));
                    tL_contacts_deleteContacts.id.add(inputUser);
                }
            }
            getConnectionsManager().sendRequest(tL_contacts_deleteContacts, new RequestDelegate(arrayList2, arrayList) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$OZMOE9Jh8xmAwdyn7cYmtGT0eQM */
                private final /* synthetic */ ArrayList f$1;
                private final /* synthetic */ ArrayList f$2;

                {
                    this.f$1 = r2;
                    this.f$2 = r3;
                }

                @Override // im.guobwnxjuc.tgnet.RequestDelegate
                public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                    ContactsController.this.lambda$deleteContact$56$ContactsController(this.f$1, this.f$2, tLObject, tL_error);
                }
            });
        }
    }

    public /* synthetic */ void lambda$deleteContact$56$ContactsController(ArrayList arrayList, ArrayList arrayList2, TLObject tLObject, TLRPC.TL_error tL_error) {
        int indexOf;
        if (tL_error == null) {
            getMessagesController().processUpdates((TLRPC.Updates) tLObject, false);
            getMessagesStorage().deleteContacts(arrayList);
            Utilities.phoneBookQueue.postRunnable(new Runnable(arrayList2) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$sKegl6Fu2P_WTJ2njO7BSdFMFzc */
                private final /* synthetic */ ArrayList f$1;

                {
                    this.f$1 = r2;
                }

                public final void run() {
                    ContactsController.this.lambda$null$54$ContactsController(this.f$1);
                }
            });
            for (int i = 0; i < arrayList2.size(); i++) {
                TLRPC.User user = (TLRPC.User) arrayList2.get(i);
                if (!TextUtils.isEmpty(user.phone)) {
                    getMessagesStorage().applyPhoneBookUpdates(user.phone, "");
                    Contact contact = this.contactsBookSPhones.get(user.phone);
                    if (!(contact == null || (indexOf = contact.shortPhones.indexOf(user.phone)) == -1)) {
                        contact.phoneDeleted.set(indexOf, 1);
                    }
                }
            }
            AndroidUtilities.runOnUIThread(new Runnable(arrayList2) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$t9WpJcHT9LdZ5guZ6fdw2B3_f24 */
                private final /* synthetic */ ArrayList f$1;

                {
                    this.f$1 = r2;
                }

                public final void run() {
                    ContactsController.this.lambda$null$55$ContactsController(this.f$1);
                }
            });
        }
    }

    public /* synthetic */ void lambda$null$54$ContactsController(ArrayList arrayList) {
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            deleteContactFromPhoneBook(((TLRPC.User) it.next()).id);
        }
    }

    public /* synthetic */ void lambda$null$55$ContactsController(ArrayList arrayList) {
        Iterator it = arrayList.iterator();
        boolean z = false;
        while (it.hasNext()) {
            TLRPC.User user = (TLRPC.User) it.next();
            TLRPC.Contact contact = this.contactsDict.get(Integer.valueOf(user.id));
            if (contact != null) {
                this.contacts.remove(contact);
                this.contactsDict.remove(Integer.valueOf(user.id));
                z = true;
            }
        }
        if (z) {
            buildContactsSectionsArrays(false);
        }
        getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, 1);
        getNotificationCenter().postNotificationName(NotificationCenter.contactsDidLoad, new Object[0]);
    }

    public void reloadContactsStatuses() {
        saveContactsLoadTime();
        getMessagesController().clearFullUsers();
        SharedPreferences.Editor edit = MessagesController.getMainSettings(this.currentAccount).edit();
        edit.putBoolean("needGetStatuses", true).commit();
        getConnectionsManager().sendRequest(new TLRPC.TL_contacts_getStatuses(), new RequestDelegate(edit) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$mQt0Fz79vR577aBHSzfS4th6XL8 */
            private final /* synthetic */ SharedPreferences.Editor f$1;

            {
                this.f$1 = r2;
            }

            @Override // im.guobwnxjuc.tgnet.RequestDelegate
            public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                ContactsController.this.lambda$reloadContactsStatuses$58$ContactsController(this.f$1, tLObject, tL_error);
            }
        });
    }

    public /* synthetic */ void lambda$reloadContactsStatuses$58$ContactsController(SharedPreferences.Editor editor, TLObject tLObject, TLRPC.TL_error tL_error) {
        if (tL_error == null) {
            AndroidUtilities.runOnUIThread(new Runnable(editor, tLObject) {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$Vw8nqw2hpy5ld5us9bfu4DQDro */
                private final /* synthetic */ SharedPreferences.Editor f$1;
                private final /* synthetic */ TLObject f$2;

                {
                    this.f$1 = r2;
                    this.f$2 = r3;
                }

                public final void run() {
                    ContactsController.this.lambda$null$57$ContactsController(this.f$1, this.f$2);
                }
            });
        }
    }

    public /* synthetic */ void lambda$null$57$ContactsController(SharedPreferences.Editor editor, TLObject tLObject) {
        editor.remove("needGetStatuses").commit();
        TLRPC.Vector vector = (TLRPC.Vector) tLObject;
        if (!vector.objects.isEmpty()) {
            ArrayList<TLRPC.User> arrayList = new ArrayList<>();
            Iterator<Object> it = vector.objects.iterator();
            while (it.hasNext()) {
                Object next = it.next();
                TLRPC.TL_user tL_user = new TLRPC.TL_user();
                TLRPC.TL_contactStatus tL_contactStatus = (TLRPC.TL_contactStatus) next;
                if (tL_contactStatus != null) {
                    if (tL_contactStatus.status instanceof TLRPC.TL_userStatusRecently) {
                        tL_contactStatus.status.expires = -100;
                    } else if (tL_contactStatus.status instanceof TLRPC.TL_userStatusLastWeek) {
                        tL_contactStatus.status.expires = -101;
                    } else if (tL_contactStatus.status instanceof TLRPC.TL_userStatusLastMonth) {
                        tL_contactStatus.status.expires = -102;
                    }
                    TLRPC.User user = getMessagesController().getUser(Integer.valueOf(tL_contactStatus.user_id));
                    if (user != null) {
                        user.status = tL_contactStatus.status;
                    }
                    tL_user.status = tL_contactStatus.status;
                    arrayList.add(tL_user);
                }
            }
            getMessagesStorage().updateUsers(arrayList, true, true, true);
        }
        getNotificationCenter().postNotificationName(NotificationCenter.updateInterfaces, 4);
    }

    public void loadPrivacySettings() {
        if (this.loadingDeleteInfo == 0) {
            this.loadingDeleteInfo = 1;
            getConnectionsManager().sendRequest(new TLRPC.TL_account_getAccountTTL(), new RequestDelegate() {
                /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$BtUYxO8haZZ1mL8sWiWokUACmvA */

                @Override // im.guobwnxjuc.tgnet.RequestDelegate
                public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                    ContactsController.this.lambda$loadPrivacySettings$60$ContactsController(tLObject, tL_error);
                }
            });
        }
        int i = 0;
        while (true) {
            int[] iArr = this.loadingPrivacyInfo;
            if (i < iArr.length) {
                if (iArr[i] == 0) {
                    iArr[i] = 1;
                    TLRPC.TL_account_getPrivacy tL_account_getPrivacy = new TLRPC.TL_account_getPrivacy();
                    switch (i) {
                        case 0:
                            tL_account_getPrivacy.key = new TLRPC.TL_inputPrivacyKeyStatusTimestamp();
                            break;
                        case 1:
                            tL_account_getPrivacy.key = new TLRPC.TL_inputPrivacyKeyChatInvite();
                            break;
                        case 2:
                            tL_account_getPrivacy.key = new TLRPC.TL_inputPrivacyKeyPhoneCall();
                            break;
                        case 3:
                            tL_account_getPrivacy.key = new TLRPC.TL_inputPrivacyKeyPhoneP2P();
                            break;
                        case 4:
                            tL_account_getPrivacy.key = new TLRPC.TL_inputPrivacyKeyProfilePhoto();
                            break;
                        case 5:
                            tL_account_getPrivacy.key = new TLRPC.TL_inputPrivacyKeyForwards();
                            break;
                        case 6:
                            tL_account_getPrivacy.key = new TLRPC.TL_inputPrivacyKeyPhoneNumber();
                            break;
                        case 7:
                        default:
                            tL_account_getPrivacy.key = new TLRPC.TL_inputPrivacyKeyAddedByPhone();
                            break;
                        case 8:
                            tL_account_getPrivacy.key = new TLRPC.TL_inputPrivacyKeyMoment();
                            break;
                    }
                    getConnectionsManager().sendRequest(tL_account_getPrivacy, new RequestDelegate(i) {
                        /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$zvapbf9goKZ5Qew_EDhDrYzdAyY */
                        private final /* synthetic */ int f$1;

                        {
                            this.f$1 = r2;
                        }

                        @Override // im.guobwnxjuc.tgnet.RequestDelegate
                        public final void run(TLObject tLObject, TLRPC.TL_error tL_error) {
                            ContactsController.this.lambda$loadPrivacySettings$62$ContactsController(this.f$1, tLObject, tL_error);
                        }
                    });
                }
                i++;
            } else {
                getNotificationCenter().postNotificationName(NotificationCenter.privacyRulesUpdated, new Object[0]);
                return;
            }
        }
    }

    public /* synthetic */ void lambda$loadPrivacySettings$60$ContactsController(TLObject tLObject, TLRPC.TL_error tL_error) {
        AndroidUtilities.runOnUIThread(new Runnable(tL_error, tLObject) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$EnmIdRHbassJlQzpRjDWvK7FdQ */
            private final /* synthetic */ TLRPC.TL_error f$1;
            private final /* synthetic */ TLObject f$2;

            {
                this.f$1 = r2;
                this.f$2 = r3;
            }

            public final void run() {
                ContactsController.this.lambda$null$59$ContactsController(this.f$1, this.f$2);
            }
        });
    }

    public /* synthetic */ void lambda$null$59$ContactsController(TLRPC.TL_error tL_error, TLObject tLObject) {
        if (tL_error == null) {
            this.deleteAccountTTL = ((TLRPC.TL_accountDaysTTL) tLObject).days;
            this.loadingDeleteInfo = 2;
        } else {
            this.loadingDeleteInfo = 0;
        }
        getNotificationCenter().postNotificationName(NotificationCenter.privacyRulesUpdated, new Object[0]);
    }

    public /* synthetic */ void lambda$loadPrivacySettings$62$ContactsController(int i, TLObject tLObject, TLRPC.TL_error tL_error) {
        AndroidUtilities.runOnUIThread(new Runnable(tL_error, tLObject, i) {
            /* class im.guobwnxjuc.messenger.$$Lambda$ContactsController$NJZLjxL5J4zx_H5N7awt5CNugJU */
            private final /* synthetic */ TLRPC.TL_error f$1;
            private final /* synthetic */ TLObject f$2;
            private final /* synthetic */ int f$3;

            {
                this.f$1 = r2;
                this.f$2 = r3;
                this.f$3 = r4;
            }

            public final void run() {
                ContactsController.this.lambda$null$61$ContactsController(this.f$1, this.f$2, this.f$3);
            }
        });
    }

    public /* synthetic */ void lambda$null$61$ContactsController(TLRPC.TL_error tL_error, TLObject tLObject, int i) {
        if (tL_error == null) {
            TLRPC.TL_account_privacyRules tL_account_privacyRules = (TLRPC.TL_account_privacyRules) tLObject;
            getMessagesController().putUsers(tL_account_privacyRules.users, false);
            getMessagesController().putChats(tL_account_privacyRules.chats, false);
            switch (i) {
                case 0:
                    this.lastseenPrivacyRules = tL_account_privacyRules.rules;
                    break;
                case 1:
                    this.groupPrivacyRules = tL_account_privacyRules.rules;
                    break;
                case 2:
                    this.callPrivacyRules = tL_account_privacyRules.rules;
                    break;
                case 3:
                    this.p2pPrivacyRules = tL_account_privacyRules.rules;
                    break;
                case 4:
                    this.profilePhotoPrivacyRules = tL_account_privacyRules.rules;
                    break;
                case 5:
                    this.forwardsPrivacyRules = tL_account_privacyRules.rules;
                    break;
                case 6:
                    this.phonePrivacyRules = tL_account_privacyRules.rules;
                    break;
                case 7:
                default:
                    this.addedByPhonePrivacyRules = tL_account_privacyRules.rules;
                    break;
                case 8:
                    this.momentPrivacyRules = tL_account_privacyRules.rules;
                    break;
            }
            this.loadingPrivacyInfo[i] = 2;
        } else {
            this.loadingPrivacyInfo[i] = 0;
        }
        getNotificationCenter().postNotificationName(NotificationCenter.privacyRulesUpdated, new Object[0]);
    }

    public void setDeleteAccountTTL(int i) {
        this.deleteAccountTTL = i;
    }

    public int getDeleteAccountTTL() {
        return this.deleteAccountTTL;
    }

    public boolean getLoadingDeleteInfo() {
        return this.loadingDeleteInfo != 2;
    }

    public boolean getLoadingPrivicyInfo(int i) {
        return this.loadingPrivacyInfo[i] != 2;
    }

    public ArrayList<TLRPC.PrivacyRule> getPrivacyRules(int i) {
        switch (i) {
            case 0:
                return this.lastseenPrivacyRules;
            case 1:
                return this.groupPrivacyRules;
            case 2:
                return this.callPrivacyRules;
            case 3:
                return this.p2pPrivacyRules;
            case 4:
                return this.profilePhotoPrivacyRules;
            case 5:
                return this.forwardsPrivacyRules;
            case 6:
                return this.phonePrivacyRules;
            case 7:
                return this.addedByPhonePrivacyRules;
            case 8:
                return this.momentPrivacyRules;
            default:
                return null;
        }
    }

    public void setPrivacyRules(ArrayList<TLRPC.PrivacyRule> arrayList, int i) {
        switch (i) {
            case 0:
                this.lastseenPrivacyRules = arrayList;
                break;
            case 1:
                this.groupPrivacyRules = arrayList;
                break;
            case 2:
                this.callPrivacyRules = arrayList;
                break;
            case 3:
                this.p2pPrivacyRules = arrayList;
                break;
            case 4:
                this.profilePhotoPrivacyRules = arrayList;
                break;
            case 5:
                this.forwardsPrivacyRules = arrayList;
                break;
            case 6:
                this.phonePrivacyRules = arrayList;
                break;
            case 7:
                this.addedByPhonePrivacyRules = arrayList;
                break;
            case 8:
                this.momentPrivacyRules = arrayList;
                break;
        }
        getNotificationCenter().postNotificationName(NotificationCenter.privacyRulesUpdated, new Object[0]);
        reloadContactsStatuses();
    }

    public void createOrUpdateConnectionServiceContact(int i, String str, String str2) {
        String str3;
        int i2;
        Cursor cursor;
        ArrayList<ContentProviderOperation> arrayList;
        if (hasContactsPermission()) {
            try {
                ContentResolver contentResolver = ApplicationLoader.applicationContext.getContentResolver();
                ArrayList<ContentProviderOperation> arrayList2 = new ArrayList<>();
                Uri build = ContactsContract.Groups.CONTENT_URI.buildUpon().appendQueryParameter("caller_is_syncadapter", "true").build();
                Uri build2 = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter("caller_is_syncadapter", "true").build();
                Cursor query = contentResolver.query(build, new String[]{"_id"}, "title=? AND account_type=? AND account_name=?", new String[]{"AppConnectionService", this.systemAccount.type, this.systemAccount.name}, null);
                if (query == null || !query.moveToFirst()) {
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("account_type", this.systemAccount.type);
                    contentValues.put("account_name", this.systemAccount.name);
                    contentValues.put("group_visible", (Integer) 0);
                    str3 = "account_type";
                    contentValues.put("group_is_read_only", (Integer) 1);
                    contentValues.put("title", "AppConnectionService");
                    i2 = Integer.parseInt(contentResolver.insert(build, contentValues).getLastPathSegment());
                } else {
                    i2 = query.getInt(0);
                    str3 = "account_type";
                }
                if (query != null) {
                    query.close();
                }
                Cursor query2 = contentResolver.query(ContactsContract.Data.CONTENT_URI, new String[]{"raw_contact_id"}, "mimetype=? AND data1=?", new String[]{"vnd.android.cursor.item/group_membership", i2 + ""}, null);
                int size = arrayList2.size();
                if (query2 == null || !query2.moveToFirst()) {
                    cursor = query2;
                    arrayList = arrayList2;
                    arrayList.add(ContentProviderOperation.newInsert(build2).withValue(str3, this.systemAccount.type).withValue("account_name", this.systemAccount.name).withValue("raw_contact_is_read_only", 1).withValue("aggregation_mode", 3).build());
                    arrayList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference("raw_contact_id", size).withValue("mimetype", "vnd.android.cursor.item/name").withValue("data2", str).withValue("data3", str2).build());
                    ContentProviderOperation.Builder withValue = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference("raw_contact_id", size).withValue("mimetype", "vnd.android.cursor.item/phone_v2");
                    arrayList.add(withValue.withValue("data1", "+99084" + i).build());
                    arrayList.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference("raw_contact_id", size).withValue("mimetype", "vnd.android.cursor.item/group_membership").withValue("data1", Integer.valueOf(i2)).build());
                } else {
                    int i3 = query2.getInt(0);
                    ContentProviderOperation.Builder newUpdate = ContentProviderOperation.newUpdate(build2);
                    cursor = query2;
                    arrayList = arrayList2;
                    arrayList.add(newUpdate.withSelection("_id=?", new String[]{i3 + ""}).withValue("deleted", 0).build());
                    ContentProviderOperation.Builder newUpdate2 = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
                    ContentProviderOperation.Builder withSelection = newUpdate2.withSelection("raw_contact_id=? AND mimetype=?", new String[]{i3 + "", "vnd.android.cursor.item/phone_v2"});
                    arrayList.add(withSelection.withValue("data1", "+99084" + i).build());
                    ContentProviderOperation.Builder newUpdate3 = ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI);
                    arrayList.add(newUpdate3.withSelection("raw_contact_id=? AND mimetype=?", new String[]{i3 + "", "vnd.android.cursor.item/name"}).withValue("data2", str).withValue("data3", str2).build());
                }
                if (cursor != null) {
                    cursor.close();
                }
                contentResolver.applyBatch("com.android.contacts", arrayList);
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    }

    public void deleteConnectionServiceContact() {
        if (hasContactsPermission()) {
            try {
                ContentResolver contentResolver = ApplicationLoader.applicationContext.getContentResolver();
                Cursor query = contentResolver.query(ContactsContract.Groups.CONTENT_URI, new String[]{"_id"}, "title=? AND account_type=? AND account_name=?", new String[]{"AppConnectionService", this.systemAccount.type, this.systemAccount.name}, null);
                if (query != null && query.moveToFirst()) {
                    int i = query.getInt(0);
                    query.close();
                    Cursor query2 = contentResolver.query(ContactsContract.Data.CONTENT_URI, new String[]{"raw_contact_id"}, "mimetype=? AND data1=?", new String[]{"vnd.android.cursor.item/group_membership", i + ""}, null);
                    if (query2 != null && query2.moveToFirst()) {
                        int i2 = query2.getInt(0);
                        query2.close();
                        Uri uri = ContactsContract.RawContacts.CONTENT_URI;
                        contentResolver.delete(uri, "_id=?", new String[]{i2 + ""});
                    } else if (query2 != null) {
                        query2.close();
                    }
                } else if (query != null) {
                    query.close();
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
        }
    }

    public static String formatName(String str, String str2) {
        if (!TextUtils.isEmpty(str)) {
            return str.trim();
        }
        return LocaleController.getString("UnKnown", R.string.UnKnown);
    }
}