B4A Library PatternView

This is a wrap for this Github project. You will require, other than for the attached B4A library files, the following jars in your additional library folder:
1. android-support-v4.jar
2. android-support-v7-appcompat.jar

When you start the app the first time it will prompt you (TTS and Toast) to enter a pattern. This pattern will be encrypted and saved in a file called Pattern.txt in the root folder of your device. Thus, the file will contain an encrypted representation of the pattern that you have recorded.

If you then quit and restart the app it will read the encrypted pattern from the file, decrypt it, and apply that as the unlock pattern. Draw the pattern that you have recorded previously and the pattern will be unlocked (i.e it will unlock when the saved pattern corresponds with the pattern that you are drawing after a new startup).

Posting the following:
1. B4A project for demonstration purposes
2. B4A library files - copy them to your additional library folder.
3. resource.zip - extract it and copy the folder to the same folder level as that of the /Files and /Objects folders of the B4A project.
4. Pattern.zip (it contains Pattern.txt). If you extract and copy this file to the root folder of your device then the pattern that I have drawn below will be used as the unlock pattern (until such time as what you record a new pattern). Pattern.txt contains the following string which is the encrypted string for the pattern that I have drawn in the image below(pattern was entered starting from top left):

Make sure you set this path correctly in the B4A code to reflect your own setup:
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat

The B4A project contains 4 classes with inline java code for:
1. Toast messages
2. Text To Speech
3. String manipulation
4. Encryption / decryption

View when app starts:

Enter a pattern when app starts for the first time - it will be encrypted and save in the root folder of your device in a files named Pattern.txt

The new pattern is encrypted and saved once you lift your finger after drawing your pattern

Upon successful unlock (using previously recorded pattern).

Click on the menu (top right) at any time to record a new unlock patter. The new pattern will replace the previously recorded pattern as the new unlock pattern

Device root folder:

Sample Code:

#Region  Project Attributes
    #ApplicationLabel: PatternView
    #VersionCode: 1
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#AdditionalRes: ..\resource
'#AdditionalRes: C:\Users\----------2\Documents\Basic 4 Android\JOHAN APPS\JHS LIBS\resource\b4a_appcompat, de.amberhome.objects.appcompat
#AdditionalRes: C:\ANDRIOD_SDK_TOOLS\extras\android\support\v7\appcompat\res, android.support.v7.appcompat
#Extends: android.support.v7.app.AppCompatActivity

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim t As Timer

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private pv1 As PatternView

    Dim crypt As AESCryptUtilities
    Dim su As StringUtilities
    Dim tts As TTSUtilities
    Dim toast As ToastUtilities

    Dim password As String = "Password1"                     'the password to be used during encryption and decryption
    Dim defPattern As String = ""
    Private Label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    t.Initialize("t", 1000)

    Activity.AddView(pv1, 2%x, 10%y, 96%x, 70%y)
    Label1.Visible = False

    Activity.AddMenuItem("Record new pattern", "RecordNewPattern")

    If File.ExternalWritable = False Then
        Msgbox("Cannot write on storage card.", "")
    End If

    crypt.Initialize                                         'used to encrypt the pattern
    su.Initialize                                            'some string utilities

    pv1.CircleColor = Colors.Blue
    pv1.DotColor = Colors.White
    pv1.PathColor = Colors.Yellow
    pv1.GridSize = 5
    pv1.TactileFeedbackEnabled = True

End Sub

Sub Activity_Resume

    If File.Exists(File.DirRootExternal,"Pattern.txt") Then
        Log("File Exists")
        defPattern = crypt.decrypt(password, defPattern)
        Log("File Exist = " & defPattern)
        Log("File Does Not Exists")
        t.Enabled = True

    End If     

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub pv1_correct_pattern

    tts.speakOut("correct pattern entered")
    pv1.Visible = False
    Label1.Visible = True

End Sub

Sub pv1_incorrect_pattern

    tts.speakOut("incorrect pattern entered")

End Sub

Sub pv1_new_pattern_entered

    defPattern = pv1.GetPattern
    SaveString(crypt.encrypt(password, defPattern))
    tts.speakOut("New pattern entered, encrypted, and saved")

End Sub

Sub setPattern

    For k = 1 To su.stringLength(defPattern) Step 3
        Dim i As Int = su.Mid(defPattern, k,1)
        Dim j As Int = su.Mid(defPattern, k+1, 1)

End Sub

Sub SaveString(encryptedmessage As String)

    Log("SaveString = " & encryptedmessage)
    File.WriteString(File.DirRootExternal, "Pattern.txt", encryptedmessage)

End Sub

Sub ReadString

     defPattern = File.ReadString(File.DirRootExternal, "Pattern.txt")
     Log("ReadString = " & defPattern)

End Sub

Sub RecordNewPattern_Click


End Sub

Sub t_tick

    tts.speakOut("no lock pattern exists. please define a pattern by drawing it")
    toast.displayToast("NO PATTERN EXISTS. ENTER A PATTERN")
    t.Enabled = False

End Sub

Sub Label1_Click

    Label1.Visible = False
    pv1.Visible = True

End Sub


Github: Georgi Eftimov, Wrapped by: Johan Schoeman
Version: 1.01
  • PatternView
    • correct_pattern, incorrect_pattern, new_pattern_entered
    • ba As BA
    • BringToFront
    • DesignerCreateView (base As PanelWrapper, lw As LabelWrapper, props As Map)
    • GetPattern As String
    • Initialize (EventName As String)
    • Invalidate
    • Invalidate2 (arg0 As Rect)
    • Invalidate3 (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • IsInitialized As Boolean
    • RemoveView
    • RequestFocus As Boolean
    • SendToBack
    • SetBackgroundImage (arg0 As Bitmap)
    • SetColorAnimated (arg0 As Int, arg1 As Int, arg2 As Int)
    • SetLayout (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int)
    • SetLayoutAnimated (arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int, arg4 As Int)
    • SetVisibleAnimated (arg0 As Int, arg1 As Boolean)
    • addPatternCell (row As Int, column As Int)
      Set the display mode of the current pattern. This can be useful, for
      instance, after detecting a pattern to tell this view whether change the
      in progress result to correct or wrong.
      displayMode: The display mode.
    • clearPattern
    • Background As Drawable
    • CircleColor As Int [write only]
    • Color As Int [write only]
    • DotColor As Int [write only]
    • Enabled As Boolean
    • GridSize As Int [write only]
    • Height As Int
    • Left As Int
    • Parent As Object [read only]
    • PathColor As Int [write only]
    • TactileFeedbackEnabled As Boolean [write only]
      Set whether the view will use tactile feedback. If true, there will be
      tactile feedback as the user enters the pattern.
    • Tag As Object
    • Top As Int
    • Visible As Boolean
    • Width As Int


  • resource.zip
    423 bytes · Views: 184
  • b4aPatternView.zip
    39 KB · Views: 179
  • Pattern.zip
    228 bytes · Views: 184
  • PatternViewLibFiles.zip
    25.5 KB · Views: 195
Last edited: