diff --git a/OsmAnd/res/layout/purchases_layout.xml b/OsmAnd/res/layout/purchases_layout.xml index 306f37f690..2140d2cc5c 100644 --- a/OsmAnd/res/layout/purchases_layout.xml +++ b/OsmAnd/res/layout/purchases_layout.xml @@ -1,6 +1,8 @@ + + + + + + + + + + + + + + + + + + getEverMadeSubscriptions() { + List subscriptions = new ArrayList<>(); + for (InAppSubscription subscription : getLiveUpdates().getVisibleSubscriptions()) { + SubscriptionState state = subscription.getState(); + if (state != null && !SubscriptionState.UNDEFINED.equals(state)) { + subscriptions.add(subscription); + } + } + return subscriptions; + } + public abstract void isInAppPurchaseSupported(@NonNull final Activity activity, @Nullable final InAppPurchaseInitCallback callback); public boolean hasInventory() { diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/CountrySelectionFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/CountrySelectionFragment.java index f070559121..9c29e9762b 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/CountrySelectionFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/CountrySelectionFragment.java @@ -35,6 +35,8 @@ import java.util.List; public class CountrySelectionFragment extends BaseOsmAndDialogFragment { + public static final String TAG = CountrySelectionFragment.class.getSimpleName(); + private List countryItems = new ArrayList<>(); private OnFragmentInteractionListener mListener; diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java index 02bdad0a43..631b1a7f07 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/LiveUpdatesFragment.java @@ -49,6 +49,7 @@ import net.osmand.plus.inapp.InAppPurchaseHelper.InAppPurchaseListener; import net.osmand.plus.inapp.InAppPurchaseHelper.InAppPurchaseTaskType; import net.osmand.plus.inapp.InAppPurchases.InAppSubscription; import net.osmand.plus.resources.IncrementalChangesManager; +import net.osmand.plus.settings.fragments.PurchasesFragment; import net.osmand.util.Algorithms; import java.io.File; @@ -181,28 +182,9 @@ public class LiveUpdatesFragment extends BaseOsmAndFragment implements InAppPurc statusTextView.setText(getString(R.string.osm_live_active)); statusIcon.setImageDrawable(app.getUIUtilities().getThemedIcon(R.drawable.ic_action_done)); - regionNameHeaderTextView.setText(R.string.osm_live_support_region); - String countryName = app.getSettings().BILLING_USER_COUNTRY.get(); - InAppPurchaseHelper purchaseHelper = getInAppPurchaseHelper(); - if (purchaseHelper != null) { - InAppSubscription monthlyPurchased = purchaseHelper.getPurchasedMonthlyLiveUpdates(); - if (monthlyPurchased != null && monthlyPurchased.isDonationSupported()) { - if (Algorithms.isEmpty(countryName)) { - if (app.getSettings().BILLING_USER_COUNTRY_DOWNLOAD_NAME.get().equals(OsmandSettings.BILLING_USER_DONATION_NONE_PARAMETER)) { - regionNameHeaderTextView.setText(R.string.default_buttons_support); - countryName = getString(R.string.osmand_team); - } else { - countryName = getString(R.string.shared_string_world); - } - } - } else { - regionNameHeaderTextView.setText(R.string.default_buttons_support); - countryName = getString(R.string.osmand_team); - } - } else { - regionNameHeaderTextView.setText(R.string.default_buttons_support); - countryName = getString(R.string.osmand_team); - } + String countryName = PurchasesFragment.getSupportRegionName(app, getInAppPurchaseHelper()); + String header = PurchasesFragment.getSupportRegionHeader(app, countryName); + regionNameHeaderTextView.setText(header); regionNameTextView.setText(countryName); View subscriptionsButton = subscriptionHeader.findViewById(R.id.button_subscriptions); diff --git a/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java b/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java index a2afb3d933..b73bf20c6e 100644 --- a/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java +++ b/OsmAnd/src/net/osmand/plus/liveupdates/SubscriptionFragment.java @@ -177,7 +177,7 @@ public class SubscriptionFragment extends BaseOsmAndDialogFragment implements In CountrySelectionFragment countryCountrySelectionFragment = countrySelectionFragment; countryCountrySelectionFragment - .show(getChildFragmentManager(), "CountriesSearchSelectionFragment"); + .show(getChildFragmentManager(), CountrySelectionFragment.TAG); } return false; } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/PurchasesFragment.java b/OsmAnd/src/net/osmand/plus/settings/fragments/PurchasesFragment.java index 6b5a9c5d5f..af834f73b8 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/PurchasesFragment.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/PurchasesFragment.java @@ -21,11 +21,6 @@ import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.fragment.app.FragmentActivity; -import androidx.fragment.app.FragmentManager; - import com.google.android.material.appbar.AppBarLayout; import net.osmand.AndroidUtils; @@ -39,21 +34,37 @@ import net.osmand.plus.base.BaseOsmAndFragment; import net.osmand.plus.chooseplan.ChoosePlanDialogFragment; import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.inapp.InAppPurchaseHelper.InAppPurchaseListener; +import net.osmand.plus.inapp.InAppPurchases.InAppSubscription; +import net.osmand.plus.liveupdates.CountrySelectionFragment; +import net.osmand.plus.liveupdates.CountrySelectionFragment.CountryItem; +import net.osmand.plus.liveupdates.CountrySelectionFragment.OnFragmentInteractionListener; import net.osmand.plus.liveupdates.LiveUpdatesFragmentNew; import net.osmand.plus.liveupdates.OsmLiveActivity; +import net.osmand.plus.settings.backend.OsmandSettings; import net.osmand.plus.wikipedia.WikipediaDialogFragment; +import net.osmand.util.Algorithms; -public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurchaseListener { +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.fragment.app.FragmentActivity; +import androidx.fragment.app.FragmentManager; + +public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurchaseListener, OnFragmentInteractionListener { public static final String TAG = PurchasesFragment.class.getName(); public static final String KEY_IS_SUBSCRIBER = "action_is_new"; private static final String PLAY_STORE_SUBSCRIPTION_URL = "https://play.google.com/store/account/subscriptions"; private static final String PLAY_STORE_SUBSCRIPTION_DEEPLINK_URL = "https://play.google.com/store/account/subscriptions?sku=%s&package=%s"; + + private OsmandApplication app; + private Context context; private InAppPurchaseHelper purchaseHelper; + private View mainView; private SubscriptionsCard subscriptionsCard; - private Context context; - private OsmandApplication app; + + private CountrySelectionFragment countrySelectionFragment = new CountrySelectionFragment(); + private String url; private Boolean isSubscriber; @@ -79,20 +90,14 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { app = getMyApplication(); context = requireContext(); - purchaseHelper = getInAppPurchaseHelper(); isSubscriber = Version.isPaidVersion(app); - final MapActivity mapActivity = (MapActivity) getActivity(); - final boolean nightMode = !getMyApplication().getSettings().isLightContent(); + final MapActivity mapActivity = getMapActivity(); + final boolean nightMode = !app.getSettings().isLightContent(); LayoutInflater themedInflater = UiUtilities.getInflater(context, nightMode); if (isSubscriber) { mainView = themedInflater.inflate(R.layout.purchases_layout, container, false); setSubscriptionClick(mapActivity); - if (mapActivity != null && purchaseHelper != null) { - ViewGroup subscriptionsCardContainer = mainView.findViewById(R.id.subscriptions_card_container); - subscriptionsCard = new SubscriptionsCard(mapActivity, purchaseHelper); - subscriptionsCardContainer.addView(subscriptionsCard.build(mapActivity)); - } } else { mainView = themedInflater.inflate(R.layout.empty_purchases_layout, container, false); LinearLayout osmandLive = mainView.findViewById(R.id.osmand_live); @@ -138,6 +143,21 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha return mainView; } + @Override + public void onResume() { + super.onResume(); + purchaseHelper = getInAppPurchaseHelper(); + if (isSubscriber) { + MapActivity mapActivity = getMapActivity(); + if (getMapActivity() != null && purchaseHelper != null) { + ViewGroup subscriptionsCardContainer = mainView.findViewById(R.id.subscriptions_card_container); + subscriptionsCard = new SubscriptionsCard(mapActivity, purchaseHelper); + subscriptionsCardContainer.addView(subscriptionsCard.build(mapActivity)); + } + setupSupportRegion(); + } + } + @Override public void onSaveInstanceState(@NonNull Bundle outState) { super.onSaveInstanceState(outState); @@ -189,6 +209,54 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha } + private void setupSupportRegion() { + String region = getSupportRegionName(app, purchaseHelper); + String header = getSupportRegionHeader(app, region); + TextView supportRegionHeader = mainView.findViewById(R.id.support_region_header); + TextView supportRegion = mainView.findViewById(R.id.support_region); + supportRegionHeader.setText(header); + supportRegion.setText(region); + + View supportRegionContainer = mainView.findViewById(R.id.support_region_container); + supportRegionContainer.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + CountrySelectionFragment countryCountrySelectionFragment = countrySelectionFragment; + countryCountrySelectionFragment.show(getChildFragmentManager(), CountrySelectionFragment.TAG); + } + }); + + countrySelectionFragment.initCountries(app); + } + + public static String getSupportRegionName(OsmandApplication app, InAppPurchaseHelper purchaseHelper) { + OsmandSettings settings = app.getSettings(); + String countryName = settings.BILLING_USER_COUNTRY.get(); + if (purchaseHelper != null) { + InAppSubscription monthlyPurchased = purchaseHelper.getPurchasedMonthlyLiveUpdates(); + if (monthlyPurchased != null && monthlyPurchased.isDonationSupported()) { + if (Algorithms.isEmpty(countryName)) { + if (OsmandSettings.BILLING_USER_DONATION_NONE_PARAMETER.equals(settings.BILLING_USER_COUNTRY_DOWNLOAD_NAME.get())) { + countryName = app.getString(R.string.osmand_team); + } else { + countryName = app.getString(R.string.shared_string_world); + } + } + } else { + countryName = app.getString(R.string.osmand_team); + } + } else { + countryName = app.getString(R.string.osmand_team); + } + return countryName; + } + + public static String getSupportRegionHeader(OsmandApplication app, String supportRegion) { + return supportRegion.equals(app.getString(R.string.osmand_team)) ? + app.getString(R.string.default_buttons_support) : + app.getString(R.string.osm_live_support_region); + } + @Nullable public InAppPurchaseHelper getInAppPurchaseHelper() { Activity activity = getActivity(); @@ -263,6 +331,23 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha } } + @Override + public void onSearchResult(CountryItem selectedCountryItem) { + String countryName = selectedCountryItem != null ? selectedCountryItem.getLocalName() : ""; + String countryDownloadName = selectedCountryItem != null ? + selectedCountryItem.getDownloadName() : OsmandSettings.BILLING_USER_DONATION_WORLD_PARAMETER; + + OsmandApplication app = getMyApplication(); + if (app != null) { + TextView supportRegionHeader = mainView.findViewById(R.id.support_region_header); + TextView supportRegion = mainView.findViewById(R.id.support_region); + supportRegionHeader.setText(getSupportRegionHeader(app, countryName)); + supportRegion.setText(countryName); + app.getSettings().BILLING_USER_COUNTRY.set(countryName); + app.getSettings().BILLING_USER_COUNTRY_DOWNLOAD_NAME.set(countryDownloadName); + } + } + @Override public void onError(InAppPurchaseHelper.InAppPurchaseTaskType taskType, String error) { } @@ -288,4 +373,8 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha @Override public void dismissProgress(InAppPurchaseHelper.InAppPurchaseTaskType taskType) { } + + private MapActivity getMapActivity() { + return (MapActivity) getActivity(); + } } diff --git a/OsmAnd/src/net/osmand/plus/settings/fragments/SubscriptionsCard.java b/OsmAnd/src/net/osmand/plus/settings/fragments/SubscriptionsCard.java index 40ef081602..c8c0d5202b 100644 --- a/OsmAnd/src/net/osmand/plus/settings/fragments/SubscriptionsCard.java +++ b/OsmAnd/src/net/osmand/plus/settings/fragments/SubscriptionsCard.java @@ -45,11 +45,11 @@ public class SubscriptionsCard extends BaseCard { @Override protected void updateContent() { - if (mapActivity == null) { + if (mapActivity == null || purchaseHelper == null) { return; } - List subscriptions = getActiveAndCancelledSubscriptions(); + List subscriptions = purchaseHelper.getEverMadeSubscriptions(); if (Algorithms.isEmpty(subscriptions)) { return; } @@ -162,18 +162,4 @@ public class SubscriptionsCard extends BaseCard { } return ""; } - - private List getActiveAndCancelledSubscriptions() { - List subscriptions = new ArrayList<>(); - for (InAppSubscription subscription : purchaseHelper.getLiveUpdates().getVisibleSubscriptions()) { - if (shouldShowSubscription(subscription)) { - subscriptions.add(subscription); - } - } - return subscriptions; - } - - private boolean shouldShowSubscription(InAppSubscription s) { - return s.getState() != null && !SubscriptionState.UNDEFINED.equals(s.getState()); - } } \ No newline at end of file