Skip to content

Commit

Permalink
Disable screen lock programatically
Browse files Browse the repository at this point in the history
  • Loading branch information
WonderCsabo committed Jun 26, 2015
1 parent f64dd5e commit 39c5936
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
android:versionCode="1"
android:versionName="1.0.0-SNAPSHOT">

<!-- these are only needed for the instrumentation test-->
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="19"/>
<application android:name="MorseFlashApplication" android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="ConfigureMorseActivity" android:label="@string/app_name">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<application>
<uses-library android:name="android.test.runner"/>
</application>
<instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
<instrumentation android:name="com.simpligility.android.morseflash.ScreenUnlockerTestRunner"
android:targetPackage="com.simpligility.android.morseflash"
android:label="Tests for MorseFlash."/>
</manifest>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.simpligility.android.morseflash;

import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;
import android.support.test.runner.AndroidJUnitRunner;

import static android.content.Context.KEYGUARD_SERVICE;
import static android.content.Context.POWER_SERVICE;
import static android.os.PowerManager.ACQUIRE_CAUSES_WAKEUP;
import static android.os.PowerManager.FULL_WAKE_LOCK;
import static android.os.PowerManager.ON_AFTER_RELEASE;

/**
* Instrumentation which unlocks the screen before running any test.
* This needs the following permissions in the <b>application</b> manifest:
*
* <pre>
* <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
* <uses-permission android:name="android.permission.WAKE_LOCK"/>
* </pre>
*
* Adapted from https://github.com/JakeWharton/u2020/blob/master/src/androidTestInternal/java/com/jakewharton/u2020/U2020TestRunner.java.
*/
public final class ScreenUnlockerTestRunner extends AndroidJUnitRunner {

@Override
public void onStart() {
runOnMainSync(new Runnable() {
@SuppressWarnings("deprecation")
// We don't care about deprecation here.
public void run() {
Context app = getTargetContext().getApplicationContext();

String name = ScreenUnlockerTestRunner.class.getSimpleName();
// Unlock the device so that the tests can input keystrokes.
KeyguardManager keyguard = (KeyguardManager) app.getSystemService(KEYGUARD_SERVICE);
keyguard.newKeyguardLock(name).disableKeyguard();
// Wake up the screen.
PowerManager power = (PowerManager) app.getSystemService(POWER_SERVICE);
power.newWakeLock(FULL_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE, name).acquire();
}
});

super.onStart();
}
}

0 comments on commit 39c5936

Please sign in to comment.