Weird crashes "java.lang.ArithmeticException divide by zero" - android

Recently I found out from Firebase Crashlytics that some HTC/Fortuneship
devices crashes with the next exception and I can't understand why
val formattedViews: String
get() = String.format(Locale.getDefault(), "%,d %s", viewCount, Util.getString(R.string.views))
I don't get how such exception can happen in this code
Is there something wrong with those devices?

This is a bug in the JDK shipping with Android 7: https://bugs.openjdk.java.net/browse/JDK-8167567
You are passing Locale.getDefault() to format(), which is the same as not specifying a locale at all. The only known workaround appears to be to use a known-good locale to do the formatting (e.g. Locale.US), but of course this means you won't get locale-specific thousands groupings for users outside the US.
Or you could change your format specification to not use groupings (i.e. %d without the comma).
Perhaps you could catch the exception and fall back to US formatting for users who would otherwise crash? That's what these guys did: https://github.com/wordpress-mobile/WordPress-Android/pull/5604/files

Related

Android: Kotlin Collections.random() is repeating random character sequences between app sessions

I am looking to generate a random alphanumeric value to append to a String for an Android app. We are appending 10 digits of randomness using Kotlins kotlin.collections.random
We are seeing intermittent collisions on this randomness within fairly small data sets (<10), and they only seem to occur between app sessions (e.g you can keep generating them without issue with no app restart).
Our extension method for appending randomness:
private val values = ('A'..'Z') + ('0'..'9')
internal fun String.appendRandomCharacters(charactersToAdd: Int): String {
val randomness = (1..charactersToAdd).map { values.random() }.joinToString("")
return this + randomness
}
Running this in tests I can generate a million random strings without issue or collision.
However I can see that identical random strings are generated with high frequency if the app is killed and rerun.
I have done a fair bit of googling without much success so am hoping StackOverflow might be able to shed some light.
I had the same problem, but magically it worked with java.util.Random class and with Kotlin i kept getting the same repeated 'random' values.

Correct Value(s) to store in TAG_GPS_PROCESSING_METHOD

Thanks for reading this question. I am sure the experts on this site will be able to provide the help I need.
I am trying to write an app which allows users to edit the exif information of the photos on their Android Phone.
As a part of improved user experience, I want to apply data validation where ever possible.
For the Exif Tag - TAG_GPS_PROCESSING_METHOD I am not able to apply the validation correctly.
Here is the part of code that I have applied :
String strGPSProc = etGPSProc.getText().toString();
if(strGPSProc.equalsIgnoreCase("GPS") || strGPSProc.equalsIgnoreCase("CELLID") || strGPSProc.equalsIgnoreCase("WLAN") || strGPSProc.equalsIgnoreCase("MANUAL") ) {
returnValue = true;
}else {
returnValue=false;
showToast("Incorrect value for GPS Processing Method. Correct value options are GPS, CELLID, WLAN or MANUAL.");
etGPSProc.requestFocus();
}
This code checks if the value entered in the EditText meant for GPSProcessingMethod, has any one of the four prescribed value as described in the documentation of EXIF.
But when I try to save this using setAttribute() and saveAttributes() functions, a non catch-able exception appears in logcat.
Unsupported encoding for GPSProcessingMethod
I understand from Exif Documentation that values for GPSProcessingMethod needs to be stored with some header information.
I need some expert advise on how to implement this correctly, with out using any other 3rd part classes.
Accoridng to the Exif specification:
GPSProcessingMethod
A character string recording the name of the method used for location finding. The first byte indicates the character
code used (Table 6、Table 7), and this is followed by the name of the method. Since the Type is not ASCII, NULL
termination is not necessary
Atually, Table 6 lists the character codes as 8 byte sequences, so the above should probably read "The first bytes indicate...". Anyway, the character code designation for ASCII is defined as 41.H, 53.H, 43.H, 49.H, 49.H, 00.H, 00.H, 00.H., Unicode is (unsurprisingly) 55.H, 4E.H, 49.H, 43.H, 4F.H, 44.H, 45.H, 00.H. I guess these should be all you need.
Hope that helps.
EDIT:
Just discovered that ExifInterface.setAttribute() only supports String values... You could try encoding the value at the beginning of your string, but I doubt that would work. Sounds like the encoding should be handled by the setAttribute() or saveAttributes() method. Could it be a bug in the API? I had a look at the source code, but the actual writing of values is done by native code so I stopped digging further.

Why does TalkBack ignore currency values equal to zero?

