matthew womersley
Jun 20, 2016
3:33 pm

Activating ELM licence fails to activate first time

Hi

I have the following code that activates my deveopment licences.

The screen shows that has the T&Cs and checkbox. I check the checkbox and click confrm.

The app tries to validate the licenses but always fails on the first time. When i click the back button and run the Activity again it works fine.

I noticed that onPause has code that unregister the KnoxCustomReceiver, so i put code in that only unregisters it if the license has been validated.

Has anyone code code that i can use to validate the licenses or does anyone know why this is happening?

I'd like that KnoxActivity to validate the license then launch the MenuActivity on successful validation.

Thanks

 

Matt



public class KnoxActivity extends Activity  {

    public static final String TAG = KnoxActivity.class.getSimpleName();
    private final String mPassCode = "12345";
    private KnoxCustomReceiver mKnoxCustomReceiver = null;
    private Handler mHandler = new Handler();
    AppObj appObj;






    protected class KnoxCustomReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String intentAction = intent.getAction();
            Log.e(TAG, intentAction + " received");
            if(intentAction.equals(EnterpriseLicenseManager.ACTION_LICENSE_STATUS)) {
                String str = intent.getStringExtra(EnterpriseLicenseManager.EXTRA_LICENSE_STATUS);
                if (str.equals("fail")) {
                    Toast.makeText(context, "Failed to activate Enterprise license!", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "Failed to activate Enterprise license!");
                }else {
                    Toast.makeText(context, "Enterprise license activated Successfully.", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "Enterprise license activated Successfully.");

                    Intent intent2 = new Intent(KnoxActivity.this, MenuActivity.class);
                    intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent2);
                }

            } else if(intentAction.equals(KnoxEnterpriseLicenseManager.ACTION_LICENSE_STATUS)) {
                String str = intent.getStringExtra(KnoxEnterpriseLicenseManager.EXTRA_LICENSE_STATUS);
                if (str.equals("fail")) {
                    Toast.makeText(context, "Failed to activate KnoxEnterprise license!", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "Failed to activate KnoxEnterprise license!");
                }else {
                    Toast.makeText(context, "KnoxEnterprise license activated Successfully.", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "KnoxEnterprise license activated Successfully.");

                    Intent intent3 = new Intent(KnoxActivity.this, MenuActivity.class);
                    intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent3);
                }
            }


        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_knox);

        Log.e(TAG, "inside onCreate");

        appObj = (AppObj)getApplication();


        if (mKnoxCustomReceiver == null) {

            Log.e(TAG, " mKnoxCustomReceiver == null so going create and register it");

            mKnoxCustomReceiver = new KnoxCustomReceiver();

            IntentFilter filter = new IntentFilter();
            filter.addAction(EnterpriseLicenseManager.ACTION_LICENSE_STATUS);
            filter.addAction(KnoxEnterpriseLicenseManager.ACTION_LICENSE_STATUS);
            registerReceiver(mKnoxCustomReceiver, filter);

        }else{

            Log.e(TAG, " mKnoxCustomReceiver not null so just registering it");

            IntentFilter filter = new IntentFilter();
            filter.addAction(EnterpriseLicenseManager.ACTION_LICENSE_STATUS);
            filter.addAction(KnoxEnterpriseLicenseManager.ACTION_LICENSE_STATUS);
            registerReceiver(mKnoxCustomReceiver, filter);
        }

        // Ensure application has device admin privileges
        ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
        DevicePolicyManager dpm =
                (DevicePolicyManager)getSystemService(Context.DEVICE_POLICY_SERVICE);
        if(!dpm.isAdminActive(deviceAdmin)) {
            Log.e(TAG, "enabling application as device admin");
            try {
                Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdmin);
                intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "");
                startActivity(intent);
            } catch (Exception e) {
                Log.d(TAG, "Exception: " + e);
            }
        }else{

            Log.e(TAG, "device admin already active");
        }



        KnoxCustomManager kcm = KnoxCustomManager.getInstance();
        String permission = "android.permission.sec.MDM_HW_CONTROL";
        boolean bIsELMActive = kcm.checkEnterprisePermission(permission);


        if(bIsELMActive == true){

            Log.e(TAG, "ELM already active");

            Intent intent = new Intent(this, MenuActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

        }else {


            Log.e(TAG, "ELM not active");
            onClickActivateELM();
            onClickActivateKLM();


        }


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (mKnoxCustomReceiver != null) {
            Log.e(TAG, " mKnoxCustomReceiver not null so going to register it in onresume");
            IntentFilter filter = new IntentFilter();
            filter.addAction(EnterpriseLicenseManager.ACTION_LICENSE_STATUS);
            filter.addAction(KnoxEnterpriseLicenseManager.ACTION_LICENSE_STATUS);
            registerReceiver(mKnoxCustomReceiver, filter);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        Log.e(TAG, "inside onPause");

        if(checkIfElmIsActive() == true) {

                if (mKnoxCustomReceiver != null) {
                    Log.e(TAG, " mKnoxCustomReceiver not null so going to unregiter it !!!!!!!!!!!");
                    unregisterReceiver(mKnoxCustomReceiver);
                }

        }
    }

    @Override
    public void onBackPressed() {

        exitApp();
    }



    public void onClickActivateELM() {


        EnterpriseLicenseManager elm = EnterpriseLicenseManager.getInstance(getApplicationContext());

        String key = "32B739335DBF83240DE7C64475E8C734227ADBF60AFF4D312E042B0FF08E78B0B749B7061CE3B18B95FBB9AFC47695BEBBD049920A511F785DBBF2CA4F4BA2C8";
        elm.activateLicense(key);
        Log.e(TAG, "just called activate elm licence");
    }

    public void onClickActivateKLM() {


        KnoxEnterpriseLicenseManager klm = KnoxEnterpriseLicenseManager.getInstance(getApplicationContext());
        String klmKey = "KLM03-25BEU-MQKW6-CUS85-OBGZK-BMO1C";

        klm.activateLicense(klmKey);
        Log.e(TAG, "just called activate klm licence");
    }

    

    private boolean exitApp() {
        // Disable sealed mode if already enabled
        KnoxCustomManager kcm = KnoxCustomManager.getInstance();
        if( true == kcm.getSealedState() ){
            String msg = "setSealedState TRUE";
            try{
                int result = 0;
                result = kcm.setSealedState(false, mPassCode);
                msg = msg + "\nResult = " + getResultString( result );
            } catch ( Exception e ) {
                msg = msg + "\nException = " + e;
            }
            Toast.makeText( this, msg, Toast.LENGTH_SHORT ).show();
        }

        // Now, exit the app
        finish();

        return true;
    }

    boolean checkIfElmIsActive(){



        KnoxCustomManager kcm = KnoxCustomManager.getInstance();
        String permission = "android.permission.sec.MDM_HW_CONTROL";
        boolean bIsELMActive = kcm.checkEnterprisePermission(permission);

        return bIsELMActive;




    }

    private String getResultString( int result ) {
        String resultString = null;

        switch ( result ) {
            case KnoxCustomManager.SUCCESS:
                resultString = "SUCCESS";
                break;
            case KnoxCustomManager.FAIL:
                resultString = "FAIL";
                break;
            case KnoxCustomManager.SEALED_MODE_NOT_ACTIVE:
                resultString = "SEALED_MODE_NOT_ACTIVE";
                break;
            case KnoxCustomManager.SEALED_MODE_ACTIVE:
                resultString = "SEALED_MODE_ACTIVE";
                break;
            case KnoxCustomManager.PERMISSION_DENIED:
                resultString = "PERMISSION_DENIED";
                break;
            case KnoxCustomManager.BUSY:
                resultString = "BUSY";
                break;
            case KnoxCustomManager.NOT_SUPPORTED:
                resultString = "NOT_SUPPORTED";
                break;
            case KnoxCustomManager.POLICY_RESTRICTED:
                resultString = "POLICY_RESTRICTED";
                break;
            case KnoxCustomManager.INVALID_PASSCODE:
                resultString = "INVALID_PASSCODE";
                break;
            case KnoxCustomManager.INVALID_PACKAGE:
                resultString = "INVALID_PACKAGE";
                break;
            case KnoxCustomManager.INVALID_RING_TONE_TYPE:
                resultString = "INVALID_RING_TONE_TYPE";
                break;
            case KnoxCustomManager.RING_TONE_NOT_FOUND:
                resultString = "RING_TONE_NOT_FOUND";
                break;
            case KnoxCustomManager.INVALID_ADDRESS:
                resultString = "INVALID_ADDRESS";
                break;
            case KnoxCustomManager.INVALID_PERMISSION:
                resultString = "INVALID_PERMISSION";
                break;
            case KnoxCustomManager.INVALID_SOUND_TYPE:
                resultString = "INVALID_SOUND_TYPE";
                break;
            case KnoxCustomManager.INVALID_ROTATION_TYPE:
                resultString = "INVALID_ROTATION_TYPE";
                break;
            case KnoxCustomManager.INVALID_STRING:
                resultString = "INVALID_STRING";
                break;
            case KnoxCustomManager.INVALID_STRING_TYPE:
                resultString = "INVALID_STRING_TYPE";
                break;
            case KnoxCustomManager.INVALID_PERCENT_VALUE:
                resultString = "INVALID_PERCENT_VALUE";
                break;
            case KnoxCustomManager.INVALID_LOCALE:
                resultString = "INVALID_LOCALE";
                break;
            case KnoxCustomManager.INVALID_TIMEOUT:
                resultString = "INVALID_TIMEOUT";
                break;
            case KnoxCustomManager.INVALID_UID:
                resultString = "INVALID_UID";
                break;
            case KnoxCustomManager.INVALID_DEVICE:
                resultString = "INVALID_DEVICE";
                break;
            case KnoxCustomManager.ERROR_UNKNOWN:
                resultString = "ERROR_UNKNOWN";
                break;

            default:
                resultString = "Unknown " + result;
                break;
        }
        resultString = " " + resultString + " ";
        return resultString;
    }
}

 

output on first run of Activity:

edm.intent.action.license.status received

E/KnoxActivity﹕ Failed to activate Enterprise license!

Similar topics

No similar topics found.
Kieran M
Jun 20, 2016
4:29 pm

Hi Matthew,

Can you let me know if the ELM license activation fails the first time in the following sample app?

https://seap.samsung.com/sample-app/get-started-knox-standard-sdk

Comments

Hi Matt,
If it fits your use case then you are free to use this code.

Can you please share the thrown exception?

Kieran MJun 20, 2016 at 6:18 pm

Hi Kieran

yes it does activate the elm license first time.

it seems to throw a security exception first and the the license activates.

should i use this code?

 

thanks

 

Matt

 

[EDIT]

The exception is a SecurityException. it says Admin does not have MDM_HW_CONTROL permission

matthew womersleyJun 20, 2016 at 6:23 pm
2