Skip to content

Commit 339bda9

Browse files
author
Mohammad Rezaei
committed
fix: Add a fixture for parsing date in rtl languages
1 parent b2b65bc commit 339bda9

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

pflockscreen/src/main/java/com/beautycoder/pflockscreen/security/PFSecurityUtilsOld.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.beautycoder.pflockscreen.security;
22

33
import android.content.Context;
4+
import android.content.res.Configuration;
5+
import android.content.res.Resources;
46
import android.security.KeyPairGeneratorSpec;
57
import android.security.keystore.KeyProperties;
68
import androidx.annotation.NonNull;
@@ -19,6 +21,7 @@
1921
import java.security.cert.CertificateException;
2022
import java.util.ArrayList;
2123
import java.util.Calendar;
24+
import java.util.Locale;
2225

2326
import javax.crypto.Cipher;
2427
import javax.crypto.CipherInputStream;
@@ -172,6 +175,12 @@ private boolean generateKeyOld(
172175
boolean isAuthenticationRequired
173176
) {
174177
try {
178+
// Set English locale as default (workaround for rtl parsing date exception)
179+
// From https://stackoverflow.com/a/46602170
180+
// FIXME: A temporary fixture for issue described at https://issuetracker.google.com/issues/37095309
181+
Locale initialLocale = Locale.getDefault();
182+
setLocale(context, Locale.ENGLISH);
183+
175184
final Calendar start = Calendar.getInstance();
176185
final Calendar end = Calendar.getInstance();
177186
end.add(Calendar.YEAR, 25);
@@ -193,6 +202,8 @@ private boolean generateKeyOld(
193202

194203
keyGen.initialize(spec);
195204
keyGen.generateKeyPair();
205+
// Reset default locale
206+
setLocale(context, initialLocale);
196207
return true;
197208

198209
} catch ( NoSuchAlgorithmException
@@ -203,6 +214,14 @@ private boolean generateKeyOld(
203214
}
204215
}
205216

217+
private void setLocale(Context context, Locale locale) {
218+
Locale.setDefault(locale);
219+
Resources resources = context.getResources();
220+
Configuration config = resources.getConfiguration();
221+
config.locale = locale;
222+
resources.updateConfiguration(config, resources.getDisplayMetrics());
223+
}
224+
206225
@Override
207226
public boolean isKeystoreContainAlias(String alias) throws PFSecurityException {
208227
final KeyStore keyStore = loadKeyStore();

0 commit comments

Comments
 (0)