下面列出了android.app.admin.DevicePolicyManager#isDeviceOwnerApp ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean isSetWallpaperAllowed(String callingPackage) {
final PackageManager pm = mContext.getPackageManager();
String[] uidPackages = pm.getPackagesForUid(Binder.getCallingUid());
boolean uidMatchPackage = Arrays.asList(uidPackages).contains(callingPackage);
if (!uidMatchPackage) {
return false; // callingPackage was faked.
}
final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
if (dpm.isDeviceOwnerApp(callingPackage) || dpm.isProfileOwnerApp(callingPackage)) {
return true;
}
final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
return !um.hasUserRestriction(UserManager.DISALLOW_SET_WALLPAPER);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
DevicePolicyManager manager =
(DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (manager.isDeviceOwnerApp(getApplicationContext().getPackageName())) {
// This app is set up as the device owner. Show the main features.
Log.d(TAG, "The app is the device owner.");
showFragment(DeviceOwnerFragment.newInstance());
} else {
// This app is not set up as the device owner. Show instructions.
Log.d(TAG, "The app is not the device owner.");
showFragment(InstructionFragment.newInstance());
}
}
}
@ReactMethod
public void startLockTask() {
try {
Activity mActivity = reactContext.getCurrentActivity();
if (mActivity != null) {
DevicePolicyManager myDevicePolicyManager = (DevicePolicyManager) mActivity.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName mDPM = new ComponentName(mActivity, MyAdmin.class);
if (myDevicePolicyManager.isDeviceOwnerApp(mActivity.getPackageName())) {
String[] packages = {mActivity.getPackageName()};
myDevicePolicyManager.setLockTaskPackages(mDPM, packages);
mActivity.startLockTask();
} else {
mActivity.startLockTask();
}
}
} catch (Exception e) {
}
}
public void toggleKeyguardAndStatusBar(boolean status)
{
if (!MainActivity.isSDKCompatible())
{
MainActivity.log("OBSystemsManager:toggleKeyguardAndStatusBar: incompatible SDK version. exiting function");
return;
}
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) MainActivity.mainActivity.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName adminReceiver = OBDeviceAdminReceiver.getComponentName(MainActivity.mainActivity);
//
if (devicePolicyManager.isDeviceOwnerApp(MainActivity.mainActivity.getPackageName()))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
devicePolicyManager.setKeyguardDisabled(adminReceiver, !status);
}
MainActivity.log("OBSystemsManager.keyguard has been " + (status ? "enabled" : "disabled"));
//
// devicePolicyManager.setStatusBarDisabled(adminReceiver, !status);
// MainActivity.log("OBSystemsManager.status bar has been " + (status ? "enabled" : "disabled"));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_real);
if (savedInstanceState == null) {
DevicePolicyManager manager =
(DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (manager.isDeviceOwnerApp(getApplicationContext().getPackageName())) {
// This app is set up as the device owner. Show the main features.
Log.d(TAG, "The app is the device owner.");
showFragment(DeviceOwnerFragment.newInstance());
} else {
// This app is not set up as the device owner. Show instructions.
Log.d(TAG, "The app is not the device owner.");
showFragment(InstructionFragment.newInstance());
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
ComponentName deviceAdmin = new ComponentName(this, AdminReceiver.class);
mDpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!mDpm.isAdminActive(deviceAdmin)) {
Toast.makeText(this, getString(R.string.not_device_admin), Toast.LENGTH_SHORT).show();
}
if (mDpm.isDeviceOwnerApp(getPackageName())) {
mDpm.setLockTaskPackages(deviceAdmin, new String[]{getPackageName()});
} else {
Toast.makeText(this, getString(R.string.not_device_owner), Toast.LENGTH_SHORT).show();
}
mDecorView = getWindow().getDecorView();
mWebView.loadUrl("http://www.vicarasolutions.com/");
}
/**
* @return true if the device or profile is already owned
*/
public static boolean isManaged(Context context) {
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(
Context.DEVICE_POLICY_SERVICE);
List<ComponentName> admins = devicePolicyManager.getActiveAdmins();
if (admins == null) return false;
for (ComponentName admin : admins) {
String adminPackageName = admin.getPackageName();
if (devicePolicyManager.isDeviceOwnerApp(adminPackageName)
|| devicePolicyManager.isProfileOwnerApp(adminPackageName)) {
return true;
}
}
return false;
}
private int getCurrentAdmin() {
final DevicePolicyManager dpm =
(DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
final String packageName = mContext.getPackageName();
if (dpm.isDeviceOwnerApp(packageName)) {
return ADMIN_DEVICE_OWNER;
}
if (dpm.isProfileOwnerApp(packageName)) {
Boolean orgOwned = Util.SDK_INT >= VERSION_CODES.R &&
dpm.isOrganizationOwnedDeviceWithManagedProfile();
if (orgOwned) {
return ADMIN_ORG_OWNED_PROFILE_OWNER;
} else {
return ADMIN_PROFILE_OWNER;
}
}
return ADMIN_NONE;
}
/**
* Throws SecurityException if the calling package is not allowed to retrieve information
* provided by the service.
*
* @param callingPackage The calling package name.
*
* @throws SecurityException if something other than the device owner, the current VR service,
* or a caller holding the {@link Manifest.permission#DEVICE_POWER} permission tries to
* retrieve information provided by this service.
*/
private void enforceHardwarePropertiesRetrievalAllowed(String callingPackage)
throws SecurityException {
mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
final int userId = UserHandle.getUserId(Binder.getCallingUid());
final VrManagerInternal vrService = LocalServices.getService(VrManagerInternal.class);
final DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
if (!dpm.isDeviceOwnerApp(callingPackage)
&& mContext.checkCallingOrSelfPermission(Manifest.permission.DEVICE_POWER)
!= PackageManager.PERMISSION_GRANTED
&& (vrService == null || !vrService.isCurrentVrListener(callingPackage, userId))) {
throw new SecurityException("The caller is neither a device owner"
+ ", nor holding the DEVICE_POWER permission, nor the current VrListener.");
}
}
public boolean isDeviceOwner()
{
MainActivity.log("OBSystemsManager.isDeviceOwner");
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) MainActivity.mainActivity.getSystemService(Context.DEVICE_POLICY_SERVICE);
boolean result = devicePolicyManager.isDeviceOwnerApp(MainActivity.mainActivity.getPackageName());
MainActivity.log(result ? "It is device owner" : "It's NOT device owner");
return result;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDpm = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
mPackageName = getActivity().getPackageName();
final boolean isDeviceOwner = mDpm.isDeviceOwnerApp(mPackageName);
final boolean isProfileOwner = mDpm.isProfileOwnerApp(mPackageName);
mIsDeviceOrProfileOwner = isDeviceOwner || isProfileOwner;
// Show DO-only delegations if we are DO or delegated app i.e. we are not PO, ignoring the
// case where we are neither PO or DO (in which case this fragment is not accessible at all)
mDelegations = DelegationScope.defaultDelegationScopes(!isProfileOwner);
getActivity().getActionBar().setTitle(R.string.generic_delegation);
}
@Override
public void onCreate(Bundle savedInstanceState) {
// Check arguments- see whether we're supposed to run on behalf of the parent profile.
final Bundle arguments = getArguments();
if (arguments != null) {
mParentInstance = arguments.getBoolean(EXTRA_PARENT_PROFILE, false);
}
mAdminComponent = DeviceAdminReceiver.getComponentName(getActivity());
// Get a device policy manager for the current user.
mDevicePolicyManager = (DevicePolicyManager)
getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
// Store whether we are the profile owner for faster lookup.
mProfileOwner = mDevicePolicyManager.isProfileOwnerApp(getActivity().getPackageName());
mDeviceOwner = mDevicePolicyManager.isDeviceOwnerApp(getActivity().getPackageName());
if (mParentInstance) {
mDevicePolicyManager = mDevicePolicyManager.getParentProfileInstance(mAdminComponent);
}
// Put at last to make sure all initializations above are done before subclass's
// onCreatePreferences is called.
super.onCreate(savedInstanceState);
// Switch to parent profile if we are running on their behalf.
// This needs to be called after super.onCreate because preference manager is set up
// inside super.onCreate.
if (mParentInstance) {
final PreferenceManager pm = getPreferenceManager();
pm.setSharedPreferencesName(pm.getSharedPreferencesName() + TAG_PARENT);
}
}
/**
* @return true if the device or profile is already owned by TestDPC
*/
public static boolean isManagedByTestDPC(Context context) {
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) context.getSystemService(
Context.DEVICE_POLICY_SERVICE);
String packageName = context.getPackageName();
return devicePolicyManager.isProfileOwnerApp(packageName)
|| devicePolicyManager.isDeviceOwnerApp(packageName);
}
@Override
public boolean isAvailable(Context context) {
DevicePolicyManager dpm = (DevicePolicyManager)
context.getSystemService(Context.DEVICE_POLICY_SERVICE);
return dpm.isDeviceOwnerApp(context.getPackageName())
&& Util.getBindDeviceAdminTargetUsers(context).size() == 1;
}
public void disableAdministratorPrivileges()
{
if (!MainActivity.isSDKCompatible())
{
MainActivity.log("OBSystemsManager:disableAdministratorPrivileges: incompatible SDK version. exiting function");
return;
}
MainActivity.log("OBSystemsManager.disableAdministratorPrivileges");
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) MainActivity.mainActivity.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName adminReceiver = OBDeviceAdminReceiver.getComponentName(MainActivity.mainActivity);
//
if (devicePolicyManager.isDeviceOwnerApp(MainActivity.mainActivity.getPackageName()))
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
devicePolicyManager.setKeyguardDisabled(adminReceiver, false);
}
MainActivity.log("OBSystemsManager.keyguard restored");
//
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
devicePolicyManager.setStatusBarDisabled(adminReceiver, false);
}
MainActivity.log("OBSystemsManager.status bar restored");
//
try
{
MainActivity.log("OBSystemsManager.disableAdministratorPrivileges: removing active admin");
devicePolicyManager.removeActiveAdmin(adminReceiver);
MainActivity.log("OBSystemsManager.disableAdministratorPrivileges: clearing device owner");
devicePolicyManager.clearDeviceOwnerApp(MainActivity.mainActivity.getPackageName());
MainActivity.log("OBSystemsManager.disableAdministratorPrivileges: done");
} catch (Exception e)
{
MainActivity.log("OBSystemsManager.disableAdministratorPrivileges: exception caught");
// e.printStackTrace();
// App might not be the device owner at this point
}
}
if (kioskModeActive)
{
MainActivity.mainActivity.stopLockTask();
kioskModeActive = false;
}
}
public void pinApplication()
{
MainActivity.log("OBSystemsManager.pinApplication");
//
if (OBConfigManager.sharedManager.shouldPinApplication())
{
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) MainActivity.mainActivity.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName adminReceiver = OBDeviceAdminReceiver.getComponentName(MainActivity.mainActivity);
//
if (devicePolicyManager.isDeviceOwnerApp(MainActivity.mainActivity.getPackageName()))
{
MainActivity.log("OBSystemsManager.pinApplication: attempting to pin app");
String[] packages = {MainActivity.mainActivity.getPackageName()};
devicePolicyManager.setLockTaskPackages(adminReceiver, packages);
//
if (devicePolicyManager.isLockTaskPermitted(MainActivity.mainActivity.getPackageName()))
{
if (isAppIsInForeground())
{
try
{
MainActivity.log("OBSystemsManager.pinApplication: starting locked task");
MainActivity.mainActivity.startLockTask();
kioskModeActive = true;
} catch (Exception e)
{
MainActivity.log("OBSystemsManager.pinApplication: exception caught");
// e.printStackTrace();
kioskModeActive = false;
}
} else
{
MainActivity.log("OBSystemsManager.pinApplication:application is not in foreground, cancelling");
}
}
} else
{
MainActivity.log("OBSystemsManager.pinApplication: unable to pin application, not a device owner");
}
toggleKeyguardAndStatusBar(false);
} else
{
MainActivity.log("OBSystemsManager.pinApplication: disabled in settings");
}
}
public void unpinApplication()
{
MainActivity.log("OBSystemsManager.unpinApplication");
//
if (OBConfigManager.sharedManager.shouldPinApplication())
{
DevicePolicyManager devicePolicyManager = (DevicePolicyManager) MainActivity.mainActivity.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName adminReceiver = OBDeviceAdminReceiver.getComponentName(MainActivity.mainActivity);
//
if (devicePolicyManager.isDeviceOwnerApp(MainActivity.mainActivity.getPackageName()))
{
MainActivity.log("OBSystemsManager.unpinApplication: attempting to unpin app");
String[] packages = {MainActivity.mainActivity.getPackageName()};
devicePolicyManager.setLockTaskPackages(adminReceiver, packages);
//
if (devicePolicyManager.isLockTaskPermitted(MainActivity.mainActivity.getPackageName()))
{
if (isAppIsInForeground())
{
try
{
MainActivity.log("OBSystemsManager.unpinApplication: starting locked task");
MainActivity.mainActivity.stopLockTask();
kioskModeActive = false;
} catch (Exception e)
{
MainActivity.log("OBSystemsManager.unpinApplication: exception caught");
}
} else
{
MainActivity.log("OBSystemsManager.unpinApplication:application is not in foreground, cancelling");
}
}
} else
{
MainActivity.log("OBSystemsManager.unpinApplication: unable to unpin application, not a device owner");
}
toggleKeyguardAndStatusBar(false);
} else
{
MainActivity.log("OBSystemsManager.unpinApplication: disabled in settings");
}
}
public static boolean isDeviceOwnerApp(Context context) {
DevicePolicyManager manager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
return manager.isDeviceOwnerApp(context.getPackageName());
}
private static void updatePasswordConstraintNotification(Context context) {
final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
Context.DEVICE_POLICY_SERVICE);
final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
if (!dpm.isProfileOwnerApp(context.getPackageName())
&& !dpm.isDeviceOwnerApp(context.getPackageName())) {
// Only try to update the notification if we are a profile or device owner.
return;
}
final NotificationManager nm = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
final ArrayList<CharSequence> problems = new ArrayList<>();
if (!dpm.isActivePasswordSufficient()) {
problems.add(context.getText(R.string.password_not_compliant_title));
}
if (um.hasUserRestriction(UserManager.DISALLOW_UNIFIED_PASSWORD)
&& Util.isManagedProfileOwner(context)
&& isUsingUnifiedPassword(context)) {
problems.add(context.getText(R.string.separate_challenge_required_title));
}
if (!problems.isEmpty()) {
final NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
style.setBigContentTitle(
context.getText(R.string.set_new_password_notification_content));
for (final CharSequence problem : problems) {
style.addLine(problem);
}
final NotificationCompat.Builder warn =
NotificationUtil.getNotificationBuilder(context);
warn.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(style)
.setContentIntent(PendingIntent.getActivity(context, /*requestCode*/ -1,
new Intent(DevicePolicyManager.ACTION_SET_NEW_PASSWORD), /*flags*/ 0));
nm.notify(CHANGE_PASSWORD_NOTIFICATION_ID, warn.getNotification());
} else {
nm.cancel(CHANGE_PASSWORD_NOTIFICATION_ID);
}
}
@TargetApi(VERSION_CODES.LOLLIPOP)
public static boolean isDeviceOwner(Context context) {
final DevicePolicyManager dpm = getDevicePolicyManager(context);
return dpm.isDeviceOwnerApp(context.getPackageName());
}