Android test if haptic feedback is available - android

I have seen this question : check phone settings for haptic feedback but I don't understand the solution (for me it does not work).
Is there another solution ? I did not find anything on the javadoc.. ?
Thanks

You can add this method to any of your classes and use it to check whether haptic feedback is enabled.
public static boolean isHapticFeedbackEnabled(Context context) {
int enabled = Settings.System.getInt(context.getContentResolver(),
android.provider.Settings.System.HAPTIC_FEEDBACK_ENABLED, 0);
return enabled != 0;
}
The method goes into system settings database and checks value of "haptic_feedback_enabled" setting. If there is 0 then haptic feedback is disables, otherwise it is enabled.

Related

Get status of setting: "Control notifications on your lock screen"

On Android you have the setting "Control notifications on your lock screen" under Notifications, and to back that up you have the ability to set the visibility of your notification, as explained in the notification Tutorial.
My queston is then, is it possible to get the state of this setting?
The reason i would like the state of the setting is to give the user more options, if they have enabled content hiding, but not bother them if they haven't.
Yes, that's possible. There are two constants:
Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS = "lock_screen_allow_private_notifications"
Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS = "lock_screen_show_notifications"
However, since these values are not part of public API, they might change in future, or might not work on all devices.
int show_all = Settings.Secure.getInt(getContentResolver(), "lock_screen_allow_private_notifications", -1);
int noti_enabled = Settings.Secure.getInt(getContentResolver(), "lock_screen_show_notifications", -1);
if(show_all > 0 && noti_enabled > 0){
// Post notification
// ...
}

Android 5.0 (L) - Check data Roaming Setting

I have a problem identifying the data roaming setting in Android L. In previous versions of Android, I was able to access either Settings.Secure or Settings.Global (depending on the Android version), and get the setting.
But now, on Android L, this no longer works. Whether data roaming is on or off, the return from the Settings.Global is always 0.
Android L supports multi SIM out of the box, so, a new manager was created to handle this: SubscriptionManager. This subscription manager, handles the several settings of the several SIM cards in the form of SubInfoRecord classes. I can retrieve the settings per SIM card.
However, the dataRoaming filed inside that class is always 0 as well.
Does anyone know how can this be achieved on the new API?
My app is a system app that comes embedded in the phones from factory so, I should be able to access all the APIs available.
However, I've spent a long time looking in the source code but I found nothing. In the Settings.Global class there's no indication that that setting no longer works on Android.
Does anyone have a clue on where this setting was moved to?
Thanks in advance!
Check this DevicePolicyManager.setGlobalSetting
as from documentation this can only be called by device owner app.
Is your app is installed as device owner ?
If not you can check the following links
Create device owner without root
Create device owner with root
Do something like this
DevicePolicyManager manager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
manager.setGlobalSetting(<Admin_Component>, Settings.Global.DATA_ROAMING, <value>);
Admin_Component: Component instance
Value: "0" for disable or "1" for enable
Since android 5.0, android supports multiple SIM cards, use the following code to check for data roaming.
public static boolean isDataRoamingEnabled(Context context) {
SubscriptionManager subMngr = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
int id = SubscriptionManager.getDefaultDataSubscriptionId();
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return false;
}
SubscriptionInfo ino = subMngr.getActiveSubscriptionInfo(id);
if (ino == null)
return false;
return ino.getDataRoaming() == 1;
}

How to know if Android Wifi settings pane is on top?

I need to know if the Wifi settings pane is on top. I found this way:
ActivityManager a = (ActivityManager)context.getSystemService( Context.ACTIVITY_SERVICE );
List<RunningTaskInfo> t = a.getRunningTasks( 1 );
if ( t.get( 0 ).topActivity.getClassName().equals( "com.android.settings.Settings$WifiSettingsActivity" ) )
{
return true; // Yes it is!
}
else
{
return false; // No it's not!
}
Actually, it works fine. But in Android reference doc here it is written that we should not use this method (read the link for more details). So does somebody know how to perform this check in an other way? Note that I target SDK level 14 and higher.
Thanks!
Don't detect the settings pane, rather focus on why you would want to open the settings panel (you need WiFi connection and it is disabled, you need a particular SSID and the current one is not that one, ...).
For example, show your activity and if WiFi is disabled, show a popup to ask the user what he wants to do.

How to inhibit "Loud music may harm your hearing..." warning in Android

