How to provide customized vibration on specific incoming calls - android

The program functions like this: the user has a list of phone numbers, for which the cellphone could vibrate upon an incoming call only when no other system-wide application would provide vibration (such as in mute mode). I know that this is somehow against the rules, for that an application should respect the users' settings, but the application is limited to some certain users with this need. I have tried two ways but neither of them are satisfying:
Listen to the telephony state and directly trigger the vibration service with my own pattern (with Vibrator.vibrate()). This method is effective with no incoming calls yet randomly effective when the phone is in CALL_STATE_RINGING state and I guess it's because of the conflict with the system-wide application that actually handles the vibration upon incoming call.
Judge whether the cellphone is vibrating upon an incoming call (with AudioManager.shouldVibrate()), and decide whether to change the vibrate settings (with AudioManager.setRingerMode() and AudioManager.setVibrateSetting()). If the vibrate settings are changed by my application, they are to be restored once the cellphone is back to CALL_STATE_IDLE state. This method, however, is still not functioning sometimes, without any sign of the reason.
I hope that someone could give some advice on this issue. Comments on these two ways or other suggest are welcome.

You need to play with two settings in order for your phone to vibrate. The first one is the sound mode which needs to be set by using AufioManager:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(targetSoundMode);
The second part is not properly documented and I believe this is the missing part of your code:
String VIBRATE_IN_SILENT_SETTING_NAME = "vibrate_in_silent";
Settings.System.putInt(getContentResolver(), VIBRATE_IN_SILENT_SETTING_NAME, 1);
use 1 to turn vibrate on and 0 to turn vibrate off.
to fully understand how you should work with vibrate settings and mode take a look at the following link:
http://hi-android.info/src/com/android/settings/SoundSettings.java.html

Related

How to mute sound and vibration for all notifications in Android 5.0+?

I would like to disable sound and vibration for all incoming notifications. I don't mind if phone call vibration gets switched off, too. But the users volume settings for phone calls shouldn't be touched.
Thanks to the AudioManager, muting the sound of notifications is easy:
audioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
Disabling the vibration of notifications, however, is harder than expected.
The AudioManager has this deprecated method for vibration settings:
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF)
This, however, seems to have no effect on my Android 5.0 device and I can't find any alternatives. :(
I know that Lollipop has introduced the stuff around "Priority Mode" but I do not want to fiddle with modes. Switching the mode would probably have some other side-effect (based on the users priority preferences).
Any ideas how to mute notification sound and vibration on Android 5.0+?
Since nobody came up with a solution, I'm going to answer my own question.
It seems that there is really no way to achieve what I wanted, at least with the latest Android versions. So I ended up doing the following ugly workaround:
I'm now muting the whole phone (incoming phone calls and notifications) via RINGER_MODE_SILENT but also listen for incoming phone calls via a BroadcastReceiver. As soon as an incoming call is detected, the RingerMode gets reverted to its previous state. This effectively leads to the functionality I wanted to achieve.
I'm, however, extremely unsatisfied with this workaround.

Android: How to catch 2 times short volume-up button push or long hold?

I would like to run predefined service in my app if I do two short volume up pushes (or one long hold).
It should works also if phone has locked screen or another app is in foreground.
Thanks for any advice or example how can i do it.
The official Broadcast Receiver for volume changes only gets triggered when the state changes from volume to/from silent to/from vibrate. So perhaps, it could detect a long volume up hold under some circumstances, but I don't know. That Broadcast Receiver is not granular at all.
I've seen non-rooted apps on Google Play that detect the volume keys at a more granular level, but I believe those apps are making use of undocumented functionality (of which I do not know about).
If you're making your own rom as your username might imply, detecting those keys would be much easier to do. Is that the case? Are you making your own rom? Will the app you're making have root access?

Broadcast Receiver for volume up button

I am aware that this question has been asked already in this site, but I could not find my answer. SO this is what i am trying ot do. My power button in my phone is not working properly. So could is there a way to override my volume up button to unlock my phone.
I am planning to implement broadcast receiver But could not find the appropriate intent-filter. Another alternative that I thought was to run a service when the screen is locked, but I am not quite sure if I can listen to key events.
So if any of the above mentioned is possible, could you hint on how to get it done or suggest another alternative?
No, It do not think so. This can not be done.
There is no broadcast for volume up or down button presently. You can implement a service that will listen for any changes in the volume but it is not recommended to run a service all the time since that consumes both battery and memory.
The only solution that I can think of is using ContentObservers for listening to the changes in the settings such as volume of streams. This answer here will provide you with the code to use ContentObserver to listen for volume up or down. https://stackoverflow.com/a/7017516/1979347
This functionality you speak of has already been done multiple times and can be found on Google Play. However, I'm not sure how they did it, or how much their solution drains the battery.
I just did a test on my G2 (Android 4.2.2), I launched a broadcast monitor, I switched to the homescreen, and I changed the volume from there.
Apparently, it can detect if the volume is turned up only when it changes from silent mode > to Vibration mode > or to Sound mode. Conversely, when pressing the down volume, I think we can detect the change when it goes from the Sound Mode > to Vibration Mode > or to Silent Mode.
This is probably not what you wanted to hear, but I just wanted to confirm what the others were saying. On a non-rooted phone, what they're saying is mostly correct. On a rooted phone however, there are custom ROMs that have solved this issue.
By the way, what brand/model is your phone? On my phone, when the headset gets connected, the phone lights up. So I suppose, one could always do it the way iPhone owners do it for their phones. You'd have to cut the extremity of a head phone jack, coat half of the pin in plastic, and super glue a button on top of it to allow you to easily twist the head.

Check volume button usage when screen is off

