From 4dac25e744b66b7beeb13e9cab30418fb7cb2c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=97=D0=B0=D1=86=D0=B5=D0=BF=D0=B8=D0=BD?= Date: Thu, 7 Nov 2019 17:30:15 +0300 Subject: [PATCH] [android] Extracted getting of product ids into subscription type class, refactored the purchase factory class --- .../bookmarks/BookmarksCatalogFragment.java | 6 +-- .../maps/purchase/PurchaseFactory.java | 49 ++++++------------- .../maps/purchase/SubscriptionType.java | 24 ++++++++- .../maps/settings/SettingsPrefsFragment.java | 7 +-- 4 files changed, 45 insertions(+), 41 deletions(-) diff --git a/android/src/com/mapswithme/maps/bookmarks/BookmarksCatalogFragment.java b/android/src/com/mapswithme/maps/bookmarks/BookmarksCatalogFragment.java index 9cbcdedc94..352da45b72 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BookmarksCatalogFragment.java +++ b/android/src/com/mapswithme/maps/bookmarks/BookmarksCatalogFragment.java @@ -8,8 +8,6 @@ import android.net.Uri; import android.net.http.SslError; import android.os.Build; import android.os.Bundle; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.Menu; @@ -25,6 +23,8 @@ import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import com.android.billingclient.api.SkuDetails; import com.mapswithme.maps.Framework; import com.mapswithme.maps.PrivateVariables; @@ -159,7 +159,7 @@ public class BookmarksCatalogFragment extends BaseWebViewMwmFragment mFailedPurchaseController.initialize(requireActivity()); mFailedPurchaseController.validateExistingPurchases(); mPurchaseChecker = new FailedBookmarkPurchaseChecker(); - mProductDetailsLoadingManager = PurchaseFactory.createInAppBillingManager(requireContext()); + mProductDetailsLoadingManager = PurchaseFactory.createInAppBillingManager(); mProductDetailsLoadingManager.initialize(requireActivity()); mProductDetailsLoadingCallback = new ProductDetailsLoadingCallback(); View root = inflater.inflate(R.layout.fragment_bookmarks_catalog, container, false); diff --git a/android/src/com/mapswithme/maps/purchase/PurchaseFactory.java b/android/src/com/mapswithme/maps/purchase/PurchaseFactory.java index da6831122d..7a4c4bf0e4 100644 --- a/android/src/com/mapswithme/maps/purchase/PurchaseFactory.java +++ b/android/src/com/mapswithme/maps/purchase/PurchaseFactory.java @@ -1,13 +1,11 @@ package com.mapswithme.maps.purchase; import android.content.Context; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; - import com.android.billingclient.api.BillingClient; -import com.mapswithme.maps.PrivateVariables; import com.mapswithme.maps.PurchaseOperationObservable; -import com.mapswithme.util.Utils; public class PurchaseFactory { @@ -16,41 +14,32 @@ public class PurchaseFactory // Utility class. } - @NonNull public static PurchaseController createAdsRemovalPurchaseController( @NonNull Context context) { - BillingManager billingManager - = new PlayStoreBillingManager(BillingClient.SkuType.SUBS); - PurchaseOperationObservable observable = PurchaseOperationObservable.from(context); - PurchaseValidator validator = new DefaultPurchaseValidator(observable); - String yearlyProduct = PrivateVariables.adsRemovalYearlyProductId(); - String monthlyProduct = PrivateVariables.adsRemovalMonthlyProductId(); - String weeklyProduct = PrivateVariables.adsRemovalWeeklyProductId(); - String[] productIds = Utils.concatArrays(PrivateVariables.adsRemovalNotUsedList(), - yearlyProduct, monthlyProduct, weeklyProduct); - return new SubscriptionPurchaseController(validator, billingManager, - SubscriptionType.ADS_REMOVAL, productIds); + return createSubscriptionPurchaseController(context, SubscriptionType.ADS_REMOVAL); + } + + public static PurchaseController createBookmarksSubscriptionPurchaseController( + @NonNull Context context) + { + return createSubscriptionPurchaseController(context, SubscriptionType.BOOKMARKS); } @NonNull - public static PurchaseController createBookmarksSubscriptionPurchaseController( - @NonNull Context context) + private static PurchaseController createSubscriptionPurchaseController( + @NonNull Context context, @NonNull SubscriptionType type) { BillingManager billingManager = new PlayStoreBillingManager(BillingClient.SkuType.SUBS); PurchaseOperationObservable observable = PurchaseOperationObservable.from(context); PurchaseValidator validator = new DefaultPurchaseValidator(observable); - String yearlyProduct = PrivateVariables.bookmarksSubscriptionYearlyProductId(); - String monthlyProduct = PrivateVariables.bookmarksSubscriptionMonthlyProductId(); - String[] productIds = Utils.concatArrays(PrivateVariables.bookmarksSubscriptionNotUsedList(), - yearlyProduct, monthlyProduct); - return new SubscriptionPurchaseController(validator, billingManager, - SubscriptionType.BOOKMARKS, productIds); + String[] productIds = type.getProductIds(); + return new SubscriptionPurchaseController(validator, billingManager, type, productIds); } @NonNull - public static PurchaseController createBookmarkPurchaseController( + static PurchaseController createBookmarkPurchaseController( @NonNull Context context, @Nullable String productId, @Nullable String serverId) { BillingManager billingManager @@ -60,13 +49,6 @@ public class PurchaseFactory return new BookmarkPurchaseController(validator, billingManager, productId, serverId); } - @NonNull - public static PurchaseController createBookmarkPurchaseController( - @NonNull Context context) - { - return createBookmarkPurchaseController(context, null, null); - } - @NonNull public static PurchaseController createFailedBookmarkPurchaseController( @NonNull Context context) @@ -79,14 +61,13 @@ public class PurchaseFactory } @NonNull - public static BillingManager createInAppBillingManager( - @NonNull Context context) + public static BillingManager createInAppBillingManager() { return new PlayStoreBillingManager(BillingClient.SkuType.INAPP); } @NonNull - public static BillingManager createSubscriptionBillingManager() + static BillingManager createSubscriptionBillingManager() { return new PlayStoreBillingManager(BillingClient.SkuType.SUBS); } diff --git a/android/src/com/mapswithme/maps/purchase/SubscriptionType.java b/android/src/com/mapswithme/maps/purchase/SubscriptionType.java index 4c6d910a8a..f17901e948 100644 --- a/android/src/com/mapswithme/maps/purchase/SubscriptionType.java +++ b/android/src/com/mapswithme/maps/purchase/SubscriptionType.java @@ -1,8 +1,8 @@ package com.mapswithme.maps.purchase; import androidx.annotation.NonNull; - import com.mapswithme.maps.PrivateVariables; +import com.mapswithme.util.Utils; public enum SubscriptionType { @@ -21,6 +21,16 @@ public enum SubscriptionType { return PrivateVariables.adsRemovalVendor(); } + + @NonNull + @Override + String[] getProductIds() + { + return Utils.concatArrays(PrivateVariables.adsRemovalNotUsedList(), + PrivateVariables.adsRemovalYearlyProductId(), + PrivateVariables.adsRemovalMonthlyProductId(), + PrivateVariables.adsRemovalWeeklyProductId()); + } }, BOOKMARKS { @@ -37,6 +47,15 @@ public enum SubscriptionType { return PrivateVariables.bookmarksSubscriptionVendor(); } + + @NonNull + @Override + String[] getProductIds() + { + return Utils.concatArrays(PrivateVariables.bookmarksSubscriptionNotUsedList(), + PrivateVariables.bookmarksSubscriptionYearlyProductId(), + PrivateVariables.bookmarksSubscriptionMonthlyProductId()); + } }; @NonNull @@ -44,4 +63,7 @@ public enum SubscriptionType @NonNull abstract String getVendor(); + + @NonNull + abstract String[] getProductIds(); } diff --git a/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java b/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java index 745162377d..720b95e462 100644 --- a/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java +++ b/android/src/com/mapswithme/maps/settings/SettingsPrefsFragment.java @@ -296,15 +296,16 @@ public class SettingsPrefsFragment extends BaseXmlSettingsFragment @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - mAdsRemovalPurchaseController = PurchaseFactory.createAdsRemovalPurchaseController(getContext()); - mAdsRemovalPurchaseController.initialize(getActivity()); + mAdsRemovalPurchaseController = PurchaseFactory.createAdsRemovalPurchaseController(requireContext()); + mAdsRemovalPurchaseController.initialize(requireActivity()); return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onDestroyView() { - mAdsRemovalPurchaseController.destroy(); + if (mAdsRemovalPurchaseController != null) + mAdsRemovalPurchaseController.destroy(); super.onDestroyView(); }