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 |
|
16 |
package com.example.android.trivialdrivesample.util; |
17 |
|
18 |
import java.util.ArrayList; |
19 |
import java.util.HashMap; |
20 |
import java.util.List; |
21 |
import java.util.Map; |
22 |
|
23 |
/** |
24 |
* Represents a block of information about in-app items. |
25 |
* An Inventory is returned by such methods as {@link IabHelper#queryInventory}. |
26 |
*/ |
27 |
public class Inventory { |
28 |
Map<String,SkuDetails> mSkuMap = new HashMap<String,SkuDetails>(); |
29 |
Map<String,Purchase> mPurchaseMap = new HashMap<String,Purchase>(); |
30 |
|
31 |
Inventory() { } |
32 |
|
33 |
/** Returns the listing details for an in-app product. */ |
34 |
public SkuDetails getSkuDetails(String sku) { |
35 |
return mSkuMap.get(sku); |
36 |
} |
37 |
|
38 |
/** Returns purchase information for a given product, or null if there is no purchase. */ |
39 |
public Purchase getPurchase(String sku) { |
40 |
return mPurchaseMap.get(sku); |
41 |
} |
42 |
|
43 |
/** Returns whether or not there exists a purchase of the given product. */ |
44 |
public boolean hasPurchase(String sku) { |
45 |
return mPurchaseMap.containsKey(sku); |
46 |
} |
47 |
|
48 |
/** Return whether or not details about the given product are available. */ |
49 |
public boolean hasDetails(String sku) { |
50 |
return mSkuMap.containsKey(sku); |
51 |
} |
52 |
|
53 |
/** |
54 |
* Erase a purchase (locally) from the inventory, given its product ID. This just |
55 |
* modifies the Inventory object locally and has no effect on the server! This is |
56 |
* useful when you have an existing Inventory object which you know to be up to date, |
57 |
* and you have just consumed an item successfully, which means that erasing its |
58 |
* purchase data from the Inventory you already have is quicker than querying for |
59 |
* a new Inventory. |
60 |
*/ |
61 |
public void erasePurchase(String sku) { |
62 |
if (mPurchaseMap.containsKey(sku)) mPurchaseMap.remove(sku); |
63 |
} |
64 |
|
65 |
/** Returns a list of all owned product IDs. */ |
66 |
List<String> getAllOwnedSkus() { |
67 |
return new ArrayList<String>(mPurchaseMap.keySet()); |
68 |
} |
69 |
|
70 |
/** Returns a list of all owned product IDs of a given type */ |
71 |
List<String> getAllOwnedSkus(String itemType) { |
72 |
List<String> result = new ArrayList<String>(); |
73 |
for (Purchase p : mPurchaseMap.values()) { |
74 |
if (p.getItemType().equals(itemType)) result.add(p.getSku()); |
75 |
} |
76 |
return result; |
77 |
} |
78 |
|
79 |
/** Returns a list of all purchases. */ |
80 |
List<Purchase> getAllPurchases() { |
81 |
return new ArrayList<Purchase>(mPurchaseMap.values()); |
82 |
} |
83 |
|
84 |
void addSkuDetails(SkuDetails d) { |
85 |
mSkuMap.put(d.getSku(), d); |
86 |
} |
87 |
|
88 |
void addPurchase(Purchase p) { |
89 |
mPurchaseMap.put(p.getSku(), p); |
90 |
} |
91 |
} |