'Class module
Sub Class_Globals
Dim trialLength_inDays As Int
Dim thePkg As String
Dim installedDate As String
Dim todaysDate As String
Dim toast As Boolean
Dim msgbx As Boolean
Dim extraTxt As String
Dim trialDIR As String
End Sub
'Initializes the object.
'Example:
'In your activity - Private tdc As TrialDateChecker
'
'tdc.Initialize("com.pkgname.trial", 15, False, True, "Try the Paid Version!")
'Where the package name is the full package name.
'The length is the time in Days for the trial period. Use -1 to force the uninstall
'Use Toast as Boolean
'Use Messagebox as Boolean
'Extra text for the tag line on the messageboxes and toast. Can also be "".
'-------------------------------------------
'NOTE: If the package you're trying to work with has been Installed, but NEVER been Opened,
'Directory structures are NOT created on the device yet!!
'-------------------------------------------
Public Sub Initialize(pkgName As String, tLength As Int, usetoast As Boolean, usemsgbox As Boolean, extraText As String)
thePkg = pkgName
trialLength_inDays = tLength
todaysDate = DateTime.Date(DateTime.Now)
msgbx = usemsgbox
toast = usetoast
extraTxt = extraText
'check_For_Trial'commented to stop premature uninstall for paid version
End Sub
'checks if the package exists
'returns true or false
'shows messages if initialized as true for toast or messageboxes
'sets the Class Global Variable 'installedDate' to the packages first install date
'Calls to 'IsThisPackageNameInstalled' which uses PackageManager to get the date
'
Sub check_For_Trial As Boolean
If IsThisPackageNameInstalled = True Then
installedDate = DateTime.Date(GetFirstInstallTime)
show_Msg(True)
Return True
Else
show_Msg(False)
Return False
End If
End Sub
'checks if the package is installed
'return true or false
Sub IsThisPackageNameInstalled As Boolean
'Dim OldPackageName As String
Dim pm As PackageManager
Dim lst As List
lst.Initialize
lst=pm.GetInstalledPackages
Dim str As String
For i=0 To lst.Size -1
str = lst.Get(i)
If str.CompareTo (thePkg)=0 Then Return True
Next
Return False
End Sub
'Returns the package firstInstallTime As Long, as initialized
'Can be used as standalone
'It first checks that the package exists, returns 0 if it does not!
'
Sub GetFirstInstallTime As Long
If IsThisPackageNameInstalled = True Then
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod("getPackageManager")
r.Target = r.RunMethod3("getPackageInfo", thePkg, "java.lang.String", 0x00000001, "java.lang.int")
'Log("Getftinstall: " &r.GetField("firstInstallTime"))
Return r.GetField("firstInstallTime")
Else
Return 0
End If
End Sub
'Shows the Messageboxes or Toast or Both as initialized
'
Sub show_Msg(pkgTrue As Boolean)
Dim days_Left As Int
If pkgTrue = True Then
days_Left = how_many_Days_Left
If days_Left <= trialLength_inDays AND days_Left >= 0 Then
If days_Left = 0 Then
'Log("1. Days Left:" &days_Left&msgbx)
If msgbx = True Then
Msgbox ("This is your LAST Day"&CRLF&"to use this '"&trialLength_inDays&"' Day Trial version."&CRLF&CRLF _
&"Date Installed: " &installedDate&CRLF _
&extraTxt,"Thank you")
End If
If toast = True Then
ToastMessageShow("This is your LAST Day"&CRLF&"to use this Trial version."&CRLF&CRLF _
&"Date Installed: " &installedDate&CRLF _
&extraTxt, True)
End If
Else If days_Left = 1 Then
'msgbox or toast
If msgbx = True Then
Msgbox ("You have '1' Day Left"&CRLF&"to use this Trial version."&CRLF&CRLF _
&"Thank You For trying our Software. Todays Date: "&todaysDate&CRLF _
&"Date Installed: " &installedDate&CRLF _
&extraTxt,"Thank you")
End If
If toast = True Then
ToastMessageShow("You have '1' Day Left"&CRLF&"to use this Trial version."&CRLF&CRLF _
&"Thank You For trying our Software. Todays Date: "&todaysDate&CRLF _
&"Date Installed: " &installedDate&CRLF _
&extraTxt, True)
End If
Else
'msgbox or toast
If msgbx = True Then
Msgbox ("You have '"&days_Left&"' Days Left"&CRLF&"to use this Trial version."&CRLF&CRLF _
&"Thank You For trying our Software. Todays Date: "&todaysDate&CRLF _
&"Date Installed: " &installedDate&CRLF _
&extraTxt,"Thank you")
End If
If toast = True Then
ToastMessageShow("You have '"&days_Left&"' Days Left"&CRLF&"to use this Trial version."&CRLF&CRLF _
&"Thank You For trying our Software. Todays Date: "&todaysDate&CRLF _
&"Date Installed: " &installedDate&CRLF _
&extraTxt, True)
End If
End If
Else
If msgbx = True Then
Msgbox ("The Trial period is over," _
&"Thank You For trying our Software. Todays Date: "&todaysDate&CRLF _
&"Date Installed: " &installedDate&CRLF _
&extraTxt,"Thank you")
End If
If toast = True Then
ToastMessageShow("The Trial period is over," _
&"Thank You For trying our Software. Todays Date: "&todaysDate&CRLF _
&"Date Installed: " &installedDate&CRLF _
&extraTxt, True)
End If
TryUninstall
ExitApplication
End If
Else
If msgbx = True Then
Msgbox ("The Trial Version was not found, probably already uninstalled...Moving Onward..."&CRLF _
,"Trial Version Not Found")
End If
If toast = True Then
ToastMessageShow("The Trial Version was not found, probably already uninstalled...Moving Onward..."&CRLF _
, True)
End If
Log ("The package name is not found, probably already uninstalled.")
End If
End Sub
'Returns how many days are left in the trial period as initialized
'Based on Time TicksPerDay
Sub how_many_Days_Left As Int
Dim installedDOY As Long
Dim todaysDOY As Long
Dim difference_inDays As Long
Dim howmany_Days_Left As Int
installedDOY = GetFirstInstallTime
'Log("Installed DOY : " &installedDOY)
todaysDOY = DateTime.Now
'Log("Todays DOY : " &todaysDOY)
difference_inDays = (todaysDOY - installedDOY)/DateTime.TicksPerDay
'Log("difference: " &difference_inDays)
howmany_Days_Left = trialLength_inDays - difference_inDays
'Log("How Many Left: " &howmany_Days_Left)
Return howmany_Days_Left -1
End Sub
'Used for GetSourceDir as initialized
'returns the "applicationInfo" as an Object or Null if the package doesn't exists
'can be used as a standalone
'
Sub GetActivitiesInfo As Object
If IsThisPackageNameInstalled = True Then
Try
Dim r As Reflector
r.Target = r.GetContext
r.Target = r.RunMethod("getPackageManager")
r.Target = r.RunMethod3("getPackageInfo", thePkg, "java.lang.String", 0x00000001, "java.lang.int")
Return r.GetField("applicationInfo")
Catch
Log("GetActivitiesInfo Error: " &LastException.Message)
End Try
End If
Return Null
End Sub
'Gets the directory where the APK is installed
'Returns the directory of the apk as initialized
'Example:
'tdc.GetSourceDir
'returns - "/data/app/location/your.package.apk" as a string
'Or, it returns Empty "" as string
Sub GetSourceDir As String
Try
Dim AppInfo As Object
AppInfo = GetActivitiesInfo
Dim r As Reflector
r.Target = AppInfo
Return r.GetField("sourceDir")
Catch
Log("GetSource Error: " &LastException.Message)
End Try
Return ""
End Sub
'Uses some directory structure assumptions of internal/external structure, NOT DirRootExternal
'Returns a modified directory structure based on the current processes installed working directory
'Used to retrieve files from the Trial Version working directory.
'Example:
'tdc.GetWorkingDir("external", "com.pkgname.paid", "com.pkgname.trial")
'
Sub GetWorkingDir(InOrExt As String, pkgName1 As String, pkgName2 As String) As String
If InOrExt = "internal" Then
trialDIR = File.DirInternal
'Log("Trial B4: " &trialDIR)
trialDIR = trialDIR.Replace(pkgName1, pkgName2)'if android installs to the same directory structure, then this works
'Log("Trial After: " &trialDIR)
'Return trialDIR
End If
If InOrExt = "external" Then
trialDIR = File.DirDefaultExternal
'Log("Trial B4: " &trialDIR)
trialDIR = trialDIR.Replace(pkgName1, pkgName2)'if android installs to the same directory structure, then this works
'Log("Trial After: " &trialDIR)
'Return trialDIR
End If
Return trialDIR
End Sub
'Used to Uninstall the package as Initialized
'Be careful here!, you don't want to Uninstall the wrong package!
'
'Only Un-comment ... for realtime use!
'
Sub TryUninstall
Log("Call to TryUninstall Sub: for package"&thePkg)
'Dim i As Intent
' i.Initialize("android.intent.action.DELETE", "package:" & thePkg)
' StartActivity(i)
End Sub
'Returns true if last year was a leap year, otherwise false
'Used to calculate Trail periods that cross over to a new year
'Based on Day of the Year
Sub was_lastYear_leapYear As Boolean
Dim year_Of_Install As String
year_Of_Install = DateTime.GetYear(DateTime.DateParse(installedDate))
Dim ndate As Int
ndate = year_Of_Install
'Log(ndate)
If DateTime.GetDayOfYear(DateTime.DateParse("12/31/"&ndate)) = 366 Then
'Log("LY True")
Return True
Else
'Log("LY False")
Return False
End If
End Sub