Skip to content

Commit

Permalink
bug fix cache refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Jan 7, 2013
1 parent 88a94c8 commit 4a4fa9d
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 10 deletions.
92 changes: 92 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -0,0 1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="ClockworkModBilling" default="help">

<!-- The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
Version Control Systems. -->
<property file="local.properties" />

<!-- The ant.properties file can be created by you. It is only edited by the
'android' tool to add properties to it.
This is the place to change some Ant specific build properties.
Here are some properties you may want to change/update:
source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.
For other overridable properties, look at the beginning of the rules
files in the SDK, at tools/ant/build.xml
Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.
This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->
<property file="ant.properties" />

<!-- if sdk.dir was not set from one of the property file, then
get it from the ANDROID_HOME env var.
This must be done before we load project.properties since
the proguard config can use sdk.dir -->
<property environment="env" />
<condition property="sdk.dir" value="${env.ANDROID_HOME}">
<isset property="env.ANDROID_HOME" />
</condition>

<!-- The project.properties file is created and updated by the 'android'
tool, as well as ADT.
This contains project specific properties such as project target, and library
dependencies. Lower level build properties are stored in ant.properties
(or in .classpath for Eclipse projects).
This file is an integral part of the build system for your
application and should be checked into Version Control Systems. -->
<loadproperties srcFile="project.properties" />

<!-- quick check on sdk.dir -->
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
unless="sdk.dir"
/>

<!--
Import per project custom build rules if present at the root of the project.
This is the place to put custom intermediary targets such as:
-pre-build
-pre-compile
-post-compile (This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir})
-post-package
-post-build
-pre-clean
-->
<import file="custom_rules.xml" optional="true" />

<!-- Import the actual build file.
To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<import> task.
- customize it to your needs.
- Customize the whole content of build.xml
- copy/paste the content of the rules files (minus the top node)
into this file, replacing the <import> task.
- customize to your needs.
***********************
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"
-->
<!-- version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml" />

</project>
9 changes: 9 additions & 0 deletions gen/R.java.d
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
/Volumes/Data/src/ClockworkModBilling/gen/com/clockworkmod/billing/R.java \
: /Volumes/Data/src/ClockworkModBilling/res/drawable-hdpi/icon.png \
/Volumes/Data/src/ClockworkModBilling/res/drawable-ldpi/icon.png \
/Volumes/Data/src/ClockworkModBilling/res/drawable-mdpi/icon.png \
/Volumes/Data/src/ClockworkModBilling/res/values/strings.xml \
/Volumes/Data/src/ClockworkModBilling/bin/res/drawable-hdpi/icon.png \
/Volumes/Data/src/ClockworkModBilling/bin/res/drawable-ldpi/icon.png \
/Volumes/Data/src/ClockworkModBilling/bin/res/drawable-mdpi/icon.png \
/Volumes/Data/src/ClockworkModBilling/bin/AndroidManifest.xml \
2 changes: 1 addition & 1 deletion gen/com/android/vending/billing/IMarketBillingService.java
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /Users/koush/Documents/workspace/DesktopSms/ClockworkModBilling/src/com/android/vending/billing/IMarketBillingService.aidl
* Original file: /Volumes/Data/src/ClockworkModBilling/src/com/android/vending/billing/IMarketBillingService.aidl
*/
package com.android.vending.billing;
public interface IMarketBillingService extends android.os.IInterface
Expand Down
3 changes: 3 additions & 0 deletions gen/com/android/vending/billing/IMarketBillingService.java.d
Original file line number Diff line number Diff line change
@@ -0,0 1,3 @@
/Volumes/Data/src/ClockworkModBilling/gen/com/android/vending/billing/IMarketBillingService.java: \
/Volumes/Data/src/ClockworkModBilling/src/com/android/vending/billing/IMarketBillingService.aidl \

2 changes: 1 addition & 1 deletion gen/com/clockworkmod/billing/BuildConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 2,5 @@
package com.clockworkmod.billing;

public final class BuildConfig {
public final static boolean DEBUG = true;
public final static boolean DEBUG = false;
}
10 changes: 10 additions & 0 deletions local.properties
Original file line number Diff line number Diff line change
@@ -0,0 1,10 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.

# location of the SDK. This is only used by Ant
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=/Users/koush/src/android-sdk
20 changes: 20 additions & 0 deletions proguard-project.txt
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
24 changes: 18 additions & 6 deletions src/com/clockworkmod/billing/ClockworkModBillingClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 35,7 @@

import android.accounts.Account;
import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.PendingIntent;
Expand Down Expand Up @@ -506,6 507,7 @@ static long generateNonce(Context context) {
return nonce;
}

