package com.pairip.licensecheck3;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Parcel;
import android.os.RemoteException;
import android.util.Log;
import com.pairip.licensecheck.ILicenseV2ResultListener;
/* loaded from: classes8.dex */
public class LicenseClientV3 implements ServiceConnection {
    private static final int ERROR_INVALID_PACKAGE_NAME = 3;
    private static final int FLAG_RPC_CALL = 0;
    private static final int LICENSED = 0;
    private static final int MAX_RETRIES = 3;
    private static final int MILLIS_PER_SEC = 1000;
    private static final int NOT_LICENSED = 2;
    private static final String PAYLOAD_PAYWALL = "PAYWALL_INTENT";
    private static final int RETRY_DELAY_MILLIS = 1000;
    private static final String SERVICE_INTERFACE_CLASS_NAME = "com.android.vending.licensing.ILicensingService";
    private static final String SERVICE_PACKAGE = "com.android.vending";
    private static final String TAG = "LicenseClientV3";
    private static final int TRANSACTION_CHECK_LICENSE_V2 = 2;
    public static LicenseCheckState licenseCheckState = LicenseCheckState.CHECK_REQUIRED;
    public static String licensePubKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqiOL4a5cvS7L9Re+Rxur4N/N6Dk0ua6XhZRIgn9z9StfuLBf5joSbngtnrE19EfCA3oIlahTc9XILu/3WTrluMahGO+G6hu3WSpVYQy0Wdntv5pwkqtENywJSitDMUF7ODLk2fnL8nnHmVgSBZHz6nDiOlMdVOFlUadwp15l5YTxDZPKh3eISaxqRpECyv1QBs3ISaOzMQf19Py4RmLzzTDCf5RjYdltVITElXmvvYhJyfPh5v3XbJ4GLN1Sqi6oNeyHU3+2uVDSztHlGILjFk7zK4Sl5RulMvHcyedx5AC5H0ZSIs8wLaYaqLRtzLqhPDO13YFMwiuMBCJnbO7gbQIDAQAB";
    public static String packageName = "me.eladga.finddiamonds";
    private static Bundle responsePayload;
    private final Activity activity;
    private final DelayedTaskExecutor delayedTaskExecutor = new DelayedTaskExecutor();
    private int retryNum = 0;

    /* loaded from: classes8.dex */
    public enum LicenseCheckState {
        CHECK_REQUIRED,
        OK
    }

    public static void onActivityCreate(Activity activity) {
        new LicenseClientV3(activity).initializeLicenseCheck();
    }

    public LicenseClientV3(Activity activity) {
        this.activity = activity;
    }

