B4A Library LockScreen: Create your own lockscreen with just 5 lines of code

Discussion in 'Additional libraries, classes and official updates' started by Periklis Koutsogiannis, Jul 28, 2014.

  1. Periklis Koutsogiannis

    Periklis Koutsogiannis Active Member Licensed User

    version 2.0 requires B4A version 3.80[Android-L compatible]

    Introduction

    One of the most wanted features since the birth of android is the lockscreen. After a long and exhausting 2 weeks research, I did not find a single complete functional lockscreen example that can address all aspects of a stable lockscreen. Just do a search in GitHub and you will understand what I mean.

    The difficult part was the testing and as mentioned before, to address every single lockscreen system requirement with the minimum amount of complexity and code while keeping in mind that we are playing in B4A and not in eclipse. And in the end, offer the B4A community a fully encapsulated library that contains 100% of the lockscreen functionality (permissions, services, listeners, auto restart when killed, handling the keyguard and phone calls, waking/locking the device and so on) leaving you enough space for your creativity and app logic.

    Latest package (library files and samples)
    https://www.dropbox.com/s/ju1z09r90lx5m4g/lockscreen.zip

    Compiled demos (APK)
    sample1 (5LinesOfCode), sample2 (push button lockscreen), sample3 (slide button lockscreen), sample4 (sliding panel lockscreen)

    upload_2014-8-6_16-30-37.png upload_2014-7-29_9-7-47.png upload_2014-7-29_8-56-1.png upload_2014-7-29_8-56-15.png


    Getting started
    • Extract the contents of the package anywhere in your hard disk
    • Copy the lockscreen.jar and lockscreen.xml to your B4A libraries folder
    • Explore the samples


    Creating a lockscreen app with just 5 lines of code (based on samples\sample1)

    Create 2 activities. One to be your main (configuration) activity and one that will be your actual lockscreen activity.

    Copy the Assets folder from any of the samples folder to your project root and in the main activity's project attributes add the following lines:
    Code:
    'Kitkat transparent status/navigation bars support with custom theme and DeviceAdmin XML
    #AdditionalRes: ..\Assets
    In the main activity's attributes add the following magic line:
    Code:
    #Extends: com.datasteam.b4a.system.lockscreen.MainActivity
    In the lockscreen activity's attributes add the following magic line:
    Code:
    #Extends: com.datasteam.b4a.system.lockscreen.LockScreenActivity

    Now let's write the actual code.

    1. Declare a LockscreenController object in Sub Process_Globals of the Main activity
    Code:
    Sub Process_Globals
        
    Dim Controller As LockScreenController
    End Sub
    2. Load the main layout
    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("Main")
    End Sub
    3. Drop a button in your main layout and create the click handler that will show the lockscreen
    Code:
    Sub BtnPreview_Click
        
    StartActivity(LockScreen)
    End Sub
    4. Load the layout of the lockscreen activity
    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("LockScreen")
    End Sub
    5. Drop a button in your lockscreen layout and create the click handler that will unlock the lockscreen
    Code:
    Sub BtnUnlock_Click
        Main.Controller.Unlock
    End Sub

    Done with just 5 lines of code!

    Add the following text to your manifest to complete the setup.
    IMPORTANT: In the meta-data node and in the android:value field, put the name of your lockscreen activity so that the library engine will find it and launch it when needed.
    Code:
    AddPermission(android.permission.GET_TASKS)
    AddPermission(android.permission.WAKE_LOCK)
    AddPermission(android.permission.DISABLE_KEYGUARD)
    AddPermission(android.permission.RECEIVE_BOOT_COMPLETED)
    AddPermission(android.permission.READ_PHONE_STATE)

    'IMPORTANT: The android:value of meta-data contains the *NAME* of your lockscreen activity!
    AddApplicationText(
        <meta-data android:name=
    "lockscreen_activity" android:value="LockScreen"/>)
    SetActivityAttribute(
        LockScreen, 
    "android:theme""@style/Theme.Lockscreen")
    AddApplicationText(
        <
    service android:name="com.datasteam.b4a.system.lockscreen.LockScreenService">
            <
    intent-filter >
                <action android:name=
    "com.datasteam.b4a.system.lockscreen.LockScreenService" />
            </
    intent-filter >
        </
    service>)
    AddApplicationText(
        <receiver android:name=
    "com.datasteam.b4a.system.lockscreen.LockScreenDeviceAdminReceiver" android:permission="android.permission.BIND_DEVICE_ADMIN" >
            <meta-data
                android:name=
    "android.app.device_admin"
                android:resource=
    "@xml/lockscreen_admin" />
            <
    intent-filter>
                <action android:name=
    "android.app.action.DEVICE_ADMIN_ENABLED" />
            </
    intent-filter>
        </receiver>
        <receiver android:name=
    "com.datasteam.b4a.system.lockscreen.LockScreenBootReceiver" >
           <
    intent-filter >
                <action android:name=
    "android.intent.action.BOOT_COMPLETED" />
           </
    intent-filter>
        </receiver>)
    AddApplicationText(
        <
    activity android:name="com.datasteam.b4a.system.lockscreen.LockScreenHomeResetActivity" android:label="HomeReset" android:theme="@android:style/Theme.NoDisplay" android:enabled="false" android:taskAffinity="" android:stateNotNeeded="true" android:excludeFromRecents="true" android:launchMode="singleTask" android:configChanges="keyboard|keyboardHidden|orientation">
            <
    intent-filter>
                <action android:name=
    "android.intent.action.MAIN" />
                <category android:name=
    "android.intent.category.HOME" />
                <category android:name=
    "android.intent.category.DEFAULT" />
            </
    intent-filter>
        </
    activity>)
    AddApplicationText(
        <
    activity android:name="com.datasteam.b4a.system.lockscreen.LockScreenHomeActivity" android:theme="@android:style/Theme.NoDisplay" android:enabled="false" android:taskAffinity="" android:stateNotNeeded="true" android:excludeFromRecents="true" android:configChanges="keyboard|keyboardHidden|orientation" android:label="$LABEL$ Launcher">
            <
    intent-filter>
                <action android:name=
    "android.intent.action.MAIN" />
                <category android:name=
    "android.intent.category.HOME" />
                <category android:name=
    "android.intent.category.DEFAULT" />
            </
    intent-filter>
        </
    activity>)
    That's all! Run your project and see your first lockscreen app in full swing!

    There is only one non-visual class exposed in the library: LockScreenController. This class will be used to enable/disable the lockscreen and save/load its settings. Explore the SlideButton and SlidePanel samples to see how it is used.

    If you want to wake the device from any of your services, use the LockScreenController.Wake method and the LockScreenController.Lock method from your lockscreen activity to lock the device.

    The SlidePanel sample uses the excellent ClsSlidingSidebar class (included in sample4) from Informatix (Frédéric Leneuf-Magaud).

    Now, go get the NotificationListener library from Erel and the Clocks library from Agraham (or use this nice clock trick) and you will soon have the next lockscreen app hit!


    Blocking the Home Button

    Well-designed and top google play store Lockscreen apps block the home button by allowing the user to set the lockscreen as the home launcher (and the lockscreen will launch the actual launcher). This method is also implemented through the LockScreenController.HomeBlockerEnabled property. See sample2 for detailed usage instructions.


    Known issues

    Third party lockscreens rely on the stock lockscreen when it comes for security. You can set your stock lockscreen pattern or pin security and when the 3rd party lockscreen unlocks, your stock security lockscreen will appear. Custom lockscreens disable the swipe keyguard only. There is no gain in "recreating the wheel" since stock lockscreens provide proven security.

    --

    That's all for now folks! :D

    Version history:

    2.0
    • Fixed: FC and incorrect handling of phone state changes (ringing, offhook)
    • Added: HomeBlocker functionality through LockScreenController.HomeBlockerEnabled property
    • Updated: Samples, manifest code and forum tutorial
    1.1
    • Renamed: LockScreenConfigurator class renamed to LockScreenController
    • Added: LockScreenController.LockNow method which uses the DeviceAdmin Api (use the latest updated manifest code)
    • Samples updated to the latest version
    1.0
    • Initial version
     
    Last edited: Aug 6, 2014
  2. ivan.tellez

    ivan.tellez Active Member Licensed User

    Really nice :D

    What is the minimum API level?
     
  3. Periklis Koutsogiannis

    Periklis Koutsogiannis Active Member Licensed User

    Thanks! The minimum api level is 8 (actually 4)
     
    Douglas Farias likes this.
  4. Douglas Farias

    Douglas Farias Expert Licensed User

    What the next lib lol ? xD
     
  5. ivan.tellez

    ivan.tellez Active Member Licensed User

    Hi

    Maybe Im doing something wrong or Im confused about the concept of the lockScreen. :confused:

    It supose that the lockscreen should regulate immediate access to a device by requiring that the user perform a certain action in order to receive access, like a password, a button press or a gesture.

    So I suposed that the device should be Locked and only could be unlocked with actions in the LockScreen.

    But when compiling the project, it shows the "lockScreen", but this is dissmised if I press the home buton. also when soing a swipe gesture from top of the screen to botom, it shows the notifications panel and lauch apps from here, so, the device its not really "locked".

    I Tested other "lockscreen apps" and have the same issue, its a way to create an app that truly locks the screen?
     
    Last edited: Jul 29, 2014
  6. ivan.tellez

    ivan.tellez Active Member Licensed User

  7. Periklis Koutsogiannis

    Periklis Koutsogiannis Active Member Licensed User

    Third party lockscreens rely on the stock lockscreen when it comes for security. You can set your stock lockscreen pattern or pin security and when the 3rd party lockscreen unlocks, your stock security lockscreen appear. Custom lockescreens disable the swipe keyguard only. No lockscreen developer will "recreate the wheel" since stock lockscreens provide proven security.

    Regarding the home button, most lockscreens block the home button by allowing the user to set the lockscreen as the home launcher (and the lockscreen will launch the actual launcher) which is up to the lockscreen developer to implement for now.

    http://stackoverflow.com/questions/22298322/home-button-override-in-android

    :)
     
    Last edited: Jul 29, 2014
  8. Periklis Koutsogiannis

    Periklis Koutsogiannis Active Member Licensed User

    Locking the phone using the lockscreen library will come in the next version.
     
    ivan.tellez likes this.
  9. Periklis Koutsogiannis

    Periklis Koutsogiannis Active Member Licensed User

  10. Reids

    Reids Member Licensed User

    I got following error while tried to compile the source :

    Code:
    Parsing code.                          0.02
    Compiling code.                        
    0.09
    Compiling layouts code.                
    0.02
    Generating R 
    file.                      Error
    ..\Assets\values-v19\styles.xml:
    5: error: Error: No resource found that matches the given name: attr 'android:windowTranslucentNavigation'.
    ..\Assets\values-v19\styles.xml:4: error: Error: No resource found that matches the given name: attr 'android:windowTranslucentStatus'.
    but solved by removing following line on Asset\values-v19\styles.xml
    Code:
    <item name="android:windowTranslucentStatus">true</item>
            <item name=
    "android:windowTranslucentNavigation">true</item>
    This library is very great!! I Like It!
     
  11. Periklis Koutsogiannis

    Periklis Koutsogiannis Active Member Licensed User

    Thanks Reids!

    This is weird. What is your installed android sdk version?
     
  12. Periklis Koutsogiannis

    Periklis Koutsogiannis Active Member Licensed User

    Update: 1.1

    • Renamed: LockScreenConfigurator class renamed to LockScreenController
    • Added: LockScreenController.LockNow method which uses the DeviceAdmin Api (use the latest updated manifest code)
    • Samples updated to the latest version
    @ivan.tellez :D
     
    Last edited: Jul 29, 2014
    ivan.tellez likes this.
  13. ivan.tellez

    ivan.tellez Active Member Licensed User

    Hi, thanks for the LockNow

    Im afraid @Reids is right, the proyect causes an error, that is because 'android:windowTranslucentNavigation'. And 'android:windowTranslucentStatus'. are only available on API 19 (KitKat)

    I compiled changing the Paths of the android.jar to android-19\android.jar
     
    Reids and Periklis Koutsogiannis like this.
  14. Periklis Koutsogiannis

    Periklis Koutsogiannis Active Member Licensed User

    This is why I asked him about his sdk version ;)
     
    Douglas Farias likes this.
  15. Periklis Koutsogiannis

    Periklis Koutsogiannis Active Member Licensed User

    I am automating the home launcher selection in order to "disable" the home button btw :)
     
    Douglas Farias likes this.
  16. bluedude

    bluedude Well-Known Member Licensed User

    Great additions for this community!
     
    Douglas Farias likes this.
  17. susu

    susu Well-Known Member Licensed User

    On my LG Optimus G Pro, I can press Home button to pass lockscreen. Is it normal?
     
  18. Periklis Koutsogiannis

    Periklis Koutsogiannis Active Member Licensed User

    @susu yeap. Please read the known issues in the tutorial
     
    susu likes this.
  19. ivan.tellez

    ivan.tellez Active Member Licensed User

    Im testing the demo in my Galaxy S4.

    There is some random problems on incomming calls. The app just quits (LockR has a problem and has to close) and no loger appears the lockscreen until i open the config again.

    Any ideas?
     
  20. tpakis

    tpakis Active Member Licensed User

    Thank you for this excelent library!!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice