Permalink
Browse files

[+] Android in-app billing donations added

[+] AndroidBillingLibrary added
  • Loading branch information...
1 parent ecc0e43 commit f220b11fea9d4896a1e7ca79b65bcc9688f08ddd @artiomchi committed Dec 5, 2011
View
@@ -4,3 +4,6 @@
[submodule "lib/MixPanel"]
path = lib/MixPanel
url = git://github.com/mixpanel/mixpanel-android.git
+[submodule "lib/AndroidBilling"]
+ path = lib/AndroidBilling
+ url = git://github.com/robotmedia/AndroidBillingLibrary.git
View
@@ -37,6 +37,15 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
+ <!-- In-app Market Billing -->
+ <service android:name="net.robotmedia.billing.BillingService" />
+ <receiver android:name="net.robotmedia.billing.BillingReceiver">
+ <intent-filter>
+ <action android:name="com.android.vending.billing.IN_APP_NOTIFY" />
+ <action android:name="com.android.vending.billing.RESPONSE_CODE" />
+ <action android:name="com.android.vending.billing.PURCHASE_STATE_CHANGED" />
+ </intent-filter>
+ </receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.vending.BILLING" />
View
@@ -42,6 +42,7 @@
</orderEntry>
<orderEntry type="module" module-name="ActionBarSherlock" />
<orderEntry type="module" module-name="ActionBarSherlockPreference" />
+ <orderEntry type="module" module-name="AndroidBillingLibrary" />
</component>
</module>
View
@@ -181,6 +181,7 @@
<modules>
<module fileurl="file://$PROJECT_DIR$/lib/ActionBarSherlock_mod/ActionBarSherlock.iml" filepath="$PROJECT_DIR$/lib/ActionBarSherlock_mod/ActionBarSherlock.iml" />
<module fileurl="file://$PROJECT_DIR$/lib/ActionBarSherlockPreference_mod/ActionBarSherlockPreference.iml" filepath="$PROJECT_DIR$/lib/ActionBarSherlockPreference_mod/ActionBarSherlockPreference.iml" />
+ <module fileurl="file://$PROJECT_DIR$/lib/AndroidBilling_mod/AndroidBillingLibrary.iml" filepath="$PROJECT_DIR$/lib/AndroidBilling_mod/AndroidBillingLibrary.iml" />
<module fileurl="file://$PROJECT_DIR$/DualBattery.iml" filepath="$PROJECT_DIR$/DualBattery.iml" />
</modules>
</component>
Submodule AndroidBilling added at 450313
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+ <component name="FacetManager">
+ <facet type="android" name="Android">
+ <configuration>
+ <option name="GEN_FOLDER_RELATIVE_PATH_APT" value="/../AndroidBilling/AndroidBillingLibrary/gen" />
+ <option name="GEN_FOLDER_RELATIVE_PATH_AIDL" value="/../AndroidBilling/AndroidBillingLibrary/gen" />
+ <option name="MANIFEST_FILE_RELATIVE_PATH" value="/../AndroidBilling/AndroidBillingLibrary/AndroidManifest.xml" />
+ <option name="RES_FOLDER_RELATIVE_PATH" value="/../AndroidBilling/AndroidBillingLibrary/res" />
+ <option name="ASSETS_FOLDER_RELATIVE_PATH" value="/../AndroidBilling/AndroidBillingLibrary/assets" />
+ <option name="LIBS_FOLDER_RELATIVE_PATH" value="/../AndroidBilling/AndroidBillingLibrary/libs" />
+ <option name="REGENERATE_R_JAVA" value="true" />
+ <option name="REGENERATE_JAVA_BY_AIDL" value="true" />
+ <option name="USE_CUSTOM_APK_RESOURCE_FOLDER" value="false" />
+ <option name="CUSTOM_APK_RESOURCE_FOLDER" value="" />
+ <option name="USE_CUSTOM_COMPILER_MANIFEST" value="false" />
+ <option name="CUSTOM_COMPILER_MANIFEST" value="" />
+ <option name="APK_PATH" value="" />
+ <option name="LIBRARY_PROJECT" value="true" />
+ <option name="RUN_PROCESS_RESOURCES_MAVEN_TASK" value="true" />
+ <option name="GENERATE_UNSIGNED_APK" value="false" />
+ <option name="CUSTOM_DEBUG_KEYSTORE_PATH" value="" />
+ </configuration>
+ </facet>
+ </component>
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
+ <exclude-output />
+ <content url="file://$MODULE_DIR$/../AndroidBilling/AndroidBillingLibrary">
+ <sourceFolder url="file://$MODULE_DIR$/../AndroidBilling/AndroidBillingLibrary/src" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/../AndroidBilling/AndroidBillingLibrary/gen" isTestSource="false" />
+ </content>
+ <orderEntry type="jdk" jdkName="Android 3.2 Platform" jdkType="Android SDK" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ </component>
+</module>
+
View
@@ -45,4 +45,9 @@
<item>@string/sett_margin_Bottom</item>
<item>@string/sett_margin_Both</item>
</string-array>
+ <string-array name="donation_market">
+ <item>@string/donation_market_1</item>
+ <item>@string/donation_market_3</item>
+ <item>@string/donation_market_7</item>
+ </string-array>
</resources>
View
@@ -125,4 +125,8 @@
<string name="battery_info_dock_status_discharging">Discharging</string>
<string name="battery_info_dock_status_docked">Docked</string>
<string name="battery_info_dock_status_undocked">Not docked</string>
+
+ <string name="donation_market_1">Standard (£0.99)</string>
+ <string name="donation_market_3">Buy me a pint (£3.00)</string>
+ <string name="donation_market_7">If you really like number 7 (£7.77)</string>
</resources>
@@ -17,6 +17,11 @@
package org.flexlabs.widgets.dualbattery;
import android.app.Application;
+import android.app.PendingIntent;
+import net.robotmedia.billing.BillingController;
+import net.robotmedia.billing.BillingRequest;
+import net.robotmedia.billing.IBillingObserver;
+import net.robotmedia.billing.model.Transaction;
public class BatteryApplication extends Application {
private static BatteryApplication _instance;
@@ -26,5 +31,31 @@
public void onCreate() {
super.onCreate();
_instance = this;
+
+ BillingController.setDebug(Constants.DEBUG);
+ BillingController.setConfiguration(new BillingController.IConfiguration() {
+ @Override
+ public byte[] getObfuscationSalt() {
+ return new byte[] {41, -90, -116, -41, 77, -53, 127, -110, -127, -96, -88, 77, 127, 117, 1, 73, 57, 110, 48, -116};
+ }
+
+ @Override
+ public String getPublicKey() {
+ return "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmRLx8FRLePsmwi0uXID5uUzf6oWe8KFmUtLaApbiNIG+qDrPAVScKjE1KEdNWBAlzyc70Ohihh73e7/BBuzLECZFjZo7Xbks6JdZ2zxii8OCclDdYq5MZQkkuLUCrNd2B97+JwKaYjdDdjkIVcUP0jWyGWEXnFo6pjZK0VRLEFITDbt4vq/NfJpxWrnI8j95GWJUTlZ26TdY/1tjUaXr6l3GuWj71RlvRQuCPnjneLwZjdLjxYfZknGRhHTCXlIVfdGhcbuaOem1IL+R5xFbJftAXJfM2kgoNIb/FhbNLWjM1jdjemWaWyhhcz3AhEk92Fbc5ZLxhsh4oYcVB0uLRwIDAQAB";
+ }
+ });
+ BillingController.registerObserver(new IBillingObserver() {
+ @Override
+ public void onBillingChecked(boolean supported) {
+ Constants.HAS_MARKET_BILLING = supported;
+ BillingController.unregisterObserver(this);
+ }
+
+ @Override public void onPurchaseIntent(String itemId, PendingIntent purchaseIntent) { }
+ @Override public void onPurchaseStateChanged(String itemId, Transaction.PurchaseState state) { }
+ @Override public void onRequestPurchaseResponse(String itemId, BillingRequest.ResponseCode response) { }
+ @Override public void onTransactionsRestored() { }
+ });
+ BillingController.checkBillingSupported(this);
}
}
@@ -21,6 +21,8 @@
public class Constants {
public static final String LOG = "FlexLabs.DBW";
+ public static final boolean DEBUG = false;
+ public static boolean HAS_MARKET_BILLING = false;
public static final String EXTRA_WIDGET_IDS = "widgetIds";
public static final String SETTINGS_PREFIX = "widgetPref_";
@@ -23,10 +23,7 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
+import android.content.*;
import android.database.Cursor;
import android.graphics.Color;
import android.net.Uri;
@@ -42,9 +39,11 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
-import android.widget.LinearLayout;
-import android.widget.TableRow;
-import android.widget.TextView;
+import android.widget.*;
+import net.robotmedia.billing.BillingController;
+import net.robotmedia.billing.BillingRequest;
+import net.robotmedia.billing.helper.AbstractBillingObserver;
+import net.robotmedia.billing.model.Transaction;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.XYMultipleSeriesDataset;
@@ -64,7 +63,8 @@
import java.util.regex.Pattern;
public class BatteryInfoViewManager extends BroadcastReceiver {
- public static final int DIALOG_ABOUT = 0;
+ public static final int DIALOG_ABOUT = 1;
+ public static final int DIALOG_DONATE_MARKET = 2;
private TextView mStatus, mLevel, mScale;
private TextView mHealth;
@@ -86,7 +86,9 @@
private XYSeries mMainSeries, mDockSeries;
private GraphicalView mChartView;
private LinearLayout mChartContainer;
-
+
+ AbstractBillingObserver mBillingObserver;
+
public void loadData(Activity activity, View view, int appWidgetId) {
mActivity = activity;
@@ -111,13 +113,34 @@ public void loadData(Activity activity, View view, int appWidgetId) {
this.appWidgetId = appWidgetId;
tempUnitsC = mActivity.getSharedPreferences(Constants.SETTINGS_PREFIX + appWidgetId, Context.MODE_PRIVATE)
.getInt(Constants.SETTING_TEMP_UNITS, Constants.SETTING_TEMP_UNITS_DEFAULT) == Constants.TEMP_UNIT_CELSIUS;
+
+ if (mBillingObserver == null) {
+ mBillingObserver = new AbstractBillingObserver(mActivity) {
+ @Override
+ public void onPurchaseStateChanged(String itemId, Transaction.PurchaseState state) {
+ if (state == Transaction.PurchaseState.PURCHASED) {
+ Toast.makeText(mActivity, "Thanks a lot for supporting me!", Toast.LENGTH_LONG).show();
+ }
+ }
+
+ @Override public void onBillingChecked(boolean supported) { }
+ @Override public void onRequestPurchaseResponse(String itemId, BillingRequest.ResponseCode response) { }
+ };
+ BillingController.registerObserver(mBillingObserver);
+ }
+ }
+
+ public void onDestroy() {
+ if (mBillingObserver != null) {
+ BillingController.unregisterObserver(mBillingObserver);
+ mBillingObserver = null;
+ }
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
-
mLevel.setText("" + intent.getIntExtra("level", 0));
mScale.setText("" + intent.getIntExtra("scale", 0));
int voltage = intent.getIntExtra("voltage", 0);
@@ -290,11 +313,12 @@ public boolean onMenuItemSelected(MenuItem item) {
return true;
case R.id.donate_market :
- break;
+ mActivity.showDialog(DIALOG_DONATE_MARKET);
+ return true;
case R.id.about :
- mActivity.showDialog(DIALOG_ABOUT);
- return true;
+ mActivity.showDialog(DIALOG_ABOUT);
+ return true;
}
return false;
}
@@ -381,15 +405,17 @@ public void onClick(View view) {
}
};
- public static Dialog onCreateDialog(Context context, int id) {
+ public Dialog onCreateDialog(Context context, int id) {
+ Dialog result;
+
switch (id) {
case DIALOG_ABOUT :
final SpannableString s = new SpannableString(
context.getString(R.string.about_full) +
context.getString(R.string.about_translations));
Linkify.addLinks(s, Linkify.ALL);
- Dialog result = new AlertDialog.Builder(context)
+ result = new AlertDialog.Builder(context)
.setTitle(R.string.propTitle_About)
.setMessage(s)
.setPositiveButton("OK", null)
@@ -398,6 +424,24 @@ public static Dialog onCreateDialog(Context context, int id) {
((TextView)result.findViewById(android.R.id.message))
.setMovementMethod(LinkMovementMethod.getInstance());
return result;
+
+ case DIALOG_DONATE_MARKET :
+ result = new AlertDialog.Builder(context)
+ .setItems(R.array.donation_market, new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialogInterface, int i) {
+ String marketItem = "donation.amount.0.99";
+ switch (i) {
+ case 1:
+ marketItem = "donation.amount.3.00"; break;
+ case 2:
+ marketItem = "donation.amount.7.77"; break;
+ }
+ BillingController.requestPurchase(mActivity, marketItem, true);
+ }
+ })
+ .create();
+ return result;
default: return null;
}
@@ -29,6 +29,7 @@
import android.view.View;
import android.view.MenuInflater;
import android.widget.Button;
+import org.flexlabs.widgets.dualbattery.Constants;
import org.flexlabs.widgets.dualbattery.R;
public class WidgetActivity extends FragmentActivity {
@@ -71,6 +72,12 @@ public void onClick(View view) {
});
}
+ @Override
+ protected void onDestroy() {
+ super.onDestroy();
+ batteryInfoViewManager.onDestroy();
+ }
+
@Override
public void onPause() {
super.onPause();
@@ -86,14 +93,16 @@ protected void onResume() {
@Override
protected Dialog onCreateDialog(int id) {
- return BatteryInfoViewManager.onCreateDialog(this, id);
+ return batteryInfoViewManager.onCreateDialog(this, id);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.widget_batteryinfo, menu);
inflater.inflate(R.menu.widget, menu);
+ if (!Constants.HAS_MARKET_BILLING)
+ menu.findItem(R.id.donate_market).setEnabled(false);
return true;
}
@@ -49,6 +49,12 @@ public void onBuildHeaders(List<Header> target) {
}
}
+ @Override
+ protected void onDestroy() {
+ super.onDestroy();
+ batteryInfoViewManager.onDestroy();
+ }
+
public static class PropertiesFragment extends PreferenceFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
@@ -66,7 +72,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
@Override
protected Dialog onCreateDialog(int id) {
- return BatteryInfoViewManager.onCreateDialog(this, id);
+ return batteryInfoViewManager.onCreateDialog(this, id);
}
@Override
@@ -81,6 +87,8 @@ protected void onStop() {
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.widget, menu);
+ if (!Constants.HAS_MARKET_BILLING)
+ menu.findItem(R.id.donate_market).setEnabled(false);
return true;
}

0 comments on commit f220b11

Please sign in to comment.