package com.paypal.android.sdk.onetouch.core.sdk;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;

import com.braintreepayments.api.internal.SignatureVerification;

import java.util.List;

public class WalletAppHelper {

    private static final String WALLET_APP_PACKAGE = "com.paypal.android.p2pmobile";
    private static final String WALLET_APP_CERT_SUBJECT = "O=Paypal";
    private static final String WALLET_APP_CERT_ISSUER = "O=Paypal";
    private static final int WALLET_APP_PUBLIC_KEY_HASH_CODE = 34172764;

    /**
     * Creates an intent to the Wallet app with the given name.
     *
     * @return
     */
    protected Intent createSdkIntent(String action, String className) {
        Intent intent = new Intent(action);
        intent.setComponent(ComponentName.unflattenFromString(className));
        intent.setPackage(WALLET_APP_PACKAGE);
        return intent;
    }

    /**
     * Returns true if the authenticator (p2p app) is present, we're in an environment that supports
     * authenticator, and all the right permissions are present in the wallet app.
     *
     * @return
     */
    public boolean isValidP2pMobileAuthenticatorInstalled(Context context,
            boolean isAuthenticatorSecurityEnabled) {
        return isValidGenericAuthenticatorInstalled(context,
                isAuthenticatorSecurityEnabled,
                WALLET_APP_PACKAGE);
    }

    /**
     * This method supports the case where we configure and/or switch the target app package.
     *
     * @param context
     * @param isAuthenticatorSecurityEnabled
     * @param appPackage
     * @return {@code true} if the authenticator (p2p app) is present, we're in an environment that
     * supports authenticator, and all the right permissions are present in the wallet app.
     * Otherwise returns {@code false}.
     */
    public boolean isValidGenericAuthenticatorInstalled(Context context,
            boolean isAuthenticatorSecurityEnabled, String appPackage) {
        return (isAuthenticatorSecurityEnabled && SignatureVerification.isSignatureValid(context,
                appPackage, WALLET_APP_CERT_SUBJECT, WALLET_APP_CERT_ISSUER,
                WALLET_APP_PUBLIC_KEY_HASH_CODE));
    }

    public boolean isWalletIntentSafe(Context context, String intentAction, String intentClass) {
        Intent intent = createSdkIntent(intentAction, intentClass);
        List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent, 0);
        return (null != activities) && activities.size() > 0;
    }
}

@@ -145,16 +145,122 @@
 Intent;%0A
+import android.content.pm.PackageManager;%0Aimport android.content.pm.PackageManager.NameNotFoundException;%0A
 import a
@@ -1492,32 +1492,16 @@
 context,
-%0A               
  isAuthe
@@ -2172,16 +2172,57 @@
 return (
+isAppInstalled(context, appPackage) && (!
 isAuthen
@@ -2244,18 +2244,34 @@
 Enabled 
-&&
+%7C%7C%0A               
  Signatu
@@ -2310,32 +2310,16 @@
 context,
-%0A               
  appPack
@@ -2347,16 +2347,40 @@
 SUBJECT,
+%0A                       
  WALLET_
@@ -2395,32 +2395,16 @@
 _ISSUER,
-%0A               
  WALLET_
@@ -2429,16 +2429,17 @@
 H_CODE))
+)
 ;%0A    %7D%0A
@@ -2773,11 +2773,311 @@
 0;%0A    %7D
+%0A%0A    private boolean isAppInstalled(Context context, String packageName) %7B%0A        try %7B%0A            context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);%0A            return true;%0A        %7D catch (NameNotFoundException e) %7B%0A            return false;%0A        %7D%0A    %7D
 %0A%7D%0A

Fix PayPal Wallet installed check