Java Code Examples for android.app.admin.DevicePolicyManager#isProfileOwnerApp()

The following examples show how to use android.app.admin.DevicePolicyManager#isProfileOwnerApp() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WallpaperManagerService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: MainActivity.java    From enterprise-samples with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    if (savedInstanceState == null) {
        DevicePolicyManager manager =
                (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        if (manager.isProfileOwnerApp(getApplicationContext().getPackageName())) {
            // If the managed profile is already set up, we show the main screen.
            showMainFragment();
        } else {
            // If not, we show the set up screen.
            showSetupProfile();
        }
    }
}
 
Example 3
Source File: ProvisioningStateUtil.java    From android-testdpc with Apache License 2.0 6 votes vote down vote up
/**
 * @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;
}
 
Example 4
Source File: DpcPreferenceHelper.java    From android-testdpc with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 5
Source File: MainActivity.java    From android-BasicManagedProfile with Apache License 2.0 6 votes vote down vote up
@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.isProfileOwnerApp(getApplicationContext().getPackageName())) {
            // If the managed profile is already set up, we show the main screen.
            showMainFragment();
        } else {
            // If not, we show the set up screen.
            showSetupProfile();
        }
    }
}
 
Example 6
Source File: DelegationFragment.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: ProfileOrParentFragment.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example 8
Source File: ProvisioningStateUtil.java    From android-testdpc with Apache License 2.0 5 votes vote down vote up
/**
 * @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);
}
 
Example 9
Source File: MainActivity.java    From enterprise-samples with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (null == savedInstanceState) {
        DevicePolicyManager devicePolicyManager =
                (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        PackageManager packageManager = getPackageManager();
        if (!devicePolicyManager.isProfileOwnerApp(getApplicationContext().getPackageName())) {
            // If the managed profile is not yet set up, we show the setup screen.
            showSetupProfile();
        } else {
            try {
                int packageFlags;
                if (Build.VERSION.SDK_INT < 24) {
                    //noinspection deprecation
                    packageFlags = PackageManager.GET_UNINSTALLED_PACKAGES;
                } else {
                    packageFlags = PackageManager.MATCH_UNINSTALLED_PACKAGES;
                }
                ApplicationInfo info = packageManager.getApplicationInfo(
                        Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA,
                        packageFlags);
                if (0 == (info.flags & ApplicationInfo.FLAG_INSTALLED)) {
                    // Need to reinstall the sample app
                    showStatusProfile();
                } else if (devicePolicyManager.isApplicationHidden(
                        EnforcerDeviceAdminReceiver.getComponentName(this),
                        Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA)) {
                    // The app is installed but hidden in this profile
                    showStatusProfile();
                } else {
                    // Everything is clear; show the main screen
                    showMainFragment();
                }
            } catch (PackageManager.NameNotFoundException e) {
                showStatusProfile();
            }
        }
    }
}
 
Example 10
Source File: DeviceAdminReceiver.java    From android-testdpc with Apache License 2.0 4 votes vote down vote up
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);
    }
}
 
Example 11
Source File: Util.java    From android-testdpc with Apache License 2.0 4 votes vote down vote up
@TargetApi(VERSION_CODES.LOLLIPOP)
public static boolean isProfileOwner(Context context) {
    final DevicePolicyManager dpm = getDevicePolicyManager(context);
    return dpm.isProfileOwnerApp(context.getPackageName());
}
 
Example 12
Source File: MainActivity.java    From android-AppRestrictionEnforcer with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_real);
    if (null == savedInstanceState) {
        DevicePolicyManager devicePolicyManager =
                (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        PackageManager packageManager = getPackageManager();
        if (!devicePolicyManager.isProfileOwnerApp(getApplicationContext().getPackageName())) {
            // If the managed profile is not yet set up, we show the setup screen.
            showSetupProfile();
        } else {
            try {
                int packageFlags;
                if (Build.VERSION.SDK_INT < 24) {
                    //noinspection deprecation
                    packageFlags = PackageManager.GET_UNINSTALLED_PACKAGES;
                } else {
                    packageFlags = PackageManager.MATCH_UNINSTALLED_PACKAGES;
                }
                ApplicationInfo info = packageManager.getApplicationInfo(
                        Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA,
                        packageFlags);
                if (0 == (info.flags & ApplicationInfo.FLAG_INSTALLED)) {
                    // Need to reinstall the sample app
                    showStatusProfile();
                } else if (devicePolicyManager.isApplicationHidden(
                        EnforcerDeviceAdminReceiver.getComponentName(this),
                        Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA)) {
                    // The app is installed but hidden in this profile
                    showStatusProfile();
                } else {
                    // Everything is clear; show the main screen
                    showMainFragment();
                }
            } catch (PackageManager.NameNotFoundException e) {
                showStatusProfile();
            }
        }
    }
}