Menu

Initialize the app

This section shows you how to set up the MainActivity and utility classes.

Import utilities classes

For this tutorial to work seamlessly, you must include a utility class, as shown in the image below.

To do this, create a class called Utils. This handle functions such as the logger and exceptions that are used in app.

package com.samsung.knox.example.container;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.widget.TextView;

import com.samsung.android.knox.EnterpriseDeviceManager;
import com.samsung.android.knox.container.KnoxContainerManager;

import java.util.List;

public class Utils {

    private TextView textView;
    private String TAG;

    public Utils(TextView view, String className) {
        textView = view;
        TAG = className;
    }

    /** Check Knox API level on device, if it does not meet minimum requirement, end user
     * cannot use the applciation */
    public void checkApiLevel(int apiLevel, final Context context)
    {
        if(EnterpriseDeviceManager.getAPILevel() < apiLevel) {
            AlertDialog.Builder builder;
            builder = new AlertDialog.Builder(context);
            String msg = context.getResources().getString(R.string.api_level_message, EnterpriseDeviceManager.getAPILevel(),apiLevel);
            builder.setTitle(R.string.app_name)
                    .setMessage(msg)
                    .setCancelable(false)
                    .setPositiveButton("CLOSE",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    System.exit(0);
                                }
                            })
                    .show();

        } else {
            return;
        }
    }

    /** Log results to a textView in application UI */
    public void log(String text) {
        textView.append(text);
        textView.append("\n\n");
        textView.invalidate();
        Log.d(TAG,text);
    }

    /** Process the exception */
    public void processException(Exception ex, String TAG) {
        if (ex != null) {
            // present the exception message
            String msg = ex.getClass().getCanonicalName() + ": " + ex.getMessage();
            textView.append(msg);
            textView.append("\n\n");
            textView.invalidate();
            Log.e(TAG, msg);
        }
    }

    public int findMyContainerId() {

        int theId = -1;
        List<Integer> ids = KnoxContainerManager.getContainers();
        if (ids != null && !ids.isEmpty()) {
            theId = ids.get(0);
        }
        Log.d(TAG, "findMyContainerId: " + theId);
        return theId;
    }
}

Set up your MainActivity

Here, we set up your MainActivity with some basic functions:

onCreate():

  • Links the xml layout to the activities.
  • Sets the enabled state of the buttons and declares and initializesa onClickListeners to call the buttons created in step 1.
  • Checks if the device supports Knox 3.0 (minimum SDK level 24).
  • Checks if the device already has a Android Profile installed.

 public class MainActivity extends AppCompatActivity {

        public static final String TAG = "MainActivity";
        private Utils mUtils;

    @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 = (TextView) findViewById(R.id.logview_id);
        LogView.setMovementMethod(new ScrollingMovementMethod());
        mUtils = new Utils(LogView, TAG);

        // Check if device supports Knox SDK
        mUtils.checkApiLevel(24, this);

        Button CreateAndroidProfile = (Button) findViewById(R.id.CreateAndroidProfilebtn);
        CreateAndroidProfile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                createAndroidProfile();
            }
        });
        Button ActivateLicencebtn = (Button) findViewById(R.id.ActivateLicencebtn);
        ActivateLicencebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activateLicence();
            }
        });
        Button DeActivateLicencebtn = (Button) findViewById(R.id.DeActivateLicencebtn);
        DeActivateLicencebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deActivateLicence();
            }
        });
        Button ToggleCamerabtn = (Button) findViewById(R.id.ToggleCamerabtn);
        ToggleCamerabtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                toggleCameraState();
            }
        });

        // Check if the application is a Profile Owner
        DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        boolean isProfileOwner = devicePolicyManager.isProfileOwnerApp(getPackageName());
        if(isProfileOwner){
            ToggleCamerabtn.setEnabled(true);
            CreateAndroidProfile.setEnabled(false);
        } else {
            ActivateLicencebtn.setEnabled(false);
            DeActivateLicencebtn.setEnabled(false);
            ToggleCamerabtn.setEnabled(false);
        }
    }

Tutorial Progress

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

Share it: