Prevent screen from locking but allow dimming - android

I'm trying to get my Android app to keep the screen unlocked as long as the app is running. I easily solved this problem with setting this flag - WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON - in each activity.
But now I'd also like to allow the screen to dim and stay dimmed until a touch event is registered. I tried to mess with window object's screenBrightness and dimAmount but none of these worked (no changes in the brightness were made). Any ideas if I can implement this feature?
As a last resort I'd think a black dialog with alpha .5 covering the whole screen would work, but I don't know if this solution will have the same effect as an actual dim, battery-saving wise

Related

Always on without ambient mode? OR do not dim when enter ambient?

For experiment/user study purpose, I need to keep my screen on for few minutes.
I have follow the guide from http://developer.android.com/training/wearables/apps/always-on.html and it works partially for my purpose.
The code allows my screen always on BUT it will enter ambient mode after few seconds. I DO NOT want ambient mode.
My trick is I commented out onEnterAmbient(), onUpdateAmbient() and onExitAmbient().
So that my screen drawing is still the same color (not changing to black & white).
HOWEVER the screen is DIMMED darker when entering ambient mode.
I don't want the DIMMED effect, I need to keep my screen as bright as possible for the duration of my user study.
Anyone has any clue?
You may try
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
in your activity's onCreate() or use
android:keepScreenOn="true"
in your activity's layout. See this training material for details.

Any other way to set window brightness?

I maintain an auto-brightness app which uses an overlay to set screen brightness. The brightness is set through LayoutParams.screenBrightness.
Up to lollipop, I used TYPE_SYSTEM_OVERLAY for overlay type and everything was fine.
However, lollipop sees this overlay type as a threat or something and:
Doesn't turn on the screen automatically at all if such an overlay is present
If overlay is added later, the full screen notification such as alarm or incoming phone call will be suspended and reduced to notification bar
Naturally, this is a huge issue for me.
I have tested other window types and found that TYPE_PRIORITY_PHONE, TYPE_SYSTEM_ALERT, TYPE_TOAST all work (in the sense that they don't cancel the full-screen notification), but they won't respect brightness commands while in lock screen.
Is there any other way I could set screen brightness while in lock screen?

Refreshing screen brightness instantaneously

I'm trying to set System Screen Brightness instantaneously. I'm using a service to do that. Here it the code that I'm using.
Settings.System.putInt(cResolver,
Settings.System.SCREEN_BRIGHTNESS, 255);
This code works fine. But it is not instantaneous. The screen brightness value changes but the screen still looks dull. I can confirm this by reading back the screen brightness value and it is 255. If I lock the screen and come back, the screen is fully bright. How can I achieve this that the moment I set the system brightness to the maximum, I see fully bright screen?
BTW, I want to do this using a service and not an activity!
I would take a look at this link, specifically the first answer. It's a bit hackish though.
Changing screen brightness programmatically (as with the power widget)
Essentially you need to force the screen to refresh through starting a dummy activity and then finish() said dummy activity. I would have commented but I don't have enough points.

Set brightness below the system limit using alpha and save value

I've been trying to set the brightness under the limit of Android settings. There are two similar apps in Google Play doing this:
Shades
Screen Filter
The second one, even allows to use the phone with a black screen, just what I need in my app.
There are a lot of other questions about brightness but not to set it under this system limit, and I've found one question asking almost the same but not helping me:
Brightness Screen Filter
So after a lot of search I've got following code:
int brightness=0; //0 to 255, so I set it to a low level (not enough for me)
ContentResolver cResolver = getContentResolver();
//set the system brightness value, and if active, disable auto mode.
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
System.putInt(cResolver, System.SCREEN_BRIGHTNESS_MODE, System.SCREEN_BRIGHTNESS_MODE_MANUAL);
//previous lines don't set the brightness to the current screen so we set it like this:
LayoutParams layoutpars = getWindow().getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
//Now we have brightness to the minimum allowed by Android but
//to achieve what these apps do, I have to reduce alpha of the window to the min
//then the screen is black like I want, and totally usable!
layoutpars.alpha=0.05f;
window.setAttributes(layoutpars);
Here the alpha is the key, this is doing what I want, but only for current window/screen, and I need to save to the system this alpha as default, like I'm doing with the brightness and the Manual mode for applying everywhere.
Do you know how I should do this? I'm unable to find a value "System.SCREEN_ALPHA" to set it with System.putInt or another way to do this.
In the other question I linked before, pheelicks replied with a suggestion of using a transparent non-touchable over screen, but this is not working for me, and the result does not give the feeling of a turned off screen like previous apps.
Thanks.
I decided to implemented this feature just inside my app with alpha. So I couldn't solve the initial question at all....
Anyways, in the end seems that the solution to this question is the one Pheelicks replied here: https://stackoverflow.com/a/4287597/1667990
-This is launching an activity to be always on top,
-with alpha to 0.0f so it be transparent,
-that redirects the touch to the activity behind:
//Let touches go through to apps/activities underneath.
Window window = activity.getWindow();
window.addFlags(FLAG_NOT_TOUCHABLE);
-And most important thing not explained in the previous link, is to set the dim behind our window to 1 (like the dim in a messagebox, but setting to the max to be black behind :D )
window.setDimAmount ((float)1.0) ;
window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
I haven't tried that and decided not to do it in my app as it could bring unexpected behavior in my circumstances, but I think it should definitely work, if anyone try please add your feedback!

Android - Views overlaid on the last frame of a fullscreen video don't redraw properly when turning the screen on and off

I have a video that plays in portrait mode. At the end of the video, I need to display some views over it. This works fine so far.
I am however, having a problem where views that are over the last frame of a video don't redraw properly when coming back to the activity after turning the screen off, then on again, then unlocking the screen.
What i'm observing is that when the screen comes back on and I unlock. My video and images are first rendered outside of fullscreen mode (with the status bar still showing) then the screen will go into fullscreen mode shifting all of the views up and causing artifacting.
It seems like the views are being shifted out of their view bounds by the transition to fullscreen after they are rendered.
I'm really stumped as to how to prevent this from happening.
Here is the sandbox project on github to avoid making this a post full of code.
The basic setup for the project is this:
Fragment activity has a video view and a button view on it's layout.
It then adds a fragment into a contentView container. The contentView fades in 1 second prior to the end of video playback.
Everything works smoothly and the problem is with returning back to the app after powering the screen on and off.
Also, sometimes the video will just drop out entirely, leaving the views sitting atop a black background.
Thanks in advance for any help you can provide.
Here's the artifacting that happens when you turn the screen off, back on, and unlock.
Note that I had to take a picture of it. On DDMS the screenshot tool sees the images properly.
rather than prevent the screen from turning off, you can opt in to receive an event when the user unlocks the keyguard after waking the phone.
At this point, it might be a good idea to call View.invalidate on both of your views, this should cause a redraw. The draw chain is very flaky while the lock screen is up, because your app is technically visible, just under the lock screen.
registerReceiver(new BroadcastReceiver() {
#Override
public void onReceive(Context ctx, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
}
}, new IntentFilter(Intent.ACTION_USER_PRESENT));
It looks like the overlay layout was shifted by controller bar.
Don't you think it was affected by controller (play/pause/ff/rew + progress) area?
there may be a way to prevent the screen going off in 1st place as this would be good resolving your re draw issues, hope this helps.

Categories

Resources