Menu

Initialize the app

Here, we set up the app with some basic functions.

  • onCreate(): links the XML layout to the activities, sets the enabled state of the buttons, and declares and initializes onClickListeners to call the respective buttons: toggleAdminBtn, activateLicenseBtn, deactivateLicenseBtn, activateBackwardsCompatibleKeyBtn, toggleDefaultKioskBtn toggleCustomKioskBtn.

NOTE – Some of the methods that do not resolve are created later in the tutorial.

public class MainActivity extends AppCompatActivity {

    private final String TAG = "MainActivity";
    static final int DEVICE_ADMIN_ADD_RESULT_ENABLE = 1;

    private DevicePolicyManager mDevicePolicyManager;
    private ComponentName mDeviceAdmin;
    private Utils mUtils;
    private Button mToggleAdminBtn;
    private Button mToggleDefaultKioskBtn;
    private Button mToggleCustomKioskBtn;
    private SampleKioskReceiver mSampleKioskReceiver;

    @Override
    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
        // between Knox version 2.5 and 2.7.1 / 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);
        }

        mToggleDefaultKioskBtn = findViewById(R.id.toggleDefaultKioskBtn);
        mToggleDefaultKioskBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toggleKiosk();
            }
        });

        mToggleCustomKioskBtn = findViewById(R.id.toggleCustomKioskBtn);
        mToggleCustomKioskBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                promptCustomKioskSetting();
            }
        });

        mSampleKioskReceiver = new SampleKioskReceiver();
        mSampleKioskReceiver.setMainActivityHandler(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(KioskMode.ACTION_ENABLE_KIOSK_MODE_RESULT);
        intentFilter.addAction(KioskMode.ACTION_DISABLE_KIOSK_MODE_RESULT);
        registerReceiver(mSampleKioskReceiver, intentFilter);
    }

Tutorial Progress

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

Share it: