Inventory.java 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (c) 2012 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package com.alexdisler.inapppurchases;
  16. import java.util.ArrayList;
  17. import java.util.HashMap;
  18. import java.util.List;
  19. import java.util.Map;
  20. /**
  21. * Represents a block of information about in-app items.
  22. * An Inventory is returned by such methods as {@link IabHelper#queryInventory}.
  23. */
  24. public class Inventory {
  25. Map<String,SkuDetails> mSkuMap = new HashMap<String,SkuDetails>();
  26. Map<String,Purchase> mPurchaseMap = new HashMap<String,Purchase>();
  27. Inventory() { }
  28. /** Returns the listing details for an in-app product. */
  29. public SkuDetails getSkuDetails(String sku) {
  30. return mSkuMap.get(sku);
  31. }
  32. /** Returns purchase information for a given product, or null if there is no purchase. */
  33. public Purchase getPurchase(String sku) {
  34. return mPurchaseMap.get(sku);
  35. }
  36. /** Returns whether or not there exists a purchase of the given product. */
  37. public boolean hasPurchase(String sku) {
  38. return mPurchaseMap.containsKey(sku);
  39. }
  40. /** Return whether or not details about the given product are available. */
  41. public boolean hasDetails(String sku) {
  42. return mSkuMap.containsKey(sku);
  43. }
  44. /**
  45. * Erase a purchase (locally) from the inventory, given its product ID. This just
  46. * modifies the Inventory object locally and has no effect on the server! This is
  47. * useful when you have an existing Inventory object which you know to be up to date,
  48. * and you have just consumed an item successfully, which means that erasing its
  49. * purchase data from the Inventory you already have is quicker than querying for
  50. * a new Inventory.
  51. */
  52. public void erasePurchase(String sku) {
  53. if (mPurchaseMap.containsKey(sku)) mPurchaseMap.remove(sku);
  54. }
  55. /** Returns a list of all owned product IDs. */
  56. List<String> getAllOwnedSkus() {
  57. return new ArrayList<String>(mPurchaseMap.keySet());
  58. }
  59. /** Returns a list of all owned product IDs of a given type */
  60. List<String> getAllOwnedSkus(String itemType) {
  61. List<String> result = new ArrayList<String>();
  62. for (Purchase p : mPurchaseMap.values()) {
  63. if (p.getItemType().equals(itemType)) result.add(p.getSku());
  64. }
  65. return result;
  66. }
  67. /** Returns a list of all purchases. */
  68. List<Purchase> getAllPurchases() {
  69. return new ArrayList<Purchase>(mPurchaseMap.values());
  70. }
  71. void addSkuDetails(SkuDetails d) {
  72. mSkuMap.put(d.getSku(), d);
  73. }
  74. void addPurchase(Purchase p) {
  75. mPurchaseMap.put(p.getSku(), p);
  76. }
  77. }