Example usage for android.app.admin DevicePolicyManager ACTION_ADD_DEVICE_ADMIN

List of usage examples for android.app.admin DevicePolicyManager ACTION_ADD_DEVICE_ADMIN

Introduction

In this page you can find the example usage for android.app.admin DevicePolicyManager ACTION_ADD_DEVICE_ADMIN.

Prototype

String ACTION_ADD_DEVICE_ADMIN

To view the source code for android.app.admin DevicePolicyManager ACTION_ADD_DEVICE_ADMIN.

Click Source Link

Document

Activity action: ask the user to add a new device administrator to the system.

Usage

From source file:net.nym.devicepolicy.MainActivity.java

private boolean adminActive() {
    DevicePolicyManager devicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);

    ComponentName componentName = new ComponentName(this, MyDeviceAdminReceiver.class);
    boolean isAdminActive = devicePolicyManager.isAdminActive(componentName);
    if (isAdminActive) {
        return true;
    } else {/*from  w w w.j a v  a 2s.c o m*/
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
        startActivityForResult(intent, 1);
        return false;
    }
}

From source file:de.jerleo.samsung.knox.firewall.MainActivity.java

private void activateAdministrator() {

    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, admin);
    startActivityForResult(intent, DEVICE_ADMIN_ADD_RESULT_ENABLE);
}

From source file:com.example.samsungmdm.MainActivity.java

private void grantAdminPrivileges() {
    mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    mAdminName = new ComponentName(this, MyDeviceAdminReceiver.class);

    if (!mDPM.isAdminActive(mAdminName)) {
        //Not yet device admin
        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "This needs to be added");
        startActivityForResult(intent, REQUEST_ENABLE);
    }/*from   w w w . j  a  va 2  s .c o  m*/
}

From source file:com.decad3nce.aegis.AegisActivity.java

public void addAdmin() {
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, DEVICE_ADMIN_COMPONENT);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
            getResources().getString(R.string.device_admin_reason));
    startActivityForResult(intent, ACTIVATION_REQUEST);
}

From source file:com.toppatch.mv.ui.activities.LoginActivity2.java

private void enableAdmin() {
    DevicePolicyManager mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
    ComponentName mDeviceAdmin = new ComponentName(this, TopPatchAdmin.class);
    if (mDPM != null) {
        try {/*from   w ww .  ja  va 2 s.  c o  m*/

            //Add filter to listen to the choice made by the user to accept the terms or not.
            IntentFilter filter = new IntentFilter();
            filter.addAction(EnterpriseLicenseManager.ACTION_LICENSE_STATUS);
            actionReceiver = new SamsungReceiver();
            registerReceiver(actionReceiver, filter);

            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdmin);
            intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                    "Please Activate The Admin To Let Us Steal All Your Data And Sell It To Obama!");
            startActivityForResult(intent, RESULT_ENABLE);
        } catch (Exception e) {
            Log.w(TAG, "Exception: " + e);
        }
    } else {
        Log.e(TAG, "mDPM is null");
        //TODO device doesn't support MDM...report this shit.
    }
}

From source file:com.configurer.easyscreenlock.MainActivity.java

private void activateDeviceAdmin() {
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, compName);
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getString(R.string.usage_info));
    startActivityForResult(intent, ACTIVATION_REQUEST_CODE);
}

From source file:de.Maxr1998.xposed.maxlock.ui.settings.SettingsFragment.java

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    super.onPreferenceTreeClick(preferenceScreen, preference);
    if (preference == findPreference(Common.LOCKING_TYPE_SETTINGS)) {
        launchFragment(new LockingTypeSettingsFragment(), true, this);
        return true;
    } else if (preference == findPreference(Common.LOCKING_UI_SETTINGS)) {
        launchFragment(new LockingUISettingsFragment(), true, this);
        return true;
    } else if (preference == findPreference(Common.LOCKING_OPTIONS)) {
        PREFS.edit().putBoolean(Common.ENABLE_LOGGING, PREFS.getBoolean(Common.ENABLE_PRO, false)).apply();
        launchFragment(new LockingOptionsFragment(), true, this);
        return true;
    } else if (preference == findPreference(Common.IIMOD_OPTIONS)) {
        // Setup remain timer
        long timer = PREFS.getInt(Common.IMOD_DELAY_GLOBAL, 600000)
                - (System.currentTimeMillis() - PREFS.getLong(Common.IMOD_LAST_UNLOCK_GLOBAL, 0));
        if (timer < 0) {
            timer = 0L;/*from   w w  w . j  a va  2s . c o m*/
        }
        PREFS.edit().putInt(Common.IMOD_REMAIN_TIMER_GLOBAL, (int) timer).apply();
        launchFragment(new LockingIntikaFragment(), true, this);
        return true;
    } else if (preference == findPreference(Common.CHOOSE_APPS)) {
        launchFragment(new AppListFragment(), true, this);
        return true;
    } else if (preference == findPreference(Common.HIDE_APP_FROM_LAUNCHER)
            && preference instanceof CheckBoxPreference) {
        CheckBoxPreference hideApp = (CheckBoxPreference) preference;
        if (hideApp.isChecked()) {
            Toast.makeText(getActivity(), R.string.reboot_required, Toast.LENGTH_SHORT).show();
            ComponentName componentName = new ComponentName(getActivity(), "de.Maxr1998.xposed.maxlock.Main");
            getActivity().getPackageManager().setComponentEnabledSetting(componentName,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
        } else {
            ComponentName componentName = new ComponentName(getActivity(), "de.Maxr1998.xposed.maxlock.Main");
            getActivity().getPackageManager().setComponentEnabledSetting(componentName,
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
        }
    } else if (preference == findPreference(Common.USE_DARK_STYLE)
            || preference == findPreference(Common.ENABLE_PRO)) {
        ((SettingsActivity) getActivity()).restart();
        return true;
    } else if (preference == findPreference(Common.UNINSTALL)) {
        if (!isDeviceAdminActive()) {
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, deviceAdmin);
            startActivity(intent);
        } else {
            devicePolicyManager.removeActiveAdmin(deviceAdmin);
        }
        return true;
    }
    return false;
}

