From 31bf65bb119abecd8888a9f7e8cce1d7b7c1623e Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Sat, 29 Apr 2017 12:04:39 +0300 Subject: [PATCH] Fix #3718 --- .../osmand/plus/activities/MapActivity.java | 50 +++--- .../plus/download/DownloadActivity.java | 8 +- .../net/osmand/plus/inapp/InAppHelper.java | 167 ++++++++++++------ .../plus/liveupdates/OsmLiveActivity.java | 4 +- .../liveupdates/SubscriptionFragment.java | 5 +- 5 files changed, 148 insertions(+), 86 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index b50f6d7555..689bfa3640 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1629,34 +1629,38 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven if (inAppHelper != null) { inAppHelper.stop(); } - inAppHelper = new InAppHelper(getMyApplication(), false); - inAppHelper.addListener(new InAppHelper.InAppListener() { - @Override - public void onError(String error) { - inAppHelper = null; - } + if (Version.isGooglePlayEnabled(app)) { + inAppHelper = new InAppHelper(getMyApplication(), false); + inAppHelper.addListener(new InAppHelper.InAppListener() { + @Override + public void onError(String error) { + inAppHelper = null; + } - @Override - public void onGetItems() { - inAppHelper = null; - } + @Override + public void onGetItems() { + inAppHelper = null; + } - @Override - public void onItemPurchased(String sku) { - inAppHelper = null; - } + @Override + public void onItemPurchased(String sku) { + inAppHelper = null; + } - @Override - public void showProgress() { + @Override + public void showProgress() { - } + } - @Override - public void dismissProgress() { + @Override + public void dismissProgress() { - } - }); - inAppHelper.exec(runnable); - return inAppHelper; + } + }); + inAppHelper.exec(runnable); + return inAppHelper; + } else { + return null; + } } } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 53226b6a91..919f666165 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -223,9 +223,11 @@ public class DownloadActivity extends AbstractDownloadActivity implements Downlo public void startInAppHelper() { stopInAppHelper(); - inAppHelper = new InAppHelper(getMyApplication(), true); - inAppHelper.addListener(this); - inAppHelper.start(false); + if (Version.isGooglePlayEnabled(getMyApplication())) { + inAppHelper = new InAppHelper(getMyApplication(), true); + inAppHelper.addListener(this); + inAppHelper.start(false); + } } public void stopInAppHelper() { diff --git a/OsmAnd/src/net/osmand/plus/inapp/InAppHelper.java b/OsmAnd/src/net/osmand/plus/inapp/InAppHelper.java index 18aa1715e9..24bb811eb8 100644 --- a/OsmAnd/src/net/osmand/plus/inapp/InAppHelper.java +++ b/OsmAnd/src/net/osmand/plus/inapp/InAppHelper.java @@ -198,26 +198,33 @@ public class InAppHelper { // Start setup. This is asynchronous and the specified listener // will be called once setup completes. logDebug("Starting setup."); - mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { - public void onIabSetupFinished(IabResult result) { - logDebug("Setup finished."); + try { + mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { + public void onIabSetupFinished(IabResult result) { + logDebug("Setup finished."); - if (!result.isSuccess()) { - // Oh noes, there was a problem. - complain("Problem setting up in-app billing: " + result); - notifyError(result.getMessage()); - if (stopAfterResult) { - stop(); + if (!result.isSuccess()) { + // Oh noes, there was a problem. + //complain("Problem setting up in-app billing: " + result); + notifyError(result.getMessage()); + if (stopAfterResult) { + stop(); + } + return; } - return; + + // Have we been disposed of in the meantime? If so, quit. + if (mHelper == null) return; + + runnable.run(InAppHelper.this); } - - // Have we been disposed of in the meantime? If so, quit. - if (mHelper == null) return; - - runnable.run(InAppHelper.this); + }); + } catch (Exception e) { + logError("exec Error", e); + if (stopAfterResult) { + stop(); } - }); + } } public void start(final boolean stopAfterResult) { @@ -233,43 +240,58 @@ public class InAppHelper { // Start setup. This is asynchronous and the specified listener // will be called once setup completes. logDebug("Starting setup."); - mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { - public void onIabSetupFinished(IabResult result) { - logDebug("Setup finished."); + try { + mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { + public void onIabSetupFinished(IabResult result) { + logDebug("Setup finished."); - if (!result.isSuccess()) { - // Oh noes, there was a problem. - complain("Problem setting up in-app billing: " + result); - notifyError(result.getMessage()); - if (stopAfterResult) { - stop(); + if (!result.isSuccess()) { + // Oh noes, there was a problem. + //complain("Problem setting up in-app billing: " + result); + notifyError(result.getMessage()); + if (stopAfterResult) { + stop(); + } + return; } - return; - } - // Have we been disposed of in the meantime? If so, quit. - if (mHelper == null) return; + // Have we been disposed of in the meantime? If so, quit. + if (mHelper == null) return; - // IAB is fully set up. Now, let's get an inventory of stuff we own if needed. - if (forceRequestInventory || (!isDeveloperVersion && - (!mSubscribedToLiveUpdates - || !ctx.getSettings().BILLING_PURCHASE_TOKEN_SENT.get() - || System.currentTimeMillis() - lastValidationCheckTime > PURCHASE_VALIDATION_PERIOD_MSEC))) { + // IAB is fully set up. Now, let's get an inventory of stuff we own if needed. + if (forceRequestInventory || (!isDeveloperVersion && + (!mSubscribedToLiveUpdates + || !ctx.getSettings().BILLING_PURCHASE_TOKEN_SENT.get() + || System.currentTimeMillis() - lastValidationCheckTime > PURCHASE_VALIDATION_PERIOD_MSEC))) { - logDebug("Setup successful. Querying inventory."); - List skus = new ArrayList<>(); - skus.add(SKU_LIVE_UPDATES); - skus.add(SKU_DEPTH_CONTOURS); - skus.add(SKU_FULL_VERSION_PRICE); - mHelper.queryInventoryAsync(true, skus, mGotInventoryListener); - } else { - notifyDismissProgress(); - if (stopAfterResult) { - stop(); + logDebug("Setup successful. Querying inventory."); + List skus = new ArrayList<>(); + skus.add(SKU_LIVE_UPDATES); + skus.add(SKU_DEPTH_CONTOURS); + skus.add(SKU_FULL_VERSION_PRICE); + try { + mHelper.queryInventoryAsync(true, skus, mGotInventoryListener); + } catch (Exception e) { + logError("queryInventoryAsync Error", e); + notifyDismissProgress(); + if (stopAfterResult) { + stop(); + } + } + } else { + notifyDismissProgress(); + if (stopAfterResult) { + stop(); + } } } + }); + } catch (Exception e) { + logError("start Error", e); + if (stopAfterResult) { + stop(); } - }); + } } // Listener that's called when we finish querying the items and subscriptions we own @@ -380,7 +402,7 @@ public class InAppHelper { public void purchaseFullVersion(final Activity activity) { if (mHelper == null) { - complain("In-app hepler is not initialized!"); + //complain("In-app hepler is not initialized!"); notifyError("In-app hepler is not initialized!"); if (stopAfterResult) { stop(); @@ -390,14 +412,22 @@ public class InAppHelper { logDebug("Launching purchase flow for full version"); if (mHelper != null) { - mHelper.launchPurchaseFlow(activity, - SKU_FULL_VERSION_PRICE, RC_REQUEST, mPurchaseFinishedListener); + try { + mHelper.launchPurchaseFlow(activity, + SKU_FULL_VERSION_PRICE, RC_REQUEST, mPurchaseFinishedListener); + } catch (Exception e) { + complain("Cannot launch full version purchase!"); + logError("purchaseFullVersion Error", e); + if (stopAfterResult) { + stop(); + } + } } } public void purchaseDepthContours(final Activity activity) { if (mHelper == null) { - complain("In-app hepler is not initialized!"); + //complain("In-app hepler is not initialized!"); notifyError("In-app hepler is not initialized!"); if (stopAfterResult) { stop(); @@ -407,16 +437,32 @@ public class InAppHelper { logDebug("Launching purchase flow for sea depth contours"); if (mHelper != null) { - mHelper.launchPurchaseFlow(activity, - SKU_DEPTH_CONTOURS, RC_REQUEST, mPurchaseFinishedListener); + try { + mHelper.launchPurchaseFlow(activity, + SKU_DEPTH_CONTOURS, RC_REQUEST, mPurchaseFinishedListener); + } catch (Exception e) { + complain("Cannot launch depth contours purchase!"); + logError("purchaseDepthContours Error", e); + if (stopAfterResult) { + stop(); + } + } } } public void purchaseLiveUpdates(final Activity activity, final String email, final String userName, final String countryDownloadName, final boolean hideUserName) { - if (mHelper == null || !mHelper.subscriptionsSupported()) { - complain("Subscriptions not supported on your device yet. Sorry!"); - notifyError("Subscriptions not supported on your device yet. Sorry!"); + try { + if (mHelper == null || !mHelper.subscriptionsSupported()) { + complain("Subscriptions not supported on your device yet. Sorry!"); + notifyError("Subscriptions not supported on your device yet. Sorry!"); + if (stopAfterResult) { + stop(); + } + return; + } + } catch (Exception e) { + logError("purchaseLiveUpdates Error", e); if (stopAfterResult) { stop(); } @@ -488,9 +534,16 @@ public class InAppHelper { logDebug("Launching purchase flow for live updates subscription for userId=" + userId); String payload = userId + " " + token; if (mHelper != null) { - mHelper.launchPurchaseFlow(activity, - SKU_LIVE_UPDATES, IabHelper.ITEM_TYPE_SUBS, - RC_REQUEST, mPurchaseFinishedListener, payload); + try { + mHelper.launchPurchaseFlow(activity, + SKU_LIVE_UPDATES, IabHelper.ITEM_TYPE_SUBS, + RC_REQUEST, mPurchaseFinishedListener, payload); + } catch (Exception e) { + logError("launchPurchaseFlow Error", e); + if (stopAfterResult) { + stop(); + } + } } } else { notifyError("Empty userId"); diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/OsmLiveActivity.java b/OsmAnd/src/net/osmand/plus/liveupdates/OsmLiveActivity.java index 1339312aa0..d3915d43a6 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/OsmLiveActivity.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/OsmLiveActivity.java @@ -36,7 +36,9 @@ public class OsmLiveActivity extends AbstractDownloadActivity implements Downloa super.onCreate(savedInstanceState); setContentView(R.layout.activity_livie_updates); - inAppHelper = new InAppHelper(getMyApplication(), false); + if (Version.isGooglePlayEnabled(getMyApplication())) { + inAppHelper = new InAppHelper(getMyApplication(), false); + } if (Version.isDeveloperVersion(getMyApplication())) { inAppHelper = null; } diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java index 88f4a83420..ca04348bf1 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java @@ -214,7 +214,8 @@ public class SubscriptionFragment extends BaseOsmAndDialogFragment implements In saveChangesButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (applySettings(userNameEdit.getText().toString().trim(), + InAppHelper helper = getInAppHelper(); + if (helper != null && applySettings(userNameEdit.getText().toString().trim(), emailEdit.getText().toString().trim(), hideUserNameCheckbox.isChecked())) { final Map parameters = new HashMap<>(); @@ -223,7 +224,7 @@ public class SubscriptionFragment extends BaseOsmAndDialogFragment implements In parameters.put("email", settings.BILLING_USER_EMAIL.get()); parameters.put("cemail", prevEmail); parameters.put("userid", settings.BILLING_USER_ID.get()); - parameters.put("token", getInAppHelper().getToken()); + parameters.put("token", helper.getToken()); showProgress();