<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.pyamsoft.pydroid;

import android.content.Context;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import com.pyamsoft.pydroid.about.AboutLibrariesModule;
import com.pyamsoft.pydroid.about.LicenseProvider;
import com.pyamsoft.pydroid.ads.AdvertisementModule;
import com.pyamsoft.pydroid.donate.DonateModule;
import com.pyamsoft.pydroid.social.SocialMediaModule;
import com.pyamsoft.pydroid.version.ApiModule;
import com.pyamsoft.pydroid.version.VersionCheckModule;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.solovyev.android.checkout.Billing;
import org.solovyev.android.checkout.Purchase;
import org.solovyev.android.checkout.PurchaseVerifier;
import org.solovyev.android.checkout.RequestListener;

public class PYDroidModule {

  @NonNull private final Provider provider;

  PYDroidModule(@NonNull Context context, @NonNull LicenseProvider licenseProvider) {
    provider = new Provider(context, licenseProvider);
  }

  // Create a new one every time
  @CheckResult @NonNull final AboutLibrariesModule provideAboutLibrariesModule() {
    return new AboutLibrariesModule(provider);
  }

  // Create a new one every time
  @CheckResult @NonNull final DonateModule provideDonateModule() {
    return new DonateModule(provider);
  }

  // Create a new one every time
  @CheckResult @NonNull final SocialMediaModule provideSocialMediaModule() {
    return new SocialMediaModule();
  }

  // Create a new one every time
  //
  // NOTE: Makes a new ApiModule
  @CheckResult @NonNull final VersionCheckModule provideVersionCheckModule() {
    return new VersionCheckModule(provider, new ApiModule());
  }

  // Create a new one every time
  //
  // NOTE: Makes a new SocialMediaModule
  @CheckResult @NonNull final AdvertisementModule provideAdvertisementModule() {
    return new AdvertisementModule(provider);
  }

  public static class Provider {

    @NonNull private static final String SKU_DONATE = ".donate";
    @NonNull private static final String SKU_DONATE_ONE = SKU_DONATE + ".one";
    @NonNull private static final String SKU_DONATE_TWO = SKU_DONATE + ".two";
    @NonNull private static final String SKU_DONATE_FIVE = SKU_DONATE + ".five";
    @NonNull private static final String SKU_DONATE_TEN = SKU_DONATE + ".ten";

    // Singleton
    @NonNull private final Context appContext;
    @NonNull private final LicenseProvider licenseProvider;
    @NonNull private final Billing billing;
    @NonNull private final List<String> inAppPurchaseList;

    Provider(final @NonNull Context context, @NonNull LicenseProvider licenseProvider) {
      //noinspection ConstantConditions
      if (context == null) {
        throw new NullPointerException("Application cannot be NULL");
      }

      appContext = context.getApplicationContext();
      this.licenseProvider = licenseProvider;
      inAppPurchaseList = createInAppPurchaseList(context);
      billing =
          new Billing(appContext, new DonationBillingConfiguration(appContext.getPackageName()));
    }

    @CheckResult @NonNull final List<String> createInAppPurchaseList(@NonNull Context context) {
      final Context appContext = context.getApplicationContext();
      final String packageName = appContext.getPackageName();
      final String appSpecificSkuDonateOne = packageName + SKU_DONATE_ONE;
      final String appSpecificSkuDonateTwo = packageName + SKU_DONATE_TWO;
      final String appSpecificSkuDonateFive = packageName + SKU_DONATE_FIVE;
      final String appSpecificSkuDonateTen = packageName + SKU_DONATE_TEN;

      final List<String> skuList = new ArrayList<>();
      skuList.add(appSpecificSkuDonateOne);
      skuList.add(appSpecificSkuDonateTwo);
      skuList.add(appSpecificSkuDonateFive);
      skuList.add(appSpecificSkuDonateTen);

      if (BuildConfigChecker.getInstance().isDebugMode()) {
        skuList.add("android.test.purchased");
        skuList.add("android.test.canceled");
        skuList.add("android.test.refunded");
        skuList.add("android.test.item_unavailable");
      }

      return Collections.unmodifiableList(skuList);
    }

