package com.akexorcist.knoxkioskapp;

import android.app.enterprise.ApplicationPolicy;
import android.app.enterprise.EnterpriseDeviceManager;
import android.app.enterprise.kioskmode.KioskMode;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/**
 * Created by Akexorcist on 5/11/16 AD.
 */
public class KIOSKReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null) {
            String action = intent.getAction();
            if (action.equals(KioskMode.ACTION_ENABLE_KIOSK_MODE_RESULT)) {
                // KIOSK enabled
                addApplicationShortcut(context);
            } else if (action.equals(KioskMode.ACTION_DISABLE_KIOSK_MODE_RESULT)) {
                // KIOSK disabled
                removeApplicationShortcut(context);
            } else if (action.equals(KioskMode.ACTION_UNEXPECTED_KIOSK_BEHAVIOR)) {
                // KIOSK unexpected error
            }
        }
    }

    @SuppressWarnings("WrongConstant")
    void addApplicationShortcut(Context context) {
        EnterpriseDeviceManager edm = (EnterpriseDeviceManager) context.getSystemService(EnterpriseDeviceManager.ENTERPRISE_POLICY_SERVICE);
        ApplicationPolicy appPolicy = edm.getApplicationPolicy();
        appPolicy.addHomeShortcut(context.getPackageName(), null);
    }

    @SuppressWarnings("WrongConstant")
    void removeApplicationShortcut(Context context) {
        EnterpriseDeviceManager edm = (EnterpriseDeviceManager) context.getSystemService(EnterpriseDeviceManager.ENTERPRISE_POLICY_SERVICE);
        ApplicationPolicy appPolicy = edm.getApplicationPolicy();
        appPolicy.deleteHomeShortcut(context.getPackageName(), null);
    }
}


@@ -694,32 +694,58 @@
 Shortcut(context
+, context.getPackageName()
 );%0A            %7D
@@ -898,16 +898,42 @@
 (context
+, context.getPackageName()
 );%0A     
@@ -1162,32 +1162,52 @@
 (Context context
+, String packageName
 ) %7B%0A        Ente
@@ -1393,32 +1393,120 @@
 cationPolicy();%0A
+        String kioskPackageName = KioskMode.getInstance(context).getKioskHomePackage();%0A
         appPolic
@@ -1519,35 +1519,42 @@
 omeShortcut(
-context.get
+packageName, kiosk
 PackageName(
@@ -1548,32 +1548,24 @@
 kPackageName
-(), null
 );%0A    %7D%0A%0A  
@@ -1651,16 +1651,36 @@
  context
+, String packageName
 ) %7B%0A    
@@ -1874,32 +1874,120 @@
 cationPolicy();%0A
+        String kioskPackageName = KioskMode.getInstance(context).getKioskHomePackage();%0A
         appPolic
@@ -2007,27 +2007,34 @@
 hortcut(
-context.get
+packageName, kiosk
 PackageN
@@ -2040,16 +2040,8 @@
 Name
-(), null
 );%0A 

Change mothod fore more dynamically called