<commit_before>package com.datatheorem.android.trustkit;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.security.NetworkSecurityPolicy;
import android.support.annotation.NonNull;

import com.datatheorem.android.trustkit.config.ConfigurationException;
import com.datatheorem.android.trustkit.reporting.BackgroundReporter;
import com.datatheorem.android.trustkit.utils.TrustKitLog;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.UUID;


public class TrustKit {

    private static final String TRUSTKIT_VENDOR_ID = "TRUSTKIT_VENDOR_ID";
    private final TrustKitConfiguration trustKitConfiguration;
    protected BackgroundReporter backgroundReporter;
    protected static TrustKit trustKitInstance;

    protected TrustKit(@NonNull Context context,
                       @NonNull TrustKitConfiguration trustKitConfiguration) {
        this.trustKitConfiguration = trustKitConfiguration;

        // Create the background reporter for sending pin failure reports
        String appPackageName = context.getPackageName();
        String appVersion;
        try {
            appVersion = context.getPackageManager().getPackageInfo(appPackageName, 0).versionName;

        } catch (PackageManager.NameNotFoundException e) {
            appVersion = "N/A";
        }
        String appVendorId = getOrCreateVendorIdentifier(context);
        this.backgroundReporter = new BackgroundReporter(true, appPackageName, appVersion,
                appVendorId);
    }

    @NonNull
    private static String getOrCreateVendorIdentifier(@NonNull Context appContext) {
        SharedPreferences trustKitSharedPreferences =
                PreferenceManager.getDefaultSharedPreferences(appContext);
        // We store the vendor ID in the App's preferences
        String appVendorId = trustKitSharedPreferences.getString(TRUSTKIT_VENDOR_ID, "");
        if (appVendorId.equals("")) {
            // First time the App is running: generate and store a new vendor ID
            TrustKitLog.i("Generating new vendor identifier...");
            appVendorId = UUID.randomUUID().toString();
            SharedPreferences.Editor editor = trustKitSharedPreferences.edit();
            editor.putString(TRUSTKIT_VENDOR_ID, appVendorId);
            editor.apply();
        }
        return appVendorId;
    }

    @NonNull
    public static TrustKit getInstance() {
        if (trustKitInstance == null) {
            throw new IllegalStateException("TrustKit has not been initialized");
        }
        return trustKitInstance;
    }

    public static void initWithNetworkPolicy(@NonNull Context context) {
        // Try to get the default network policy resource ID
        final int networkSecurityConfigId = context.getResources().getIdentifier(
                "network_security_config", "xml", context.getPackageName());
        initWithNetworkPolicy(context, networkSecurityConfigId);
    }

    public static void initWithNetworkPolicy(@NonNull Context context, int policyResourceId) {
        // On Android N, ensure that the system was also able to load the policy
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M &&
                NetworkSecurityPolicy.getInstance() == null) {
            // Android did not find a policy because the supplied resource ID is wrong or the policy
            // file is not properly setup in the manifest, or contains bad data
            throw new ConfigurationException("TrustKit was initialized with a network policy that" +
                    "was not properly configured for Android N ");
        }

        // Then try to load the supplied policy
        TrustKitConfiguration trustKitConfiguration = null;
        try {
            trustKitConfiguration = TrustKitConfiguration.fromXmlPolicy(
                    context.getResources().getXml(policyResourceId)
            );
        } catch (XmlPullParserException | IOException e) {
            throw new ConfigurationException("Could not parse network security policy file");
        }

        trustKitInstance = new TrustKit(context, trustKitConfiguration);
    }

    // TODO(ad): Consider removing this? or not? or making it @deprecated?
    public static void initWithConfiguration(Context appContext, TrustKitConfiguration trustKitConfiguration) {

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M
                && NetworkSecurityPolicy.getInstance() != null) {
            throw new ConfigurationException("A NetworkSecurityConfigurationPolicy is already " +
                    "defined. Please use TrustKit.initWithNetworkPolicy(Context context); instead");
        }

        if (trustKitInstance == null) {
            trustKitInstance = new TrustKit(appContext, trustKitConfiguration);
        } else {
            throw new IllegalStateException("Already instantiated");
        }
    }

    public TrustKitConfiguration getConfiguration() { return trustKitConfiguration; }
    public BackgroundReporter getReporter() { return backgroundReporter; }
}<commit_msg>Minor tweaks<commit_after>package com.datatheorem.android.trustkit;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.security.NetworkSecurityPolicy;
import android.support.annotation.NonNull;