Does anyone know why TalkBack reads the text "Balance: $0.00" as simply "Balance"? It seems to ignore values equivalent to zero. I would expect it to be read as "Balance: zero dollars."
view.announceForAccessibility("Total: $0.00"); // Reads "Total"
view.announceForAccessibility("Total: $0"); // Reads "Total"
view.announceForAccessibility("Total: 0"); // Reads "Total: Zero"
I filed a bug on the Android project, but was wondering if anyone here had some insight.
This is a bug in the text-to-speech engine, and should be fixed in later versions of the engine.
To work around it, you can either use a different TTS engine (ex. SVOX Classic) or specify the announcement as "Total: zero dollars" when you know the value is zero.

Android - How are you dealing with 9774d56d682e549c ? Android ID

So, I thought I was being clever and using various hashes and permutations of Android's secure unique ID to identify my users....
But it turns out that 9774d56d682e549c is a magic ID returned by
Secure.getString(getContentResolver(), Secure.ANDROID_ID);
for a good number of devices... It appears every emulator I build has the same ID, and many of other peoples phones (lots of moto droids!) and flashed OS mods tend to return this same repeating value. Non-MotoDroid / Non-Flashed handsets seem to all give me a unique string back. But this one is in my DB about 60 times!
I'm going to be optimizing my app to check for that string before registering, but what would be a recommended way of handling it to get another unique value?
My current thought is to check for it, generate an EXTREMELY LARGE random value, hash it, then store than in SharedPreferences and then either use the ANDROID_ID or the one stored in sharedprefs (if the users phone is giving the value). Anyone have any better ideas, or does this seem solid enough to mitigate this crazy bug?
Take a look at the Identifying app installations article. You can't rely on ANDROID_ID.
The best solution is to generate a custom id with:
String id = UUID.randomUUID().toString();
If you want to create one with the same format as real ANDROID_IDs, you can use the same method they use here:
private static String generateAndroidId() {
String generated = null;
try {
final SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
random.setSeed( (System.nanoTime() + new SecureRandom().nextLong()).getBytes() );
generated = Long.toHexString(random.nextLong());
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "Unexpected exception", e);
}
return generated;
}
Outputs: 9e7859438099538e
Though not ideal, things like the Google AdMob SDK use the permission android.permission.READ_PHONE_STATE to read the device's phone number, etc.
There's some useful, related information in the following blog post: http://strazzere.com/blog/?p=116
This phenomenon and also this Stackoverflow thread were talked about at the summercon 2012 by Oberheide and Miller, who recently dissected Google's Bouncer: http://jon.oberheide.org/files/summercon12-bouncer.pdf
Maybe you can extract some more useful info for your project.

how to get the localized label of the phone types?

When changing the custom locale the label of the phone types change to the appropriate language. Does anybody know how to get the localized label of the phone types?
I pick a contact in my app to get its phone number and if there is more then one number I use an AlertDialog to let the user select the currect one. In this pick list, I want to show the label of the type, so it's easier for the user to select. Since they labels are somewhere in the Android system, it must be possible to get the localized label. Unfortunately, the Phone.LABEL is null when reading the phone number.
I know this is a bit old, but this:
Phone.getTypeLabel(this.getResources(), cursor.getInt(typeIdx), "");
worked for me
Yes, you can get localized phone type string with the code:
int phoneNumberType = (int)pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), phoneNumberType , "")
but for custom phone types you should cosider phone label, not only phone type:
String phoneLabel = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
Inferno's answer is a valid answer, and I was happy to find this answer because it was similar to what I was looking for. However, if you're dealing with phones installed with API Level 5 (Android 2.0) or newer, there is one small problem with this: android.R.array.phoneTypes only returns the list of phone types that were present prior to when ContactsContract class replaced the Contacts interface as of API Level 5. I verified the labels listed when creating a new contact on emulators running these Android versions (API Levels): 1.6 (4), 2.1-update 1 (7), and 2.2 (8).
When printed out, android.R.array.phoneTypes contains these valid phone types:
Home, Mobile, Work, Work Fax, Home Fax, Pager, Other, Custom
These are the valid phone types, present for phones with Android 2.0+ installed, that are missing from that same Array:
Callback, Car, Company Main, ISDN, Main, Other Fax, Radio, Telex, TTY TDD, Work Mobile, Work Pager, Assistant, MMS
Unfortunately, I have not been able to find something like android.R.array.phoneTypes that'll list all of these valid phone types for phones Android 2.0+. Has anyone come across such yet?
References
android.R.array.phoneTypes defined: http://developer.android.com/reference/android/R.array.html#phoneTypes
Note: I'm posting my other two reference links in separate answers, as I can't seem to post more than one hyperlink per post at this time.
i am using this piece of code
public void getPhoneType(){
int res;
for(int i=0;i<=20;i++){
res = ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(i);
Log.d(TAG,"i: "+ i +" type: " + context.getString(res));
}
}
didn't found any place to get the actual count of valid types but http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html#getTypeLabelResource%28int%29 says it will always give a valid res so, you can iterate until it start giving repeated values... to me after 20 gives me the custom res.

Categories

Resources