Skeleton of a billing activity

pull/14/head
Sam Bosley 14 years ago
parent 89767a10a4
commit b8d5fedf84

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buy_month"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Buy one month"/>
<Button
android:id="@+id/buy_year"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Buy one year"/>
</LinearLayout>

@ -44,4 +44,12 @@
<string name="file_err_copy">Error copying file for attachment</string>
<string name="file_err_download">Error downloading file</string>
<string name="file_err_show">Sorry, the system does not yet support this type of file</string>
<!-- in app billing -->
<string name="subscriptions_not_supported">Subscriptions not supported</string>
<string name="subscriptions_not_supported_message">Sorry! The Market billing
service on this device does not support subscriptions at this time.</string>
<string name="subscriptions_learn_more">Learn more</string>
<string name="subscriptions_help_url" formatted="false">http://market.android.com/support/bin/answer.py?answer=1050566&amp;hl=%lang%&amp;dl=%region%</string>
</resources>

@ -0,0 +1,99 @@
package com.todoroo.astrid.billing;
import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.timsu.astrid.R;
public class BillingActivity extends Activity {
private BillingService billingService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.billing_activity);
setupButtons();
billingService = new BillingService();
billingService.setContext(this);
if (!billingService.checkBillingSupported(BillingConstants.ITEM_TYPE_SUBSCRIPTION)) {
showSubscriptionsNotSupported();
}
}
private void setupButtons() {
Button buyMonth = (Button) findViewById(R.id.buy_month);
Button buyYear = (Button) findViewById(R.id.buy_year);
//TODO: Figure out if we need a payload for any reason
buyMonth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!billingService.requestPurchase(BillingConstants.PRODUCT_ID_MONTHLY,
BillingConstants.ITEM_TYPE_SUBSCRIPTION, null)) {
showSubscriptionsNotSupported();
}
}
});
buyYear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!billingService.requestPurchase(BillingConstants.PRODUCT_ID_YEARLY,
BillingConstants.ITEM_TYPE_SUBSCRIPTION, null)) {
showSubscriptionsNotSupported();
}
}
});
}
private void showSubscriptionsNotSupported() {
String helpUrl = replaceLanguageAndRegion(getString(R.string.subscriptions_help_url));
final Uri helpUri = Uri.parse(helpUrl);
new AlertDialog.Builder(this)
.setTitle(R.string.subscriptions_not_supported)
.setMessage(R.string.subscriptions_not_supported_message)
.setCancelable(false)
.setPositiveButton(R.string.DLG_ok, null)
.setNegativeButton(R.string.subscriptions_learn_more, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Intent.ACTION_VIEW, helpUri);
startActivity(intent);
}
}).create().show();
}
/**
* Replaces the language and/or country of the device into the given string.
* The pattern "%lang%" will be replaced by the device's language code and
* the pattern "%region%" will be replaced with the device's country code.
*
* @param str the string to replace the language/country within
* @return a string containing the local language and region codes
*/
@SuppressWarnings("nls")
private String replaceLanguageAndRegion(String str) {
// Substitute language and or region if present in string
if (str.contains("%lang%") || str.contains("%region%")) {
Locale locale = Locale.getDefault();
str = str.replace("%lang%", locale.getLanguage().toLowerCase());
str = str.replace("%region%", locale.getCountry().toLowerCase());
}
return str;
}
}

@ -53,6 +53,10 @@ public class BillingConstants {
public static final String ITEM_TYPE_SUBSCRIPTION = "subs";
public static final String PRODUCT_ID_MONTHLY = "premium_monthly";
public static final String PRODUCT_ID_YEARLY = "premium_yearly";
// The response codes for a request, defined by Android Market.
public enum ResponseCode {
RESULT_OK,

Loading…
Cancel
Save