Skip to content

Commit

Permalink
Remove IAP and entitlement checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljunkie committed Jul 31, 2015
1 parent 920772f commit a815353
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 326 deletions.
181 changes: 2 additions & 179 deletions Plex/source/AppManager.brs
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,17 @@ Function AppManager()
if m.AppManager = invalid then
obj = CreateObject("roAssociativeArray")

'obj.productCode = "PROD1" ' Sample product when sideloaded
obj.productCode = "plexunlock"

' The unlocked state of the app, one of: Plex Pass, Exempt, Purchased, Trial, or Limited
obj.IsPlexPass = (RegRead("IsPlexPass", "misc", "0") = "1")
obj.IsEntitled = (RegRead("IsEntitled", "misc", "0") = "1")
obj.IsPurchased = (RegRead("purchased", "misc", "0") = "1")
obj.IsAvailableForPurchase = false
obj.IsExempt = false

obj.firstPlaybackTimestamp = RegRead("first_playback_timestamp", "misc")
if obj.firstPlaybackTimestamp <> invalid then
currentTime = Now().AsSeconds()
firstPlayback = obj.firstPlaybackTimestamp.toint()
trialDuration = 30 * 24 * 60 * 60 ' 30 days
obj.IsInTrialWindow = (currentTime - firstPlayback < trialDuration)
else
' The user hasn't tried to play any media yet, still in trial.
obj.IsInTrialWindow = true
end if

obj.ResetState = managerResetState
obj.ResetState()

' Track anything that needs to be initialized before the app can start
' and an initial screen can be shown. These need to be important,
' generally related to whether the app is unlocked or not.
'
obj.Initializers = CreateObject("roAssociativeArray")
obj.AddInitializer = managerAddInitializer
obj.ClearInitializer = managerClearInitializer
obj.IsInitialized = managerIsInitialized

' Media playback is allowed if the app is unlocked or still in a trial
' period. So, basically, if it's not Limited.
obj.IsPlaybackAllowed = managerIsPlaybackAllowed

' Channel store
obj.FetchProducts = managerFetchProducts
obj.HandleChannelStoreEvent = managerHandleChannelStoreEvent
obj.StartPurchase = managerStartPurchase
obj.CheckStoreTimeout = managerCheckStoreTimeout
obj.StoreTimeout = 10

' Singleton
m.AppManager = obj

obj.FetchProducts()
end if

return m.AppManager
Expand All @@ -66,146 +30,5 @@ End Sub

Function managerIsInitialized() As Boolean
m.Initializers.Reset()
status = m.Initializers.IsEmpty()
m.CheckStoreTimeout()
return status
End Function

Function managerIsPlaybackAllowed() As Boolean
' If we've never noted a playback attempt before, write it to the registry
' now. It will serve as the start of the trial period.

if m.firstPlaybackTimestamp = invalid then
RegWrite("first_playback_timestamp", tostr(Now().AsSeconds()), "misc")
end if

return m.State <> "Limited"
return (m.Initializers.IsEmpty())
End Function

Sub managerResetState()
if m.IsPlexPass then
m.State = "Plex Pass"
else if m.IsEntitled then
m.State = "Entitlement"
else if m.IsExempt then
m.State = "Exempt"
else if m.IsPurchased then
m.State = "Purchased"
else if m.IsInTrialWindow then
m.State = "Trial"
else
m.State = "Limited"
end if

if m.State <> "Limited" and m.State <> "Trial" then
m.StateDisplay = "Unlocked"
else
m.StateDisplay = m.State
end if

Debug("App state is now: " + m.StateDisplay + " (" + m.State + ")")
End Sub

Sub managerCheckStoreTimeout()
if m.StoreTimer <> invalid and m.StoreTimer.GetElapsedSeconds() > m.StoreTimeout then
Debug("Channel Store timed out: " + tostr(m.StoreTimer.GetElapsedSeconds()))
m.StoreTimer = invalid
m.ResetState()
m.ClearInitializer("channelstore")
end if
end Sub

Sub managerFetchProducts()
' On the older firmware, the roChannelStore exists, it just doesn't seem to
' work. So don't even bother, just say that the item isn't available for
' purchase on the older firmware.

if CheckMinimumVersion(GetGlobal("rokuVersionArr", [0]), [5, 1]) then
m.AddInitializer("channelstore")

' The docs suggest we can make two requests at the same time by using the
' source identity, but it doesn't actually work. So we have to get the
' catalog and purchases serially. Start with the purchases, so that if
' we get a response we can skip the catalog request.

store = CreateObject("roChannelStore")
store.SetMessagePort(GetViewController().GlobalMessagePort)
store.GetPurchases()
m.PendingStore = store
m.PendingRequestPurchased = true
m.StoreTimer = createTimer()
else
' Rather than force these users to have a Plex Pass, we'll exempt them.
' Among other things, this allows old users to continue to work, since
' even though they've theoretically been grandfathered we don't know it.
m.IsExempt = true
Debug("Channel store isn't supported by firmware version")
m.ResetState()
end if
End Sub

Sub managerHandleChannelStoreEvent(msg)
m.StoreTimer = invalid
m.PendingStore = invalid
atLeastOneProduct = false

if msg.isRequestSucceeded() then
if m.PendingRequestPurchased then m.IsPurchased = false
for each product in msg.GetResponse()
atLeastOneProduct = true
if product.code = m.productCode then
m.IsAvailableForPurchase = true
if m.PendingRequestPurchased then
m.IsPurchased = true
RegWrite("purchased", "1", "misc")
end if
end if
next
end if

' If the cataglog had at least one product, but not ours, then the user is
' exempt. This essentially allows sideloaded channels to be exempt without
' having to muck with anything.

if NOT m.PendingRequestPurchased AND NOT m.IsAvailableForPurchase AND atLeastOneProduct then
Debug("Channel is exempt from trial period")
m.IsExempt = true
end if

' If this was a purchases request and we didn't find anything, then issue
' a catalog request now.
if m.PendingRequestPurchased AND NOT m.IsPurchased then
Debug("Channel does not appear to be purchased, checking catalog")
store = CreateObject("roChannelStore")
store.SetMessagePort(GetViewController().GlobalMessagePort)
store.GetCatalog()
m.PendingStore = store
m.PendingRequestPurchased = false
else
Debug("IAP is available: " + tostr(m.IsAvailableForPurchase))
Debug("IAP is purchased: " + tostr(m.IsPurchased))
Debug("IAP is exempt: " + tostr(m.IsExempt))
m.ResetState()
end if

if m.PendingStore = invalid then
m.ClearInitializer("channelstore")
end if
End Sub

Sub managerStartPurchase()
store = CreateObject("roChannelStore")
cart = CreateObject("roList")
order = {code: m.productCode, qty: 1}
cart.AddTail(order)
store.SetOrder(cart)

if store.DoOrder() then
Debug("Product purchased!")
RegWrite("purchased", "1", "misc")
m.IsPurchased = true
m.ResetState()
else
Debug("Product not purchased")
end if
End Sub
5 changes: 0 additions & 5 deletions Plex/source/AudioPlayer.brs
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,6 @@ Sub audioPlayerPrev()
End Sub

Sub audioPlayerSetContext(context, contextIndex, screen, startPlayer)
if NOT AppManager().IsPlaybackAllowed() then
GetViewController().ShowPlaybackNotAllowed()
return
end if

if startPlayer then
m.IgnoreTimelines = true
m.Stop()
Expand Down
40 changes: 2 additions & 38 deletions Plex/source/MyPlexManager.brs
Original file line number Diff line number Diff line change
Expand Up @@ -134,41 +134,10 @@ Sub mpProcessAccountResponse(event)
m.Protected = false
m.Admin = false

m.IsEntitled = false
if xml.entitlements <> invalid then
if tostr(xml.entitlements@all) = "1" then
m.IsEntitled = true
else
for each entitlement in xml.entitlements.GetChildElements()
if ucase(tostr(entitlement@id)) = "ROKU" then
m.IsEntitled = true
exit for
end if
end for
end if
end if

if m.IsEntitled then
RegWrite("IsEntitled", "1", "misc")
else
RegWrite("IsEntitled", "0", "misc")
end if

if m.IsPlexPass then
RegWrite("IsPlexPass", "1", "misc")
else
RegWrite("IsPlexPass", "0", "misc")
end if
Debug("Validated myPlex token, corresponds to " + tostr(m.Id) + ":" + tostr(m.Title))
Debug("PlexPass: " + tostr(m.IsPlexPass))
Debug("Entitlement: " + tostr(m.IsEntitled))
Debug("Restricted: " + tostr(m.IsRestricted))

mgr = AppManager()
mgr.IsPlexPass = m.IsPlexPass
mgr.IsEntitled = m.IsEntitled
mgr.ResetState()

