jellyfin-web/dashboard-ui/cordova/android/iap.js

182 lines
4.5 KiB
JavaScript
Raw Normal View History

2015-05-28 05:49:46 -07:00
(function () {
var updatedProducts = [];
2015-06-03 21:50:10 -07:00
function updateProductInfo(id, owned, price) {
2015-05-28 05:49:46 -07:00
updatedProducts = updatedProducts.filter(function (r) {
return r.id != id;
});
2015-06-01 07:49:23 -07:00
var product = {
2015-05-28 05:49:46 -07:00
id: id,
2015-06-03 21:50:10 -07:00
owned: owned,
price: price
2015-06-01 07:49:23 -07:00
};
updatedProducts.push(product);
Events.trigger(IapManager, 'productupdated', [product]);
2015-05-28 05:49:46 -07:00
}
2015-10-01 23:14:04 -07:00
function getProduct(feature) {
var id;
if (feature == 'embypremieremonthly') {
id = "emby.premiere.monthly";
} else {
id = "com.emby.mobile.unlock";
}
2015-10-01 23:14:04 -07:00
2015-05-28 05:49:46 -07:00
var products = updatedProducts.filter(function (r) {
return r.id == id;
});
return products.length ? products[0] : null;
}
2015-10-10 17:39:30 -07:00
var storeReady = false;
function onStoreReady() {
storeReady = true;
refreshPurchases();
}
function isPurchaseAvailable() {
2015-05-28 05:49:46 -07:00
2015-10-10 17:39:30 -07:00
return storeReady;
2015-05-28 05:49:46 -07:00
}
2015-10-01 23:14:04 -07:00
function beginPurchase(feature, email) {
2015-10-10 17:39:30 -07:00
if (feature == 'embypremieremonthly') {
return MainActivity.purchasePremiereMonthly(email);
}
if (feature == 'embypremiereweekly') {
return MainActivity.purchasePremiereWeekly(email);
}
return MainActivity.purchaseUnlock(email);
2015-05-28 05:49:46 -07:00
}
2015-05-31 12:12:58 -07:00
function onPurchaseComplete(result) {
2015-06-01 07:49:23 -07:00
if (result) {
refreshPurchases();
}
}
function refreshPurchases() {
NativeIapManager.getPurchaseInfos("window.IapManager.updateProduct");
2015-10-01 23:14:04 -07:00
}
function getSubscriptionOptions() {
var deferred = DeferredBuilder.Deferred();
var options = [];
options.push({
feature: 'embypremieremonthly',
2015-10-08 09:22:14 -07:00
buttonText: 'EmbyPremiereMonthly'
2015-10-01 23:14:04 -07:00
});
options = options.filter(function (o) {
return getProduct(o.feature) != null;
}).map(function (o) {
2015-10-02 10:55:26 -07:00
o.buttonText = Globalize.translate(o.buttonText, getProduct(o.feature).price);
2015-11-02 10:25:01 -07:00
o.owned = getProduct(o.feature).owned;
2015-10-01 23:14:04 -07:00
return o;
});
deferred.resolveWith(null, [options]);
return deferred.promise();
2015-05-31 12:12:58 -07:00
}
2015-11-06 08:02:22 -07:00
function isUnlockedOverride(feature) {
var deferred = DeferredBuilder.Deferred();
if (feature == 'playback') {
isPlaybackUnlockedViaOldApp(deferred);
} else if (feature == 'livetv') {
isLiveTvUnlockedViaOldApp(deferred);
} else {
deferred.resolveWith(null, [false]);
}
return deferred.promise();
}
function isPlaybackUnlockedViaOldApp(deferred) {
testDeviceId(ConnectionManager.deviceId()).done(function (isUnlocked) {
if (isUnlocked) {
deferred.resolveWith(null, [true]);
return;
}
testDeviceId(device.uuid).done(function (isUnlocked) {
if (isUnlocked) {
deferred.resolveWith(null, [true]);
return;
}
deferred.resolveWith(null, [false]);
});
});
}
function testDeviceId(deviceId) {
var deferred = DeferredBuilder.Deferred();
var cacheKey = 'oldapp-' + deviceId;
var cacheValue = appStorage.getItem(cacheKey);
if (cacheValue) {
deferred.resolveWith(null, [cacheValue == 'true']);
} else {
HttpClient.send({
type: 'GET',
url: 'https://mb3admin.com/admin/service/statistics/appAccess?application=AndroidV1&deviceId=' + deviceId
}).done(function () {
appStorage.setItem(cacheKey, 'true');
deferred.resolveWith(null, [true]);
}).fail(function (e) {
if (e.status == 404) {
appStorage.setItem(cacheKey, 'false');
}
deferred.resolveWith(null, [false]);
});
}
return deferred.promise();
}
function isLiveTvUnlockedViaOldApp(deferred) {
isPlaybackUnlockedViaOldApp(deferred);
}
2015-05-28 05:49:46 -07:00
window.IapManager = {
isPurchaseAvailable: isPurchaseAvailable,
2015-06-03 21:50:10 -07:00
getProductInfo: getProduct,
2015-05-28 05:49:46 -07:00
updateProduct: updateProductInfo,
2015-05-31 12:12:58 -07:00
beginPurchase: beginPurchase,
2015-10-01 23:14:04 -07:00
onPurchaseComplete: onPurchaseComplete,
2015-10-10 17:39:30 -07:00
getSubscriptionOptions: getSubscriptionOptions,
2015-11-06 08:02:22 -07:00
onStoreReady: onStoreReady,
isUnlockedOverride: isUnlockedOverride
2015-05-28 05:49:46 -07:00
};
2015-10-10 17:39:30 -07:00
NativeIapManager.initStore();
2015-05-28 05:49:46 -07:00
})();