B4A Library Device Administrator library

Status
Not open for further replies.
Starting from Android 2.2 (api level 8), Android allows application to be registered as administrators.
Administrator apps have the following special features:
- Manually lock the screen
- Set the minimum password length and quality
- Wipe the entire device
- Set the maximum allowed time before the device locks
- Request the user to change password
- Manually set a new password
- Disable the camera
- Track password changes
- Some other security features as described here.

Note that the password is the screen lock password (other passwords are not affected).

The user needs to enable the admin app before it can have any special privileges.
This is done either by calling Manager.Enable or from the Security settings page.
The user will see a message with the policies that this app requests:

SS-2012-07-02_17.35.01.png


The user can always disable an administrator app from the Security settings page. The idea is that in your app you should check whether the admin is enabled and the password meets the requirements. If they don't then you do not give access to some resource such as the company's server.

How to
A working example is attached to this project. It is recommended to start with it.
Add the following code to the manifest editor:

B4X:
AddApplicationText(<receiver android:name="anywheresoftware.b4a.objects.AdminReceiver2"
  android:permission="android.permission.BIND_DEVICE_ADMIN"
  android:exported="true">
  <meta-data android:name="android.app.device_admin"
  android:resource="@xml/device_admin" />
  <intent-filter>
  <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
  </intent-filter>
</receiver>)

CreateResource(xml, device_admin.xml,
<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
  <uses-policies>
  <limit-password />
  <reset-password />
  <force-lock />
  </uses-policies>
</device-admin>
)

3. Declare an AdminManager object. With this object you can ask the user to enable the admin app and access the special privileges.

4. (optional) Add a service named ManagerService. This service will allow you to track password changes and changes to the admin app enabled status. See the attached example.


The latest version of this library is included in the IDE.

Upgrading from v1.00

The receiver name has changed. You need to update the manifest editor code.
The user will probably need to re-enable the admin app. V1.00 library is attached to allow developers to keep the previous version if prefered.
 

Attachments

  • Administrator1.00.zip
    4.8 KB · Views: 1,312
  • AdminExample.zip
    8.2 KB · Views: 377
Last edited:

ilan

Expert
Licensed User
Longtime User
if somene wants only to lock the screen without password request do it like this

B4X:
#Region Module Attributes
    #StartAtBoot: False
#End Region

'Service module
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)
    If StartingIntent.HasExtra("admin") Then
        Select StartingIntent.GetExtra("admin")
            Case "Enabled"
                Log("admin enabled")
                AdminEnabled
            Case "Disabled"
                Log("admin disabled")
            Case "PasswordChanged"
                Log("Password changed")
        End Select
    End If
End Sub
Sub AdminEnabled
    dojob.manager.SetPasswordQuality(dojob.manager.PASSWORD_QUALITY_NUMERIC, 4)
'    If dojob.manager.PasswordSufficient = False Then
'        dojob.manager.RequestNewPassword
'    End If
End Sub
Sub Service_Destroy

End Sub
 

fransvlaarhoven

Active Member
Licensed User
Longtime User
Hallo,

i'm attempting to make a device manager working on android version 5.1.1

Disabling the camera works but setting the maximum failed password before wipe seems to fail.

I'm using the code as found here at #23

Dim numberOfFailedPasswords As Int= 4
r.Target = manager
Dim rec As Object = r.GetField("rec")
Dim dm As Object = r.GetField("dm")
r.Target = dm
r.RunMethod4("setMaximumFailedPasswordsForWipe", Array As Object(rec, numberOfFailedPasswords), Array As String("android.content.ComponentName", "java.lang.int"))

however, it seems to be that nothing happens when entering the wrong pincode

Can you help me out?

Thanks


Edit: found the solution. It seems to be the same problem as described in described in #21
 
Last edited:

khosrwb

Active Member
Licensed User
Longtime User
hi
when I run the sample
I get to under this error and when I prss listpermission button , show me : application does not any permission
 

Attachments

  • main.PNG
    main.PNG
    153 KB · Views: 373
  • permission.PNG
    permission.PNG
    157.6 KB · Views: 364
  • manifest.PNG
    manifest.PNG
    60.2 KB · Views: 370

khosrwb

Active Member
Licensed User
Longtime User
how can I change permission ,
I need to this permission : lock the screen
I will make only lock ((turn off screen))
 

Attachments

  • Screenshot_2015-12-20-17-03-32.jpg
    Screenshot_2015-12-20-17-03-32.jpg
    297.2 KB · Views: 363

peacemaker

Expert
Licensed User
Longtime User
Can Admin app install other new APK ?
 

manuel-r

Member
Licensed User
Longtime User
Hi.
Is it possible to set a new password/pin and lock the device if an app with admin-privilegs is uninstalled or if the privilegs are taken away?
 

manuel-r

Member
Licensed User
Longtime User
No. You cannot use any of the administrator features in that case.
Ok ... or not ;)
So I have to write one app that runs a service that checks if an app is still installed and if not lock the device?!
Is there any way to hide an installed app? BTW: What happens if I give an app a very small transparent applogo and no name?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

manuel-r

Member
Licensed User
Longtime User
At this time I don't use the library but I think about it to prevent removing apps and company-apps that my users (our employees) don't like ;)
If I could lock their devices in case of removing an app they have to come to me for unlocking.
As a kind of education. You know?
 

jazzzzzzz

Active Member
Licensed User
Longtime User
How can I use this lib to wake my app from lockscreen ? Provided phone does not use password protected lockscreen..
 

jazzzzzzz

Active Member
Licensed User
Longtime User
i have a sticky service running in my app,And in a particular condition i want to show my app screen even if the phone is locked(display offed state)
 

echapeta

Member
Licensed User
Longtime User
Hello,

I can compile and run the ezample, but when I try to use the library at an existing project I get the following compilation error:

AndroidManifest.xml:111: error: Error: No resource found that matches the given name (at 'resource' with value '@xml/device_admin').

The manifest line is:


'**************** Administrator Library
AddApplicationText(<receiver android:name="anywheresoftware.b4a.objects.AdminManager$AdminReceiver"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin"
android:resource="@xml/device_admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>
</receiver>)


Can you help me ?

Thank You
 
Status
Not open for further replies.
Top