2

I am building an immersion app for driving that should not display anything on screen and run in the background until the driver reaches some specific location marker. Once at or near this location, based on GPS position, the screen should wake up, and display something relevant to that location. This seems straightforward, but I have not found any solutions to do so that make sense to me. Getting and comparing GPS info is not a problem, but how do you wake up the screen based on some event, or a change of something, or something you have programmatically defined?

Would this be something I should handle with Mirror API instead to push notifications to the timeline?

The reason I am using an immersion is due to the fact that I do not want the driver to be able to multitask, as would a live card allow. Instead, he/she should start the app, and it will take over the rest (pushing notifications to the display). These notifications are free to disappear after several seconds or whenever Glass feels like going back to "sleep."

Any ideas on this matter would be greatly appreciated. I have read stuff about wake lock, but am not sure if that pertains to what I am doing. Thanks in advance.

Autex
  • 307
  • 2
  • 12

2 Answers2

2

Just wondering, have you thought about maybe dimming the screen to the absolute minimum? Maybe changing the color to black if you really don't want anything to be visible?

Here's the issue - according to Google Glass TOS, you can't actually turn off the screen while an app is running, so even if you find some hacky way to do it, you aren't supposed to. I've been having this issue with my app as well.

The solution for me (my issue was that battery was being eaten unbelievably quickly) was to use this code:

To dim the screen (even though it says 0f, it is still a tiny bit lit)

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0f;
getWindow().setAttributes(lp);

To go back to full brightness:

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 1f;
getWindow().setAttributes(lp);

Then if you want to hide what's on the screen, you could just add a black box over everything in your layout and have it set to android:visibility="gone" until you want to dim the screen, then set the box's visibility to visible and back to gone once you want the screen to turn on again and you brighten it.

In case you want to use the brightness that the person had before they opened the app instead of setting the brightness of the screen to maximum, you can use this code in your onCreate to get the original brightness so you know what to set it to later:

int previousScreenBrightness = android.provider.Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);

Also, set this flag at the top, so the app doesn't dim/turn off on its own over time:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

I hope this helps. Good luck!

Alex K
  • 8,269
  • 9
  • 39
  • 57
  • That makes sense. But when you write "according to Google Glass TOS, you can't actually turn off the screen while an app is running", did you mean you can't turn the screen "on?" That was my biggest issue: I can't find a way to tell the app to turn the screen on. Under normal operation, glass will dim and then turn the screen off after ~10 sec. This is ok, but I would like my program to turn the screen back on and display something when appropriate. It think I understand your logic as a workaround to this;to just keep the screen on the whole time but cover/uncover as needed. Is that correct? – Autex Oct 22 '14 at 21:34
  • I mean that you aren't allowed to turn the screen off with the app still running in the background and listening/taking video/tracking. And you need to have the code running to be able to turn the screen on again. – Alex K Oct 22 '14 at 22:55
  • Let me rephrase that - for you to be able to do anything through your code, you need your code to run. If you aren't allowed to have the code run in the background when the screen is off, then you can't actually issue any sort of programatic code to turn the screen back on. Does that make sense? – Alex K Oct 22 '14 at 22:57
  • I'm not sure "you arent allowed to have the code run in the bg when screen is off" is entirely true. For example, if I use android:immersive="true" in my Manifest, the code will still run, even when the screen sleeps - I think at least. In addition, a livecard will also keep running when the screen sleeps. (the stopwatch for example). – Autex Oct 23 '14 at 00:17
  • Let me go find the source that told me about that. I went and asked this on the Glass Explorer forum. – Alex K Oct 23 '14 at 01:25
  • Here is the answer given to me by a fairly high level member of the Glass Explorer forums: "The ToS for developing for glass clearly states that if you are doing something (like recording) the screen MUST be on. That being said there is a way to turn it off, but it requires root which voids your warrenty. Either way you are breaking terms with Google so I wouldn't recommend doing it." – Alex K Oct 23 '14 at 01:33
  • And I'm assuming that "recording" includes recording location data, in addition to video and sound. I could be assuming too much here though – Alex K Oct 23 '14 at 01:35
  • I did a little digging into the ToS and found this, "Don't disable or turn off the display when using the camera. The display must become active when taking a picture and stay active during a video recording as part of your application." Indeed, when using the viewfinder or recording on glass, the screen stays on. But any other app will allow the screen to sleep (unless programatically set otherwise). I'm starting to think it is possible to turn on the screen based on something specific, but only with a live card: http://stackoverflow.com/questions/24669637/popup-notifications-for-google-glass – Autex Oct 23 '14 at 03:31
  • 1
    Ah, very cool find. Thanks. I'm going to look into this and see if I can find a way to do this. If that is the case, then yes, I'm pretty sure I've seen some ways to sleep and wake the screen, but yes, as you say, only with a live card. – Alex K Oct 23 '14 at 03:33
0

When the driver reaches some specific location marker you can trigger an event, in a Service or Activity, and then use PowerManager to wake up Glass.

import android.content.Context;
import android.os.PowerManager;

public void DoSomething()
{
    // If glass is sleeping then wake up
    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "Wake Lock");

    if (powerManager.isScreenOn() == false) {
        wakeLock.acquire();
    }

    // Reveal the card in the "present" timeline
    // newCard.publish(LiveCard.PublishMode.REVEAL);
    // Or, StartActivity(myIntent);

    // *** IMPORTANT: RELEASE THE WakeLock
    if (wakeLock.isHeld()) {
        wakeLock.release();
    }
}

You will also need the WAKE_LOCK permission in the manifest.

<uses-permission android:name="android.permission.WAKE_LOCK" />

Hope that helps.