App.isLocalized() marks whether relevant translations are available

!886
This commit is contained in:
Hans-Christoph Steiner 2020-07-14 19:28:46 +02:00
parent 2021f43761
commit e0cb3f8afc
No known key found for this signature in database
GPG Key ID: 3E177817BA1B9BFA
3 changed files with 77 additions and 7 deletions

View File

@ -1,5 +1,7 @@
### 1.10-alpha0 (2020-07-20)
* Latest Tab will show better results on non-English devices
* updates to core libraries (Jackson, androidx, gradle, etc)
* use Gradle's new dependency verification

View File

@ -15,15 +15,13 @@ import android.os.Environment;
import android.os.LocaleList;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.io.filefilter.RegexFileFilter;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Preferences;
@ -101,7 +99,7 @@ public class App extends ValueObject implements Comparable<App>, Parcelable {
@JsonIgnore
public boolean isApk;
@JsonIgnore
private boolean isLocalized = false;
boolean isLocalized = false;
/**
* This is primarily for the purpose of saving app metadata when parsing an index.xml file.
@ -517,7 +515,7 @@ public class App extends ValueObject implements Comparable<App>, Parcelable {
* on other country-specific locales, rather than English.
*/
@JsonProperty("localized")
private void setLocalized(Map<String, Map<String, Object>> localized) { // NOPMD
void setLocalized(Map<String, Map<String, Object>> localized) { // NOPMD
Locale defaultLocale = Locale.getDefault();
String languageTag = defaultLocale.getLanguage();
String countryTag = defaultLocale.getCountry();
@ -571,7 +569,13 @@ public class App extends ValueObject implements Comparable<App>, Parcelable {
}
}
if (localesToUse.size() >= 1) {
for (String l : localesToUse) {
if (l.startsWith(languageTag)) {
isLocalized = true;
break;
}
}
if (localesToUse.size() > 1) {
isLocalized = true;
}

View File

@ -9,6 +9,7 @@ import android.net.Uri;
import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.TestUtils;
import org.fdroid.fdroid.data.Schema.AppMetadataTable.Cols;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
@ -17,7 +18,10 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import static org.fdroid.fdroid.Assert.assertContainsOnly;
@ -36,6 +40,8 @@ public class AppProviderTest extends FDroidProviderTest {
private static final String[] PROJ = Cols.ALL;
private static Locale defaultLocale;
@BeforeClass
public static void setRandomTimeZone() {
TimeZone.setDefault(TimeZone.getTimeZone(String.format("GMT-%d:%02d",
@ -45,10 +51,16 @@ public class AppProviderTest extends FDroidProviderTest {
@Before
public void setup() {
defaultLocale = Locale.getDefault();
TestUtils.registerContentProvider(AppProvider.getAuthority(), AppProvider.class);
Preferences.setupForTests(context);
}
@After
public void teardown() {
Locale.setDefault(defaultLocale);
}
/**
* Although this doesn't directly relate to the {@link AppProvider}, it is here because
* the {@link AppProvider} used to stumble across this bug when asking for installed apps,
@ -268,6 +280,58 @@ public class AppProviderTest extends FDroidProviderTest {
assertEquals("F-Droid", otherApp.name);
}
@Test
public void testAppSetLocalized() {
final String enSummary = "utility for getting information about the APKs that are installed on your device";
HashMap<String, Object> en = new HashMap<>();
en.put("summary", enSummary);
final String esSummary = "utilidad para obtener información sobre los APKs instalados en su dispositivo";
HashMap<String, Object> es = new HashMap<>();
es.put("summary", esSummary);
final String frSummary = "utilitaire pour obtenir des informations sur les APKs qui sont installés sur vot";
HashMap<String, Object> fr = new HashMap<>();
fr.put("summary", frSummary);
final String nlSummary = "hulpprogramma voor het verkrijgen van informatie over de APK die zijn geïnstalle";
HashMap<String, Object> nl = new HashMap<>();
nl.put("summary", nlSummary);
App app = new App();
Map<String, Map<String, Object>> localized = new HashMap<>();
localized.put("es", es);
localized.put("fr", fr);
Locale.setDefault(new Locale("nl", "NL"));
app.setLocalized(localized);
assertFalse(app.isLocalized);
localized.put("nl", nl);
app.setLocalized(localized);
assertTrue(app.isLocalized);
assertEquals(nlSummary, app.summary);
app = new App();
localized.clear();
localized.put("nl", nl);
app.setLocalized(localized);
assertTrue(app.isLocalized);
app = new App();
localized.clear();
localized.put("en-US", en);
app.setLocalized(localized);
assertFalse(app.isLocalized);
Locale.setDefault(new Locale("en", "US"));
app = new App();
localized.clear();
localized.put("en-US", en);
app.setLocalized(localized);
assertTrue(app.isLocalized);
}
@Test
public void testInsertTrimsNamesAndSummary() {
// Insert a new record with unwanted newlines...