Commit 53cb1d8eddc7604b926d4ca768929ae6ce8d8ec5

Authored by transpine
1 parent 0b0befe4

- about layout

- handle buyCoffee
app/src/main/java/net/devfac/userstory/ActivityAbout.java
1 1 package net.devfac.userstory;
2 2  
  3 +import android.app.PendingIntent;
3 4 import android.content.ComponentName;
4 5 import android.content.Context;
5 6 import android.content.Intent;
  7 +import android.content.IntentSender;
6 8 import android.content.ServiceConnection;
  9 +import android.media.Image;
7 10 import android.os.Bundle;
8 11 import android.os.IBinder;
9 12 import android.os.PersistableBundle;
10 13 import android.os.RemoteException;
11 14 import android.support.v7.app.AppCompatActivity;
12 15 import android.support.v7.widget.Toolbar;
  16 +import android.view.Menu;
  17 +import android.view.MenuInflater;
  18 +import android.view.MenuItem;
13 19 import android.view.View;
14 20 import android.widget.Button;
  21 +import android.widget.ImageView;
15 22 import android.widget.Toast;
16 23  
17 24 import com.android.vending.billing.IInAppBillingService;
... ... @@ -20,6 +27,7 @@ import net.devfac.userstory.Utils.Logger;
20 27 import net.devfac.userstory.Utils.iab.IabHelper;
21 28 import net.devfac.userstory.Utils.iab.IabResult;
22 29 import net.devfac.userstory.Utils.iab.Inventory;
  30 +import net.devfac.userstory.Utils.iab.Purchase;
