Skip to content

Instantly share code, notes, and snippets.

@billmote
Forked from JakeWharton/gist:f50f3b4d87e57d8e96e9
Last active October 15, 2023 10:48
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save billmote/3ae247867b68d6ead7dc to your computer and use it in GitHub Desktop.
Save billmote/3ae247867b68d6ead7dc to your computer and use it in GitHub Desktop.
When pushing code from your IDE, wake the device and show the activity.

WAKEUP!

How many times a day do you push code to your phone and then pick it up, press the power button to wake the device and then swipe to unlock? If you're like me that answer is probably hundreds!

By adding a couple of permissions to your src/debug/AndroidManifest.xml file and adding a helper class in src/debug/... that can be called from your activities, you can wakeup and unlock the device automatically when you push code to your phone.

<?xml version="1.0" encoding="utf-8"?>
<!-- Add this as a debug manifest so the permissions won't be required by your production app -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
</manifest>
public class BaseActivity extends Activity {
@Override
protected void onStart() {
super.onStart();
// Wake the device and show our activity
if (BuildConfig.DEBUG) {
// Calling this from your launcher activity is enough, but I needed a good example spot ;)
DebugUtils.riseAndShine(this);
}
}
}
import android.app.Activity;
import android.app.KeyguardManager;
import android.os.PowerManager;
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;
import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED;
/**
* Add this to your application's src/debug/... sources
*/
public class DebugUtils {
/**
* Show the activity over the lockscreen and wake up the device. If you launched the app manually
* both of these conditions are already true. If you deployed from the IDE, however, this will
* save you from hundreds of power button presses and pattern swiping per day!
*/
public static void riseAndShine(Activity activity) {
KeyguardManager keyguardManager = (KeyguardManager) activity.getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("Unlock!");
keyguardLock.disableKeyguard();
activity.getWindow().addFlags(FLAG_SHOW_WHEN_LOCKED);
PowerManager powerManager = (PowerManager) activity.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(FULL_WAKE_LOCK | ACQUIRE_CAUSES_WAKEUP | ON_AFTER_RELEASE, "Wakeup!");
wakeLock.acquire();
wakeLock.release();
}
}
@matpag
Copy link

matpag commented Feb 22, 2015

When i launch this on my Nexus 5 with Lollipop 5.0.1, the phone became bugged.
Home button stop to work, app icons disappear from screen and so on. The only thing i can do in this case is restart the phone

@brianegan
Copy link

Hi there! Super android n00b here. Can you point me to a guide on Using AndroidManifest.xml within a debug folder? I googled for 30 minutes and couldn't see anything myself :(

@MikolajKakol
Copy link

Add this at the beginning of riseAndShine for easier running it from tests

    if (Looper.getMainLooper().getThread() != Thread.currentThread()) {
        activity.runOnUiThread(() -> riseAndShine(activity));
        return;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment