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,281
  • AdminExample.zip
    8.2 KB · Views: 341
Last edited:

moster67

Expert
Licensed User
Longtime User
Using the role of the Device Administrator, is this the way that automation tools, such as Tasker, works?
 

moster67

Expert
Licensed User
Longtime User
I would like to find out how automation apps such as Tasker work and how they do things and I thought this library might matter.
I will open a separate question.
 

ykucuk

Well-Known Member
Licensed User
Longtime User
Hi,
I downloaded example project. I did set read-only the XML file in the Res folder.

Tried 3 different devices but doesn't work. Not even log or any error.

I attached a screenshot with unfiltered logs.

Any help?
 

Attachments

  • Screen Shot 2017-09-11 at 12.02.58.png
    Screen Shot 2017-09-11 at 12.02.58.png
    100.8 KB · Views: 364

Erel

B4X founder
Staff member
Licensed User
Longtime User
I did set read-only the XML file in the Res folder.
That is not needed. It creates the XML with CreateResource command.

I attached a screenshot with unfiltered logs.
It is always better to post text instead of screenshots. You can right click to copy.

There is nothing relevant in the logs.
 

ykucuk

Well-Known Member
Licensed User
Longtime User
That is not needed. It creates the XML with CreateResource command.



It is always better to post text instead of screenshots. You can right click to copy.

There is nothing relevant in the logs.

It gives error "file not found:deviceadmin.XML" if don't set read only.

is there way copy all log because its like listbox and can copy line by line.

I added my project file. Already tried 4 different devices and cant figure out. I need administrator right.

could you check when you have time.
 

Attachments

  • AdminExample.zip
    406.4 KB · Views: 388

Xenno

Member
Licensed User
Please, help. I followed the steps but keeps failing. No device administration dialog appear on calling "Enable".

The log shows:
B4X:
Unable to retrieve device policy ComponentInfo{com.sample.test/anywheresoftware.b4a.objects.AdminManager$AdminReceiver}

In manifest code:
B4X:
AddApplicationText(<receiver android:name="anywheresoftware.b4a.objects.AdminReceiver"
...

The device_admin.xml exists in res/xml

B4A 6.80


TIA
 

Xenno

Member
Licensed User
Here is the manifest code:
B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

AddApplicationText(<receiver android:name="anywheresoftware.b4a.objects.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>)

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>
)

Here's the code in main activity:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

    Public AdmMgr As AdminManager
End Sub

...

Sub cbEnable_CheckedChange(Checked As Boolean)
    If (Checked) Then      
        If Not(AdmMgr.Enabled) Then
            AdmMgr.Enable("Device Administration for this app is required.")
            If Not(AdmMgr.Enabled) Then
                cbEnable.Checked = False
            End If
        End If
    End If
End Sub
 
Last edited:

noeleon

Active Member
Licensed User
r.RunMethod4("setCameraDisabled", Array As Object(cm, True), _
Array As String("android.content.ComponentName", "java.lang.boolean") 'change to False to enable camera
Can't enable the camera if it was disabled by another admin app. How do i check for this?

And how to launch the Device Administrators activity in android settings?
Thanks.
 
Status
Not open for further replies.
Top