Skip to content

Commit fdda44b

Browse files
ismail-sFelicianoTech
authored andcommitted
Add better way of unlocking device (circleci#185)
1 parent f626319 commit fdda44b

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

jekyll/_docs/android.md

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,50 @@ order to prevent failing commands from being understood as passing.
135135

136136
Some useful `fb-adb shell` commands are:
137137

138-
- `fb-adb shell input keyevent 82` to unlock the emulator
139138
- `fb-adb rcmd screencap -p > $CIRCLE_ARTIFACTS/screen-$(date +"%T").png`
140139
to take a screenshot of the emulator and store it as a build artifact.
140+
- `fb-adb shell input keyevent 82` to unlock the emulator, but see the
141+
next paragraph
142+
143+
144+
Whilst the above command can be used to unlock the emulator, the emulator
145+
could lock if it takes a long time to build the tests. A more thorough
146+
way of unlocking the emulator is to add the following code to the `setUp`
147+
junit method of the test class:
148+
149+
```java
150+
package com.mypackage.espressoTest;
151+
152+
import android.view.WindowManager;
153+
import org.junit.Before;
154+
import org.junit.Rule;
155+
import org.junit.runner.RunWith;
156+
157+
import android.support.test.rule.ActivityTestRule;
158+
import android.support.test.runner.AndroidJUnit4;
159+
160+
@RunWith(AndroidJUnit4.class)
161+
class TestClass {
162+
@Rule
163+
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
164+
MainActivity.class);
165+
166+
@Before
167+
public void setUp() {
168+
MainActivity activity = mActivityRule.getActivity();
169+
Runnable wakeUpDevice = new Runnable() {
170+
public void run() {
171+
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
172+
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
173+
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
174+
}
175+
};
176+
activity.runOnUiThread(wakeUpDevice);
177+
}
178+
}
179+
```
180+
181+
This will wake up the device and try to unlock the screen. If there is a password/pattern lock on the device, then `MainActivity` will get launched on top of the lock screen instead of unlocking the device (how cool is that?).
141182

142183
[adb-shell-bug]: https://code.google.com/p/android/issues/detail?id=3254
143184
[fb-adb]:https://github.com/facebook/fb-adb

0 commit comments

Comments
 (0)