How to re-enable screen dimming? - java

I needed to keep screen on while displaying my application so I disabled Dimming using this code :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
the problem is that when I close the application, dimming is still disabled
and I want to re-enable screen dimming for power saving is there a code to reverse it ?
note my application has services working in background all the time,I think it's irrelevant as I put the code in an activity class;

You should use the View function setKeepScreenOn(). It's more friendly.

Related

Enable/Disable Touch Screen on Android

Is there any way of disabling / enabling the touch events in a certain activity
without disabling certain buttons. like if the phone is "frozen" on a certain activity page for limited time?
I need that the user wont be able to go back on page, slide out or any other events.
Is it possible? (I'm using android studio, java)
Thanks in advance!
Any answer would help :)
It's possible.
Actually there is no special function to make it.
To make it, you can make a full screen button with transparent background.
And you will define a function of the button.
Of course, it will be an empty function.
Well, you can do nothing on the screen.
and then you can hide or visible the button according to your necessary.

How to close an application without animation

I need to have an invisible button that close my application but without the animation displaying that an application was closed.
The transition must be seemless because the app will be displaying a screenshot of phone's home page and it must look likes there were no application opened when the user touch the button.
I tried removing activity animations in theme and in code but there is still an animation when the app close.
Is it possible ? How ?
This isn't possible, as that is something that is dealt with on an OS level. You could intentionally crash the app by throwing an uncaught exception or something like that, but then you will get a dialog showing that it indeed crashed.
I think you need to go back to the drawing board with coming up with a good prank. :)
Maybe be you can try this:
finish();
overridePendingTransition(0,0);
You need invoke overridePendingTransition(0,0) after finish that will disable transition animation

Android: Customizing recent apps thumbnail (screenshot by default)

The app I'm working on shows some sensitive information that must not be shown on the "Recent Tasks" screen when stopping the app by pressing the home button.
I'd like to blur the sensitive data in the screenshot or show the app logo instead.
I am aware of the following approaches but they don't fit my requirements:
Setting the actvitie's android:excludeFromRecents to true in the manifiest prevents the app from being shown at all in the recent tasks. This would disrupt the user experience.
Using FLAG_SECURE results in a blank card on the recents tasks screen. (How do I prevent Android taking a screenshot when my app goes to the background?) I don't like the blank screen. However, I'll stick to this solution if there is no workaround.
Overriding onCreateThumbnail seems like the ideal solution but, unfortunately, doesn't work as it's currently not invoked by the OS :( (https://code.google.com/p/android/issues/detail?id=29370)
And then there are some workarounds that I tried out but that didn't work as hoped:
Start a new activity that shows the app logo in onPause so that it's screenshot is shown instead of the actual activitie's one. But the new activity takes too long to open and it disrupts the user experience.
Set the activitie's content view to an image of the app logo in onPause. That seemed like a great solution to me. Unfortunately, the screenshot for the recent tasks screen is taken at an unspecified time. During testing the app logo quickly appears before the app is closed when pressing 'Home' but the resulting screenshot shows the activity a short time before that.
Removing the sensitive data from the widgets (e.g. textView.setText("")) has the same problem of screenshot timing just mentioned.
Any alternative ideas or solutions to the listed workarounds?
I looked into this a couple of months ago for the same purpose as you.
Unfortunately, I had to conclude that it is simply not possible. I dug through the android source code and confirmed it.
There is no callbacks or methods from android that allows you to customize it (that works anyway). Besides FLAG_SECURE, this part of the code does not accept any input or change.
OnPause and similar lifecycle methods are called too late (the screenshot is taken already). All lifecycle methods that would hint that you're about to go into the background runs too late.
The image you see in the recent tasks is an actual screenshot - and thus isn't affected by changes you do (too late) to your view. That means you can't modify your view just-in-time (like making it invisible, replacing with something else, adding SECURE_FLAG, or any other obstruction of the view). As an aside, these images can be found on an emulator at /data/system_ce/0/recent_images.
The only exception is using FLAG_SECURE, which will prevent the screenshot from being taken of your application. I experimented with setting this FLAG in onPause and removing it in onResume, however as mentioned already these lifecycle methods runs after the screenshot is taken already, and thus had absolutely no effect.
As discussed in How to change the snapshot shown by recent apps list? there used to be a callback that you could use to customize the thumbnail: onCreateThumbnail. However, this does not work and it is never called. To be clear, the callback is still there, it is simply never called by the OS. The fact that it stopped working is poorly documented, but apparently was silently deprecated/removed in 4.0.3
As for the thumbnail itself, it is a screenshot taken serverside. It is taken before onPause is called (or in fact before any callbacks indicating that your activity is about to go into the background is called).
When your app does go into the background, your actual view is animated (to get that zoom-out transition). That animation can be affected through changes you do in onPause (if you're fast enough that is) (I experimented with setting opacity to 0 on the window among other things). This will however only affect the animation. When the animation is finished, the view is replaced by the screenshot taken earlier.
Also see these questions that discuss this:
When does Android take its recent apps switcher screenshot?
Show custom application image in task manager on ICS or JB
Android never call method onCreateThumbnail
Currently (28/10/2020) is impossibile customizing app thumbnail in recent apps screen.
As explained by #Dellkan in the previous answer, the onCreateThumbnail method is not called anymore by the OS.
Unfortunately, also the suggestion to create a kind of launcher/splash screen without the FLAG_SECURE flag to let the app take a screenshot of that activity is not working, because the screenshot is taken on the activity you see and not at the launch of the app.
You cannot even customize the color of window background when using FLAG_SECURE as reported here.
How about implementing a layout overlay on top of your entire activity?
Make it transparent, it's click-through by default, so no negative impact on UX while in use.
In onPause() set a half-transparent, blurred image as the background of that layout, the data will be scrambled behind it. In onResume() change the background to fully transparent again. Voila.
It might be faster than other types of overlays. The positive side effect is, if you do the unblurring as a short animation effect when the user goes back (with a proper library that uses C++ instead of Java), it might even look cool and the users wouldnt even mind seeing it.
I haven't tried this myself, but it's something you haven't tried yet.
Since onPause is called to late, I use WindowFocusChangeListener to observe when the Fragment loses focus. At this moment we can hide all view which show sensitive data:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.getViewTreeObserver().addOnWindowFocusChangeListener(new ViewTreeObserver.OnWindowFocusChangeListener() {
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// hide sensitive data when window moves to background (before system screenshot is captured)
myViewWithSensitiveData.setVisibility(hasFocus ? View.VISIBLE : View.INVISIBLE);
}
});
There is a way to customize it. You need your Activities with sensitive data to FLAG_SECURE in onCreate before you setContentView. Then you need an empty Activity, which renders whatever you want to have as the customized thumbnail. This usually is some sort of splash screen. This new Activity needs to be the launcher and is the only Activity not FLAG_SECURE. This Activity is launched and in onResume starts your actual Activity with the sensitive data.
Android OS will take a screenshot of that new Activity at the beginning of your App. Unfortunately the users will also see this Activity for a short moment. Since every other Activity is FLAG_SECURE, Android OS will use the only available screenshot it made at the beginning.
Was looking for a solution and found some dirty things in case you don't want to use 'FLAG_SECURE'. It doesn't give a nice picture but protects data and doesn't prevent making screenshots for the user while they are in the app.
protected void onPause () {
this.getWindow().getDecorView().getRootView().setScaleX((float)200);
this.getWindow().getDecorView().getRootView().setScaleY((float)200);
super.onPause();
}
protected void onResume () {
this.getWindow().getDecorView().getRootView().setScaleX((float)1);
this.getWindow().getDecorView().getRootView().setScaleY((float)1);
super.onResume();
}
I think this can only achieve through BroadCastReceiver but there is no receiver present. So therefore you first disable default screenshot functionality in android and then implementing your own functionality to take screenshot and before taking screenshot you should blur your secure information.

Refresh of screens on backgrounding and foregrounding an application

I am working on an Android app with multiple activities. When moving from one activity to another in certain cases I want to refresh the display but not in others.
One case is where I background the application and foreground it again. When I foreground it, I want to refresh everything on the screen depending on which activity
I backgrounded to begin with. How can I do this? I am unfortunately a bit new to Android so some appropriate basics where applicable would also be helpful.
http://developer.android.com/guide/components/activities.html#SavingActivityState
You can use onSaveInstanceState() to save information before onPause() is called. In onResume() you can use the saved info as a case in a switch statement or some conditional to refresh what you want.

EditText selects by default?

Whenever I have an EditText field in my android application, it is highlighted with the blinking cursor for input as soon as the activity is started (though the keyboard doesn't pop up). How can I disable this?
EditText and ListView are focusable in touch mode so when you launch your application by tapping on its icon (putting you into touch mode) the first one in your application will likely get focus. Had you entered your application by using the D-pad (moving you out of touch mode) the first Button, Spinner, EditText, or ListView would get the focus since they are all focusable.
I wouldn't get too hung up on the a View being in focus when your application starts but if you really can't stand the default way of handling focus, you could try giving focus to a TextView since it doesn't look different when it is in focus. Keep in mind that it isn't normally focusable in touch mode so you will need to enable it prior to requesting focus. Since this is a bit hacky, others (myself included) will caution you against going down this route and likely encourage you to accept the normal behavior.
View tv = findViewById(R.id.MyTextView);
tv.setFocusableInTouchMode(true);
tv.requestFocus();
PS: You may also want to check out this other SO question as it is nearly identical to yours.
You could call setFocusable(false) to avoid this. But if it should become focus later then maybe you have to call setFocusable(true) again later.

Categories