Android - CaptioningManager class - android

In CastCompanionLibrary, which I use in my application, there is a part of code, which looks like this:
CaptioningManager captioningManager = (CaptioningManager) mContext.getSystemService(Context.CAPTIONING_SERVICE);
return captioningManager.isEnabled();
The documentation for CaptioningManager class is very brief and I do not know why method isEnabled() returns different values (true or false) on various devices.
Does anybody know, what determines the returned value or how can I change this value?

It returns true if at the system level user has enabled captions (Settings -> Accessibility -> Captions), false other wise.
CCL, for versions of android that do have that settings in the framework, honors that settings and uses that for styling tracks. For earlier versions of Android, it offers a preference page that provides the same set of options.

We have the next statement in the source code:
/**
* #return the user's preferred captioning enabled state
*/
public final boolean isEnabled() {
return Secure.getInt(
mContentResolver, Secure.ACCESSIBILITY_CAPTIONING_ENABLED, DEFAULT_ENABLED) == 1;
}
So, the value depends on a user's accessibility settings. I'm not sure but looks like it is settings to show subtitles by default or not.

Related

How to get android:configChanges values from ActivityInfo class

I would like to fetch activities info(Such as configchanges, resizemode, if Picture in Picture is supported) of all the packages present in the device.
I am able to fetch the activities info using PackageManager with GET_ACTIVITIES flag. With that I can get configChanges value using ActivityInfo.configChanges.
However the value returns a random int if there are multiple config values set in android:configChanges.
For ex:
if below values are set
android:configChanges="uiMode|smallestScreenSize|locale|colorMode|density"
Getting configchanges value using below code
PackageInfo packageInfo = mPackageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
ActivityInfo activityInfo[] = packageInfo.activities;
if(activityInfo!=null) {
for(ActivityInfo activity : activityInfo) {
int configChange = activity.configChanges;
}
}
I get activity.configChanges value as 23047
What does 23047 denotes, how do I decode it so that I can get the config values that are set in AndroidManifest.xml
In Addition to that is there any way we can get activity.resizeMode . I understand that it is #hide api. I can see the value in debugging mode though in Android Studio.
Any leads/help to above will be really useful.
configChanges is a bit mask.
To check if a given bit is set, you simply need to use an appropriate bitwise operator.
For example, to check if uiMode is set you could do something like this:
int configChanges = activityInfo.configChanges;
if ((configChanges & ActivityInfo.CONFIG_UI_MODE) == ActivityInfo.CONFIG_UI_MODE) {
// uiMode is set
} else {
// uiMode is not set
}
Defining a method might make it easier:
public boolean isConfigSet(int configMask, int configToCheck) {
return (configMask & configToCheck) == configToCheck;
}
And you would call it like this:
int configChanges = activityInfo.configChanges;
boolean uiModeSet = isConfigSet(configChanges, ActivityInfo.CONFIG_UI_MODE);
boolean colorModeSet = isConfigSet(configChanges, ActivityInfo.CONFIG_COLOR_MODE);
// ...
In Addition to that is there any way we can get activity.resizeMode .
I understand that it is #hide api.
Reliably, no. You might be able to access it through the reflection API, although Google released a blog post recently stating the following:
Starting in the next release of Android, some non-SDK methods and
fields will be restricted so that you cannot access them -- either
directly, via reflection, or JNI.
(accessing hidden fields via reflection is strongly discouraged anyway)

change default preference on upgrade