I have an Android application for a Samsung tablet that uses an external device which draws its power from the tablet headphone jack. When the external device is powered on (by programmatically maxing out the volume), Android briefly displays a warning popup saying: "Loud music may harm your hearing if you listen to it for too long..." I would like that message to not be displayed.
Here's the offending line of code:
mAudioMgr.setStreamVolume(AudioManager.STREAM_MUSIC, mAudioMgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
It's not an option to only turn it up halfway. In fact, I've seen the tablet display the warning (when changing the volume by hand) even on volume settings lower than the max setting.
And yes, I record the original volume, and restore it when we're done with the external device.
Thanks for any suggestions.
Since you cannot disable the systems message, and you will also have a problem when the user manually lowers the volume, I suggest you do a series of controls which will help with that.
To max the volume:
public void maxVolume() {
AudioManager audioManager = (AudioManager)context.getSystemService(this.AUDIO_SERVICE);
while ( audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) < audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) {
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
}
}
To prevent users from manually changing volume:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
|| (keyCode == KeyEvent.KEYCODE_VOLUME_UP)
|| (keyCode == KeyEvent.KEYCODE_VOLUME_MUTE)) {
return true;
}
return super.onKeyDown(keyCode, event);
}
You can call maxVolume() in your onCreate method, and later call it just before you start using the jack output procedure to make sure the system has not changed your volume.
Alternatively you can register a listener to listen for volume changes, make maxVolume() static and call it from the listener. Hope it helps you out!
I know that this question is quite old, but I just ran across this issue myself. I figured that I would add to the answers given with what I found in case it helps someone else that happens upon this.
One of the posts here mentions that the warning is not in the AOSP, but this is not true. Please reference the VolumePanel::SafteyWarning class here: VolumePanel::SafetyWarning
Once you peruse through the SafetyWarning class you'll notice that there is a hidden method inside of the AudioManager class called disableSafeMediaVolume. You can, through reflection, invoke this hidden method easily enough, but if you do then you'll get a security exception as it requires the system permission "android.permission.STATUS_BAR_SERVICE".
So... with that said, the dialog as far as I can tell, cannot be programmatically suppressed unless you have this system permission.
What we ended up doing with the blessing of product management is we cache the volume setting in SharedPreferences and then reset it from the cache when our application starts. If it happens to be above the threshold for the warning dialog then the warning will be shown and the user will just have to press OK to get the volume setting that they want, but at least they won't have to manually reset the volume themselves.
A couple of other notes on why some don't see this dialog. You will only see it if you have headphones (or earbuds, or a wired headset) plugged into the device and you attempt to set the media volume stream over a certain threshold (on the Samsung Galaxy Tab A this threshold is 60%). If you press OK at the dialog then it will not be shown again unless you either restart the device or unplug the headphones and plug them back in (with the volume set at or above the threshold).
Hope this helps someone.
I don't know if you can consider this much of an answer but I'm pretty sure you can't remove that message as its displayed by the system automatically and not by something you can access. My Samsung Galaxy S3 does the same thing.
Also from what I can read on the net some android devices also lower the volume when you put in a jack stick so "you don't harm your hearing".
Can you override the notification by displaying your own notification just before or after perhaps? The notification is just a standard Toast.

Detect Hardware Acceleration at Runtime: Android

Is it possible to consistently detect if an Activity has hardware acceleration enabled when it is created? I'm worried that users of my library will enable it through the manifest when they shouldn't, by not specifically disabling it for my Activity (as I instruct them to do.)
The only solid information I can find (http://android-developers.blogspot.com/2011/03/android-30-hardware-acceleration.html) says that I can query View.isHardwareAccelerated() and Canvas.isHardwareAccelerated(). However, for my purposes, I would like to ensure it is off when my library's Activity is shown. So far, I can't get anything to report a consistent yes/no when it is on or off. I tried hacking in a dummy view, setting it to my activity and then testing it, but it always returns false. Also, I tried testing Window.getAttributes( ).flags, but they aren't showing it either.
I am testing this because the hardware accelerated draw path for my library doesn't function correctly, and there doesn't seem like there is any way to fix it.
Try FLAG_HARDWARE_ACCELERATED in flags in ActivityInfo for the activity, which you would get from PackageManager via getActivityInfo().
I'm new in Android so I was stuck even with the clues given in the answer above.. went to search around and found this code somewhere in the sea of Google. Hope it helps someone.
/**
* Returns true if the given Activity has hardware acceleration enabled
* in its manifest, or in its foreground window.
*
* TODO(husky): Remove when initialize() is refactored (see TODO there)
* TODO(dtrainor) This is still used by other classes. Make sure to pull some version of this
* out before removing it.
*/
public static boolean hasHardwareAcceleration(Activity activity) {
// Has HW acceleration been enabled manually in the current window?
Window window = activity.getWindow();
if (window != null) {
if ((window.getAttributes().flags
& WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
return true;
}
}
// Has HW acceleration been enabled in the manifest?
try {
ActivityInfo info = activity.getPackageManager().getActivityInfo(
activity.getComponentName(), 0);
if ((info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
return true;
}
} catch (PackageManager.NameNotFoundException e) {
Log.e("Chrome", "getActivityInfo(self) should not fail");
}
return false;
}

Categories

Resources