Issue #271 paid apps fix

This commit is contained in:
Sergey Eremin
2017-09-18 05:20:22 +03:00
parent 922423b789
commit 3eba70a4a3
4 changed files with 55 additions and 35 deletions

View File

@@ -1,5 +1,6 @@
package com.github.yeriomin.yalpstore;
import android.util.Log;
import com.github.yeriomin.playstoreapi.AndroidAppDeliveryData;
import com.github.yeriomin.playstoreapi.BuyResponse;
@@ -12,6 +13,7 @@ import java.io.IOException;
public class DeliveryDataTask extends GoogleApiAsyncTask {
protected App app;
protected String downloadToken;
protected AndroidAppDeliveryData deliveryData;
public void setApp(App app) {
@@ -22,33 +24,46 @@ public class DeliveryDataTask extends GoogleApiAsyncTask {
protected Throwable doInBackground(String... params) {
try {
GooglePlayAPI api = new PlayStoreApiAuthenticator(context).getApi();
BuyResponse buyResponse = api.purchase(app.getPackageName(), app.getVersionCode(), app.getOfferType());
if (buyResponse.hasPurchaseStatusResponse()
&& buyResponse.getPurchaseStatusResponse().hasAppDeliveryData()
&& buyResponse.getPurchaseStatusResponse().getAppDeliveryData().hasDownloadUrl()
) {
deliveryData = buyResponse.getPurchaseStatusResponse().getAppDeliveryData();
return null;
}
if (!buyResponse.hasDownloadToken()) {
return null;
}
DeliveryResponse deliveryResponse = api.delivery(
app.getPackageName(),
app.getInstalledVersionCode() >= app.getVersionCode() ? 0 : app.getInstalledVersionCode(),
app.getVersionCode(),
app.getOfferType(),
GooglePlayAPI.PATCH_FORMAT.GZIPPED_GDIFF,
buyResponse.getDownloadToken()
);
if (deliveryResponse.hasAppDeliveryData()
&& deliveryResponse.getAppDeliveryData().hasDownloadUrl()
) {
deliveryData = deliveryResponse.getAppDeliveryData();
}
purchase(api);
delivery(api);
} catch (IOException e) {
return e;
}
return null;
}
protected void purchase(GooglePlayAPI api) {
try {
BuyResponse buyResponse = api.purchase(app.getPackageName(), app.getVersionCode(), app.getOfferType());
if (buyResponse.hasPurchaseStatusResponse()
&& buyResponse.getPurchaseStatusResponse().hasAppDeliveryData()
&& buyResponse.getPurchaseStatusResponse().getAppDeliveryData().hasDownloadUrl()
) {
deliveryData = buyResponse.getPurchaseStatusResponse().getAppDeliveryData();
}
if (buyResponse.hasDownloadToken()) {
downloadToken = buyResponse.getDownloadToken();
}
} catch (IOException e) {
Log.w(getClass().getName(), "Purchase for " + app.getPackageName() + " failed with " + e.getClass().getName() + ": " + e.getMessage());
}
}
protected void delivery(GooglePlayAPI api) throws IOException {
DeliveryResponse deliveryResponse = api.delivery(
app.getPackageName(),
app.getInstalledVersionCode() >= app.getVersionCode() ? 0 : app.getInstalledVersionCode(),
app.getVersionCode(),
app.getOfferType(),
GooglePlayAPI.PATCH_FORMAT.GZIPPED_GDIFF,
downloadToken
);
if (deliveryResponse.hasAppDeliveryData()
&& deliveryResponse.getAppDeliveryData().hasDownloadUrl()
) {
deliveryData = deliveryResponse.getAppDeliveryData();
} else {
throw new NotPurchasedException();
}
}
}

View File

@@ -1,4 +1,6 @@
package com.github.yeriomin.yalpstore;
public class NotPurchasedException extends Exception {
import java.io.IOException;
public class NotPurchasedException extends IOException {
}

View File

@@ -3,7 +3,6 @@ package com.github.yeriomin.yalpstore;
import android.view.View;
import android.widget.Button;
import com.github.yeriomin.playstoreapi.AuthException;
import com.github.yeriomin.yalpstore.fragment.details.DownloadOrInstall;
import java.util.Timer;
@@ -38,10 +37,4 @@ public class PurchaseCheckTask extends DeliveryDataTask {
downloadButton.setVisibility(View.VISIBLE);
timer.cancel();
}
@Override
protected void processAuthException(AuthException e) {
// Since Play Store returns 403 error on an attempt to download a non-existing version,
// we need to ignore it
}
}

View File

@@ -10,6 +10,8 @@ import android.view.WindowManager;
import com.github.yeriomin.playstoreapi.AuthException;
import java.io.IOException;
public class PurchaseTask extends DeliveryDataTask {
static public final String URL_PURCHASE = "https://play.google.com/store/apps/details?id=";
@@ -33,11 +35,12 @@ public class PurchaseTask extends DeliveryDataTask {
if (null != result) {
return result;
}
if (null == deliveryData) {
if (null != deliveryData) {
new Downloader(context).download(app, deliveryData, listener);
} else {
context.sendBroadcast(new Intent(DownloadManagerInterface.ACTION_DOWNLOAD_CANCELLED));
Log.e(getClass().getName(), app.getPackageName() + " no download link returned");
return null;
}
new Downloader(context).download(app, deliveryData, listener);
} catch (Throwable e) {
context.sendBroadcast(new Intent(DownloadManagerInterface.ACTION_DOWNLOAD_CANCELLED));
return e;
@@ -57,6 +60,13 @@ public class PurchaseTask extends DeliveryDataTask {
}
}
@Override
protected void processIOException(IOException e) {
if (!(e instanceof NotPurchasedException)) {
super.processIOException(e);
}
}
@Override
protected void processAuthException(AuthException e) {
if (e.getCode() == 403) {