Initialize 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
to handle functions like the logger and exceptions that are used in-app.
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;
public class Utils {
private TextView mTextView;
private String mTag;
public Utils(TextView view, String className) {
mTextView = view;
mTag = 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) {
mTextView.append(text);
mTextView.append("\n\n");
mTextView.invalidate();
Log.d(mTag, 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();
mTextView.append(msg);
mTextView.append("\n\n");
mTextView.invalidate();
Log.e(TAG, msg);
}
}
}
Set up your MainActivity
Here, 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 initializes on-click listeners to call the buttons created in Step 1.
- Checks if the device supports Knox 3.0 (minimum SDK level 24). If not, then it disables the ENFORCE MULTI-FACTOR AUTHENTICATION button.
- Checks if the device already has a Android profile installed.
public class MainActivity extends AppCompatActivity {
public static final String TAG = "MainActivity";
private static final int ACTIVITY_REQUEST_CODE = 1;
private static final int MINIMUM_KNOX_API_LEVEL = 24;
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 = findViewById(R.id.logview_id);
logView.setMovementMethod(new ScrollingMovementMethod());
mUtils = new Utils(logView, TAG);
// Check if device supports Knox API level 24 (Knox 3.0) at minimum
// With Knox 3.0, Knox Platform for Enterprise (KPE) is harmonized with Android Enterprise (AE)
mUtils.checkApiLevel(MINIMUM_KNOX_API_LEVEL, this);
Button createAndroidProfileBtn = findViewById(R.id.createAndroidProfileBtn);
createAndroidProfileBtn.setOnClickListener(v -> createAndroidProfile());
Button activateLicenseBtn = findViewById(R.id.activateLicenseBtn);
activateLicenseBtn.setOnClickListener(v -> activateLicense());
Button deactivateLicenseBtn = findViewById(R.id.deactivateLicenseBtn);
deactivateLicenseBtn.setOnClickListener(v -> deactivateLicense());
Button toggleCameraBtn = findViewById(R.id.toggleCameraBtn);
toggleCameraBtn.setOnClickListener(v -> toggleCameraState());
Button enforceMfaBtn = findViewById(R.id.enforceMfaBtn);
enforceMfaBtn.setOnClickListener(v -> enforceMfa());
// Check if the application is a Profile Owner
DevicePolicyManager devicePolicyManager =
(DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
boolean isProfileOwner = devicePolicyManager.isProfileOwnerApp(getPackageName());
if (isProfileOwner) {
createAndroidProfileBtn.setEnabled(false);
} else {
activateLicenseBtn.setEnabled(false);
deactivateLicenseBtn.setEnabled(false);
toggleCameraBtn.setEnabled(false);
enforceMfaBtn.setEnabled(false);
}
}
Tutorial progress
You’ve completed 2/6 steps!
NextOn this page
Is this page helpful?