// Path: KNOXSampleApp/knoxActivator/src/main/java/com/akexorcist/knoxactivator/event/AdminDeactivatedEvent.java
// public class AdminDeactivatedEvent { }
// 
// Path: KNOXSampleApp/knoxActivator/src/main/java/com/akexorcist/knoxactivator/event/LicenseActivatedEvent.java
// public class LicenseActivatedEvent { }
// 
// Path: KNOXSampleApp/knoxActivator/src/main/java/com/akexorcist/knoxactivator/event/LicenseActivationFailedEvent.java
// public class LicenseActivationFailedEvent {
//     int errorType;
// 
//     public LicenseActivationFailedEvent(int errorType) {
//         this.errorType = errorType;
//     }
// 
//     public int getErrorType() {
//         return errorType;
//     }
// }
// 
// Path: KNOXSampleApp/knoxActivator/src/main/java/com/akexorcist/knoxactivator/receiver/AdminActivationReceiver.java
// public class AdminActivationReceiver extends DeviceAdminReceiver {
//     private static final long EVENT_LISTENER_DELAY = 500;
// 
//     @Override
//     public void onEnabled(Context context, Intent intent) {
//     }
// 
//     @Override
//     public CharSequence onDisableRequested(Context context, Intent intent) {
//         return context.getString(R.string.deactivate_device_admin_warning);
//     }
// 
//     @Override
//     public void onDisabled(Context context, Intent intent) {
//         new Handler().postDelayed(new Runnable() {
//             @Override
//             public void run() {
//                 KnoxActivationBus.getInstance().getBus().post(new AdminDeactivatedEvent());
//             }
//         }, EVENT_LISTENER_DELAY);
//     }
// }


import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.app.enterprise.EnterpriseDeviceManager;
import android.app.enterprise.license.EnterpriseLicenseManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import com.akexorcist.knoxactivator.event.AdminDeactivatedEvent;
import com.akexorcist.knoxactivator.event.LicenseActivatedEvent;
import com.akexorcist.knoxactivator.event.LicenseActivationFailedEvent;
import com.akexorcist.knoxactivator.receiver.AdminActivationReceiver;
import com.squareup.otto.Subscribe;
        return !(requiredVersion != null && enterpriseDeviceManager.getEnterpriseSdkVer().ordinal() < requiredVersion.ordinal());
    }

    public void activateLicense(Context context, String licenseKey) {
        EnterpriseLicenseManager enterpriseLicenseManager = EnterpriseLicenseManager.getInstance(context);
        enterpriseLicenseManager.activateLicense(licenseKey, context.getPackageName());
    }

    public void activateDeviceAdmin(Activity activity) {
        ComponentName componentName = new ComponentName(activity, AdminActivationReceiver.class);
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
        activity.startActivityForResult(intent, REQUEST_CODE_KNOX);
    }

    public void deactivateDeviceAdmin(Context context) {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName componentName = new ComponentName(context, AdminActivationReceiver.class);
        devicePolicyManager.removeActiveAdmin(componentName);
    }

    public void onActivityResult(int requestCode, final int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_KNOX) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (activationCallback != null) {
                        if (resultCode == Activity.RESULT_OK) {
                            activationCallback.onDeviceAdminActivated();
                        } else if (resultCode == Activity.RESULT_CANCELED) {
// Path: KNOXSampleApp/knoxActivator/src/main/java/com/akexorcist/knoxactivator/event/AdminDeactivatedEvent.java
// public class AdminDeactivatedEvent { }
// 
// Path: KNOXSampleApp/knoxActivator/src/main/java/com/akexorcist/knoxactivator/event/LicenseActivatedEvent.java
// public class LicenseActivatedEvent { }
// 
// Path: KNOXSampleApp/knoxActivator/src/main/java/com/akexorcist/knoxactivator/event/LicenseActivationFailedEvent.java
// public class LicenseActivationFailedEvent {
//     int errorType;
// 
//     public LicenseActivationFailedEvent(int errorType) {
//         this.errorType = errorType;
//     }
// 
//     public int getErrorType() {
//         return errorType;
//     }
// }
// 
// Path: KNOXSampleApp/knoxActivator/src/main/java/com/akexorcist/knoxactivator/receiver/AdminActivationReceiver.java
// public class AdminActivationReceiver extends DeviceAdminReceiver {
//     private static final long EVENT_LISTENER_DELAY = 500;
// 
//     @Override
//     public void onEnabled(Context context, Intent intent) {
//     }
// 
//     @Override
//     public CharSequence onDisableRequested(Context context, Intent intent) {
//         return context.getString(R.string.deactivate_device_admin_warning);
//     }
// 
//     @Override
//     public void onDisabled(Context context, Intent intent) {
//         new Handler().postDelayed(new Runnable() {
//             @Override
//             public void run() {
//                 KnoxActivationBus.getInstance().getBus().post(new AdminDeactivatedEvent());
//             }
//         }, EVENT_LISTENER_DELAY);
//     }
// }


// Path: KNOXSampleApp/knoxActivator/src/main/java/com/akexorcist/knoxactivator/KnoxActivationManager.java
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.app.enterprise.EnterpriseDeviceManager;
import android.app.enterprise.license.EnterpriseLicenseManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import com.akexorcist.knoxactivator.event.AdminDeactivatedEvent;
import com.akexorcist.knoxactivator.event.LicenseActivatedEvent;
import com.akexorcist.knoxactivator.event.LicenseActivationFailedEvent;
import com.akexorcist.knoxactivator.receiver.AdminActivationReceiver;
import com.squareup.otto.Subscribe;

        return !(requiredVersion != null && enterpriseDeviceManager.getEnterpriseSdkVer().ordinal() < requiredVersion.ordinal());
    }

    public void activateLicense(Context context, String licenseKey) {
        EnterpriseLicenseManager enterpriseLicenseManager = EnterpriseLicenseManager.getInstance(context);
        enterpriseLicenseManager.activateLicense(licenseKey, context.getPackageName());
    }

    public void activateDeviceAdmin(Activity activity) {
        ComponentName componentName = new ComponentName(activity, AdminActivationReceiver.class);
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
        activity.startActivityForResult(intent, REQUEST_CODE_KNOX);
    }

    public void deactivateDeviceAdmin(Context context) {
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
        ComponentName componentName = new ComponentName(context, AdminActivationReceiver.class);
        devicePolicyManager.removeActiveAdmin(componentName);
    }

    public void onActivityResult(int requestCode, final int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE_KNOX) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (activationCallback != null) {
                        if (resultCode == Activity.RESULT_OK) {
                            activationCallback.onDeviceAdminActivated();
                        } else if (resultCode == Activity.RESULT_CANCELED) {
                            activationCallback.onDeviceAdminActivationCancelled();