disable app usage tracking temporily - android

I am using UsageStatsManager to get the history of app usage records in a particular day. The problem I have been facing is, for example, when user opens Facebook at 1:00pm, and at 1:01pm, he locks the screen, with facebook still in foreground, and unlocks the screen again at 2:00pm. The app usage API shows history of 1 hour usage of Facbook, whereas I want it to show just 1 minute. Is there any way I can disable the tracking when is the screen is inactive or disable the permission temporily. I am stuck and couldn't find any solution.

I think you can do it by checking whether your device is locked or not and to check that there is a simple trick -
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
//it is locked
} else {
//it is not locked
}
So everytime you like to add time just put the above check there.

Related

How Do I determine which app is causing the “Screen Overlay Detected” error?

Let me preface this with I have already read through Android "Screen Overlay Detected" message if user is trying to grant a permission when a notification is showing and I understand exactly what the overlay problem is.
I also understand how to request it as well as how to check if my current running app has permission to draw on overlays (!Settings.canDrawOverlays(this)).
I was getting scolded for my app because every time a permission request would popup, the overlays popup would be shown and even though users gave permission for my application to draw overlays, they could never get past the permissions screen.
After some digging, the problem was that some users were running a recording app on their screen:
The problem is, this is not my app! This is AZ Screen Recorder (link) which runs as an overlay, but due to the fact that they are in my app at the time this popup appears, I am blamed for the issue.
I would like to display something to the user that they need to check for any other apps that are running something as an overlay, but I do not know how to check for this.
I can check if they are able to draw overlays in my application, but I do not know how to check for others.
My question therefore is, is there a way to programmatically check if there are currently any apps that are running an overlay at runtime and if so, can I find out the package name of said apps?
(PS, I have no qualms with the screen recorder app in question, I just wanted to link to it so that anyone can download and test if they choose)
If you can make a test view and generate a tap event on that, then for nougat and oreo versions you can use:
view.setOnTouchListener((v, event) -> {
if ((event.getFlags() & 0x2) != 0) {
mPresenter.onVideoViewTapped();
return false;
}
return false;
});
Here 0x2 is the value for FLAG_WINDOW_IS_PARTIALLY_OBSCURED, which is hidden as per documentation.

isKeyguardLocked vs isDeviceLocked

I'm currently facing a troublesome bug with some Android Devices. I'm checking up on KeyguardManager to see if I need to disable audio or not (I want the audio be disabled whenever the game is not active or screen is locked etc.)
I've been using isDeviceLocked method to see if the user has access or not.
But now I found a device that continuously reports that `isDeviceLocked' == true.
Android docs says
boolean isDeviceLocked ()
Returns whether the device is currently locked and requires a PIN, pattern or password to unlock.
boolean isKeyguardLocked ()
Return whether the keyguard is currently locked.
What's the difference between isDeviceLocked and isKeyguardLocked, except for that they were added in different SDK versions?
The isDeviceLocked() method returns true only when the device is locked and requires a PIN, pattern or password to unlock. The isKeyguardLocked() method is similar but it will also return true if the device is locked without any security.

How can I prevent Galaxy S3 from stopping my app when turning idle?

I am currently updating my Android app with Samsung Galaxy S3 and was shocked that I couldn't stop the phone pausing my app when turning idle. With the Galaxy S2 of our department there doesn't occur this particular problem, if the screen goes black the app still streams data to the sd-card and over the wifi-network. The S3 stops any data-stream.
I tried now fiddling with the energy- and display-settings but I have no solution to the problem so far. My Internet-search was not succesfull either.
Possible solutions are rooting the new phone and thus making advanced settings visible
or increasing the time-out (which i dont like so much as a solution).
Do you have any ideas how to solve the issue or general input that might enlighten me?
Thnx!
BTW: here is the app in question (no ad):
Google Play Link
I have an app which needs to do something similar (it's a running trainer, so it needs to keep talking while the user keeps their phone in their pocket for half an hour or so.)
First off, a caveat for other people reading: don't do this if you don't have to. If you only need to do something periodically, rather than continuously, consider using AlarmManager, which can wake the phone up from sleep every now and again to do something, so won't hit the user's battery so hard.
But, if you're sure you need to keep the phone awake, you need to use a WakeLock. Here's roughly what I do in my service's onStartCommand:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK;, "RunClockService");
mWakeLock.acquire();
...where mWakeLock is an instance variable of type PowerManager.WakeLock. PARTIAL_WAKE_LOCK keeps the CPU running, but doesn't keep the screen on. The "RunClockService" tag is just used for debugging, according to the documentation. Change it to your class name.
Then, when I finish needing to keep the phone awake:
mWakeLock.release();
You'll also need to add WAKE_LOCK permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.WAKE_LOCK"/>

unlocking a screen via code in android

How do I unlock phone screen when some event happens?I tried the following code but it does not unlock the screeen . By unlock I mean bypass PIN or pattern
Am using following code and its get triggered when a sms is received.
private void unlockScreen(Context context){
Log.d("dialog", "unlocking screen now");
PowerManager powermanager = ((PowerManager)context.getSystemService(Context.POWER_SERVICE));
WakeLock wakeLock = powermanager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
wakeLock.acquire();
Window wind = DialogActivity.this.getWindow();
wind.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
wind.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
wind.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
}
Screen is powered on but the user has to enter PIN/pattern.How do I get over it?
Straight from the android API Site for disableKeyguard():
Disable the keyguard from showing. If the keyguard is currently
showing, hide it. The keyguard will be prevented from showing again
until reenableKeyguard() is called. A good place to call this is from
onResume() Note: This call has no effect while any DevicePolicyManager
is enabled that requires a password.
Based off that bolded statement I would probably say that you cannot do that without a password. The only way passed that is if you had yourself(app) added to the phone as a device admin, then you could control that from your device admin application of removing the password, wiping it etc.
Source : KeyguardManager.KeyguardLock & DevicePolicyManager
EDIT
I found the source code of the LockPatternUtils (I know it is from older version, but I doubt it has changed much) that is in part pattern locks and it has DevicePolicyManager all over it. I believe it has an internal service running as root in the system that does all the work. So without being a device admin, you do not even have authority to unlock the phone when it has a security setting for it.

How to tell if device is sleeping

Here's my scenario. I have an app that is playing backgound sounds. Using the BroadcastReceiver I can tell when the display turns off, and then kill the sounds. I can also tell if the screen turns back on. However, if the device is in the lock state I don't want the audio to start. Therefore I wait for the ACTION_USER_PRESENT intent to signal. That all works, except that if the user turns the screen back on quickly after it was turned off, you don't get the lock screen or the ACTION_USER_PRESENT message. So, is there a way to tell, when the screen turns back on, if the device is locked or not, which I guess also means sleeping or not?
((PowerManager) getSystemService(Context.POWER_SERVICE)).isScreenOn()
You can try the KeyguardManager to check if the device is locked. Here is some code (I haven't tried this myself):
KeyguardManager kgMgr = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean showing = kgMgr.inKeyguardRestrictedInputMode();
Good luck!
Satur9nine's solution was right at the time, but since then isKeyguardRestricatedInputMode() was deprecated. Some powerManager related functionalities are now deprecated as well.
There's a newer, more accurate solution: isKeyguardLocked() for whether the device is locked, and a different approach to obtain whether the screen is interactive; You're looking for a combination of both.
KeyguardManager appKeyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
PowerManager appPowerManager = (PowerManager) getSystemService(Context,POWER_SERVICE);
boolean showing = !appKeyguard.isKeyguardLocked() && appPowerManager.isInteractive();
((PowerManager) getSystemService(Context.POWER_SERVICE)).isScreenOn()
tells if the screen is on. So, it gets true if the screen is on but the device is locked.
Instead,
inKeyguardRestrictedInputMode()
gets true just if the device is locked.

Categories

Resources