23 31  
24 32 import org.json.JSONException;
25 33 import org.json.JSONObject;
... ... @@ -30,13 +38,17 @@ import java.util.ArrayList;
30 38 /**
31 39 * Created by Onether on 15. 11. 18..
32 40 */
33   -public class ActivityAbout extends AppCompatActivity {
  41 +public class ActivityAbout extends AppCompatActivity implements View.OnClickListener {
34 42 private IInAppBillingService mIabService;
35 43 private IabHelper mIabHelper;
36 44 private ArrayList<String> mSkuList;
37 45  
38 46 private Toolbar mToolbarAbout;
39 47  
  48 + private ImageView mImageViewAboutCoffee1;
  49 + private ImageView mImageViewAboutCoffee2;
  50 + private ImageView mImageViewAboutCoffee3;
  51 +
40 52  
41 53 private final ServiceConnection mIabServiceConnection = new ServiceConnection(){
42 54  
... ... @@ -63,6 +75,13 @@ public class ActivityAbout extends AppCompatActivity {
63 75 mToolbarAbout = (Toolbar)findViewById(R.id.toolbar_about);
64 76 setSupportActionBar(mToolbarAbout);
65 77  
  78 + //Init widget
  79 + mImageViewAboutCoffee1 = (ImageView)findViewById(R.id.imageview_about_coffee1);
  80 + mImageViewAboutCoffee2 = (ImageView)findViewById(R.id.imageview_about_coffee2);
  81 + mImageViewAboutCoffee3 = (ImageView)findViewById(R.id.imageview_about_coffee3);
  82 +
  83 + mImageViewAboutCoffee1.setOnClickListener(this);
  84 +
66 85 //PAYMENT_TARGET
67 86 mSkuList = new ArrayList<>();
68 87 mSkuList.add(Constants.PAYMENT_SKU_COFFEE_1);
... ... @@ -80,6 +99,8 @@ public class ActivityAbout extends AppCompatActivity {
80 99 mIabHelper = new IabHelper(this, base64EncodePublicKey);
81 100 mIabHelper.enableDebugLogging(true);
82 101  
  102 + mIabHelper.startSetup(mOnIabSetupFinishedListener);
  103 +
83 104 // Button test = (Button)findViewById(R.id.btn_about_iab_test);
84 105 // test.setOnClickListener(new View.OnClickListener() {
85 106 // @Override
... ... @@ -98,6 +119,24 @@ public class ActivityAbout extends AppCompatActivity {
98 119 }
99 120 }
100 121  
  122 + @Override
  123 + public boolean onCreateOptionsMenu(Menu menu) {
  124 + MenuInflater inflater = getMenuInflater();
  125 + inflater.inflate(R.menu.about, menu);
  126 +
  127 + return super.onCreateOptionsMenu(menu);
  128 + }
  129 +
  130 + @Override
  131 + public boolean onOptionsItemSelected(MenuItem item) {
  132 + switch( item.getItemId() ){
  133 + case R.id.action_about_cancel:
  134 + finish();
  135 + break;
  136 + }
  137 + return super.onOptionsItemSelected(item);
  138 + }
  139 +
101 140 private IabHelper.OnIabSetupFinishedListener mOnIabSetupFinishedListener = new IabHelper.OnIabSetupFinishedListener() {
102 141 @Override
103 142 public void onIabSetupFinished(IabResult result) {
... ... @@ -106,49 +145,126 @@ public class ActivityAbout extends AppCompatActivity {
106 145 }
107 146  
108 147 else{
109   - mIabHelper.queryInventoryAsync(true, mSkuList, new IabHelper.QueryInventoryFinishedListener() {
110   - @Override
111   - public void onQueryInventoryFinished(IabResult result, Inventory inv) {
112   - if( result.isFailure()){
113   - return;
114   - }
115   -
116   - if( inv.getSkuDetails("coffee_1") != null ) {
117   - Logger.i("coffee_1 price : " + inv.getSkuDetails("coffee_1").getPrice());
118   - }
119   - }
120   - });
121   -
122   - Bundle querySkus = new Bundle();
123   - querySkus.putStringArrayList("ITEM_ID_LIST", mSkuList);
124   -
125   - if( mIabService == null) return;
126   -
127   - try {
128   - Bundle skuDetails = mIabService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
129   -
130   - int response = skuDetails.getInt("RESPONSE_CODE");
131   -
132   - if( response == IabHelper.BILLING_RESPONSE_RESULT_OK ){
133   - ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
134   -
135   - for(String thisResponse:responseList){
136   - JSONObject object = new JSONObject(thisResponse);
137   - String sku = object.getString("productId");
138   - String price = object.getString("price");
139   - Logger.i("SKU : " + sku + " | PRICE : " + price);
140   - }
141   -
142   - }
143   - else{
144   - Logger.e("Response code : " + String.valueOf(response));
145   - }
146   - } catch (RemoteException e) {
147   - e.printStackTrace();
148   - } catch (JSONException e) {
149   - e.printStackTrace();
  148 + resumePurchaseItems();
  149 +
  150 +// mIabHelper.queryInventoryAsync(true, mSkuList, new IabHelper.QueryInventoryFinishedListener() {
  151 +// @Override
  152 +// public void onQueryInventoryFinished(IabResult result, Inventory inv) {
  153 +// if( result.isFailure()){
  154 +// return;
  155 +// }
  156 +//
  157 +// if( inv.getSkuDetails("coffee_1") != null ) {
  158 +// Logger.i("coffee_1 price : " + inv.getSkuDetails("coffee_1").getPrice());
  159 +// }
  160 +// }
  161 +// });
  162 +//
  163 +// Bundle querySkus = new Bundle();
  164 +// querySkus.putStringArrayList("ITEM_ID_LIST", mSkuList);
  165 +//
  166 +// if( mIabService == null) return;
  167 +//
  168 +// try {
  169 +// Bundle skuDetails = mIabService.getSkuDetails(3, getPackageName(), "inapp", querySkus);
  170 +//
  171 +// int response = skuDetails.getInt("RESPONSE_CODE");
  172 +//
  173 +// if( response == IabHelper.BILLING_RESPONSE_RESULT_OK ){
  174 +// ArrayList<String> responseList = skuDetails.getStringArrayList("DETAILS_LIST");
  175 +//
  176 +// for(String thisResponse:responseList){
  177 +// JSONObject object = new JSONObject(thisResponse);
  178 +// String sku = object.getString("productId");
  179 +// String price = object.getString("price");
  180 +// Logger.i("SKU : " + sku + " | PRICE : " + price);
  181 +// }
  182 +//
  183 +// }
  184 +// else{
  185 +// Logger.e("Response code : " + String.valueOf(response));
  186 +// }
  187 +// } catch (RemoteException e) {
  188 +// e.printStackTrace();
  189 +// } catch (JSONException e) {
  190 +// e.printStackTrace();
  191 +// }
  192 + }
  193 + }
  194 + };
  195 +
  196 + private void resumePurchaseItems(){
  197 + try {
  198 + Bundle ownedItems = mIabService.getPurchases(3, getPackageName(), "inapp", null);
  199 +
  200 + int response = ownedItems.getInt("RESPONSE_CODE");
  201 +
  202 + if( response == IabHelper.BILLING_RESPONSE_RESULT_OK ){
  203 + ArrayList purchasedDataList = ownedItems.getStringArrayList("INAPP_PURCHASE_DATA_LIST");
  204 + String[] tokens = new String[purchasedDataList.size()];
  205 +
  206 + for( int i=0 ; i < purchasedDataList.size() ; i++ ){
  207 + String purchasedData = (String)purchasedDataList.get(i);
  208 +
  209 + JSONObject object = new JSONObject(purchasedData);
  210 +
  211 + tokens[i] = object.getString("purchaseToken");
  212 +
  213 + mIabService.consumePurchase(3, getPackageName(), tokens[i]);
150 214 }
151 215 }
  216 + } catch (RemoteException e) {
  217 + e.printStackTrace();
  218 + } catch (JSONException e) {
  219 + e.printStackTrace();
  220 + }
  221 +
  222 +
  223 + }
  224 +
  225 + private void buyCoffee(String id_item){
  226 + try {
  227 + Bundle buyIntentBundle = mIabService.getBuyIntent(3, getPackageName(), id_item, "inapp", "userstory");
  228 + PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
  229 +
  230 +
  231 + if( pendingIntent != null ){
  232 + mIabHelper.launchPurchaseFlow(this, getPackageName(), 1001, mPurchasedFinishedListener, "userstory");
  233 + }
  234 + else{
  235 + //If payment is blocked
  236 + }
  237 + } catch (RemoteException e) {
  238 + e.printStackTrace();
  239 + }
  240 + }
  241 +
  242 + IabHelper.OnIabPurchaseFinishedListener mPurchasedFinishedListener = new IabHelper.OnIabPurchaseFinishedListener() {
  243 + @Override
  244 + public void onIabPurchaseFinished(IabResult result, Purchase info) {
152 245 }
153 246 };
  247 +
  248 + @Override
  249 + public void onClick(View v) {
  250 + switch (v.getId() ){
  251 + case R.id.imageview_about_coffee1:
  252 + buyCoffee(Constants.PAYMENT_SKU_COFFEE_1);
  253 + break;
  254 + case R.id.imageview_about_coffee2:
  255 + break;
  256 + case R.id.imageview_about_coffee3:
  257 + break;
  258 + }
  259 +
  260 + }
  261 +
  262 + @Override
  263 + protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  264 + if( mIabHelper == null ) return;
  265 +
  266 + if( mIabHelper.handleActivityResult(requestCode, resultCode, data)){
  267 + super.onActivityResult(requestCode, resultCode, data);
  268 + }
  269 + }
154 270 }
... ...
app/src/main/res/layout/activity_about.xml
... ... @@ -27,7 +27,7 @@
27 27  
28 28 <ImageView
29 29 android:layout_width="wrap_content"
30   - android:layout_height="0dp"
  30 + android:layout_height="150dp"
31 31 android:id="@+id/imageView3"
32 32 android:src="@drawable/about_main_icon_coffee"
33 33 android:layout_marginTop="10dp"
... ... @@ -65,23 +65,31 @@
65 65 android:weightSum="10"
66 66 android:gravity="center_vertical">
67 67  
68   - <ImageView
  68 + <LinearLayout
  69 + android:orientation="horizontal"
69 70 android:layout_width="0dp"
70 71 android:layout_height="wrap_content"
71   - android:id="@+id/imageView4"
72   - android:src="@drawable/about_pay_coffee_3"
73   - android:layout_weight="3"
74   - android:adjustViewBounds="true" />
  72 + android:layout_weight="5"
  73 + android:gravity="center_vertical|right">
75 74  
76   - <TextView
77   - android:layout_width="0dp"
78   - android:layout_height="wrap_content"
79   - android:textAppearance="?android:attr/textAppearanceSmall"
80   - android:text="1000์›"
81   - android:id="@+id/text_about_price1"
82   - android:layout_weight="1.8"
83   - android:gravity="right"
84   - android:layout_marginRight="10dp" />
  75 + <ImageView
  76 + android:layout_width="wrap_content"
  77 + android:layout_height="50dp"
  78 + android:id="@+id/imageview_about_coffee1"
  79 + android:src="@drawable/about_pay_coffee_1"
  80 + android:adjustViewBounds="true"
  81 + android:layout_gravity="right|top"
  82 + android:layout_marginRight="10dp" />
  83 +
  84 + <TextView
  85 + android:layout_width="wrap_content"
  86 + android:layout_height="wrap_content"
  87 + android:textAppearance="?android:attr/textAppearanceSmall"
  88 + android:text="1000์›"
  89 + android:id="@+id/text_about_price1"
  90 + android:layout_marginRight="10dp"
  91 + android:gravity="right" />
  92 + </LinearLayout>
85 93  
86 94 <FrameLayout
87 95 android:layout_width="0dp"
... ... @@ -102,11 +110,11 @@
102 110 android:layout_width="match_parent"
103 111 android:layout_height="match_parent"
104 112 android:id="@+id/image_view_about_hide1"
105   - android:background="#333333"
  113 + android:background="#888888"
106 114 android:layout_marginTop="17dp"
107 115 android:layout_marginBottom="20dp"
108 116 android:layout_marginLeft="10dp"
109   - android:layout_marginRight="90dp" />
  117 + android:layout_marginRight="40dp" />
110 118 </FrameLayout>
111 119  
112 120 </LinearLayout>
... ... @@ -118,28 +126,36 @@
118 126 android:weightSum="10"
119 127 android:gravity="center_vertical" >
120 128  
121   - <ImageView
  129 + <LinearLayout
  130 + android:orientation="horizontal"
122 131 android:layout_width="0dp"
123 132 android:layout_height="wrap_content"
124   - android:id="@+id/imageView6"
125   - android:src="@drawable/about_pay_coffee_3"
126   - android:layout_weight="3"
127   - android:adjustViewBounds="true" />
  133 + android:gravity="center_vertical|right"
  134 + android:layout_weight="5">
128 135  
129   - <TextView
130   - android:layout_width="0dp"
131   - android:layout_height="wrap_content"
132   - android:textAppearance="?android:attr/textAppearanceSmall"
133   - android:text="1000์›"
134   - android:id="@+id/text_about_price2"
135   - android:layout_weight="1.8"
136   - android:gravity="right"
137   - android:layout_marginRight="10dp" />
  136 + <ImageView
  137 + android:layout_width="wrap_content"
  138 + android:layout_height="50dp"
  139 + android:id="@+id/imageview_about_coffee2"
  140 + android:src="@drawable/about_pay_coffee_2"
  141 + android:adjustViewBounds="true"
  142 + android:layout_marginRight="10dp" />
  143 +
  144 + <TextView
  145 + android:layout_width="wrap_content"
  146 + android:layout_height="wrap_content"
  147 + android:textAppearance="?android:attr/textAppearanceSmall"
  148 + android:text="1000์›"
  149 + android:id="@+id/text_about_price2"
  150 + android:gravity="right"
  151 + android:layout_marginRight="10dp" />
  152 + </LinearLayout>
138 153  
139 154 <FrameLayout
140 155 android:layout_width="0dp"
141 156 android:layout_height="match_parent"
142   - android:layout_weight="5" >
  157 + android:layout_weight="5"
  158 + android:layout_gravity="left">
143 159  
144 160 <TextView
145 161 android:layout_width="match_parent"
... ... @@ -161,6 +177,7 @@
161 177 android:layout_marginRight="0dp"
162 178 android:layout_marginTop="4dp" />
163 179 </FrameLayout>
  180 +
164 181 </LinearLayout>
165 182  
166 183 <LinearLayout
... ... @@ -170,29 +187,37 @@
170 187 android:weightSum="10"
171 188 android:gravity="top" >
172 189  
173   - <ImageView
  190 + <LinearLayout
  191 + android:orientation="horizontal"
174 192 android:layout_width="0dp"
175   - android:layout_height="wrap_content"
176   - android:id="@+id/imageView8"
177   - android:src="@drawable/about_pay_coffee_3"
178   - android:layout_weight="3"
179   - android:adjustViewBounds="true" />
  193 + android:layout_height="match_parent"
  194 + android:layout_weight="5"
  195 + android:gravity="center_horizontal|right">
180 196  
181   - <TextView
182   - android:layout_width="0dp"
183   - android:layout_height="wrap_content"
184   - android:textAppearance="?android:attr/textAppearanceSmall"
185   - android:text="1000์›"
186   - android:id="@+id/text_about_price3"
187   - android:layout_weight="1.8"
188   - android:gravity="right"
189   - android:layout_marginRight="10dp"
190   - android:layout_marginTop="20dp" />
  197 + <ImageView
  198 + android:layout_width="wrap_content"
  199 + android:layout_height="50dp"
  200 + android:id="@+id/imageview_about_coffee3"
  201 + android:src="@drawable/about_pay_coffee_3"
  202 + android:adjustViewBounds="true"
  203 + android:layout_marginRight="10dp" />
  204 +
  205 + <TextView
  206 + android:layout_width="wrap_content"
  207 + android:layout_height="wrap_content"
  208 + android:textAppearance="?android:attr/textAppearanceSmall"
  209 + android:text="1000์›"
  210 + android:id="@+id/text_about_price3"
  211 + android:gravity="right"
  212 + android:layout_marginRight="10dp"
  213 + android:layout_marginTop="20dp" />
  214 + </LinearLayout>
191 215  
192 216 <FrameLayout
193 217 android:layout_width="0dp"
194 218 android:layout_height="wrap_content"
195   - android:layout_weight="5">
  219 + android:layout_weight="5"
  220 + android:layout_gravity="left">
196 221  
197 222 <LinearLayout
198 223 android:orientation="vertical"
... ... @@ -222,10 +247,10 @@
222 247 android:layout_width="match_parent"
223 248 android:layout_height="match_parent"
224 249 android:id="@+id/image_view_about_hide3"
225   - android:background="#444444"
  250 + android:background="#AAAAAA"
226 251 android:layout_marginLeft="9dp"
227 252 android:layout_marginTop="12dp"
228   - android:layout_marginRight="10dp" />
  253 + android:layout_marginRight="20dp" />
229 254  
230 255 </FrameLayout>
231 256  
... ...