Menu

Initialize the app

Here, we'll set up some basic functions for the app.

  • onCreate(): links the xml layout to the activities, sets the enabled state of the buttons, then declares and initializes onClickListeners to call these respective buttons: toggleAdmin, activateLicense, deactivateLicense, activateBackwardsCompatibleKey, toggleCameraState, toggleTimaState.

NOTE – Some of the methods do not resolve, and will be created later in the tutorial.

   protected void onCreate(Bundle savedInstanceState) {
        //...called when the activity is starting. This is where most initialization should go.
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView logView = findViewById(R.id.logview_id);
        logView.setMovementMethod(new ScrollingMovementMethod());
        mUtils = new Utils(logView, TAG);

        mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        mDeviceAdmin = new ComponentName(MainActivity.this, SampleAdminReceiver.class);

        mToggleAdminBtn = findViewById(R.id.toggleAdminBtn);
        mToggleAdminBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {toggleAdmin();
            }
        });

        Button activateLicenseBtn = findViewById(R.id.activateLicenseBtn);
        activateLicenseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activateLicense();
            }
        });

        Button deactivateLicenseBtn = findViewById(R.id.deactivateLicenseBtn);
        deactivateLicenseBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deactivateLicense();
            }
        });

        Button activateBackwardsCompatibleKeyBtn = findViewById(R.id.activateBackwardsCompatibleKeyBtn);
        activateBackwardsCompatibleKeyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activateBackwardsCompatibleKey();
            }
        });

        // Backwards Compatibility button is only visible if the device is on at least
        // knox version 2.5 AND lower than knox version 2.8 (Knox API 17 to 21)
        if(EnterpriseDeviceManager.getAPILevel() < EnterpriseDeviceManager.KNOX_VERSION_CODES.KNOX_2_8 &&
                EnterpriseDeviceManager.getAPILevel() >= EnterpriseDeviceManager.KNOX_VERSION_CODES.KNOX_2_5) {
            activateBackwardsCompatibleKeyBtn.setVisibility(View.VISIBLE);
        }

        Button toggleCameraBtn = findViewById(R.id.toggleCameraBtn);
        toggleCameraBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                toggleCameraState();
            }
        });

        Button toggleTimaKeystoreStateBtn = findViewById(R.id.toggleTimaKeystoreState);
        toggleTimaKeystoreStateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleTimaKeystoreState();
            }
        });
    }

Tutorial Progress

You are 2/7 done! Go to the next step.

Share it: