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

The following examples show how to use android.app.admin.DevicePolicyManager#isApplicationHidden() . 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: StatusFragment.java    From enterprise-samples with Apache License 2.0 5 votes vote down vote up
private void updateUi(Activity activity) {
    PackageManager packageManager = activity.getPackageManager();
    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);
        DevicePolicyManager devicePolicyManager =
                (DevicePolicyManager) activity.getSystemService(Activity.DEVICE_POLICY_SERVICE);
        if ((info.flags & ApplicationInfo.FLAG_INSTALLED) != 0) {
            if (!devicePolicyManager.isApplicationHidden(
                    EnforcerDeviceAdminReceiver.getComponentName(activity),
                    Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA)) {
                // The app is ready to enforce restrictions
                // This is unlikely to happen in this sample as unhideApp() handles it.
                mListener.onStatusUpdated();
            } else {
                // The app is installed but hidden in this profile
                mTextStatus.setText(R.string.status_not_activated);
                mButtonUnhide.setVisibility(View.VISIBLE);
            }
        } else {
            // Need to reinstall the sample app
            mTextStatus.setText(R.string.status_need_reinstall);
            mButtonUnhide.setVisibility(View.GONE);
        }
    } catch (PackageManager.NameNotFoundException e) {
        // Need to reinstall the sample app
        mTextStatus.setText(R.string.status_need_reinstall);
        mButtonUnhide.setVisibility(View.GONE);
    }
}
 
Example 2
Source File: BasicManagedProfileFragment.java    From enterprise-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the application is available in this profile.
 *
 * @param packageName The package name
 * @return True if the application is available in this profile.
 */
private boolean isApplicationEnabled(String packageName) {
    Activity activity = getActivity();
    PackageManager packageManager = activity.getPackageManager();
    try {
        int packageFlags;
        if(Build.VERSION.SDK_INT < 24){
            //noinspection deprecation
            packageFlags = PackageManager.GET_UNINSTALLED_PACKAGES;
        }else{
            packageFlags = PackageManager.MATCH_UNINSTALLED_PACKAGES;
        }
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(
                packageName, packageFlags);
        // Return false if the app is not installed in this profile
        if (0 == (applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED)) {
            return false;
        }
        // Check if the app is not hidden in this profile
        DevicePolicyManager devicePolicyManager =
                (DevicePolicyManager) activity.getSystemService(Activity.DEVICE_POLICY_SERVICE);
        return !devicePolicyManager.isApplicationHidden(
                BasicDeviceAdminReceiver.getComponentName(activity), packageName);
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
 
Example 3
Source File: StatusFragment.java    From android-AppRestrictionEnforcer with Apache License 2.0 5 votes vote down vote up
private void updateUi(Activity activity) {
    PackageManager packageManager = activity.getPackageManager();
    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);
        DevicePolicyManager devicePolicyManager =
                (DevicePolicyManager) activity.getSystemService(Activity.DEVICE_POLICY_SERVICE);
        if ((info.flags & ApplicationInfo.FLAG_INSTALLED) != 0) {
            if (!devicePolicyManager.isApplicationHidden(
                    EnforcerDeviceAdminReceiver.getComponentName(activity),
                    Constants.PACKAGE_NAME_APP_RESTRICTION_SCHEMA)) {
                // The app is ready to enforce restrictions
                // This is unlikely to happen in this sample as unhideApp() handles it.
                mListener.onStatusUpdated();
            } else {
                // The app is installed but hidden in this profile
                mTextStatus.setText(R.string.status_not_activated);
                mButtonUnhide.setVisibility(View.VISIBLE);
            }
        } else {
            // Need to reinstall the sample app
            mTextStatus.setText(R.string.status_need_reinstall);
            mButtonUnhide.setVisibility(View.GONE);
        }
    } catch (PackageManager.NameNotFoundException e) {
        // Need to reinstall the sample app
        mTextStatus.setText(R.string.status_need_reinstall);
        mButtonUnhide.setVisibility(View.GONE);
    }
}
 
Example 4
Source File: BasicManagedProfileFragment.java    From android-BasicManagedProfile with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the application is available in this profile.
 *
 * @param packageName The package name
 * @return True if the application is available in this profile.
 */
private boolean isApplicationEnabled(String packageName) {
    Activity activity = getActivity();
    PackageManager packageManager = activity.getPackageManager();
    try {
        int packageFlags;
        if(Build.VERSION.SDK_INT < 24){
            //noinspection deprecation
            packageFlags = PackageManager.GET_UNINSTALLED_PACKAGES;
        }else{
            packageFlags = PackageManager.MATCH_UNINSTALLED_PACKAGES;
        }
        ApplicationInfo applicationInfo = packageManager.getApplicationInfo(
                packageName, packageFlags);
        // Return false if the app is not installed in this profile
        if (0 == (applicationInfo.flags & ApplicationInfo.FLAG_INSTALLED)) {
            return false;
        }
        // Check if the app is not hidden in this profile
        DevicePolicyManager devicePolicyManager =
                (DevicePolicyManager) activity.getSystemService(Activity.DEVICE_POLICY_SERVICE);
        return !devicePolicyManager.isApplicationHidden(
                BasicDeviceAdminReceiver.getComponentName(activity), packageName);
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
 
Example 5
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 6
Source File: PolicyUtils.java    From Mount with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isApplicationMount(Context context, String packageName) {
    DevicePolicyManager manager = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
    return !manager.isApplicationHidden(new ComponentName(context, MountReceiver.class), packageName);
}
 
Example 7
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();
            }
        }
    }
}