For this question I'm going to quote another user who got no response to their question:
I've written an Andoid app that uses the hardware Volume buttons for another purpose.
It works fine if the app is running and visible, but when I turn the
screen off or let it time out, the button clicks don't get into my
handlers.
Does anyone know if there is a way to detect these button clicks when
the screen is off?
Source: AV695's question
I'm working on an app myself that makes use of the volume buttons, but as this user also noted, the normal behavior of checking buttons with onKeyPress stops working once the screen is off. This is because the Activity gets paused on screen off.
Is there a way to keep the activity running while the screen is off, or check for the usage of the volume buttons when the screen is off? I tried using a Service for this before but it's impossible to check for the volume keys like that as noted by Commonsware.
I doubt that this is supported (without resorting to a battery-draining wakelock) at either the platform, kernel, or underlying radio firmware levels without modifications to the last to bring volume presses during sleep to the attention of the kernel.
Within the realm of reasonable system-ROM modifications, a more reasonable one might be to modify an existing open source ROM for the device to insert some custom platform level code into the handling of the power button usually used to wake up the device preparatory to unlocking it - that at least we know does get the attention of the kernel. That code could then inform the user by sound or vibration if there are unacknowledged notifications.
You could optionally wait briefly, check device orientation, or look for another key press to avoid doing this in an annoying way when the user is holding the device outside their pocket and trying to unlock it.
Or you could not use the volume key and just set a timer to wake up every 15 minutes and vibrate if there are unacknowledged notifications, avoiding the need to fumble in ones pockets.
You mention it's a custom request: if implies it's one off or low-volume, another option to consider would be that a few vendors have "bluetooth watches" out with an SDK that lets you push notifications from an android device.
If you can capture the notification when it's generated, you could push it to the user's wrist, and then let the phone go back to sleep.
You cannot intercept the key while your application is in background, but instead of listening to the KeyPress itself. You can register a ContentObserver, as described in this question.
As Chris Stratton mentioned, the only way to keep your App alive is by using battery-draining wake locks.
However, since I found myself in the same situation, I came up with another solution. Unfortunately, you'll need a rooted device as well as the Xposed framework.
With Xposed, which replaces the zygot process so you can hook yourself into any constructor and method of the system, you will be able to catch the raw KeyEvents before the system handles them.
This is done in PhoneWindowManager.interceptKeyBeforeQueueing(). By using a XC_MethodHook, you can use beforeHookedMethod() on the afore mentioned method to catch every hardware button event, even if the device is in deep sleep.
After catching events you are interested in, you can create a temporary wake lock to do your things but don't forget to release the wake lock after you finished your work.
A good example of how to accomplish this is the Xposed Torch Module.
If you, however, rely on a non rooted system, the bad news is that it's simply not possible without draining the battery...
I was also trying to implement volume button press detection in my app and I left that part to be developed later once the core part is done. I was able to detect volume key press while screen is on even when phone is locked, from a background service.
Background Video Recorder 2 (BVR2) (and possible BVR1 also, I did not try) is one of the apps that can detect volume key press even when screen is off. While trying to implement volume key detection while screen is off in my app, I installed BVR2, hoping to find how it works. To my surprise it gave my app the ablity to detect volume keys even when screen is off. My app had a ContentObserver to monitor volume changes, but was not working when screen is off. When BVR2 is active my app also could detect volume key press when screen is off. Still digging.
But BVR2 has its own trigger action, that is to record video, an action you may not want to occur just for the sake of you application detecting volume key presses.
Another app is QuickClick. This app can give your app what it lacks, the power to detect volume key presses even when screen is off, without extra unwanted actions. Just install QuickClick and do not configure any action. Create a ContentObserver to monitor for stream volume changes and you are ready. You app will now be able to detect volume key presses even when screen is off.
Please note that my app runs as a background service.
Both of the apps mentioned above are meant for other uses, but uses volume key detection to perform action. I am in no way connected to any of the apps mentioned.
If these apps, and possibly dozens others, can detect volume key press, it can be done. I request experts to find out how to do it, so that we can implement in our app without relying on another app.
If you find this answer useful, please up-vote.
I am not sure if it is as simple as this but check this android blog:
Allowing applications to play nice(r) with each other: Handling remote control buttons
It explains the usage of a broadcast receiver that receives the up/down volume controls and other music controls.
In summary you should use registerMediaButtonEventReceiver

Can I block incoming calls without disabling Bluetooth - i.e., DND (do not disturb) mode - on Android

I tried posting this on the Android Developers list and didn't hear anything - maybe some of you have insight into this!
I'm working on an app that should have the ability to disable incoming calls for a user-specified period of time. It should work much the way a "DND" (do not disturb) button works on an office phone.
I've looked over the SDK, and I can't find any way to send phone calls to voicemail or disable the big incoming call screen without putting the phone in airplane mode. That would be OK if I could keep Bluetooth enabled. (I'd like the user to be able to listen to music via a Bluetooth headset while phone calls are blocked.)
In summary, any of the following would work if I could do them from my app:
Programmatically send all calls to voicemail (like a "DND" button) this would be ideal since it would still be in the call log at the end of the "do not disturb" time.
Intercept the incoming call in my app and bounce it.
Disable all radios but Bluetooth and Wi-Fi - This has the disadvantage of losing the call log.
Prevent the built-in phone call app from displaying its incoming call screen (I can disable the ringer and vibration) even though the call is received
Is any of these possible? Or maybe another approach I haven't thought of?
Just a side note: For Android phones to be the best possible productivity tool, it should be possible to use a device for email/Internet/apps without receiving constant interruptions from calls. Humans have a significant context-switch time.
As a security measure, the SDK does not provide APIs to handle incoming calls. Apart from cutting off the network radio and going into a semi-airplane mode (Option 3), I don't see how this is possible, IMHO.

Categories

Resources