Skip to content

Instantly share code, notes, and snippets.

@raiym
Last active February 21, 2023 07:54
Show Gist options
  • Save raiym/2861fcc4a46bd706b7830cb037541538 to your computer and use it in GitHub Desktop.
Save raiym/2861fcc4a46bd706b7830cb037541538 to your computer and use it in GitHub Desktop.
Activate KNOX Standard SDK Demo
public class DeviceAdminInteractor {
private static final String LOG_TAG = DeviceAdminInteractor.class.getName();
// Can be dev and production key
private static final String ELM_LICENSE_KEY
= "SECRETKEY";
public ComponentName componentName;
private DevicePolicyManager devicePolicyManager;
/**
* Samsung KNOX Standard SDK related variables
*/
private EnterpriseDeviceManager enterpriseDeviceManager;
// more info about restriction policy https://seap.samsung.com/api-references/android-standard/reference/android/app/enterprise/RestrictionPolicy.html
private RestrictionPolicy mRestrictionPolicy;
// more info about location policy https://seap.samsung.com/api-references/android-standard/reference/android/app/enterprise/LocationPolicy.html
private LocationPolicy mLocationPolicy;
// more info about application policy https://seap.samsung.com/api-references/android-standard/reference/android/app/enterprise/ApplicationPolicy.html
private ApplicationPolicy mApplicationPolicy;
private Context mContext;
public DeviceAdminInteractor(Context context) {
this.mContext = context;
devicePolicyManager =
(DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
componentName = new ComponentName(mContext, CustomDeviceAdminReceiver.class);
enterpriseDeviceManager =
(EnterpriseDeviceManager) mContext.getSystemService(EnterpriseDeviceManager.ENTERPRISE_POLICY_SERVICE);
if (isKnoxEnbaled()) {
mRestrictionPolicy = enterpriseDeviceManager.getRestrictionPolicy();
mLocationPolicy = enterpriseDeviceManager.getLocationPolicy();
mApplicationPolicy = enterpriseDeviceManager.getApplicationPolicy();
}
}
/**
* Check if admin enabled
*
* @return void
*/
public boolean isActiveAdmin() {
return devicePolicyManager.isAdminActive(componentName);
}
/**
* Force user to enadle administrator
*/
public void forceEnableAdmin() {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Policy provider");
((Activity) mContext).startActivityForResult(intent, RESULT_ENABLE);
}
/**
* Force to activate Samsung KNOX Standard SDK
*/
public void forceActivateKnox() throws Exception {
try {
EnterpriseLicenseManager.getInstance(mContext)
.activateLicense(ELM_LICENSE_KEY);
mRestrictionPolicy = enterpriseDeviceManager.getRestrictionPolicy();
mLocationPolicy = enterpriseDeviceManager.getLocationPolicy();
mApplicationPolicy = enterpriseDeviceManager.getApplicationPolicy();
} catch (Exception e) {
Log.e(LOG_TAG, "Failed to activate license", e);
throw new Exception("Failed to activate license");
}
}
/**
* Check if KNOX enabled
*/
public boolean isKnoxEnbaled() {
return mContext.checkCallingOrSelfPermission("android.permission.sec.MDM_RESTRICTION")
== PackageManager.PERMISSION_GRANTED;
}
/**
* Example how to install applicaton
*/
public boolean installApk(String pathToApk) {
try {
boolean result = mApplicationPolicy.installApplication(pathToApk, false);
Log.i(LOG_TAG, "Is Application installed: " + result);
return result;
} catch (Throwable e) {
Log.e(LOG_TAG, "Failed to install application", e);
return false;
}
}
/**
* Example of using locationPolicy
*/
/**
* Allow or disallow changing state of providers
*
* @param allow true if allow. false if not
* @return is state changed
*/
public boolean allowProvidersStateChange(boolean allow) {
Log.d(LOG_TAG, "Entering allowProvidersStateChange method, allow: " + allow);
boolean isApplied = mLocationPolicy.setGPSStateChangeAllowed(allow);
Log.d(LOG_TAG, "Is policy applid: " + isApplied);
return isApplied;
}
}
// Assume we have activity
public class MainActivity {
private static final String LOG_TAG = MainActivity.class.getName();
private DeviceAdminInteractor mAdminInteractor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// DeviceAdminInteractor see next file
mAdminInteractor = new DeviceAdminInteractor(this);
}
@Override
protected void onResume() {
super.onResume();
if (!mAdminInteractor.isActiveAdmin()) {
Log.d(LOG_TAG, "Admin is not active. Request enabling");
mAdminInteractor.forceEnableAdmin();
}
if (mAdminInteractor.isActiveAdmin()) {
Log.d(LOG_TAG, "Admin already enabled");
Log.d(LOG_TAG, "Checking Samsung KNOX SDK");
if (!mAdminInteractor.isKnoxEnbaled()) {
Log.d(LOG_TAG, "Admin is not enabled. Request enabling.");
try {
mAdminInteractor.forceActivateKnox();
} catch (Exception e) {
Log.e(LOG_TAG, "Failed to activate admin", e);
}
}
if (mAdminInteractor.isKnoxEnbaled()) {
Log.i(LOG_TAG, "Everything okay, admin and knox enabled");
}
}
if (mAdminInteractor.isActiveAdmin() && mAdminInteractor.isKnoxEnbaled()) {
// Let's install Instagram apk silently (You can download it from https://apkpure.com/instagram/com.instagram.android)
// Assume that you downloaded apk to Download folder in your device
File file = Environment.getExternalStorageDirectory();
String apkPath = file.getAbsolutePath() + "/Download/instagram.apk";
if (apkFile.exists()) {
Log.d(LOG_TAG, "Instagram file exists. Trying to install application");
mAdminInteractor.installApk(apkPath);
} else {
Log.w(LOG_TAG, "Instagram file doesn't exist.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment