Add getDefaultLocale utils method

This commit is contained in:
AsamK 2021-11-10 21:58:39 +01:00
parent 62dd2ee49e
commit d3490facc8
3 changed files with 21 additions and 17 deletions

View file

@ -26,6 +26,7 @@ import org.asamk.signal.manager.helper.PinHelper;
import org.asamk.signal.manager.storage.SignalAccount; import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.identities.TrustNewIdentity; import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
import org.asamk.signal.manager.util.KeyUtils; import org.asamk.signal.manager.util.KeyUtils;
import org.asamk.signal.manager.util.Utils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.libsignal.util.KeyHelper; import org.whispersystems.libsignal.util.KeyHelper;
@ -48,7 +49,6 @@ import org.whispersystems.signalservice.internal.util.DynamicCredentialsProvider
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Locale;
public class RegistrationManager implements Closeable { public class RegistrationManager implements Closeable {
@ -123,7 +123,7 @@ public class RegistrationManager implements Closeable {
public void register(boolean voiceVerification, String captcha) throws IOException, CaptchaRequiredException { public void register(boolean voiceVerification, String captcha) throws IOException, CaptchaRequiredException {
final ServiceResponse<RequestVerificationCodeResponse> response; final ServiceResponse<RequestVerificationCodeResponse> response;
if (voiceVerification) { if (voiceVerification) {
response = accountManager.requestVoiceVerificationCode(getDefaultLocale(), response = accountManager.requestVoiceVerificationCode(Utils.getDefaultLocale(),
Optional.fromNullable(captcha), Optional.fromNullable(captcha),
Optional.absent(), Optional.absent(),
Optional.absent()); Optional.absent());
@ -140,18 +140,6 @@ public class RegistrationManager implements Closeable {
} }
} }
private Locale getDefaultLocale() {
final var locale = Locale.getDefault();
try {
Locale.LanguageRange.parse(locale.getLanguage() + "-" + locale.getCountry());
} catch (IllegalArgumentException e) {
logger.debug("Invalid locale, ignoring: {}", locale);
return null;
}
return locale;
}
public Manager verifyAccount( public Manager verifyAccount(
String verificationCode, String pin String verificationCode, String pin
) throws IOException, PinLockedException, IncorrectPinException { ) throws IOException, PinLockedException, IncorrectPinException {

View file

@ -33,7 +33,6 @@ import java.util.Base64;
import java.util.Date; import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
@ -229,7 +228,7 @@ public final class ProfileHelper {
} }
private SignalServiceProfile retrieveProfileSync(String username) throws IOException { private SignalServiceProfile retrieveProfileSync(String username) throws IOException {
final var locale = Locale.getDefault(); final var locale = Utils.getDefaultLocale();
return dependencies.getMessageReceiver().retrieveProfileByUsername(username, Optional.absent(), locale); return dependencies.getMessageReceiver().retrieveProfileByUsername(username, Optional.absent(), locale);
} }
@ -310,7 +309,7 @@ public final class ProfileHelper {
var profileService = dependencies.getProfileService(); var profileService = dependencies.getProfileService();
Single<ServiceResponse<ProfileAndCredential>> responseSingle; Single<ServiceResponse<ProfileAndCredential>> responseSingle;
final var locale = Locale.getDefault(); final var locale = Utils.getDefaultLocale();
try { try {
responseSingle = profileService.getProfile(address, profileKey, unidentifiedAccess, requestType, locale); responseSingle = profileService.getProfile(address, profileKey, unidentifiedAccess, requestType, locale);
} catch (NoClassDefFoundError e) { } catch (NoClassDefFoundError e) {

View file

@ -1,5 +1,7 @@
package org.asamk.signal.manager.util; package org.asamk.signal.manager.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.libsignal.IdentityKey; import org.whispersystems.libsignal.IdentityKey;
import org.whispersystems.libsignal.fingerprint.Fingerprint; import org.whispersystems.libsignal.fingerprint.Fingerprint;
import org.whispersystems.libsignal.fingerprint.NumericFingerprintGenerator; import org.whispersystems.libsignal.fingerprint.NumericFingerprintGenerator;
@ -13,9 +15,12 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URLConnection; import java.net.URLConnection;
import java.nio.file.Files; import java.nio.file.Files;
import java.util.Locale;
public class Utils { public class Utils {
private final static Logger logger = LoggerFactory.getLogger(Utils.class);
public static String getFileMimeType(File file, String defaultMimeType) throws IOException { public static String getFileMimeType(File file, String defaultMimeType) throws IOException {
var mime = Files.probeContentType(file.toPath()); var mime = Files.probeContentType(file.toPath());
if (mime == null) { if (mime == null) {
@ -68,4 +73,16 @@ public class Utils {
theirId, theirId,
theirIdentityKey); theirIdentityKey);
} }
public static Locale getDefaultLocale() {
final var locale = Locale.getDefault();
try {
Locale.LanguageRange.parse(locale.getLanguage() + "-" + locale.getCountry());
} catch (IllegalArgumentException e) {
logger.debug("Invalid locale, ignoring: {}", locale);
return null;
}
return locale;
}
} }