import com.datatheorem.android.trustkit.config.ConfigurationException;
import com.datatheorem.android.trustkit.reporting.BackgroundReporter;
import com.datatheorem.android.trustkit.utils.TrustKitLog;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.UUID;


public class TrustKit {

    private static final String TRUSTKIT_VENDOR_ID = "TRUSTKIT_VENDOR_ID";
    private final TrustKitConfiguration trustKitConfiguration;
    protected BackgroundReporter backgroundReporter;
    protected static TrustKit trustKitInstance;

    protected TrustKit(@NonNull Context context,
                       @NonNull TrustKitConfiguration trustKitConfiguration) {
        this.trustKitConfiguration = trustKitConfiguration;

        // Create the background reporter for sending pin failure reports
        String appPackageName = context.getPackageName();
        String appVersion;
        try {
            appVersion = context.getPackageManager().getPackageInfo(appPackageName, 0).versionName;

        } catch (PackageManager.NameNotFoundException e) {
            appVersion = "N/A";
        }
        String appVendorId = getOrCreateVendorIdentifier(context);
        this.backgroundReporter = new BackgroundReporter(true, appPackageName, appVersion,
                appVendorId);
    }

    @NonNull
    private static String getOrCreateVendorIdentifier(@NonNull Context appContext) {
        SharedPreferences trustKitSharedPreferences =
                PreferenceManager.getDefaultSharedPreferences(appContext);
        // We store the vendor ID in the App's preferences
        String appVendorId = trustKitSharedPreferences.getString(TRUSTKIT_VENDOR_ID, "");
        if (appVendorId.equals("")) {
            // First time the App is running: generate and store a new vendor ID
            TrustKitLog.i("Generating new vendor identifier...");
            appVendorId = UUID.randomUUID().toString();
            SharedPreferences.Editor editor = trustKitSharedPreferences.edit();
            editor.putString(TRUSTKIT_VENDOR_ID, appVendorId);
            editor.apply();
        }
        return appVendorId;
    }

    @NonNull
    public static TrustKit getInstance() {
        if (trustKitInstance == null) {
            throw new IllegalStateException("TrustKit has not been initialized");
        }
        return trustKitInstance;
    }

    public static void initWithNetworkPolicy(@NonNull Context context) {
        // Try to get the default network policy resource ID
        final int networkSecurityConfigId = context.getResources().getIdentifier(
                "network_security_config", "xml", context.getPackageName());
        initWithNetworkPolicy(context, networkSecurityConfigId);
    }

    public static void initWithNetworkPolicy(@NonNull Context context, int policyResourceId) {
        // On Android N, ensure that the system was also able to load the policy
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M &&
                NetworkSecurityPolicy.getInstance() == null) {
            // Android did not find a policy because the supplied resource ID is wrong or the policy
            // file is not properly setup in the manifest, or contains bad data
            throw new ConfigurationException("TrustKit was initialized with a network policy that" +
                    "was not properly configured for Android N ");
        }

        // Then try to load the supplied policy
        TrustKitConfiguration trustKitConfiguration;
        try {
            trustKitConfiguration = TrustKitConfiguration.fromXmlPolicy(
                    context.getResources().getXml(policyResourceId)
            );
        } catch (XmlPullParserException | IOException e) {
            throw new ConfigurationException("Could not parse network security policy file");
        }

        trustKitInstance = new TrustKit(context, trustKitConfiguration);
    }

    // TODO(ad): Consider removing this? or not? or making it @deprecated?
    public static void initWithConfiguration(Context appContext, TrustKitConfiguration trustKitConfiguration) {

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M
                && NetworkSecurityPolicy.getInstance() != null) {
            throw new ConfigurationException("A NetworkSecurityConfigurationPolicy is already " +
                    "defined. Please use TrustKit.initWithNetworkPolicy(Context context); instead");
        }

        if (trustKitInstance == null) {
            trustKitInstance = new TrustKit(appContext, trustKitConfiguration);
        } else {
            throw new IllegalStateException("Already instantiated");
        }
    }

    public TrustKitConfiguration getConfiguration() { return trustKitConfiguration; }
    
    public BackgroundReporter getReporter() { return backgroundReporter; }
}
{'fraction_non_alphanumeric': 0.07164207887378267, 'fraction_numerical': 0.0003856908687686819, 'mean_word_length': 3.0499804763764153, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2277685', 'n_tokens_mistral': 2470, 'n_tokens_neox': 2410, 'n_words': 680}