/**
   * docs in {@link
   * PurchaseObserver#onRestoreTransactionsResponse(com.soomla.billing.BillingService.RestoreTransactions,
   * com.soomla.billing.Consts.ResponseCode)}.
   */
  @Override
  public void onRestoreTransactionsResponse(
      BillingService.RestoreTransactions request, Consts.ResponseCode responseCode) {

    if (responseCode == Consts.ResponseCode.RESULT_OK) {
      StoreUtils.LogDebug(TAG, "RestoreTransactions succeeded");

      SharedPreferences prefs =
          new ObscuredSharedPreferences(
              SoomlaApp.getAppContext(),
              SoomlaApp.getAppContext()
                  .getSharedPreferences(StoreConfig.PREFS_NAME, Context.MODE_PRIVATE));
      SharedPreferences.Editor edit = prefs.edit();

      edit.putBoolean("RESTORED", true);
      edit.commit();

      BusProvider.getInstance().post(new RestoreTransactionsEvent(true));
    } else {
      StoreUtils.LogDebug(TAG, "RestoreTransactions error: " + responseCode);

      BusProvider.getInstance().post(new RestoreTransactionsEvent(false));
    }

    // we're stopping the billing service only if the store was not opened while the request was
    // sent
    if (!mStoreOpen) {
      stopBillingService();
    }
  }
  /**
   * Start a purchase process with Google Play.
   *
   * @param googleMarketItem is the item to purchase. This item has to be defined EXACTLY the same
   *     in Google Play.
   * @param payload a payload to get back when this purchase is finished.
   */
  public boolean buyWithGooglePlay(GoogleMarketItem googleMarketItem, String payload) {
    if (!checkInit()) return false;

    SharedPreferences prefs =
        new ObscuredSharedPreferences(
            SoomlaApp.getAppContext(),
            SoomlaApp.getAppContext()
                .getSharedPreferences(StoreConfig.PREFS_NAME, Context.MODE_PRIVATE));
    String publicKey = prefs.getString(StoreConfig.PUBLIC_KEY, "");
    if (publicKey.isEmpty() || publicKey.equals("[YOUR PUBLIC KEY FROM GOOGLE PLAY]")) {
      StoreUtils.LogError(TAG, "You didn't provide a public key! You can't make purchases.");
      return false;
    }

    if (!mBillingService.requestPurchase(
        googleMarketItem.getProductId(), Consts.ITEM_TYPE_INAPP, payload)) {
      return false;
    }
    try {
      BusProvider.getInstance()
          .post(
              new PlayPurchaseStartedEvent(
                  StoreInfo.getPurchasableItem(googleMarketItem.getProductId())));
    } catch (VirtualItemNotFoundException e) {
      StoreUtils.LogError(
          TAG,
          "Couldn't find a purchasable item with productId: " + googleMarketItem.getProductId());
    }
    return true;
  }
 /** Answers the question: "Were transactions already restored for this game?" */
 public boolean transactionsAlreadyRestored() {
   SharedPreferences prefs =
       new ObscuredSharedPreferences(
           SoomlaApp.getAppContext(),
           SoomlaApp.getAppContext()
               .getSharedPreferences(StoreConfig.PREFS_NAME, Context.MODE_PRIVATE));
   return prefs.getBoolean("RESTORED", false);
 }
  /**
   * This initializer also initializes {@link StoreInfo}.
   *
   * @param storeAssets is the definition of your application specific assets.
   * @param publicKey is the public key given to you from Google.
   * @param customSecret is your encryption secret (it's used to encrypt your data in the database)
   */
  public void initialize(IStoreAssets storeAssets, String publicKey, String customSecret) {

    if (mInitialized) {
      StoreUtils.LogError(
          TAG, "StoreController is already initialized. You can't initialize it twice!");
      return;
    }

    SharedPreferences prefs =
        new ObscuredSharedPreferences(
            SoomlaApp.getAppContext(),
            SoomlaApp.getAppContext()
                .getSharedPreferences(StoreConfig.PREFS_NAME, Context.MODE_PRIVATE));
    SharedPreferences.Editor edit = prefs.edit();
    if (publicKey != null && !publicKey.isEmpty()) {
      edit.putString(StoreConfig.PUBLIC_KEY, publicKey);
    } else if (prefs.getString(StoreConfig.PUBLIC_KEY, "").isEmpty()) {
      StoreUtils.LogError(TAG, "publicKey is null or empty. can't initialize store !!");
      return;
    }
    if (customSecret != null && !customSecret.isEmpty()) {
      edit.putString(StoreConfig.CUSTOM_SEC, customSecret);
    } else if (prefs.getString(StoreConfig.CUSTOM_SEC, "").isEmpty()) {
      StoreUtils.LogError(TAG, "customSecret is null or empty. can't initialize store !!");
      return;
    }
    edit.putInt("SA_VER_NEW", storeAssets.getVersion());
    edit.commit();

    if (storeAssets != null) {
      StoreInfo.setStoreAssets(storeAssets);
    }

    if (startBillingService()) {
      // We're not restoring transactions automatically anymore.
      // Call storeController.getInstance().restoreTransactions() when you want to do that.
      //            tryRestoreTransactions();
    }

    mInitialized = true;
  }
  private boolean startBillingService() {
    mLock.lock();
    if (mBillingService == null) {
      ResponseHandler.register(this);
      mBillingService = new BillingService();
      mBillingService.setContext(SoomlaApp.getAppContext());

      if (!mBillingService.checkBillingSupported(Consts.ITEM_TYPE_INAPP)) {
        StoreUtils.LogDebug(TAG, "There's no connectivity with the billing service.");

        mLock.unlock();
        return false;
      }
    }

    mLock.unlock();
    return true;
  }