From 3eba70a4a3d59873ac8f8e1229a2fa4bb24e36ff Mon Sep 17 00:00:00 2001 From: Sergey Eremin Date: Mon, 18 Sep 2017 05:20:22 +0300 Subject: [PATCH] Issue #271 paid apps fix --- .../yeriomin/yalpstore/DeliveryDataTask.java | 63 ++++++++++++------- .../yalpstore/NotPurchasedException.java | 4 +- .../yeriomin/yalpstore/PurchaseCheckTask.java | 7 --- .../yeriomin/yalpstore/PurchaseTask.java | 16 ++++- 4 files changed, 55 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/github/yeriomin/yalpstore/DeliveryDataTask.java b/app/src/main/java/com/github/yeriomin/yalpstore/DeliveryDataTask.java index 7e1bceae8..67967973f 100644 --- a/app/src/main/java/com/github/yeriomin/yalpstore/DeliveryDataTask.java +++ b/app/src/main/java/com/github/yeriomin/yalpstore/DeliveryDataTask.java @@ -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(); + } + } } diff --git a/app/src/main/java/com/github/yeriomin/yalpstore/NotPurchasedException.java b/app/src/main/java/com/github/yeriomin/yalpstore/NotPurchasedException.java index b999fb4be..5cfda37db 100644 --- a/app/src/main/java/com/github/yeriomin/yalpstore/NotPurchasedException.java +++ b/app/src/main/java/com/github/yeriomin/yalpstore/NotPurchasedException.java @@ -1,4 +1,6 @@ package com.github.yeriomin.yalpstore; -public class NotPurchasedException extends Exception { +import java.io.IOException; + +public class NotPurchasedException extends IOException { } diff --git a/app/src/main/java/com/github/yeriomin/yalpstore/PurchaseCheckTask.java b/app/src/main/java/com/github/yeriomin/yalpstore/PurchaseCheckTask.java index 3ff49e01b..f5a4b4a9f 100644 --- a/app/src/main/java/com/github/yeriomin/yalpstore/PurchaseCheckTask.java +++ b/app/src/main/java/com/github/yeriomin/yalpstore/PurchaseCheckTask.java @@ -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 - } } diff --git a/app/src/main/java/com/github/yeriomin/yalpstore/PurchaseTask.java b/app/src/main/java/com/github/yeriomin/yalpstore/PurchaseTask.java index 762c915b9..fad41cbf7 100644 --- a/app/src/main/java/com/github/yeriomin/yalpstore/PurchaseTask.java +++ b/app/src/main/java/com/github/yeriomin/yalpstore/PurchaseTask.java @@ -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) {