Skip to content

Instantly share code, notes, and snippets.

@osben
Created July 31, 2023 23:15
Show Gist options
  • Save osben/5743fecd829b353beeb19225040c0fd6 to your computer and use it in GitHub Desktop.
Save osben/5743fecd829b353beeb19225040c0fd6 to your computer and use it in GitHub Desktop.
package com.x.test.KioskMode;
import android.app.ActivityManager;
import android.app.admin.DevicePolicyManager;
import android.app.admin.SystemUpdatePolicy;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Build;
import android.os.UserManager;
import android.provider.Settings;
import android.util.Log;
import androidx.annotation.RequiresApi;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.incust.fufit.MainActivity;
@CapacitorPlugin(name = "KioskMode")
public class KioskModePlugin extends Plugin {
boolean isLockTaskModeRunning = false;
private ActivityManager am;
private Context context;
private String PackageName;
private DevicePolicyManager devicePolicyManager;
private ComponentName adminComponentName;
private String TAG = "Capacitor Kiosk Plugin";
public void load() {
context = getContext();
am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
PackageName = context.getApplicationContext().getPackageName();
devicePolicyManager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
adminComponentName = com.incust.fufit.kiosk.KioskModeDeviceAdminReceiver.getComponentName(context);
}
private boolean isLockTaskPermittedStatus() {
Log.i(TAG, "isLockTaskPermittedStatus: " + PackageName);
return devicePolicyManager.isLockTaskPermitted(PackageName);
}
public boolean isDeviceOwnerAppStatus() {
Log.i(TAG, "isDeviceOwnerAppStatus: " + PackageName);
return devicePolicyManager.isDeviceOwnerApp(PackageName);
}
@PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
public void isLockTaskPermitted(PluginCall call) {
Log.i(TAG, "isLockTaskPermitted");
call.resolve(new JSObject().put("isLockTaskPermitted", isLockTaskPermittedStatus()));
}
@PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
public void isDeviceOwnerApp(PluginCall call) {
Log.i(TAG, "isDeviceOwnerApp");
call.resolve(new JSObject().put("isDeviceOwnerApp", isDeviceOwnerAppStatus()));
}
@PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
public void enterKioskMode(PluginCall call) {
Log.i(TAG, "enterKioskMode");
setKioskPolicies(true, isDeviceOwnerAppStatus());
call.resolve();
}
@PluginMethod(returnType = PluginMethod.RETURN_PROMISE)
public void exitKioskMode(PluginCall call) {
Log.i(TAG, "exitKioskMode");
setKioskPolicies(false, isDeviceOwnerAppStatus());
call.resolve();
}
private void setKioskPolicies(boolean enable, boolean isAdmin) {
if (isAdmin) {
setRestrictions(enable);
setKeyGuardEnabled(enable);
enableStayOnWhilePluggedIn(enable);
setUpdatePolicy(enable);
setAsHomeApp(enable);
setLockTaskPackages(enable);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
setImmersiveMode(enable);
}
}
}
@RequiresApi(api = Build.VERSION_CODES.P)
private void setImmersiveMode(boolean active) {
Log.i(TAG, "setImmersiveMode: " + active);
if (active) {
devicePolicyManager.setLockTaskFeatures(
adminComponentName,
DevicePolicyManager.LOCK_TASK_FEATURE_SYSTEM_INFO |
DevicePolicyManager.LOCK_TASK_FEATURE_OVERVIEW |
DevicePolicyManager.LOCK_TASK_FEATURE_HOME |
DevicePolicyManager.LOCK_TASK_FEATURE_KEYGUARD
);
// hide navigation bar
// getActivity().getWindow().getDecorView().setSystemUiVisibility(
// SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
// );
} else {
devicePolicyManager.setLockTaskFeatures(
adminComponentName,
DevicePolicyManager.LOCK_TASK_FEATURE_NONE
);
}
}
private void setRestrictions(boolean disallow) {
setUserRestriction(UserManager.DISALLOW_SAFE_BOOT, disallow);
setUserRestriction(UserManager.DISALLOW_FACTORY_RESET, disallow);
setUserRestriction(UserManager.DISALLOW_ADD_USER, disallow);
setUserRestriction(UserManager.DISALLOW_MOUNT_PHYSICAL_MEDIA, disallow);
setUserRestriction(UserManager.DISALLOW_ADJUST_VOLUME, disallow);
}
private void setUserRestriction(String restriction, boolean disallow) {
if (disallow) {
devicePolicyManager.addUserRestriction(adminComponentName,
restriction);
} else {
devicePolicyManager.clearUserRestriction(adminComponentName,
restriction);
}
}
private void setUpdatePolicy(boolean active) {
if (active) {
devicePolicyManager.setSystemUpdatePolicy(
adminComponentName,
SystemUpdatePolicy.createWindowedInstallPolicy(60, 120)
);
} else {
devicePolicyManager.setSystemUpdatePolicy(adminComponentName, null);
}
}
private void setAsHomeApp(boolean enable) {
if (enable) {
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN);
intentFilter.addCategory(Intent.CATEGORY_HOME);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
devicePolicyManager.addPersistentPreferredActivity(
adminComponentName, intentFilter, new ComponentName(
PackageName, MainActivity.class.getName()
)
);
//
// mDecorView.setSystemUiVisibility(flags);
// mDecorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
// public void onSystemUiVisibilityChange(int visibility) {
// // Note that system bars will only be "visible" if none of the
// // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
// if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// mDecorView.setSystemUiVisibility(flags);
// } else {
// // TODO: The system bars are NOT visible. Make any desired
// // adjustments to your UI, such as hiding the action bar or
// // other navigational controls.
// }
// }
// });
} else {
devicePolicyManager.clearPackagePersistentPreferredActivities(
adminComponentName, PackageName
);
}
}
private void setKeyGuardEnabled(boolean enable) {
devicePolicyManager.setKeyguardDisabled(adminComponentName, enable);
devicePolicyManager.setStatusBarDisabled(adminComponentName, enable);
}
private void setLockTaskPackages(boolean enable) {
if (enable) {
devicePolicyManager.setLockTaskPackages(
adminComponentName,
new String[]{PackageName}
);
// devicePolicyManager.lockNow();
} else {
devicePolicyManager.setLockTaskPackages(
adminComponentName,
new String[]{}
);
}
}
private void enableStayOnWhilePluggedIn(boolean enabled) {
if (enabled) {
devicePolicyManager.setGlobalSetting(
adminComponentName,
Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
Integer.toString(BatteryManager.BATTERY_PLUGGED_AC
| BatteryManager.BATTERY_PLUGGED_USB
| BatteryManager.BATTERY_PLUGGED_WIRELESS)
);
} else {
devicePolicyManager.setGlobalSetting(
adminComponentName,
Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
"0"
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment