package com.enterprise.mdm.device import android.app.admin.DevicePolicyManager import android.content.Context import android.graphics.BitmapFactory import android.net.Uri import java.io.InputStream import java.net.URL /** * Wraps DevicePolicyManager operations. Destructive operations (factory reset, * password policy) require Device Owner mode provisioned per Android Enterprise. */ class PolicyManager(private val context: Context) { private val dpm = context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager private val admin = MdmDeviceAdminReceiver.componentName(context) fun isAdminActive(): Boolean = dpm.isAdminActive(admin) fun isDeviceOwner(): Boolean = dpm.isDeviceOwnerApp(context.packageName) fun lock(): Boolean = try { if (isAdminActive()) { dpm.lockNow(); true } else false } catch (e: SecurityException) { false } fun setPasswordPolicy(minLength: Int): Boolean = try { if (isDeviceOwner()) { dpm.setPasswordQuality(admin, DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC) dpm.setPasswordMinimumLength(admin, minLength.coerceIn(4, 16)) true } else false } catch (e: SecurityException) { false } fun factoryReset(): Boolean = try { // Device Owner only – performs a legitimate, authorized enterprise wipe. if (isDeviceOwner()) { dpm.wipeData(0); true } else false } catch (e: SecurityException) { false } fun setWallpaper(imageUrl: String): Boolean = try { val wm = android.app.WallpaperManager.getInstance(context) val input: InputStream = URL(imageUrl).openStream() val bmp = BitmapFactory.decodeStream(input) input.close() if (bmp != null) { wm.setBitmap(bmp); true } else false } catch (e: Exception) { false } fun disableCamera(disabled: Boolean): Boolean = try { if (isAdminActive()) { dpm.setCameraDisabled(admin, disabled); true } else false } catch (e: SecurityException) { false } }