From source file:edu.ucdenver.androidsecurity.MainActivity.java

private void loadAdminRequestScreen() {
    Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminPoliciesManager.getmDeviceAdmin());
    intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, getString(R.string.description_receiver));
    startActivityForResult(intent, DEVICE_ADMIN_REQUEST);
}

From source file:org.wso2.mdm.agent.AlreadyRegisteredActivity.java

/**
 * Start device admin activation request.
 * @param cdmDeviceAdmin - Device admin component.
 *//*from   www .  ja  v a  2 s. c  o m*/
private void startDeviceAdminPrompt(ComponentName cdmDeviceAdmin) {
    Intent deviceAdminIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
    deviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cdmDeviceAdmin);
    deviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
            getResources().getString(R.string.device_admin_enable_alert));
    startActivityForResult(deviceAdminIntent, ACTIVATION_REQUEST);
}

From source file:com.tomer.alwayson.SettingsFragment.java

@Override
public boolean onPreferenceChange(final Preference preference, Object o) {
    if (preference.getKey().equals("watchface_clock")) {
        int value = Integer.parseInt((String) o);
        if (value > 2) {
            if (Globals.ownedItems != null) {
                if (Globals.ownedItems.size() > 0) {
                    return true;
                } else {
                    PreferencesActivity.quicklyPromptToSupport(getActivity(), Globals.mService, rootView);
                    return false;
                }/*from w w w  . j a v  a 2  s . c o m*/
            } else {
                PreferencesActivity.quicklyPromptToSupport(getActivity(), Globals.mService, rootView);
            }
        } else {
            return true;
        }
    }

    prefs.apply();
    Utils.logDebug("Preference change", preference.getKey() + " Value:" + o.toString());

    if (preference.getKey().equals("notifications_alerts")) {
        if ((boolean) o)
            return checkNotificationsPermission(context, true);
        return true;
    }
    if (preference.getKey().equals("persistent_notification") && !(boolean) o) {
        Snackbar.make(rootView, R.string.warning_1_harm_performance, 10000)
                .setAction(R.string.action_revert, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((CheckBoxPreference) preference).setChecked(true);
                        restartService();
                    }
                }).show();
        restartService();
    }
    if (preference.getKey().equals("enabled")) {
        restartService();
    }
    if (preference.getKey().equals("proximity_to_lock")) {
        if (Shell.SU.available() || Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            return true;
        else {
            DevicePolicyManager mDPM = (DevicePolicyManager) context
                    .getSystemService(Context.DEVICE_POLICY_SERVICE);
            final ComponentName mAdminName = new ComponentName(context, DAReceiver.class);
            if ((mDPM != null && mDPM.isAdminActive(mAdminName))) {
                return true;
            }
            new AlertDialog.Builder(getActivity())
                    .setTitle(getString(android.R.string.dialog_alert_title) + "!")
                    .setMessage(getString(R.string.warning_7_disable_fingerprint))
                    .setPositiveButton(getString(android.R.string.yes), (dialogInterface, i) -> {
                        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
                        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
                        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                                getString(R.string.settings_proximity));
                        startActivityForResult(intent, DEVICE_ADMIN_REQUEST_CODE);
                    }).setNegativeButton(getString(android.R.string.no), (dialogInterface, i) -> {
                        dialogInterface.dismiss();
                    }).show();
            return false;
        }
    }
    if (preference.getKey().equals("startafterlock") && !(boolean) o) {
        Snackbar.make(rootView, R.string.warning_4_device_not_secured, 10000)
                .setAction(R.string.action_revert, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ((CheckBoxPreference) preference).setChecked(true);
                    }
                }).show();
    }
    if (preference.getKey().equals("doze_mode") && (boolean) o) {
        if (Shell.SU.available()) {
            if (!DozeManager.isDumpPermissionGranted(context))
                DozeManager.grantPermission(context, "android.permission.DUMP");
            if (!DozeManager.isDevicePowerPermissionGranted(context))
                DozeManager.grantPermission(context, "android.permission.DEVICE_POWER");
            return true;
        }
        Snackbar.make(rootView, R.string.warning_11_no_root, Snackbar.LENGTH_LONG).show();
        return false;
    }
    if (preference.getKey().equals("camera_shortcut") || preference.getKey().equals("google_now_shortcut")) {
        try {
            if (!hasUsageAccess()) {
                Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
                PackageManager packageManager = getActivity().getPackageManager();
                if (intent.resolveActivity(packageManager) != null) {
                    startActivity(intent);
                } else {
                    Toast.makeText(context,
                            "Please grant usage access permission manually for the app, your device can't do it automatically.",
                            Toast.LENGTH_LONG).show();
                }
                return false;
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
    if (preference.getKey().equals("battery_saver"))
        if ((boolean) o) {
            ((TwoStatePreference) findPreference("doze_mode")).setChecked(true);
            setUpBatterySaverPermission();
        }
    return true;
}