@SuppressLint("NewApi")
public static String getSafeDeviceId(Context context) {
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
Expand Down Expand Up @@ -547,6 549,12 @@ private static boolean checkNonce(Context context, long nonce, long cacheDuratio
return false;
return maskedNonce == deviceNonce;
}

static long getTimestampFromNonce(long nonce) {
long dateNonce = nonce & 0x00000000FFFFFFFFL;
dateNonce *= 1000L;
return dateNonce;
}

public static final long CACHE_DURATION_FOREVER = Long.MAX_VALUE;

Expand All @@ -569,6 577,7 @@ public List<ClockworkOrder> getCachedClockworkPurchases(String buyerId) {
proof = new JSONObject(signedData);
if (proof.optBoolean("sandbox", true) != mSandbox)
throw new Exception("sandbox mismatch");
long timestamp = proof.getLong("timestamp");
String sellerId = proof.optString("seller_id", null);
if (!mSellerId.equals(sellerId))
throw new Exception("seller_id mismatch");
Expand All @@ -577,7 586,7 @@ public List<ClockworkOrder> getCachedClockworkPurchases(String buyerId) {
JSONArray orders = proof.getJSONArray("orders");
for (int i = 0; i < orders.length(); i ) {
JSONObject order = orders.getJSONObject(i);
ret.add(new ClockworkOrder(order));
ret.add(new ClockworkOrder(order, timestamp));
}
}
catch (Exception ex) {
Expand Down Expand Up @@ -609,10 618,11 @@ public List<InAppOrder> getCachedInAppPurchases() {
if (!checkNonce(mContext, nonce, CACHE_DURATION_FOREVER))
throw new Exception("nonce failure");
proof = new JSONObject(signedData);
long timestamp = getTimestampFromNonce(nonce);
JSONArray orders = proof.getJSONArray("orders");
for (int i = 0; i < orders.length(); i ) {
JSONObject order = orders.getJSONObject(i);
orderMap.put(orderId, new InAppOrder(order));
orderMap.put(orderId, new InAppOrder(order, timestamp));
}
}
catch (Exception ex) {
Expand Down Expand Up @@ -654,9 664,9 @@ private CheckPurchaseResult[] checkCachedPurchases(Context context, String produ
long nonce = proof.getLong("nonce");
if (!checkNonce(context, nonce, marketCacheDuration))
throw new Exception("nonce failure");

long timestamp = getTimestampFromNonce(nonce);
Log.i(LOGTAG, "Cached in app billing success");
result[0] = result[1] = CheckPurchaseResult.purchased(new InAppOrder(order));
result[0] = result[1] = CheckPurchaseResult.purchased(new InAppOrder(order, timestamp));
return result;
}
}
Expand Down Expand Up @@ -744,7 754,7 @@ else if (!found) {
JSONObject order = orders.getJSONObject(i);
if (productId.equals(order.getString("product_id"))) {
Log.i(LOGTAG, "Cached server billing success");
result[0] = result[2] = CheckPurchaseResult.purchased(new ClockworkOrder(order));
result[0] = result[2] = CheckPurchaseResult.purchased(new ClockworkOrder(order, timestamp));
return result;
}
}
Expand Down Expand Up @@ -858,12 868,14 @@ public void run() {
});
Log.i(LOGTAG, "CheckPurchase result: " syncResult.toString());
state.reportedPurchase = true;
long til = System.currentTimeMillis() - syncResult.getOrder().getPurchaseTime();
long til = syncResult.getOrder().getTimestamp() - System.currentTimeMillis();
if (syncResult.getOrder() instanceof ClockworkOrder) {
til = billingCacheDuration;
if (billingCacheDuration / 3 < til || billingCacheDuration == CACHE_DURATION_FOREVER)
return syncResult;
}
else {
til = marketCacheDuration;
if (marketCacheDuration / 3 < til || marketCacheDuration == CACHE_DURATION_FOREVER)
return syncResult;
}
Expand Down
9 changes: 8 additions & 1 deletion src/com/clockworkmod/billing/ClockworkOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 8,9 @@ public JSONObject getRawJSONObject() {
}

JSONObject mOrder;
ClockworkOrder(JSONObject order) {
ClockworkOrder(JSONObject order, long timestamp) {
mOrder = order;
this.timestamp = timestamp;
}

@Override
Expand All @@ -24,4 25,10 @@ public String getProductId() {
public long getPurchaseTime() {
return mOrder.optLong("order_date", 0);
}

private long timestamp;
@Override
public long getTimestamp() {
return timestamp;
}
}
9 changes: 8 additions & 1 deletion src/com/clockworkmod/billing/InAppOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 8,9 @@ public JSONObject getRawJSONObject() {
}

JSONObject mOrder;
InAppOrder(JSONObject order) {
InAppOrder(JSONObject order, long timestamp) {
mOrder = order;
this.timestamp = timestamp;
}

@Override
Expand All @@ -24,4 25,10 @@ public String getProductId() {
public long getPurchaseTime() {
return mOrder.optLong("purchaseTime", 0);
}

private long timestamp;
@Override
public long getTimestamp() {
return timestamp;
}
}
1 change: 1 addition & 0 deletions src/com/clockworkmod/billing/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 7,5 @@ public interface Order {
String getDeveloperPayload();
String getProductId();
long getPurchaseTime();
long getTimestamp();
}

0 comments on commit 4a4fa9d

Please sign in to comment.