Skip to content

Commit 76c0ffb

Browse files
committed
[app][Use case #1] Add console view
1 parent 3a8966b commit 76c0ffb

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

app/src/main/java/org/solovyev/android/checkout/app/StaticActivity.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.solovyev.android.checkout.Billing;
55
import org.solovyev.android.checkout.Checkout;
66
import org.solovyev.android.checkout.EmptyRequestListener;
7+
import org.solovyev.android.checkout.Logger;
78
import org.solovyev.android.checkout.ProductTypes;
89
import org.solovyev.android.checkout.Purchase;
910

@@ -14,6 +15,9 @@
1415
import android.view.View;
1516
import android.widget.ArrayAdapter;
1617
import android.widget.Spinner;
18+
import android.widget.TextView;
19+
20+
import javax.annotation.Nonnull;
1721

1822
import butterknife.BindView;
1923
import butterknife.ButterKnife;
@@ -27,6 +31,8 @@ public class StaticActivity extends AppCompatActivity implements View.OnClickLis
2731
View mBuy;
2832
@BindView(R.id.skus)
2933
Spinner mSkus;
34+
@BindView(R.id.console)
35+
TextView mConsole;
3036
private ActivityCheckout mCheckout;
3137

3238
@Override
@@ -39,6 +45,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
3945
mBuy.setOnClickListener(this);
4046
mSkus.setAdapter(new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, new String[]{"android.test.purchased", "android.test.canceled", "android.test.refunded", "android.test.item_unavailable"}));
4147
mSkus.setSelection(0);
48+
Billing.setLogger(Billing.newMainThreadLogger(new BillingLogger(mConsole)));
4249

4350
final Billing billing = CheckoutApplication.get(this).getBilling();
4451
mCheckout = Checkout.forActivity(this, billing);
@@ -54,6 +61,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
5461
@Override
5562
protected void onDestroy() {
5663
mCheckout.stop();
64+
Billing.setLogger(Billing.newLogger());
5765
super.onDestroy();
5866
}
5967

@@ -62,8 +70,82 @@ public void onClick(View v) {
6270
switch (v.getId()) {
6371
case R.id.buy:
6472
final String sku = (String) mSkus.getSelectedItem();
73+
mConsole.setText("");
6574
mCheckout.startPurchaseFlow(ProductTypes.IN_APP, sku, null, new EmptyRequestListener<Purchase>());
6675
break;
6776
}
6877
}
78+
79+
private static class BillingLogger implements Logger {
80+
private final TextView mView;
81+
82+
public BillingLogger(TextView view) {
83+
mView = view;
84+
}
85+
86+
@Override
87+
public void v(@Nonnull String tag, @Nonnull String msg) {
88+
log("v", tag, msg, null);
89+
}
90+
91+
private void log(String level, @Nonnull String tag, @Nonnull String msg, @Nullable Throwable e) {
92+
if (!tag.equals("Checkout")) {
93+
return;
94+
}
95+
final boolean empty = mView.getText().length() == 0;
96+
if(!empty) {
97+
mView.append("\n");
98+
}
99+
mView.append(level + ": " + msg);
100+
if (e != null) {
101+
mView.append("\n");
102+
mView.append(e.getMessage());
103+
}
104+
}
105+
106+
@Override
107+
public void v(@Nonnull String tag, @Nonnull String msg, @Nonnull Throwable e) {
108+
log("v", tag, msg, e);
109+
}
110+
111+
@Override
112+
public void d(@Nonnull String tag, @Nonnull String msg) {
113+
log("d", tag, msg, null);
114+
}
115+
116+
@Override
117+
public void d(@Nonnull String tag, @Nonnull String msg, @Nonnull Throwable e) {
118+
log("d", tag, msg, e);
119+
}
120+
121+
@Override
122+
public void i(@Nonnull String tag, @Nonnull String msg) {
123+
log("i", tag, msg, null);
124+
}
125+
126+
@Override
127+
public void i(@Nonnull String tag, @Nonnull String msg, @Nonnull Throwable e) {
128+
log("i", tag, msg, e);
129+
}
130+
131+
@Override
132+
public void w(@Nonnull String tag, @Nonnull String msg) {
133+
log("w", tag, msg, null);
134+
}
135+
136+
@Override
137+
public void w(@Nonnull String tag, @Nonnull String msg, @Nonnull Throwable e) {
138+
log("w", tag, msg, e);
139+
}
140+
141+
@Override
142+
public void e(@Nonnull String tag, @Nonnull String msg) {
143+
log("e", tag, msg, null);
144+
}
145+
146+
@Override
147+
public void e(@Nonnull String tag, @Nonnull String msg, @Nonnull Throwable e) {
148+
log("e", tag, msg, e);
149+
}
150+
}
69151
}

app/src/main/res/layout/activity_static.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
android:text="@string/buy" />
3737
</LinearLayout>
3838

39+
<TextView
40+
android:id="@+id/console"
41+
android:layout_width="match_parent"
42+
android:layout_height="wrap_content" />
43+
3944
</LinearLayout>
4045
</ScrollView>
4146

0 commit comments

Comments
 (0)