Small fixes and corrections

This commit is contained in:
cepprice 2021-03-26 11:01:37 +05:00
parent 837176a3d1
commit 67449365b4
10 changed files with 119 additions and 125 deletions

View file

@ -1,20 +1,23 @@
package net.osmand; package net.osmand;
import java.text.ParseException; import java.text.ParseException;
import java.util.Calendar;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class Period { public class Period {
public enum PeriodUnit { public enum PeriodUnit {
YEAR("Y"), YEAR("Y", Calendar.YEAR),
MONTH("M"), MONTH("M", Calendar.MONTH),
WEEK("W"), WEEK("W", Calendar.WEEK_OF_YEAR),
DAY("D"); DAY("D", Calendar.DATE);
private String unitStr; private String unitStr;
private int calendarIdx;
PeriodUnit(String unitStr) { PeriodUnit(String unitStr, int calendarIdx) {
this.calendarIdx = calendarIdx;
this.unitStr = unitStr; this.unitStr = unitStr;
} }
@ -22,6 +25,10 @@ public class Period {
return unitStr; return unitStr;
} }
public int getCalendarIdx() {
return calendarIdx;
}
public double getMonthsValue() { public double getMonthsValue() {
switch (this) { switch (this) {
case YEAR: case YEAR:

View file

@ -4,7 +4,7 @@
xmlns:osmand="http://schemas.android.com/apk/res-auto" xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:background="?attr/bg_color" android:background="?attr/bg_color"
android:orientation="vertical"> android:orientation="vertical">

View file

@ -7,7 +7,6 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:orientation="horizontal"
android:background="@android:drawable/list_selector_background"
android:paddingStart="@dimen/content_padding" android:paddingStart="@dimen/content_padding"
android:paddingLeft="@dimen/content_padding" android:paddingLeft="@dimen/content_padding"
android:paddingRight="@dimen/content_padding" android:paddingRight="@dimen/content_padding"

View file

@ -2,15 +2,15 @@ package net.osmand.plus.inapp;
import android.content.Context; import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.android.billingclient.api.SkuDetails; import com.android.billingclient.api.SkuDetails;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class InAppPurchasesImpl extends InAppPurchases { public class InAppPurchasesImpl extends InAppPurchases {
private static final InAppPurchase FULL_VERSION = new InAppPurchaseFullVersion(); private static final InAppPurchase FULL_VERSION = new InAppPurchaseFullVersion();
@ -285,6 +285,11 @@ public class InAppPurchasesImpl extends InAppPurchases {
this.details = details; this.details = details;
} }
@Override
public int getPeriodTypeString() {
return R.string.monthly_subscription;
}
@Override @Override
public String getDefaultPrice(Context ctx) { public String getDefaultPrice(Context ctx) {
return ""; return "";

View file

@ -22,17 +22,6 @@ import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
@ -59,6 +48,17 @@ import org.apache.commons.logging.Log;
import java.util.List; import java.util.List;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
public abstract class ChoosePlanDialogFragment extends BaseOsmAndDialogFragment implements InAppPurchaseListener { public abstract class ChoosePlanDialogFragment extends BaseOsmAndDialogFragment implements InAppPurchaseListener {
public static final String TAG = ChoosePlanDialogFragment.class.getSimpleName(); public static final String TAG = ChoosePlanDialogFragment.class.getSimpleName();
private static final Log LOG = PlatformUtil.getLog(ChoosePlanDialogFragment.class); private static final Log LOG = PlatformUtil.getLog(ChoosePlanDialogFragment.class);
@ -489,14 +489,18 @@ public abstract class ChoosePlanDialogFragment extends BaseOsmAndDialogFragment
buttonView.setOnClickListener(new OnClickListener() { buttonView.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
subscribe(s.getSku()); if (getActivity() != null) {
subscribe(app, getActivity(), purchaseHelper, s.getSku());
}
} }
}); });
} else { } else {
buttonExView.setOnClickListener(new OnClickListener() { buttonExView.setOnClickListener(new OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
subscribe(s.getSku()); if (getActivity() != null) {
subscribe(app, getActivity(), purchaseHelper, s.getSku());
}
} }
}); });
} }
@ -526,14 +530,14 @@ public abstract class ChoosePlanDialogFragment extends BaseOsmAndDialogFragment
} }
} }
private void subscribe(String sku) { public static void subscribe(@NonNull OsmandApplication app, Activity activity,
InAppPurchaseHelper purchaseHelper, String sku) {
if (!app.getSettings().isInternetConnectionAvailable(true)) { if (!app.getSettings().isInternetConnectionAvailable(true)) {
Toast.makeText(app, R.string.internet_not_available, Toast.LENGTH_LONG).show(); Toast.makeText(app, R.string.internet_not_available, Toast.LENGTH_LONG).show();
} else { } else {
FragmentActivity ctx = getActivity(); if (activity != null && purchaseHelper != null) {
if (ctx != null && purchaseHelper != null) {
OsmandSettings settings = app.getSettings(); OsmandSettings settings = app.getSettings();
purchaseHelper.purchaseLiveUpdates(ctx, sku, purchaseHelper.purchaseLiveUpdates(activity, sku,
settings.BILLING_USER_EMAIL.get(), settings.BILLING_USER_EMAIL.get(),
settings.BILLING_USER_NAME.get(), settings.BILLING_USER_NAME.get(),
settings.BILLING_USER_COUNTRY_DOWNLOAD_NAME.get(), settings.BILLING_USER_COUNTRY_DOWNLOAD_NAME.get(),

View file

@ -8,9 +8,6 @@ import android.os.AsyncTask;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import net.osmand.AndroidNetworkUtils; import net.osmand.AndroidNetworkUtils;
import net.osmand.AndroidNetworkUtils.OnRequestResultListener; import net.osmand.AndroidNetworkUtils.OnRequestResultListener;
import net.osmand.AndroidNetworkUtils.OnRequestsResultListener; import net.osmand.AndroidNetworkUtils.OnRequestsResultListener;
@ -43,6 +40,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public abstract class InAppPurchaseHelper { public abstract class InAppPurchaseHelper {
// Debug tag, for logging // Debug tag, for logging
protected static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(InAppPurchaseHelper.class); protected static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(InAppPurchaseHelper.class);
@ -203,7 +203,7 @@ public abstract class InAppPurchaseHelper {
List<InAppSubscription> subscriptions = new ArrayList<>(); List<InAppSubscription> subscriptions = new ArrayList<>();
for (InAppSubscription subscription : getLiveUpdates().getVisibleSubscriptions()) { for (InAppSubscription subscription : getLiveUpdates().getVisibleSubscriptions()) {
SubscriptionState state = subscription.getState(); SubscriptionState state = subscription.getState();
if (state != null && !SubscriptionState.UNDEFINED.equals(state)) { if (state != SubscriptionState.UNDEFINED) {
subscriptions.add(subscription); subscriptions.add(subscription);
} }
} }

View file

@ -7,12 +7,6 @@ import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.Period; import net.osmand.Period;
import net.osmand.Period.PeriodUnit; import net.osmand.Period.PeriodUnit;
@ -34,6 +28,12 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
public abstract class InAppPurchases { public abstract class InAppPurchases {
protected InAppPurchase fullVersion; protected InAppPurchase fullVersion;
@ -645,6 +645,9 @@ public abstract class InAppPurchases {
this.skuNoVersion = sku; this.skuNoVersion = sku;
} }
@StringRes
public abstract int getPeriodTypeString();
@NonNull @NonNull
private List<InAppSubscription> getUpgrades() { private List<InAppSubscription> getUpgrades() {
return new ArrayList<>(upgrades.values()); return new ArrayList<>(upgrades.values());
@ -835,6 +838,11 @@ public abstract class InAppPurchases {
this(sku, false); this(sku, false);
} }
@Override
public int getPeriodTypeString() {
return R.string.monthly_subscription;
}
@Override @Override
public void setPriceValue(double priceValue) { public void setPriceValue(double priceValue) {
super.setPriceValue(priceValue); super.setPriceValue(priceValue);
@ -883,6 +891,11 @@ public abstract class InAppPurchases {
super(sku, false); super(sku, false);
} }
@Override
public int getPeriodTypeString() {
return R.string.three_months_subscription;
}
@Override @Override
public void setPriceValue(double priceValue) { public void setPriceValue(double priceValue) {
super.setPriceValue(priceValue); super.setPriceValue(priceValue);
@ -926,6 +939,11 @@ public abstract class InAppPurchases {
super(sku, false); super(sku, false);
} }
@Override
public int getPeriodTypeString() {
return R.string.annual_subscription;
}
@Override @Override
public void setPriceValue(double priceValue) { public void setPriceValue(double priceValue) {
super.setPriceValue(priceValue); super.setPriceValue(priceValue);

View file

@ -24,6 +24,7 @@ import android.widget.TextView;
import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.AppBarLayout;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities; import net.osmand.plus.UiUtilities;
@ -44,6 +45,8 @@ import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.plus.wikipedia.WikipediaDialogFragment; import net.osmand.plus.wikipedia.WikipediaDialogFragment;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity; import androidx.fragment.app.FragmentActivity;
@ -51,10 +54,17 @@ import androidx.fragment.app.FragmentManager;
public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurchaseListener, OnFragmentInteractionListener { public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurchaseListener, OnFragmentInteractionListener {
private static final Log log = PlatformUtil.getLog(PurchasesFragment.class);
public static final String TAG = PurchasesFragment.class.getName(); public static final String TAG = PurchasesFragment.class.getName();
public static final String KEY_IS_SUBSCRIBER = "action_is_new"; 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_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 static final String PLAY_STORE_SUBSCRIPTION_DEEPLINK_URL = "https://play.google.com/store/account/subscriptions?sku=%s&package=%s";
private static final String EMAIL_DEEPLINK_URI = "mailto:support@osmand.net";
private static final String OSMAND_EMAIL = "support@osmand.net";
private static final String OSMAND_NEW_DEVICE_URL = "https://docs.osmand.net/en/main@latest/osmand/purchases#new-device--new-account";
private static final String OSMAND_PURCHASES_URL = "https://docs.osmand.net/en/main@latest/osmand/purchases";
private OsmandApplication app; private OsmandApplication app;
private Context context; private Context context;
@ -66,17 +76,18 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha
private CountrySelectionFragment countrySelectionFragment = new CountrySelectionFragment(); private CountrySelectionFragment countrySelectionFragment = new CountrySelectionFragment();
private String url; private String url;
private Boolean isSubscriber; private Boolean isPaidVersion;
public static boolean showInstance(FragmentManager fragmentManager) { public static boolean showInstance(FragmentManager fragmentManager) {
try { try {
PurchasesFragment fragment = new PurchasesFragment(); PurchasesFragment fragment = new PurchasesFragment();
fragmentManager.beginTransaction() fragmentManager.beginTransaction()
.add(R.id.fragmentContainer, fragment, TAG) .replace(R.id.fragmentContainer, fragment, TAG)
.addToBackStack(TAG) .addToBackStack(TAG)
.commitAllowingStateLoss(); .commitAllowingStateLoss();
return true; return true;
} catch (Exception e) { } catch (Exception e) {
log.error(e);
return false; return false;
} }
} }
@ -90,12 +101,12 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
app = getMyApplication(); app = getMyApplication();
context = requireContext(); context = requireContext();
isSubscriber = Version.isPaidVersion(app); isPaidVersion = Version.isPaidVersion(app);
final MapActivity mapActivity = getMapActivity(); final MapActivity mapActivity = getMapActivity();
final boolean nightMode = !app.getSettings().isLightContent(); final boolean nightMode = !app.getSettings().isLightContent();
LayoutInflater themedInflater = UiUtilities.getInflater(context, nightMode); LayoutInflater themedInflater = UiUtilities.getInflater(context, nightMode);
if (isSubscriber) { if (isPaidVersion) {
mainView = themedInflater.inflate(R.layout.purchases_layout, container, false); mainView = themedInflater.inflate(R.layout.purchases_layout, container, false);
setSubscriptionClick(mapActivity); setSubscriptionClick(mapActivity);
} else { } else {
@ -117,11 +128,6 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha
} }
AndroidUtils.addStatusBarPadding21v(getActivity(), mainView); AndroidUtils.addStatusBarPadding21v(getActivity(), mainView);
createToolbar(mainView, nightMode); createToolbar(mainView, nightMode);
mainView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
LinearLayout purchasesRestore = mainView.findViewById(R.id.restore_purchases); LinearLayout purchasesRestore = mainView.findViewById(R.id.restore_purchases);
purchasesRestore.setOnClickListener(new View.OnClickListener() { purchasesRestore.setOnClickListener(new View.OnClickListener() {
@Override @Override
@ -135,7 +141,7 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha
newDeviceAccountContainer.setOnClickListener(new View.OnClickListener() { newDeviceAccountContainer.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
WikipediaDialogFragment.showFullArticle(context, Uri.parse("https://docs.osmand.net/en/main@latest/osmand/purchases#new-device--new-account"), nightMode); WikipediaDialogFragment.showFullArticle(context, Uri.parse(OSMAND_NEW_DEVICE_URL), nightMode);
} }
}); });
@ -147,10 +153,11 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
purchaseHelper = getInAppPurchaseHelper(); purchaseHelper = getInAppPurchaseHelper();
if (isSubscriber) { if (isPaidVersion) {
MapActivity mapActivity = getMapActivity(); MapActivity mapActivity = getMapActivity();
if (getMapActivity() != null && purchaseHelper != null) { if (getMapActivity() != null && purchaseHelper != null) {
ViewGroup subscriptionsCardContainer = mainView.findViewById(R.id.subscriptions_card_container); ViewGroup subscriptionsCardContainer = mainView.findViewById(R.id.subscriptions_card_container);
subscriptionsCardContainer.removeAllViews();
subscriptionsCard = new SubscriptionsCard(mapActivity, purchaseHelper); subscriptionsCard = new SubscriptionsCard(mapActivity, purchaseHelper);
subscriptionsCardContainer.addView(subscriptionsCard.build(mapActivity)); subscriptionsCardContainer.addView(subscriptionsCard.build(mapActivity));
} }
@ -161,7 +168,7 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha
@Override @Override
public void onSaveInstanceState(@NonNull Bundle outState) { public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
outState.putBoolean(KEY_IS_SUBSCRIBER, isSubscriber); outState.putBoolean(KEY_IS_SUBSCRIBER, isPaidVersion);
} }
@Override @Override
@ -277,7 +284,7 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha
icon.setOnClickListener(new View.OnClickListener() { icon.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
WikipediaDialogFragment.showFullArticle(context, Uri.parse("https://docs.osmand.net/en/main@latest/osmand/purchases"), nightMode); WikipediaDialogFragment.showFullArticle(context, Uri.parse(OSMAND_PURCHASES_URL), nightMode);
} }
}); });
ImageButton backButton = toolbar.findViewById(R.id.close_button); ImageButton backButton = toolbar.findViewById(R.id.close_button);
@ -301,14 +308,12 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha
TextView supportDescription = mainView.findViewById(R.id.support_link_title); TextView supportDescription = mainView.findViewById(R.id.support_link_title);
SpannableString spannableStringSupport = new SpannableString(getString(R.string.contact_support)); SpannableString spannableStringSupport = new SpannableString(getString(R.string.contact_support));
String urlSupport = "mailto:support@osmand.net"; spannableStringSupport.setSpan(new URLSpan(EMAIL_DEEPLINK_URI), 0, spannableStringSupport.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableStringSupport.setSpan(new URLSpan(urlSupport), 0, spannableStringSupport.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
String emailString = "support@osmand.net"; String supportDescriptionString = getString(R.string.contact_support_description, OSMAND_EMAIL);
String supportDescriptionString = getString(R.string.contact_support_description, emailString);
SpannableString spannableStringMail = new SpannableString(supportDescriptionString); SpannableString spannableStringMail = new SpannableString(supportDescriptionString);
int startIndex = supportDescriptionString.indexOf(emailString); int startIndex = supportDescriptionString.indexOf(OSMAND_EMAIL);
int endIndex = startIndex + emailString.length(); int endIndex = startIndex + OSMAND_EMAIL.length();
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD); StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
spannableStringMail.setSpan(boldSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannableStringMail.setSpan(boldSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
@ -322,7 +327,7 @@ public class PurchasesFragment extends BaseOsmAndFragment implements InAppPurcha
private void getSkuAppId() { private void getSkuAppId() {
InAppPurchaseHelper purchaseHelper = app.getInAppPurchaseHelper(); InAppPurchaseHelper purchaseHelper = app.getInAppPurchaseHelper();
if (purchaseHelper != null) { if (purchaseHelper != null && purchaseHelper.getFullVersion() != null) {
String sku = purchaseHelper.getFullVersion().getSku(); String sku = purchaseHelper.getFullVersion().getSku();
url = String.format(PLAY_STORE_SUBSCRIPTION_DEEPLINK_URL, url = String.format(PLAY_STORE_SUBSCRIPTION_DEEPLINK_URL,
sku, context.getPackageName()); sku, context.getPackageName());

View file

@ -5,34 +5,34 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.Period; import net.osmand.Period;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.UiUtilities;
import net.osmand.plus.activities.MapActivity; import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.chooseplan.ChoosePlanDialogFragment;
import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.inapp.InAppPurchaseHelper;
import net.osmand.plus.inapp.InAppPurchases.InAppSubscription; import net.osmand.plus.inapp.InAppPurchases.InAppSubscription;
import net.osmand.plus.inapp.InAppPurchases.InAppSubscription.SubscriptionState; import net.osmand.plus.inapp.InAppPurchases.InAppSubscription.SubscriptionState;
import net.osmand.plus.routepreparationmenu.cards.BaseCard; import net.osmand.plus.routepreparationmenu.cards.BaseCard;
import net.osmand.plus.settings.backend.OsmandSettings;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Locale;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.view.ContextThemeWrapper;
import androidx.core.content.ContextCompat;
public class SubscriptionsCard extends BaseCard { public class SubscriptionsCard extends BaseCard {
private final InAppPurchaseHelper purchaseHelper; private final InAppPurchaseHelper purchaseHelper;
private final SimpleDateFormat dateFormat;
@Override @Override
public int getCardLayoutId() { public int getCardLayoutId() {
return R.layout.subscriptions_card; return R.layout.subscriptions_card;
@ -41,33 +41,29 @@ public class SubscriptionsCard extends BaseCard {
public SubscriptionsCard(@NonNull MapActivity mapActivity, @NonNull InAppPurchaseHelper purchaseHelper) { public SubscriptionsCard(@NonNull MapActivity mapActivity, @NonNull InAppPurchaseHelper purchaseHelper) {
super(mapActivity); super(mapActivity);
this.purchaseHelper = purchaseHelper; this.purchaseHelper = purchaseHelper;
this.dateFormat = new SimpleDateFormat("MMM d, yyyy", Locale.getDefault());
} }
@Override @Override
protected void updateContent() { protected void updateContent() {
if (mapActivity == null || purchaseHelper == null) { if (purchaseHelper == null || Algorithms.isEmpty(purchaseHelper.getEverMadeSubscriptions())) {
return; return;
} }
List<InAppSubscription> subscriptions = purchaseHelper.getEverMadeSubscriptions(); LayoutInflater inflater = UiUtilities.getInflater(mapActivity, nightMode);
if (Algorithms.isEmpty(subscriptions)) {
return;
}
ContextThemeWrapper ctx = new ContextThemeWrapper(mapActivity, !nightMode ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme);
LayoutInflater inflater = LayoutInflater.from(ctx);
((ViewGroup) view).removeAllViews(); ((ViewGroup) view).removeAllViews();
List<InAppSubscription> subscriptions = purchaseHelper.getEverMadeSubscriptions();
for (int i = 0; i < subscriptions.size(); i++) { for (int i = 0; i < subscriptions.size(); i++) {
InAppSubscription subscription = subscriptions.get(i); InAppSubscription subscription = subscriptions.get(i);
SubscriptionState state = subscription.getState(); SubscriptionState state = subscription.getState();
boolean autoRenewed = SubscriptionState.ACTIVE.equals(state) || SubscriptionState.IN_GRACE_PERIOD.equals(state); boolean autoRenewed = state == SubscriptionState.ACTIVE || state == SubscriptionState.IN_GRACE_PERIOD;
View card = inflater.inflate(R.layout.subscription_layout, null, false); View card = inflater.inflate(R.layout.subscription_layout, null, false);
((ViewGroup) view).addView(card); ((ViewGroup) view).addView(card);
TextView subscriptionPeriod = card.findViewById(R.id.subscription_type); TextView subscriptionPeriod = card.findViewById(R.id.subscription_type);
String period = getSubscriptionPeriod(subscription.getSubscriptionPeriod()); String period = app.getString(subscription.getPeriodTypeString());
if (!Algorithms.isEmpty(period)) { if (!Algorithms.isEmpty(period)) {
subscriptionPeriod.setText(period); subscriptionPeriod.setText(period);
AndroidUiHelper.updateVisibility(subscriptionPeriod, true); AndroidUiHelper.updateVisibility(subscriptionPeriod, true);
@ -80,33 +76,31 @@ public class SubscriptionsCard extends BaseCard {
nextBillingDate.setText(app.getString(R.string.next_billing_date, date)); nextBillingDate.setText(app.getString(R.string.next_billing_date, date));
AndroidUiHelper.updateVisibility(nextBillingDate, true); AndroidUiHelper.updateVisibility(nextBillingDate, true);
} }
} } else {
TextView status = card.findViewById(R.id.status);
status.setText(app.getString(state.getStringRes()));
status.setBackgroundDrawable(ContextCompat.getDrawable(mapActivity, state.getBackgroundRes()));
if (!autoRenewed) {
View renewContainer = card.findViewById(R.id.renewContainer); View renewContainer = card.findViewById(R.id.renewContainer);
AndroidUiHelper.updateVisibility(renewContainer, true); AndroidUiHelper.updateVisibility(renewContainer, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AndroidUtils.setBackground(ctx, renewContainer, nightMode, R.drawable.ripple_light, R.drawable.ripple_dark); AndroidUtils.setBackground(mapActivity, renewContainer, nightMode, R.drawable.ripple_light, R.drawable.ripple_dark);
} else { } else {
AndroidUtils.setBackground(ctx, renewContainer, nightMode, R.drawable.btn_unstroked_light, R.drawable.btn_unstroked_dark); AndroidUtils.setBackground(mapActivity, renewContainer, nightMode, R.drawable.btn_unstroked_light, R.drawable.btn_unstroked_dark);
} }
final String sku = subscription.getSku(); final String sku = subscription.getSku();
renewContainer.setOnClickListener(new View.OnClickListener() { renewContainer.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
subscribe(sku); ChoosePlanDialogFragment.subscribe(app, mapActivity, purchaseHelper, sku);
} }
}); });
View renew = card.findViewById(R.id.renew); View renew = card.findViewById(R.id.renew);
AndroidUtils.setBackground(ctx, renew, nightMode, AndroidUtils.setBackground(mapActivity, renew, nightMode,
R.drawable.btn_solid_border_light, R.drawable.btn_solid_border_dark); R.drawable.btn_solid_border_light, R.drawable.btn_solid_border_dark);
} }
TextView status = card.findViewById(R.id.status);
status.setText(app.getString(state.getStringRes()));
AndroidUtils.setBackground(status, app.getUIUtilities().getIcon(state.getBackgroundRes()));
int dividerLayout = i + 1 == subscriptions.size() ? R.layout.simple_divider_item : R.layout.divider_half_item; int dividerLayout = i + 1 == subscriptions.size() ? R.layout.simple_divider_item : R.layout.divider_half_item;
View divider = inflater.inflate(dividerLayout, (ViewGroup) view, false); View divider = inflater.inflate(dividerLayout, (ViewGroup) view, false);
((ViewGroup) view).addView(divider); ((ViewGroup) view).addView(divider);
@ -114,52 +108,14 @@ public class SubscriptionsCard extends BaseCard {
} }
private String getHumanDate(long time, Period period) { private String getHumanDate(long time, Period period) {
Date date = new Date(time);
int monthsCount;
if (period == null || period.getUnit() == null) { if (period == null || period.getUnit() == null) {
return ""; return "";
} else if (period.getUnit().equals(Period.PeriodUnit.YEAR)) {
monthsCount = 12;
} else {
monthsCount = period.getNumberOfUnits();
} }
Date date = new Date(time);
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(date); calendar.setTime(date);
calendar.add(Calendar.MONTH, monthsCount); calendar.add(period.getUnit().getCalendarIdx(), period.getNumberOfUnits());
date = calendar.getTime(); date = calendar.getTime();
SimpleDateFormat format = new SimpleDateFormat("MMM d, yyyy", app.getLocaleHelper().getPreferredLocale()); return dateFormat.format(date);
return format.format(date);
}
private void subscribe(String sku) {
if (app == null) {
return;
}
if (!app.getSettings().isInternetConnectionAvailable(true)) {
Toast.makeText(app, R.string.internet_not_available, Toast.LENGTH_LONG).show();
} else if (mapActivity != null && purchaseHelper != null) {
OsmandSettings settings = app.getSettings();
purchaseHelper.purchaseLiveUpdates(mapActivity, sku,
settings.BILLING_USER_EMAIL.get(),
settings.BILLING_USER_NAME.get(),
settings.BILLING_USER_COUNTRY_DOWNLOAD_NAME.get(),
settings.BILLING_HIDE_USER_NAME.get());
}
}
private String getSubscriptionPeriod(Period period) {
if (period == null || period.getUnit() == null) {
return "";
} else if (period.getUnit().equals(Period.PeriodUnit.YEAR)) {
return app.getString(R.string.annual_subscription);
} else if (period.getUnit().equals(Period.PeriodUnit.MONTH)) {
int unitsNumber = period.getNumberOfUnits();
if (unitsNumber == 1) {
return app.getString(R.string.monthly_subscription);
} else if (unitsNumber == 3) {
return app.getString(R.string.three_months_subscription);
}
}
return "";
} }
} }

View file

@ -130,7 +130,7 @@ public class GpxEditDescriptionDialogFragment extends BaseOsmAndDialogFragment {
View btnSave = view.findViewById(R.id.btn_save); View btnSave = view.findViewById(R.id.btn_save);
int drawableRes = isNightMode(true) ? R.drawable.btn_solid_border_dark : R.drawable.btn_solid_border_light; int drawableRes = isNightMode(true) ? R.drawable.btn_solid_border_dark : R.drawable.btn_solid_border_light;
btnSave.setBackgroundDrawable(ContextCompat.getDrawable(ctx, drawableRes)); AndroidUtils.setBackground(btnSave, getMyApplication().getUIUtilities().getIcon(drawableRes));
} }
private void showDismissDialog() { private void showDismissDialog() {