Added subscription state reading
This commit is contained in:
parent
ec698399b9
commit
424c213a72
1 changed files with 85 additions and 14 deletions
|
@ -22,8 +22,6 @@ import net.osmand.AndroidNetworkUtils.OnRequestsResultListener;
|
||||||
import net.osmand.AndroidNetworkUtils.RequestResponse;
|
import net.osmand.AndroidNetworkUtils.RequestResponse;
|
||||||
import net.osmand.PlatformUtil;
|
import net.osmand.PlatformUtil;
|
||||||
import net.osmand.plus.OsmandApplication;
|
import net.osmand.plus.OsmandApplication;
|
||||||
import net.osmand.plus.settings.backend.OsmandSettings;
|
|
||||||
import net.osmand.plus.settings.backend.OsmandSettings.OsmandPreference;
|
|
||||||
import net.osmand.plus.R;
|
import net.osmand.plus.R;
|
||||||
import net.osmand.plus.Version;
|
import net.osmand.plus.Version;
|
||||||
import net.osmand.plus.inapp.InAppPurchases.InAppPurchase;
|
import net.osmand.plus.inapp.InAppPurchases.InAppPurchase;
|
||||||
|
@ -36,6 +34,8 @@ import net.osmand.plus.inapp.util.BillingManager;
|
||||||
import net.osmand.plus.inapp.util.BillingManager.BillingUpdatesListener;
|
import net.osmand.plus.inapp.util.BillingManager.BillingUpdatesListener;
|
||||||
import net.osmand.plus.liveupdates.CountrySelectionFragment;
|
import net.osmand.plus.liveupdates.CountrySelectionFragment;
|
||||||
import net.osmand.plus.liveupdates.CountrySelectionFragment.CountryItem;
|
import net.osmand.plus.liveupdates.CountrySelectionFragment.CountryItem;
|
||||||
|
import net.osmand.plus.settings.backend.OsmandSettings;
|
||||||
|
import net.osmand.plus.settings.backend.OsmandSettings.OsmandPreference;
|
||||||
import net.osmand.util.Algorithms;
|
import net.osmand.util.Algorithms;
|
||||||
|
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
|
@ -51,6 +51,7 @@ import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class InAppPurchaseHelper {
|
public class InAppPurchaseHelper {
|
||||||
|
@ -64,6 +65,7 @@ public class InAppPurchaseHelper {
|
||||||
private InAppPurchases purchases;
|
private InAppPurchases purchases;
|
||||||
private long lastValidationCheckTime;
|
private long lastValidationCheckTime;
|
||||||
private boolean inventoryRequested;
|
private boolean inventoryRequested;
|
||||||
|
private final Map<String, SubscriptionState> subscriptionStateMap = new HashMap<>();
|
||||||
|
|
||||||
private static final long PURCHASE_VALIDATION_PERIOD_MSEC = 1000 * 60 * 60 * 24; // daily
|
private static final long PURCHASE_VALIDATION_PERIOD_MSEC = 1000 * 60 * 60 * 24; // daily
|
||||||
// (arbitrary) request code for the purchase flow
|
// (arbitrary) request code for the purchase flow
|
||||||
|
@ -119,6 +121,32 @@ public class InAppPurchaseHelper {
|
||||||
PURCHASE_DEPTH_CONTOURS
|
PURCHASE_DEPTH_CONTOURS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum SubscriptionState {
|
||||||
|
UNDEFINED("undefined"),
|
||||||
|
ACTIVE("active"),
|
||||||
|
CANCELLED("cancelled"),
|
||||||
|
IN_GRACE_PERIOD("in_grace_period"),
|
||||||
|
ON_HOLD("on_hold"),
|
||||||
|
PAUSED("paused"),
|
||||||
|
EXPIRED("expired");
|
||||||
|
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
SubscriptionState(@NonNull String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static SubscriptionState getByName(@NonNull String name) {
|
||||||
|
for (SubscriptionState state : SubscriptionState.values()) {
|
||||||
|
if (state.name.equals(name)) {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return UNDEFINED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public interface InAppRunnable {
|
public interface InAppRunnable {
|
||||||
// return true if done and false if async task started
|
// return true if done and false if async task started
|
||||||
boolean run(InAppPurchaseHelper helper);
|
boolean run(InAppPurchaseHelper helper);
|
||||||
|
@ -194,6 +222,16 @@ public class InAppPurchaseHelper {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<String> getSubscriptionsByState(@NonNull SubscriptionState state) {
|
||||||
|
List<String> res = new ArrayList<>();
|
||||||
|
for (Entry<String, SubscriptionState> entry : subscriptionStateMap.entrySet()) {
|
||||||
|
if (entry.getValue() == state) {
|
||||||
|
res.add(entry.getKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
private BillingManager getBillingManager() {
|
private BillingManager getBillingManager() {
|
||||||
return billingManager;
|
return billingManager;
|
||||||
}
|
}
|
||||||
|
@ -286,6 +324,7 @@ public class InAppPurchaseHelper {
|
||||||
for (Purchase p : purchases) {
|
for (Purchase p : purchases) {
|
||||||
skuSubscriptions.add(p.getSku());
|
skuSubscriptions.add(p.getSku());
|
||||||
}
|
}
|
||||||
|
skuSubscriptions.addAll(subscriptionStateMap.keySet());
|
||||||
|
|
||||||
BillingManager billingManager = getBillingManager();
|
BillingManager billingManager = getBillingManager();
|
||||||
// Have we been disposed of in the meantime? If so, quit.
|
// Have we been disposed of in the meantime? If so, quit.
|
||||||
|
@ -779,21 +818,33 @@ public class InAppPurchaseHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("StaticFieldLeak")
|
@SuppressLint("StaticFieldLeak")
|
||||||
private class RequestInventoryTask extends AsyncTask<Void, Void, String> {
|
private class RequestInventoryTask extends AsyncTask<Void, Void, String[]> {
|
||||||
|
|
||||||
RequestInventoryTask() {
|
RequestInventoryTask() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String doInBackground(Void... params) {
|
protected String[] doInBackground(Void... params) {
|
||||||
try {
|
try {
|
||||||
Map<String, String> parameters = new HashMap<>();
|
Map<String, String> parameters = new HashMap<>();
|
||||||
parameters.put("androidPackage", ctx.getPackageName());
|
parameters.put("androidPackage", ctx.getPackageName());
|
||||||
addUserInfo(parameters);
|
addUserInfo(parameters);
|
||||||
return AndroidNetworkUtils.sendRequest(ctx,
|
String activeSubscriptionsIds = AndroidNetworkUtils.sendRequest(ctx,
|
||||||
"https://osmand.net/api/subscriptions/active",
|
"https://osmand.net/api/subscriptions/active",
|
||||||
parameters, "Requesting active subscriptions...", false, false);
|
parameters, "Requesting active subscriptions...", false, false);
|
||||||
|
|
||||||
|
String subscriptionsState = null;
|
||||||
|
String userId = ctx.getSettings().BILLING_USER_ID.get();
|
||||||
|
String userToken = ctx.getSettings().BILLING_USER_TOKEN.get();
|
||||||
|
if (!Algorithms.isEmpty(userId) && !Algorithms.isEmpty(userToken)) {
|
||||||
|
parameters.put("userId", userId);
|
||||||
|
parameters.put("userToken", userToken);
|
||||||
|
subscriptionsState = AndroidNetworkUtils.sendRequest(ctx,
|
||||||
|
"https://osmand.net/api/subscriptions/get",
|
||||||
|
parameters, "Requesting subscriptions state...", false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new String[] { activeSubscriptionsIds, subscriptionsState };
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logError("sendRequest Error", e);
|
logError("sendRequest Error", e);
|
||||||
}
|
}
|
||||||
|
@ -801,19 +852,39 @@ public class InAppPurchaseHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPostExecute(String response) {
|
protected void onPostExecute(String[] response) {
|
||||||
logDebug("Response=" + response);
|
logDebug("Response=" + Arrays.toString(response));
|
||||||
if (response != null) {
|
String activeSubscriptionsIdsJson = response[0];
|
||||||
|
String subscriptionsStateJson = response[1];
|
||||||
|
if (activeSubscriptionsIdsJson != null) {
|
||||||
inventoryRequested = true;
|
inventoryRequested = true;
|
||||||
try {
|
try {
|
||||||
JSONObject obj = new JSONObject(response);
|
JSONObject obj = new JSONObject(activeSubscriptionsIdsJson);
|
||||||
JSONArray names = obj.names();
|
JSONArray names = obj.names();
|
||||||
for (int i = 0; i < names.length(); i++) {
|
if (names != null) {
|
||||||
String skuType = names.getString(i);
|
for (int i = 0; i < names.length(); i++) {
|
||||||
JSONObject subObj = obj.getJSONObject(skuType);
|
String skuType = names.getString(i);
|
||||||
|
JSONObject subObj = obj.getJSONObject(skuType);
|
||||||
|
String sku = subObj.getString("sku");
|
||||||
|
if (!Algorithms.isEmpty(sku)) {
|
||||||
|
getLiveUpdates().upgradeSubscription(sku);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
logError("Json parsing error", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (subscriptionsStateJson != null) {
|
||||||
|
inventoryRequested = true;
|
||||||
|
try {
|
||||||
|
JSONArray subArrJson = new JSONArray(subscriptionsStateJson);
|
||||||
|
for (int i = 0; i < subArrJson.length(); i++) {
|
||||||
|
JSONObject subObj = subArrJson.getJSONObject(i);
|
||||||
String sku = subObj.getString("sku");
|
String sku = subObj.getString("sku");
|
||||||
if (!Algorithms.isEmpty(sku)) {
|
String state = subObj.getString("state");
|
||||||
getLiveUpdates().upgradeSubscription(sku);
|
if (!Algorithms.isEmpty(sku) && !Algorithms.isEmpty(state)) {
|
||||||
|
subscriptionStateMap.put(sku, SubscriptionState.getByName(state));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
|
|
Loading…
Reference in a new issue