title: How to leave in-app-billing-purchase within a class
body: <p>I already have in my application the integration with "in-app-billing-purchase" however I have several activity where I need to be duplicating all the code.</p>
  
  <p>So I thought about leaving everything focused on a single class, I wastancio the class and I arrive if the item was bought.</p>
  
  <p>Just that I have a big problem the "in-app-billing-purchase" uses Listener <code>OnIabPurchaseFinishedListener</code> and <code>QueryInventoryFinishedListener</code></p>
  
  <p>How can I leave all this organized in a single class and just check with a simple call in my activity?</p>
  
  <p><strong>Calling code:</strong></p>
  
  <pre><code>Billing bl = new Billing(getActivity().getApplicationContext());
  if (bl.Comprado())
      Toast.makeText(getActivity(), "Comprado", Toast.LENGTH_SHORT).show();
  else
      Toast.makeText(getActivity(), "Erro comprado", Toast.LENGTH_SHORT).show();
  </code></pre>
  
  <p><strong>Class I created to be called multiple times</strong></p>
  
  <pre><code>public class Billing {
  // Item name for premium status
  private static final String SKU_PREMIUM = "premium";
  // private static final String SKU_PREMIUM = "tirarbanner";
  // Flag set to true when we have premium status
  private static boolean mIsPremium = false;
  // In-app Billing helper
  private IabHelper mAbHelper;
  
  public Billing(Context context) {
  
      String base64EncodedPublicKey = "xxx";
      // Create in-app billing helper
      mAbHelper = new IabHelper(context, base64EncodedPublicKey);
      // and start setup. If setup is successfull, query inventory we already
      // own
      mAbHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
          public void onIabSetupFinished(IabResult result) {
  
              if (!result.isSuccess()) {
                  return;
              }
              mAbHelper.queryInventoryAsync(mGotInventoryListener);
          }
      });
  }
  
  public static boolean Comprado() {
      return mIsPremium;
  }
  
  /**
   * Listener that is called when we finish purchase flow.
   */
  IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
      public void onIabPurchaseFinished(IabResult result, Purchase purchase) {
  
          if (result.isFailure()) {
              return;
          }
          // Purchase was successfull, set premium flag and update interface
          if (purchase.getSku().equals(SKU_PREMIUM)) {
              mIsPremium = true;
          }
      }
  };
  
  /**
   * Listener that's called when we finish querying the items and
   * subscriptions we own
   */
  IabHelper.QueryInventoryFinishedListener mGotInventoryListener = new IabHelper.QueryInventoryFinishedListener() {
      public void onQueryInventoryFinished(IabResult result, Inventory inventory) {
  
          // Have we been disposed of in the meantime? If so, quit.
          if (mAbHelper == null)
              return;
  
          // Is it a failure?
          if (result.isFailure()) {
              return;
          }
  
          // Do we have the premium upgrade?
          Purchase premiumPurchase = inventory.getPurchase(SKU_PREMIUM);
          mIsPremium = premiumPurchase != null;
  
      }
  };
  </code></pre>
  
  <p>}</p>