Initialize app
Here, we set up the app with some basic functions.
onCreate()
— links the XML layout to the activities and sets the enabled state of the buttons. It also declares and initializes onClickListeners
to call the buttons created in Step 2 of this tutorial.
Some of the methods that do not resolve are created later in the tutorial.
public class MainActivity extends AppCompatActivity {
private static final int ADD_PACKAGE = 1;
private static final int REMOVE_PACKAGE = 2;
private static final int CHECK_PACKAGE = 3;
private final String TAG = "MainActivity";
static final int DEVICE_ADMIN_ADD_RESULT_ENABLE = 1;
private Button mToggleAdminBtn;
private ComponentName mDeviceAdmin;
private DevicePolicyManager mDPM;
private CCMProfile mCCMProfileObj;
private CertificateProfile mCertificateProfile;
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());
mDeviceAdmin = new ComponentName(MainActivity.this, SampleAdminReceiver.class);
mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
mUtils = new Utils(LogView, TAG);
// Check if device supports Knox SDK
mUtils.checkApiLevel(24, this);
mToggleAdminBtn = (Button) findViewById(R.id.ToggleAdminbtn);
Button ActivateLicenseBtn = (Button) findViewById(R.id.ActivateLicensebtn);
Button DeactivateLicenseBtn = (Button) findViewById(R.id.DeactivateLicensebtn);
Button SetCCMProfileBtn = (Button) findViewById(R.id.SetCCMProfilebtn);
Button GetCCMProfileBtn = (Button) findViewById(R.id.GetCCMProfilebtn);
Button DeleteCCMProfileBtn = (Button) findViewById(R.id.DeleteCCMProfilebtn);
Button GetDefaultCertProfileBtn = (Button) findViewById(R.id.GetDefaultCertAliasbtn);
Button InstallCertBtn = (Button) findViewById(R.id.InstallCertificatebtn);
Button DeleteCertBtn = (Button) findViewById(R.id.DeleteCertificatebtn);
Button AddPackageExemptListBtn = (Button) findViewById(R.id.AddPackageExemptListbtn);
Button RemovePackageExemptListBtn = (Button) findViewById(R.id.RemovePackageExemptListbtn);
Button CheckCCMEnabledForPackageBtn = (Button) findViewById(R.id.IsCCMPolicyEnabledForPackagebtn);
Button ToggleTimaKeystoreBtn = (Button) findViewById(R.id.ToggleTimaKeystorebtn);
Button IsTimaKeystoreEnabledBtn = (Button) findViewById(R.id.IsTimaKeystoreEnabledbtn);
mToggleAdminBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleAdmin();
}
});
ActivateLicenseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
activateLicense();
}
});
DeactivateLicenseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deactivateLicense();
}
});
SetCCMProfileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createCCMProfile();
}
});
GetCCMProfileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getCCMProfile();
}
});
DeleteCCMProfileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteCCMProfile();
}
});
GetDefaultCertProfileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDefaultCertificateAlias();
}
});
InstallCertBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createCertificate();
}
});
DeleteCertBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptUserToDeleteCertificate();
}
});
AddPackageExemptListBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptUserForPackage(ADD_PACKAGE);
}
});
RemovePackageExemptListBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptUserForPackage(REMOVE_PACKAGE);
}
});
CheckCCMEnabledForPackageBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
promptUserForPackage(CHECK_PACKAGE);
}
});
ToggleTimaKeystoreBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleTimaKeystore();
}
});
IsTimaKeystoreEnabledBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isTimaKeystoreEnabled();
}
});
}
//the logic for onClickListener functions are done here
}
Tutorial progress
You’ve completed 2/7 steps!
NextOn this page
Is this page helpful?