    private void initializeLicenseCheck() {
        int ordinal = licenseCheckState.ordinal();
        if (ordinal == 0) {
            connectToLicensingService();
        } else if (ordinal != 1) {
        } else {
            try {
                ResponseValidator.validateResponse(responsePayload, packageName);
            } catch (LicenseCheckException e) {
                handleError(e);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void connectToLicensingService() {
        Log.d(TAG, "Connecting to the licensing service...");
        try {
            if (this.activity.getApplicationContext().bindService(new Intent(SERVICE_INTERFACE_CLASS_NAME).setPackage("com.android.vending").setAction(SERVICE_INTERFACE_CLASS_NAME), this, 1)) {
                return;
            }
            retryOrThrow(new LicenseCheckException("Could not bind with the licensing service."));
        } catch (SecurityException e) {
            retryOrThrow(new LicenseCheckException("Not allowed to bind with the licensing service.", e));
        }
    }

    @Override // android.content.ServiceConnection
    public void onServiceConnected(ComponentName componentName, final IBinder licensingServiceBinder) {
        Log.d(TAG, "Connected to the licensing service.");
        if (licenseCheckState.equals(LicenseCheckState.OK)) {
            return;
        }
        new Thread(new Runnable() { // from class: com.pairip.licensecheck3.LicenseClientV3$$ExternalSyntheticLambda2
            @Override // java.lang.Runnable
            public final void run() {
                LicenseClientV3.this.lambda$onServiceConnected$0(licensingServiceBinder);
            }
        }).start();
    }

    @Override // android.content.ServiceConnection
    public void onServiceDisconnected(ComponentName componentName) {
        Log.w(TAG, "Unexpectedly disconnected from the licensing service.");
        retryOrThrow(new LicenseCheckException("Licensing service unexpectedly disconnected."));
    }

    /* renamed from: checkLicenseInternal */
    public void lambda$onServiceConnected$0(IBinder licensingServiceBinder) {
        if (licensingServiceBinder == null) {
            retryOrThrow(new LicenseCheckException("Received a null binder."));
            return;
        }
        Log.d(TAG, "Sending request to licensing service...");
        Parcel obtain = Parcel.obtain();
        Parcel obtain2 = Parcel.obtain();
        try {
            try {
                populateInputData(obtain, licensingServiceBinder);
                if (!licensingServiceBinder.transact(2, obtain, obtain2, 0)) {
                    handleError(new LicenseCheckException("Licensing service could not process request."));
                }
            } catch (DeadObjectException e) {
                retryOrThrow(new LicenseCheckException("Licensing service process died.", e));
            } catch (RemoteException e2) {
                handleError(new LicenseCheckException("Error when calling licensing service.", e2));
            }
        } finally {
            obtain.recycle();
            obtain2.recycle();
            Log.d(TAG, "Request to licensing service sent.");
        }
    }

    private void populateInputData(Parcel inputData, IBinder licensingService) throws RemoteException {
        inputData.writeInterfaceToken(licensingService.getInterfaceDescriptor());
        inputData.writeString(packageName);
        inputData.writeStrongBinder(createResultListener(this).asBinder());
        inputData.writeInt(0);
    }

    private static ILicenseV2ResultListener createResultListener(LicenseClientV3 client) {
        return new ILicenseV2ResultListener.Stub() { // from class: com.pairip.licensecheck3.LicenseClientV3.1
            @Override // com.pairip.licensecheck.ILicenseV2ResultListener
            public void verifyLicense(int responseCode, Bundle responsePayload2) {
                LicenseClientV3.this.processResponse(responseCode, responsePayload2);
            }
        };
    }

    private void retryOrThrow(LicenseCheckException error) {
        int i = this.retryNum;
        if (i < 3) {
            this.retryNum = i + 1;
            this.delayedTaskExecutor.schedule(new Runnable() { // from class: com.pairip.licensecheck3.LicenseClientV3$$ExternalSyntheticLambda3
                @Override // java.lang.Runnable
                public final void run() {
                    LicenseClientV3.this.connectToLicensingService();
                }
            }, 1000L);
            Log.d(TAG, String.format("Retry #%d. License check failed with error '%s'. Next try in %ds...", Integer.valueOf(this.retryNum), error.getMessage(), 1L));
            return;
        }
        handleError(error);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void processResponse(int responseCode, Bundle responsePayload2) {
        try {
            if (responseCode == 3) {
                throw new LicenseCheckException("Request package name invalid.");
            }
            if (responseCode != 0) {
                if (responseCode == 2) {
                    showPaywall((PendingIntent) responsePayload2.getParcelable(PAYLOAD_PAYWALL));
                    return;
                }
                throw new LicenseCheckException(String.format("Unexpected response code %d received.", Integer.valueOf(responseCode)));
            }
            ResponseValidator.validateResponse(responsePayload2, packageName);
            Log.i(TAG, "License check succeeded.");
            licenseCheckState = LicenseCheckState.OK;
            responsePayload = responsePayload2;
        } catch (LicenseCheckException e) {
            handleError(e);
        }
    }

    private void handleError(LicenseCheckException ex) {
        String stackTraceString = Log.getStackTraceString(ex);
        Log.e(TAG, "Error while checking license: " + stackTraceString);
        if (licenseCheckState.equals(LicenseCheckState.OK)) {
            return;
        }
        showErrorDialog();
    }

    private void showErrorDialog() {
        licenseCheckState = LicenseCheckState.CHECK_REQUIRED;
        this.activity.runOnUiThread(new Runnable() { // from class: com.pairip.licensecheck3.LicenseClientV3$$ExternalSyntheticLambda1
            @Override // java.lang.Runnable
            public final void run() {
                LicenseClientV3.this.lambda$showErrorDialog$0();
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$showErrorDialog$0() {
        try {
            new AlertDialog.Builder(this.activity).setTitle("Something went wrong").setMessage("Check that Google Play is enabled on your device and that you're using an up-to-date version before opening the app. If the problem persists try reinstalling the app.").setPositiveButton("Close", new DialogInterface.OnClickListener() { // from class: com.pairip.licensecheck3.LicenseClientV3$$ExternalSyntheticLambda4
                @Override // android.content.DialogInterface.OnClickListener
                public final void onClick(DialogInterface dialogInterface, int i) {
                    LicenseClientV3.this.lambda$showErrorDialog$1(dialogInterface, i);
                }
            }).setCancelable(false).show();
        } catch (RuntimeException e) {
            String stackTraceString = Log.getStackTraceString(e);
            Log.d(TAG, "Couldn't show the error dialog. " + stackTraceString);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$showErrorDialog$1(DialogInterface dialogInterface, int i) {
        onDialogExitClick();
    }

    private void onDialogExitClick() {
        this.activity.finishAndRemoveTask();
    }

    private void showPaywall(final PendingIntent paywallIntent) {
        if (paywallIntent == null) {
            handleError(new LicenseCheckException("Paywall intent is null."));
            return;
        }
        licenseCheckState = LicenseCheckState.CHECK_REQUIRED;
        this.activity.runOnUiThread(new Runnable() { // from class: com.pairip.licensecheck3.LicenseClientV3$$ExternalSyntheticLambda0
            @Override // java.lang.Runnable
            public final void run() {
                LicenseClientV3.this.lambda$showPaywall$0(paywallIntent);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ void lambda$showPaywall$0(PendingIntent pendingIntent) {
        try {
            pendingIntent.send();
            this.activity.finishAndRemoveTask();
        } catch (PendingIntent.CanceledException e) {
            handleError(new LicenseCheckException("Paywall intent unexpectedly cancelled.", e));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes8.dex */
    public static final class DelayedTaskExecutor {
        private final Handler handler;

        private DelayedTaskExecutor() {
            this.handler = new Handler(Looper.getMainLooper());
        }

        public void schedule(Runnable task, long delayMillis) {
            this.handler.postDelayed(task, delayMillis);
        }
    }

    /* loaded from: classes8.dex */
    public static final class LicenseCheckException extends Exception {
        public LicenseCheckException(String message) {
            super(message);
        }

        public LicenseCheckException(String message, Throwable cause) {
            super(message, cause);
        }
    }
}