    // Singleton
    @CheckResult @NonNull public final Context provideContext() {
      return appContext;
    }

    // Singleton
    @CheckResult @NonNull public final Billing provideBilling() {
      return billing;
    }

    // Singleton
    @CheckResult @NonNull public final LicenseProvider provideLicenseProvider() {
      return licenseProvider;
    }

    // Singleton
    @CheckResult @NonNull public final List<String> provideInAppPurchaseList() {
      return inAppPurchaseList;
    }

    @SuppressWarnings("WeakerAccess") static class DonationBillingConfiguration
        extends Billing.DefaultConfiguration {

      @NonNull private final String publicKey;

      DonationBillingConfiguration(@NonNull String publicKey) {
        //noinspection ConstantConditions
        if (publicKey == null) {
          throw new NullPointerException("Public Key cannot be NULL");
        }
        this.publicKey = publicKey;
      }

      @NonNull @Override public String getPublicKey() {
        return publicKey;
      }

      /**
       * We do not really need any purchase verification as they are all just donations anyway.
       * Our public key is not used, so the default verifier fails anyway.
       *
       * Pass a verifier which always passes
       */
      @NonNull @Override public PurchaseVerifier getPurchaseVerifier() {
        return new AlwaysPurchaseVerifier();
      }
    }

    @SuppressWarnings("WeakerAccess") static class AlwaysPurchaseVerifier
        implements PurchaseVerifier {

      /**<commit_msg>Do not auto connect as it causes errors<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.pyamsoft.pydroid;

import android.content.Context;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;
import com.pyamsoft.pydroid.about.AboutLibrariesModule;
import com.pyamsoft.pydroid.about.LicenseProvider;
import com.pyamsoft.pydroid.ads.AdvertisementModule;
import com.pyamsoft.pydroid.donate.DonateModule;
import com.pyamsoft.pydroid.social.SocialMediaModule;
import com.pyamsoft.pydroid.version.ApiModule;
import com.pyamsoft.pydroid.version.VersionCheckModule;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.solovyev.android.checkout.Billing;
import org.solovyev.android.checkout.Purchase;
import org.solovyev.android.checkout.PurchaseVerifier;
import org.solovyev.android.checkout.RequestListener;

public class PYDroidModule {

  @NonNull private final Provider provider;

  PYDroidModule(@NonNull Context context, @NonNull LicenseProvider licenseProvider) {
    provider = new Provider(context, licenseProvider);
  }

  // Create a new one every time
  @CheckResult @NonNull final AboutLibrariesModule provideAboutLibrariesModule() {
    return new AboutLibrariesModule(provider);
  }

  // Create a new one every time
  @CheckResult @NonNull final DonateModule provideDonateModule() {
    return new DonateModule(provider);
  }

  // Create a new one every time
  @CheckResult @NonNull final SocialMediaModule provideSocialMediaModule() {
    return new SocialMediaModule();
  }

  // Create a new one every time
  //
  // NOTE: Makes a new ApiModule
  @CheckResult @NonNull final VersionCheckModule provideVersionCheckModule() {
    return new VersionCheckModule(provider, new ApiModule());
  }

  // Create a new one every time
  //
  // NOTE: Makes a new SocialMediaModule
  @CheckResult @NonNull final AdvertisementModule provideAdvertisementModule() {
    return new AdvertisementModule(provider);
  }

  public static class Provider {

    @NonNull private static final String SKU_DONATE = ".donate";
    @NonNull private static final String SKU_DONATE_ONE = SKU_DONATE + ".one";
    @NonNull private static final String SKU_DONATE_TWO = SKU_DONATE + ".two";
    @NonNull private static final String SKU_DONATE_FIVE = SKU_DONATE + ".five";
    @NonNull private static final String SKU_DONATE_TEN = SKU_DONATE + ".ten";

    // Singleton
    @NonNull private final Context appContext;
    @NonNull private final LicenseProvider licenseProvider;
    @NonNull private final Billing billing;
    @NonNull private final List<String> inAppPurchaseList;

    Provider(final @NonNull Context context, @NonNull LicenseProvider licenseProvider) {
      //noinspection ConstantConditions
      if (context == null) {
        throw new NullPointerException("Application cannot be NULL");
      }

      appContext = context.getApplicationContext();
      this.licenseProvider = licenseProvider;
      inAppPurchaseList = createInAppPurchaseList(context);
      billing =
          new Billing(appContext, new DonationBillingConfiguration(appContext.getPackageName()));
    }

    @CheckResult @NonNull final List<String> createInAppPurchaseList(@NonNull Context context) {
      final Context appContext = context.getApplicationContext();
      final String packageName = appContext.getPackageName();
      final String appSpecificSkuDonateOne = packageName + SKU_DONATE_ONE;
      final String appSpecificSkuDonateTwo = packageName + SKU_DONATE_TWO;
      final String appSpecificSkuDonateFive = packageName + SKU_DONATE_FIVE;
      final String appSpecificSkuDonateTen = packageName + SKU_DONATE_TEN;

      final List<String> skuList = new ArrayList<>();
      skuList.add(appSpecificSkuDonateOne);
      skuList.add(appSpecificSkuDonateTwo);
      skuList.add(appSpecificSkuDonateFive);
      skuList.add(appSpecificSkuDonateTen);

      if (BuildConfigChecker.getInstance().isDebugMode()) {
        skuList.add("android.test.purchased");
        skuList.add("android.test.canceled");
        skuList.add("android.test.refunded");
        skuList.add("android.test.item_unavailable");
      }

      return Collections.unmodifiableList(skuList);
    }

    // Singleton
    @CheckResult @NonNull public final Context provideContext() {
      return appContext;
    }

    // Singleton
    @CheckResult @NonNull public final Billing provideBilling() {
      return billing;
    }

    // Singleton
    @CheckResult @NonNull public final LicenseProvider provideLicenseProvider() {
      return licenseProvider;
    }

    // Singleton
    @CheckResult @NonNull public final List<String> provideInAppPurchaseList() {
      return inAppPurchaseList;
    }

    @SuppressWarnings("WeakerAccess") static class DonationBillingConfiguration
        extends Billing.DefaultConfiguration {

      @NonNull private final String publicKey;

      DonationBillingConfiguration(@NonNull String publicKey) {
        //noinspection ConstantConditions
        if (publicKey == null) {
          throw new NullPointerException("Public Key cannot be NULL");
        }
        this.publicKey = publicKey;
      }

      @NonNull @Override public String getPublicKey() {
        return publicKey;
      }

      /**
       * Auto connect causes crashes on devices which do not support Billing as it
       * ends up attempting to register duplicate listeners
       */
      @Override public boolean isAutoConnect() {
        return false;
      }

      /**
       * We do not really need any purchase verification as they are all just donations anyway.
       * Our public key is not used, so the default verifier fails anyway.
       *
       * Pass a verifier which always passes
       */
      @NonNull @Override public PurchaseVerifier getPurchaseVerifier() {
        return new AlwaysPurchaseVerifier();
      }
    }

    @SuppressWarnings("WeakerAccess") static class AlwaysPurchaseVerifier
        implements PurchaseVerifier {

      /**
{'fraction_non_alphanumeric': 0.07920254441082926, 'fraction_numerical': 0.001241176014273524, 'mean_word_length': 3.705109489051095, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '4822298', 'n_tokens_mistral': 3572, 'n_tokens_neox': 3487, 'n_words': 999}