m.Publish()

' update the list of users in the home
Expand Down Expand Up @@ -271,20 +240,15 @@ Function mpCreateRequest(sourceUrl As String, path As String, appendToken=true A
End Function

Sub mpDisconnect()
Debug("Disconnect Plex Account")

RegDelete("AuthToken", "myplex")
' remove all auth tokens for any server
RegDeleteSection("server_tokens")
RegDeleteSection("user_cache")
' reset the current admin state
GetGlobalAA().AddReplace("IsAdmin", true)

Debug("Disconnect Plex Account - Reset Plex Pass and Entitlement status")
RegWrite("IsPlexPass", "0", "misc")
RegWrite("IsEntitled", "0", "misc")
AppManager().IsPlexPass = false
AppManager().IsEntitled = false
AppManager().ResetState()

' reset the MyPlexManager singleton
GetGlobalAA().Delete("MyPlexManager")
MyPlexManager()
Expand Down
47 changes: 0 additions & 47 deletions Plex/source/PreferenceScreen.brs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ Function createPreferencesScreen(viewController) As Object
}

obj.checkMyPlexOnActivate = false
obj.checkStatusOnActivate = false

return obj
End Function
Expand Down Expand Up @@ -283,7 +282,6 @@ Sub showPreferencesScreen()
m.AddItem({title: "Screensaver"}, "screensaver", m.GetEnumValue("screensaver"))
m.AddItem({title: "Logging"}, "debug")
m.AddItem({title: "Advanced Preferences"}, "advanced")
m.AddItem({title: "Channel Status: " + AppManager().StateDisplay}, "status")

m.AddItem({title: "Close Preferences"}, "close")

Expand Down Expand Up @@ -360,35 +358,6 @@ Function prefsMainHandleMessage(msg) As Boolean
m.ViewController.InitializeOtherScreen(screen, invalid)
screen.Show()
end if
else if command = "status" then
m.checkStatusOnActivate = true
m.statusIndex = msg.GetIndex()

dialog = createBaseDialog()
dialog.Title = "Channel Status"

manager = AppManager()
if manager.State = "Trial" then
if manager.IsAvailableForPurchase then
dialog.Text = "Plex is currently in a trial period. To fully unlock the channel, you can purchase it or connect a Plex Pass account."
dialog.SetButton("purchase", "Purchase the channel")
else
dialog.Text = "Plex is currently in a trial period. To fully unlock the channel, you must connect a Plex Pass account."
end if
else if manager.State = "Limited" then
if manager.IsAvailableForPurchase then
dialog.Text = "Your Plex trial has expired and playback is currently disabled. To fully unlock the channel, you can purchase it or connect a Plex Pass account."
dialog.SetButton("purchase", "Purchase the channel")
else
dialog.Text = "Your Plex trial has expired and playback is currently disabled. To fully unlock the channel, you must connect a Plex Pass account."
end if
else
dialog.Text = "Plex is fully unlocked."
end if

dialog.SetButton("close", "Close")
dialog.HandleButton = channelStatusHandleButton
dialog.Show()
else if command = "quality" OR command = "quality_remote" OR command = "level" OR command = "fivepointone" OR command = "directplay" OR command = "screensaver" or command = "autologin" then
m.HandleEnumPreference(command, msg.GetIndex())
else if command = "slideshow" then
Expand Down Expand Up @@ -442,25 +411,9 @@ Sub prefsMainActivate(priorScreen)
if home <> invalid and home.loader <> invalid then home.loader.CreateMyPlexRequests(false)
end if
m.SetTitle(m.myPlexIndex, getCurrentMyPlexLabel())
else if m.checkStatusOnActivate then
m.checkStatusOnActivate = false
m.SetTitle(m.statusIndex, "Channel Status: " + AppManager().StateDisplay)
end if
End Sub

Function channelStatusHandleButton(key, data) As Boolean
if key = "purchase" then
AppManager().StartPurchase()
else if key = "connect" then
screen = createMyPlexPinScreen(GetViewController())
GetViewController().InitializeOtherScreen(screen, invalid)
screen.Show()
else if key = "close" then
MyPlexManager().RefreshAccountInfo()
end if
return true
End Function

'*** Slideshow Preferences ***

Function createSlideshowPrefsScreen(viewController) As Object
Expand Down
Loading

0 comments on commit a815353

Please sign in to comment.