package com.seewo.vcommons.utils;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.seewo.hiddenapi.UserHandleInsider;
import com.seewo.vcommons.constants.SeewoConstants;
import com.seewo.vcommons.data.PropertiesUtils;
import com.seewo.vcommons.utils.reflect.Reflect;

/* JADX INFO: loaded from: classes.dex */
public class GoogleAccountUtils {
    private static final String TAG = "GoogleAccountUtils";
    private static final GoogleAccountUtils mInstance = new GoogleAccountUtils();
    private final Handler mHandle = new Handler(Looper.getMainLooper());
    private Runnable mRetryRunnable = null;

    public static GoogleAccountUtils getInstance() {
        return mInstance;
    }

    public boolean hasGoogleAccount(Context context) {
        return ((Account[]) Reflect.on(AccountManager.get(context)).call("getAccountsAsUser", Integer.valueOf(UserHandleInsider.myUserId())).get()).length != 0;
    }

    public void logoutGoogleAccount(final Context context) {
        try {
            AccountManager accountManager = AccountManager.get(context);
            if (accountManager == null) {
                Log.e(TAG, "AccountManager not found");
                return;
            }
            Account[] accounts = accountManager.getAccounts();
            if (accounts != null && accounts.length != 0) {
                for (Account account : accounts) {
                    if ("com.google".equals(account.type)) {
                        Log.d(TAG, "remove google account:" + account);
                        accountManager.removeAccount(account, new AccountManagerCallback<Boolean>() { // from class: com.seewo.vcommons.utils.GoogleAccountUtils.1
                            @Override // android.accounts.AccountManagerCallback
                            public void run(AccountManagerFuture<Boolean> accountManagerFuture) {
                                boolean z = false;
                                try {
                                    accountManagerFuture.getResult();
                                    z = Boolean.getBoolean("booleanResult");
                                    if (!z) {
                                        GoogleAccountUtils.this.postLogoutAccountForce(context);
                                    }
                                } catch (Exception e) {
                                    Log.e(GoogleAccountUtils.TAG, "", e);
                                }
                                Log.d(GoogleAccountUtils.TAG, "remove result:" + z);
                            }
                        }, null);
                    }
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "", e);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void removeAccountForce(Context context) {
        Log.d(TAG, "removeAccountForce");
        AccountManager accountManager = AccountManager.get(context);
        Account[] accountArr = (Account[]) Reflect.on(accountManager).call("getAccountsAsUser", Integer.valueOf(UserHandleInsider.USER_SYSTEM)).get();
        if (accountArr != null) {
            PropertiesUtils.setProperty("sys.remove_account_verify_caller", "false");
            for (Account account : accountArr) {
                Reflect.on(accountManager).call("removeAccountExplicitly", account);
            }
            PropertiesUtils.setProperty("sys.remove_account_verify_caller", SeewoConstants.VALUE_LOCK_TOUCH_OFF);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void postLogoutAccountForce(final Context context) {
        if (this.mRetryRunnable == null) {
            this.mRetryRunnable = new Runnable() { // from class: com.seewo.vcommons.utils.GoogleAccountUtils.2
                @Override // java.lang.Runnable
                public void run() {
                    GoogleAccountUtils.this.removeAccountForce(context);
                    if (PropertiesUtils.getProperty("sys.remove_account_verify_caller", true)) {
                        return;
                    }
                    PropertiesUtils.setProperty("sys.remove_account_verify_caller", SeewoConstants.VALUE_LOCK_TOUCH_OFF);
                }
            };
        }
        this.mHandle.removeCallbacks(this.mRetryRunnable);
        this.mHandle.postDelayed(this.mRetryRunnable, 1000L);
    }
}
