Example usage for android.app.admin DevicePolicyManager lockNow

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

Introduction

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

Prototype

public void lockNow() 

Source Link

Document

Make the device lock immediately, as if the lock screen timeout has expired at the point of this call.

Usage

From source file:com.achep.acdisplay.ui.activities.MainActivity.java

/**
 * Turns screen off and sends a test notification.
 *
 * @param cheat {@code true} if it simply starts {@link AcDisplayActivity},
 *              {@code false} if it turns device off and then uses notification
 *              to wake it up.//from   w  ww . ja v  a  2s  .  c  o m
 */
private void startAcDisplayTest(boolean cheat) {
    if (cheat) {
        startActivity(new Intent(this, AcDisplayActivity.class));
        sendTestNotification(this);
        return;
    }

    int delay = getResources().getInteger(R.integer.config_test_notification_delay);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Test notification.");
    wakeLock.acquire(delay);

    try {
        // Go sleep
        DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        dpm.lockNow();

        new Handler().postDelayed(new Runnable() {

            private final Context context = getApplicationContext();

            @Override
            public void run() {
                sendTestNotification(context);
            }
        }, delay);
    } catch (SecurityException e) {
        Log.wtf(TAG, "Failed to turn screen off!");
        wakeLock.release();
    }
}

From source file:com.bullmobi.message.ui.activities.MainActivity.java

/**
 * Turns screen off and sends a test notification.
 *
 * @param cheat {@code true} if it simply starts {@link EasyNotificationActivity},
 *              {@code false} if it turns device off and then uses notification
 *              to wake it up.//  w  w  w.jav  a 2s. co  m
 */
private void startEasyNotificationTest(boolean cheat) {
    if (cheat) {
        startActivity(new Intent(this, EasyNotificationActivity.class));
        sendTestNotification(this);
        return;
    }

    int delay = getResources().getInteger(R.integer.config_test_notification_delay);

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Test notification.");
    wakeLock.acquire(delay);

    try {
        // Go sleep
        DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
        dpm.lockNow();

        new Handler().postDelayed(new Runnable() {

            private final Context context = getApplicationContext();

            @Override
            public void run() {
                sendTestNotification(context);
            }
        }, delay);
    } catch (SecurityException e) {
        Log.wtf(TAG, "Failed to turn screen off!");
        wakeLock.release();
    }
}

From source file:org.digitalcampus.oppia.application.AdminGCMListener.java

@Override
public void onMessageReceived(String from, Bundle messageData) {
    Log.d(TAG, "Push message received from: " + from);

    if (from.startsWith("/topics/")) {
        // message received from some topic.
    } else {/*from w  w w  .j a  v  a 2 s .  c  o  m*/
        String type = messageData.getString(MESSAGE_TYPE);
        if ((type != null) && (type.equals(TYPE_ADMIN))) {

            if (!BuildConfig.FLAVOR.equals("admin")) {
                //Is not the admin-flavor app (we don't have the permission, would produce crash)
                Log.d(TAG, "Device Administration is disabled :(");
                return;
            }

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
            boolean adminEnabled = prefs.getBoolean(PrefsActivity.PREF_REMOTE_ADMIN, false);
            //First, we need to check if admin option is enabled
            if (!adminEnabled) {
                Log.d(TAG, "Device Administration is disabled :(");
                return;
            }

            String action = messageData.getString(MESSAGE_ACTION);
            Log.d(TAG, "Remote admin action: " + action);
            if (ACTION_DISABLE_CAMERA.equals(action)) {
                ComponentName adminReceiver = new ComponentName(this, AdminReceiver.class);
                DevicePolicyManager policyManager = (DevicePolicyManager) getSystemService(
                        Context.DEVICE_POLICY_SERVICE);
                policyManager.setCameraDisabled(adminReceiver, true);
                sendNotification(getString(R.string.notification_remote_admin_camera_disabled));
            } else if (ACTION_ENABLE_CAMERA.equals(action)) {
                ComponentName adminReceiver = new ComponentName(this, AdminReceiver.class);
                DevicePolicyManager policyManager = (DevicePolicyManager) getSystemService(
                        Context.DEVICE_POLICY_SERVICE);
                policyManager.setCameraDisabled(adminReceiver, false);
                sendNotification(getString(R.string.notification_remote_admin_camera_enabled));
            } else if (ACTION_PASSWORD_LOCK.equals(action)) {
                String password = messageData.getString(MESSAGE_PASSWORD);
                if ((password != null) && !password.equals("")) {
                    DevicePolicyManager policyManager = (DevicePolicyManager) getSystemService(
                            Context.DEVICE_POLICY_SERVICE);
                    policyManager.resetPassword(password, DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
                    policyManager.lockNow();
                }
            }
        }

    }

}

From source file:com.afwsamples.testdpc.policy.PolicyManagementFragment.java

@TargetApi(Build.VERSION_CODES.N)
private void lockNow() {
    if (Util.isBeforeN() || !Util.isManagedProfile(getActivity(), mAdminComponentName)) {
        mDevicePolicyManager.lockNow();//from w w w .ja v  a  2  s. c o  m
    } else {
        // In N for work profiles we should call lockNow on the parent instance to
        // lock the device.
        DevicePolicyManager parentDpm = mDevicePolicyManager.getParentProfileInstance(mAdminComponentName);
        parentDpm.lockNow();
    }
}