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)
Related
I am trying to check whether an external application (i.e. not the one I am writing, but another one running on the debug device) has draw-over permissions. However, regardless of the method I use, I always get "false" as answer, whence I know for certain that said application can draw over other apps. In fact, not only I explicitly gave it permissions through the Settings interface, but I actually witnessed it performing a view overlay.
This is the first method I used:
pm.checkPermission(Manifest.permission.SYSTEM_ALERT_WINDOW, "target.package")
This is the second method I used (packageInfo being the PackageInfo object associated with "target.package"):
boolean requestedPermissionGranted = false;
for (int i = 0; i < packageInfo.requestedPermissions.length; ++i) {
if (packageInfo.requestedPermissions[i].equals(Manifest.permission.SYSTEM_ALERT_WINDOW)) {
requestedPermissionGranted =
(packageInfo.requestedPermissionsFlags[i] & PackageInfo.REQUESTED_PERMISSION_GRANTED) != 0;
break;
}
}
hasPermission = requestedPermissionGranted;
Both expressions return false. By the way, I verified that packageInfo.requestedPermissions actually contains SYSTEM_ALERT_WINDOW.
I searched far and wide, but I could not find any topic that would answer my doubts. I only managed to find threads about how to check an app's own permissions, which I am not interested in.
P.S: I cannot use the shell through getRuntime().exec() and list active windows, as I get permission denied.
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
I have following situation. Another application is calling mine (using startActivityForResult).
Since I have to be sure that the calling activity comes from developer I trust I'd like to read the developer public key and compare it with value that is hardcoded in my app.
I tried following:
String packageName = callingActivity.getPackageName();
String signature = null;
try {
PackageInfo pi = manager.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
// assumption: get the first available signature
// actually according to Google for applications there will be always one element
signature = pi.signatures[0]. toCharsString();
}
But this gives me application (not developer) signature.
Use android custom permissions for this purpose. By defining a permission in your application, you can restrict other apps that use your activity/service unless they have a uses-permission in their manifest.
Read this for more info :
http://developer.android.com/training/articles/security-tips.html#Permissions
For an example:
https://stackoverflow.com/a/8817231/607968
original question
I have a standard texttospeech, android.speech.tts.TextToSpeech
I initialize it and set a language by using tts.setLanguage(Locale.getDefault())
That default Locale is de_DE (for germany, correct).
Right after setting it, i ask the tts to give me its language tts.getLanguage()
now it tells me that its set to "deu_DEU"
There is no Locale with that setting. So i cant even check if its set to the right language because i cant find the Locale object that has the matching values.
Issue might be related to Android 4.3, but i didnt find any info.
Background is, that i need to show values with the same decimal symbol, but tts needs the correct symbol or it says "dot" in german which makes NO sense at all.
Conclusion:
A Locale is a container that contains a string that is composed of a language, a country and an optional string. Every text-to-speech engine can return a custom Locale like "eng_USA_texas".
Furthermore the Locale that is returned by the tts engine can only be a "close match" to the wanted Locale. So "en_US" instead of "en_UK".
However, Locale has a method called getLanguage() and it returns the first part of above mentioned string. "en" or "eng". Those Language codes are regulated by ISO and one can hope that everyone sticks to it. (see link in the accepted answer)
So checking for tts.getLanguage().getLanguage().startsWith("en") should always be true if its some form of english language setting and the ISO standards are fulfilled.
It is important to mention that Locales should not be compared by locale_a == locale_b as both can be different objects yet have the same content, they are containers of sort.
Always compare with locale_a.equals(locale_b)
I hope this helps people sort out some problems with tts and language
You're right, it's frustrating how the locale codes the TTS object uses are different to those of the device locale. I don't understand why this decision was made.
To add further complication, the TTS Engine can supply all kinds of different locales, such as eng_US_sarah or en-US-female etc. It's down to the TTS Engine how these are stored and displayed.
I've had to write additional code to iterate through the returned locales and attempt to match them to the locale the system can use, or vica-versa.
To start with, take a look at how the engines you have installed are returning their locale information. You can then start to collate in your code a list to associate 'deu_DEU' to 'de_De'.
This is often simplistic by using split("_") & startsWith(String), but unfortunately not for all locales.
Here's some base code I've used to analyse the installed TTS Engines' locale structure.
private void getEngines() {
final Intent ttsIntent = new Intent();
ttsIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
final PackageManager pm = getActivity().getPackageManager();
final List<ResolveInfo> list = pm.queryIntentActivities(ttsIntent, PackageManager.GET_META_DATA);
final ArrayList<Intent> intentArray = new ArrayList<Intent>(list.size());
for (int i = 0; i < list.size(); i++) {
final Intent getIntent = new Intent();
getIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
getIntent.setPackage(list.get(i).activityInfo.applicationInfo.packageName);
getIntent.getStringArrayListExtra(TextToSpeech.Engine.EXTRA_AVAILABLE_VOICES);
intentArray.add(getIntent);
}
for (int i = 0; i < intentArray.size(); i++) {
startActivityForResult(intentArray.get(i), i);
}
}
#Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
try {
if (data != null) {
System.out.print(data.getStringArrayListExtra("availableVoices").toString());
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
From the above ISO-3 codes and the device locale format, you should be able to come up with something for the locales you are concerned with.
I've been intending to submit an enhancement request to AOSP for a while, as all TTS Engines need to use constant values and extras such as gender etc need to be added to use the TTS Engines to their full capabilities.
EDIT: Further to your edit, note the wording regarding setLanguage(). The individual TTS Engine will try and match as close as possible to the requested locale, but that applied locale may be completely wrong, depending on how lenient the Engine provider is in their code and their response.
After creating an object of TextToSpeech class, you should configure it (or check it's available state/values) into TextToSpeech.OnInitListener's onInit() callback. You will get reliable information there about your TextToSpeech object.
Check my answer here:
https://stackoverflow.com/a/65620221/7835969
I want to check if my app is running on a background mode.
The problem is that i have many activities(list activities, map activities etc.). Initially I have tried in the life cycle's resume and pause(or the onUserLeaveHint) methods to set a static boolean as true or false and work with this way. But this obviously can't work because when I move from one activity to another, the previous one get paused.
Also, I've read here on stackoverflow that the getRunningTasks() should be used only for debugging purposes. I did a huge research but I can't find a solution. All I want to do is to be able to detect if a the app is running on a background. Can anyone propose me a way, or express any thought on how can I do that?
You can try the same mechanism (a boolean attribute) but on application side rather than activity side. Create a class which extends Application, declare it in the manifest file under <application android:name=YourClassApp>.
EDIT: I assume you know that activities aren't intended for background processing, if not you should take a look at the Services.
I don't know if this will help but you can use
getApplicaton().registerActivityLifecycleCallbacks(yourClass);
To get a birds eye view of how your activities are displayed in the FG. (For older s/w you can use this)
If your Application has a Service you could have a static get/set which accesses a static variable. Do not do this in Activities though, it causes mem leaks.
But realistically speaking there is no tidy way of tracking if your application is running or not.
I had the same problemen when overwriting the Firebase push messaging default behavior (show notifications only when in the background) I checked how Firebase did this by looking in the .class file com.google.firebase.messaging.zzb:53 (firebase-messaging:19.0.1) which appears to us getRunningAppProcesses. Mind you FireBase is created by Google them self. So I'm assuming it's pretty save to use. Cleaned up version:
List<ActivityManager.RunningAppProcessInfo> runningApps;
boolean isInForeground =false;
if ((runningApps = ((ActivityManager)this.getApplication().getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses()) != null) {
Iterator runningApp = runningApps.iterator();
int myPid = Process.myPid();
while(runningApp.hasNext()) {
ActivityManager.RunningAppProcessInfo processInfo;
if ((processInfo = (ActivityManager.RunningAppProcessInfo)runningApp.next()).pid == myPid) {
isInForeground = processInfo.importance == 100;
break;
}
}
}