Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new uses-policies to existing device administrator on Android

I have a device admin app that uses the following device-admin.xml

<device-admin xmlns:android="http://schemas.android.com/apk/res/android">
 <uses-policies>
     <watch-login />
     <reset-password />
     <force-lock />
     <wipe-data />
 </uses-policies>
</device-admin>

Some users has already activated the device admin permissions. Now, in an update of the app, I want to add a new uses-policy

 <limit-password />

I am wondering how to detect that new use policies have been added programmatically so that we push the reactivation of the device admin permissions?

like image 631
lyc001 Avatar asked Nov 19 '25 16:11

lyc001


1 Answers

AFAIK the only way is read your device-admin.xml from your apk, and you can do it in this way (from an Activity):

    PackageManager packageManager = getPackageManager();
    List <PackageInfo> packageInfoList = packageManager.getInstalledPackages(0);
    for (PackageInfo packageInfo : packageInfoList) {
        String publicSourceDir = packageInfo.applicationInfo.publicSourceDir;
        if (publicSourceDir.contains("your/app/path")) { // or equals, which you prefer
            File apkFile = new File(publicSourceDir);
            if (apkFile.exists()) {
                try {
                    JarFile jarFile = new JarFile(apkFile);
                    JarEntry jarEntry = jarFile.getJarEntry("xml/device-admin.xml");
                    InputStream is = jarFile.getInputStream(jarEntry);
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String str;
                    while ((str = br.readLine()) != null) {
                        Log.d("entry: ", str); // your entries in the device-admin.xml
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            break;
        }
    }
like image 147
packmad Avatar answered Nov 22 '25 07:11

packmad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!