#Region Service Attributes
#StartAtBoot: True
#End Region
' Libraries used: ByteConverter, JavaObject, Encryption and DateUtils
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Service_Create
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StopAutomaticForeground 'Call this when the background task completes (if there is one)
If StartingIntent.Action="android.intent.action.TIMEZONE_CHANGED" Or StartingIntent.Action="android.intent.action.TIME_CHANGED" Or StartingIntent.Action="android.intent.action.TIME_SET" Then
File.Delete(File.DirInternal,"system")
End If
End Sub
Sub Service_Destroy
End Sub
'check app use is legal
Public Sub islegal() As Boolean
'1- check if system file exist
If (File.Exists(File.DirInternal,"system"))=False Then Return False
'2- check if file content match the file creation time - not created/copied by illegal user
Dim filecontent As String=File.ReadString(File.DirInternal,"system")
Dim filecreationtime As Long = GetCreationTime(File.DirInternal,"system")
If filecontent<>getMD5(filecreationtime) Then
File.Delete(File.DirInternal,"system")
Return False
End If
'3- check if usetime ellapsed against device datetime
Dim p As Period=DateUtils.PeriodBetween(filecreationtime,DateTime.Now)
If p.Months>=1 Then 'used time in months, you can change it according your needs
File.Delete(File.DirInternal,"system")
Return False
End If
'4- check if device datetime changed
'it will be triggered when change happen and delete "system" file in DirInternal
'see Service_Start
'5- check last/maximum transaction date in your app database if time diff larger than legal time
'write your code here .. return False if time consumed
Return True
End Sub
'should be called once - example when app purchase or subscription payment done
Public Sub createlockfile
If File.Exists(File.DirInternal,"system") Then
File.Delete(File.DirInternal,"system")
Sleep(200)
File.WriteString(File.DirInternal,"system",getMD5(GetCreationTime(File.DirInternal,"system")))
Else
File.WriteString(File.DirInternal,"system",getMD5(GetCreationTime(File.DirInternal,"system")))
End If
End Sub
'Android 26+ by @Erel
Sub GetCreationTime (Dir As String, FileName As String) As Long
Dim Files As JavaObject
Files.InitializeStatic("java.nio.file.Files")
Dim FileSystems As JavaObject
Dim FileSystem As JavaObject = FileSystems.InitializeStatic("java.nio.file.FileSystems").RunMethod("getDefault", Null)
Dim Path As JavaObject = FileSystem.RunMethod("getPath", Array(File.Combine(Dir, FileName), Array As String()))
Dim BasicFileAttributes As JavaObject
BasicFileAttributes.InitializeStatic("java.nio.file.attribute.BasicFileAttributes")
Dim LinkOptions As JavaObject
LinkOptions.InitializeArray("java.nio.file.LinkOption", Array())
Return Files.RunMethodJO("getAttribute", Array(Path, "creationTime", LinkOptions)).RunMethod("toMillis", Null)
End Sub
Sub getMD5(str As String) As String
Dim md As MessageDigest
Dim ByteCon As ByteConverter
Dim md5hash() As Byte
md5hash = md.GetMessageDigest(str.GetBytes("UTF8"),"MD5")
Dim md5string As String
md5string = ByteCon.HexFromBytes(md5hash)
Return md5string
End Sub