In my app, I have the following code that tells me if a feature is enabled by default :
public boolean getFeatureEnabled()
{
return mPrefs.getBoolean("FEATURE_ENABLED", DEFAULT_ENABLED);
}
This preference is overwritten only when the user changes the setting from UI. So by default it draws its value from DEFAULT_ENABLED which is a class variable somewhere.
In the current version, DEFAULT_ENABLED is true but on the next version of my app will be false.
The problem is that after the update, with the above code the old users who did not change the default setting from UI will have their feature disable - and I want to avoid this.
Any advices on how to handle this ?
As I understand, you have a feature that was enabled by default but this default was never written to SharedPreferences unless explicitly changed by the user.
Now you want the feature to be disabled by default but without affecting the behavior for users that already have it enabled.
I can think of 3 options:
Option 1 If you are already saving the last version, you could check that in your migration logic:
private void migratePreferences(Context context) {
SharedPreferences prefs = context.getSharedPreferences("your_preference_file", MODE_PRIVATE);
int lastKnownVersionCode = (prefs.getInt("LAST_INSTALLED_VERSION", BuildConfig.VERSION_CODE);
prefs.edit().putInt("LAST_INSTALLED_VERSION", BuildConfig.VERSION_CODE).apply();
//this is the old featureEnabled check
boolean oldPreferenceValue = prefs.getBoolean("FEATURE_ENABLED", true);
boolean newPreferenceValue;
if (prefs.contains("FEATURE_ENABLED")) {
//the feature was modified by the user so respect their preference
newPreferenceValue = prefs.getBoolean("FEATURE_ENABLED", false);
} else if (lastKnownVersionCode == BUGGY_VERSION_WITH_FEATURE_ENABLED_BY_DEFAULT) {
//the user is updating from the buggy version.
// this check could include a range of versions if you've released several buggy versions.
// this is also where option 2 would be inserted
newPreferenceValue = oldPreferenceValue;
} else {
//the new default that will apply to fresh installs
newPreferenceValue = false;
}
//save the preference
prefs.edit().putBoolean("FEATURE_ENABLED", newPreferenceValue).apply();
}
This, however depends on your already having a call to this method somewhere in your app startup code.
Option 2 In case you don't, there is still hope. You can check if this is your first install using the answers given in this StackOverflow answer
Option 3 You can release an intermediate version of your app that behaves as it does now but saves the unsaved default setting in SharedPreferences. This will keep the feature AS IS for your eager users but you will have to wait until a significant portion of users updates before releasing the desired behavior.
Put another flag "FIRST_TIME" as "true" in your preferences in new build. Check on the very first screen of your app
if(FIRST_TIME==true)
{
//put FEATURE_ENABLED = false;
//put FIRST_TIME = false;
}
By doing this FEATURE_ENABLED will set to false for the first time the user launches the app and will not consider the default value

ActivityManagerCompat.isLowRamDevice is useless, is always returns false

I wanted to use the helper method isLowRamDevice for my app, which streams videos. As I support devices down do API level 15, I had to use ActivityManagerCompat.isLowRamDevice().
I was really confused, that it always returned false, even if I use really old devices. Then I checked the method itself and saw this:
public static boolean isLowRamDevice(#NonNull ActivityManager am) {
if (Build.VERSION.SDK_INT >= 19) {
return ActivityManagerCompatKitKat.isLowRamDevice(am);
}
return false;
}
So no wonder it always returns false on my Android 4.0.4 device. But for me this makes absolutely no sense. Or am I missing something?
So no wonder it always returns false
It does not always return false.
On devices running Android 4.3 or older, it will always return false. That is because the system flag for being a low-RAM device did not exist back then.
On devices running Android 4.4 or higher, it will return the value of the system flag for whether this is a low-RAM device or not:
/**
* Returns true if this is a low-RAM device. Exactly whether a device is low-RAM
* is ultimately up to the device configuration, but currently it generally means
* something in the class of a 512MB device with about a 800x480 or less screen.
* This is mostly intended to be used by apps to determine whether they should turn
* off certain features that require more RAM.
*/
public boolean isLowRamDevice() {
return isLowRamDeviceStatic();
}
/** #hide */
public static boolean isLowRamDeviceStatic() {
return "true".equals(SystemProperties.get("ro.config.low_ram", "false"));
}
(from the ActivityManager source code)
AFAIK, low-RAM devices mostly will be Android One devices. Depending upon where you get your devices, you may not encounter one of those.

Android 4.0 4G toggle

This is for the Verizon LTE version of the Samsung Galaxy Nexus.
I am tasked with writing a tiny app that will effectively disable/enable 4G capability. This can be done manually via settings > mobile network > network mode and choosing either LTE/CDMA (4g enabled) or CDMA (3g only).
I have not tried anything yet because Android development isn't my strong suit. I am looking for guidance... examples, code samples etc. I am assuming this should almost be a one-liner, but it has been my experience that with Android development nothing is as simple as it appears.
Any help will be greatly appreciated.
There is a preference in the Settings.Secure class that is hidden from the SDK:
/**
* The preferred network mode 7 = Global
* 6 = EvDo only
* 5 = CDMA w/o EvDo
* 4 = CDMA / EvDo auto
* 3 = GSM / WCDMA auto
* 2 = WCDMA only
* 1 = GSM only
* 0 = GSM / WCDMA preferred
* #hide
*/
public static final String PREFERRED_NETWORK_MODE =
"preferred_network_mode";
You could use Reflection on this or just localize the constant to your project. The problem with this is that you cannot change the value of this setting (as with all secure settings), you can only read it. The aforementioned values are not the only possible ones, there are actually a few more located in com.android.internal.telephony.RILConstants, which is again hidden from the SDK and would require Reflection to access.
There is another hidden method in TelephonyManager, but again it is read only there is no other method for setting this constant. This would tell you exactly what you want to know, whether the device is set to "LTE/ CDMA" (LTE_ON_CDMA_TRUE) or "CDMA only" (LTE_ON_CDMA_FALSE):
/**
* Return if the current radio is LTE on CDMA. This
* is a tri-state return value as for a period of time
* the mode may be unknown.
*
* #return {#link Phone#LTE_ON_CDMA_UNKNOWN}, {#link Phone#LTE_ON_CDMA_FALSE}
* or {#link Phone#LTE_ON_CDMA_TRUE}
*
* #hide
*/
public int getLteOnCdmaMode() {
try {
return getITelephony().getLteOnCdmaMode();
} catch (RemoteException ex) {
// Assume no ICC card if remote exception which shouldn't happen
return Phone.LTE_ON_CDMA_UNKNOWN;
} catch (NullPointerException ex) {
// This could happen before phone restarts due to crashing
return Phone.LTE_ON_CDMA_UNKNOWN;
}
}
From my research you could not make such an application without root access and using something like setprop from the command line, but even then you may need to restart the entire Telephony process in order for this setting to take effect.
Finally, if you are still interested see com.android.phone.Settings to see how the system handles this toggle. It is rather elaborate, and as I mentioned would require permissions that a normal Android application would not be granted.
I'm also interested in changing the settings WCDMA-only, WCDMA/LTE, ...
I found the way to change Settings.secure.* with root privilege as is shown the below.
new ExecuteAsRootBase() {
#Override
protected ArrayList<String> getCommandsToExecute() {
ArrayList<String> cmds = new ArrayList<String>();
cmds.add("su -c 'chmod 755 "+mySqlite+"'");
cmds.add("echo \"UPDATE secure SET value='"+ value +"' WHERE name='"+ key +"'; \" | "+mySqlite+" /data/data/com.android.providers.settings/databases/settings.db");
//TODO: SQL injection can be done!!!
return cmds;
}
}.execute();
ExecuteAsRootBase is introduced here, and mySqlite is "/data/data/"+context.getPackageName()+"/files/sqlite3" where sqlite3 is put in advance.
However, it seems that we have to call com.android.internal.telephony.Phone.setPreferredNetworkType() for switching (WCDMA only<=>WCDMA/LTE) after setting Settings.secure.PREFERRED_NETWORK_MODE.
My phone (even set Settings.secure.PREFERRED_NETWORK_MODE = 2) attached to LTE network...
All the other answers are correct that this requires access to Settings.Secure. Take a look at how the phone app handles this setting https://github.com/dzo/packages_apps_phone/blob/master/src/com/android/phone/Use2GOnlyCheckBoxPreference.java
or take a look at the Toggle2G app source:
https://github.com/TheMasterBaron/Toggle-2G
http://developer.android.com/reference/android/provider/Settings.System.html
Aside from writing the code in your Activity.java, you will probably have to ask for permission to access these settings in the AndroidManifest.xml. So it's annoying but should be simple enough.

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