Completely removed CommaSeparatedList class, replaced with two helper methods in Utils.

The two helper methods alleviate the need for copious null checks. They also provide
consistent behaviour when there are zero elements (i.e. they return null, rather than
an empty string or empty array, as was the case before).
This commit is contained in:
Peter Serwylo 2016-06-21 00:17:11 +10:00
parent d99b357a1f
commit a192389318
10 changed files with 42 additions and 67 deletions

View File

@ -27,7 +27,7 @@ public class AppFilter {
public boolean filter(App app) {
if (app.requirements != null && !Preferences.get().filterAppsRequiringRoot()) {
for (String requirement : app.requirements) {
if (requirement.equals("root")) {
if ("root".equals(requirement)) {
return true;
}
}

View File

@ -7,6 +7,7 @@ import android.content.pm.PackageManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import org.fdroid.fdroid.compat.SupportedArchitectures;
import org.fdroid.fdroid.data.Apk;
@ -114,7 +115,7 @@ public class CompatibilityChecker {
if (!compatibleApi(apk.nativecode)) {
Collections.addAll(incompatibleReasons, apk.nativecode);
Utils.debugLog(TAG, apk.packageName + " vercode " + apk.versionCode
+ " only supports " + Utils.CommaSeparatedList.str(apk.nativecode)
+ " only supports " + TextUtils.join(", ", apk.nativecode)
+ " while your architectures are " + cpuAbisDesc);
}

View File

@ -142,13 +142,13 @@ public class RepoXMLHandler extends DefaultHandler {
curapk.added = Utils.parseDate(str, null);
break;
case "permissions":
curapk.permissions = Utils.CommaSeparatedList.make(str);
curapk.permissions = Utils.parseCommaSeparatedString(str);
break;
case "features":
curapk.features = Utils.CommaSeparatedList.make(str);
curapk.features = Utils.parseCommaSeparatedString(str);
break;
case "nativecode":
curapk.nativecode = Utils.CommaSeparatedList.make(str);
curapk.nativecode = Utils.parseCommaSeparatedString(str);
break;
}
} else if (curapp != null) {
@ -218,13 +218,13 @@ public class RepoXMLHandler extends DefaultHandler {
curapp.upstreamVersionCode = Utils.parseInt(str, -1);
break;
case "categories":
curapp.categories = Utils.CommaSeparatedList.make(str);
curapp.categories = Utils.parseCommaSeparatedString(str);
break;
case "antifeatures":
curapp.antiFeatures = Utils.CommaSeparatedList.make(str);
curapp.antiFeatures = Utils.parseCommaSeparatedString(str);
break;
case "requirements":
curapp.requirements = Utils.CommaSeparatedList.make(str);
curapp.requirements = Utils.parseCommaSeparatedString(str);
break;
}
} else if ("description".equals(localName)) {

View File

@ -57,12 +57,8 @@ import java.security.cert.CertificateEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Formatter;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
public final class Utils {
@ -403,36 +399,6 @@ public final class Utils {
return new Locale(languageTag);
}
public static final class CommaSeparatedList {
@Nullable
public static String[] make(List<String> list) {
if (list == null || list.isEmpty()) {
return null;
}
return list.toArray(new String[list.size()]);
}
@Nullable
public static String[] make(String[] list) {
if (list == null || list.length == 0) {
return null;
}
return list;
}
@Nullable
public static String[] make(@Nullable String list) {
if (TextUtils.isEmpty(list)) {
return null;
}
return list.split(",");
}
public static String str(@Nullable String[] values) {
return values == null ? null : TextUtils.join(",", values);
}
}
public static DisplayImageOptions.Builder getImageLoadingOptions() {
return new DisplayImageOptions.Builder()
.cacheInMemory(true)
@ -510,6 +476,16 @@ public final class Utils {
return result;
}
@Nullable
public static String[] parseCommaSeparatedString(String values) {
return values == null || values.length() == 0 ? null : values.split(",");
}
@Nullable
public static String serializeCommaSeparatedString(@Nullable String[] values) {
return values == null || values.length == 0 ? null : TextUtils.join(",", values);
}
private static Date parseDateFormat(DateFormat format, String str, Date fallback) {
if (str == null || str.length() == 0) {
return fallback;

View File

@ -83,7 +83,7 @@ public class Apk extends ValueObject implements Comparable<Apk> {
added = Utils.parseDate(cursor.getString(i), null);
break;
case ApkProvider.DataColumns.FEATURES:
features = Utils.CommaSeparatedList.make(cursor.getString(i));
features = Utils.parseCommaSeparatedString(cursor.getString(i));
break;
case ApkProvider.DataColumns.PACKAGE_NAME:
packageName = cursor.getString(i);
@ -104,13 +104,13 @@ public class Apk extends ValueObject implements Comparable<Apk> {
apkName = cursor.getString(i);
break;
case ApkProvider.DataColumns.PERMISSIONS:
permissions = Utils.CommaSeparatedList.make(cursor.getString(i));
permissions = Utils.parseCommaSeparatedString(cursor.getString(i));
break;
case ApkProvider.DataColumns.NATIVE_CODE:
nativecode = Utils.CommaSeparatedList.make(cursor.getString(i));
nativecode = Utils.parseCommaSeparatedString(cursor.getString(i));
break;
case ApkProvider.DataColumns.INCOMPATIBLE_REASONS:
incompatibleReasons = Utils.CommaSeparatedList.make(cursor.getString(i));
incompatibleReasons = Utils.parseCommaSeparatedString(cursor.getString(i));
break;
case ApkProvider.DataColumns.REPO_ID:
repo = cursor.getInt(i);
@ -205,10 +205,10 @@ public class Apk extends ValueObject implements Comparable<Apk> {
values.put(ApkProvider.DataColumns.TARGET_SDK_VERSION, targetSdkVersion);
values.put(ApkProvider.DataColumns.MAX_SDK_VERSION, maxSdkVersion);
values.put(ApkProvider.DataColumns.ADDED_DATE, Utils.formatDate(added, ""));
values.put(ApkProvider.DataColumns.PERMISSIONS, Utils.CommaSeparatedList.str(permissions));
values.put(ApkProvider.DataColumns.FEATURES, Utils.CommaSeparatedList.str(features));
values.put(ApkProvider.DataColumns.NATIVE_CODE, Utils.CommaSeparatedList.str(nativecode));
values.put(ApkProvider.DataColumns.INCOMPATIBLE_REASONS, Utils.CommaSeparatedList.str(incompatibleReasons));
values.put(ApkProvider.DataColumns.PERMISSIONS, Utils.serializeCommaSeparatedString(permissions));
values.put(ApkProvider.DataColumns.FEATURES, Utils.serializeCommaSeparatedString(features));
values.put(ApkProvider.DataColumns.NATIVE_CODE, Utils.serializeCommaSeparatedString(nativecode));
values.put(ApkProvider.DataColumns.INCOMPATIBLE_REASONS, Utils.serializeCommaSeparatedString(incompatibleReasons));
values.put(ApkProvider.DataColumns.IS_COMPATIBLE, compatible ? 1 : 0);
return values;
}

View File

@ -221,13 +221,13 @@ public class App extends ValueObject implements Comparable<App> {
lastUpdated = Utils.parseDate(cursor.getString(i), null);
break;
case AppProvider.DataColumns.CATEGORIES:
categories = Utils.CommaSeparatedList.make(cursor.getString(i));
categories = Utils.parseCommaSeparatedString(cursor.getString(i));
break;
case AppProvider.DataColumns.ANTI_FEATURES:
antiFeatures = Utils.CommaSeparatedList.make(cursor.getString(i));
antiFeatures = Utils.parseCommaSeparatedString(cursor.getString(i));
break;
case AppProvider.DataColumns.REQUIREMENTS:
requirements = Utils.CommaSeparatedList.make(cursor.getString(i));
requirements = Utils.parseCommaSeparatedString(cursor.getString(i));
break;
case AppProvider.DataColumns.IGNORE_ALLUPDATES:
ignoreAllUpdates = cursor.getInt(i) == 1;
@ -334,7 +334,7 @@ public class App extends ValueObject implements Comparable<App> {
apk.targetSdkVersion = minTargetMax[1];
apk.maxSdkVersion = minTargetMax[2];
apk.packageName = this.packageName;
apk.permissions = Utils.CommaSeparatedList.make(packageInfo.requestedPermissions);
apk.permissions = packageInfo.requestedPermissions;
apk.apkName = apk.packageName + "_" + apk.versionCode + ".apk";
apk.installedFile = apkFile;
@ -348,15 +348,14 @@ public class App extends ValueObject implements Comparable<App> {
abis.add(matcher.group(1));
}
}
apk.nativecode = Utils.CommaSeparatedList.make(abis.toArray(new String[abis.size()]));
apk.nativecode = abis.toArray(new String[abis.size()]);
final FeatureInfo[] features = packageInfo.reqFeatures;
if (features != null && features.length > 0) {
final String[] featureNames = new String[features.length];
apk.features = new String[features.length];
for (int i = 0; i < features.length; i++) {
featureNames[i] = features[i].name;
apk.features[i] = features[i].name;
}
apk.features = Utils.CommaSeparatedList.make(featureNames);
}
final JarEntry aSignedEntry = (JarEntry) apkJar.getEntry("AndroidManifest.xml");
@ -462,9 +461,9 @@ public class App extends ValueObject implements Comparable<App> {
values.put(AppProvider.DataColumns.SUGGESTED_VERSION_CODE, suggestedVersionCode);
values.put(AppProvider.DataColumns.UPSTREAM_VERSION_NAME, upstreamVersionName);
values.put(AppProvider.DataColumns.UPSTREAM_VERSION_CODE, upstreamVersionCode);
values.put(AppProvider.DataColumns.CATEGORIES, Utils.CommaSeparatedList.str(categories));
values.put(AppProvider.DataColumns.ANTI_FEATURES, Utils.CommaSeparatedList.str(antiFeatures));
values.put(AppProvider.DataColumns.REQUIREMENTS, Utils.CommaSeparatedList.str(requirements));
values.put(AppProvider.DataColumns.CATEGORIES, Utils.serializeCommaSeparatedString(categories));
values.put(AppProvider.DataColumns.ANTI_FEATURES, Utils.serializeCommaSeparatedString(antiFeatures));
values.put(AppProvider.DataColumns.REQUIREMENTS, Utils.serializeCommaSeparatedString(requirements));
values.put(AppProvider.DataColumns.IS_COMPATIBLE, compatible ? 1 : 0);
values.put(AppProvider.DataColumns.IGNORE_ALLUPDATES, ignoreAllUpdates ? 1 : 0);
values.put(AppProvider.DataColumns.IGNORE_THISUPDATE, ignoreThisUpdate);

View File

@ -98,7 +98,7 @@ public class AppProvider extends FDroidProvider {
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
final String categoriesString = cursor.getString(0);
String[] categoriesList = Utils.CommaSeparatedList.make(categoriesString);
String[] categoriesList = Utils.parseCommaSeparatedString(categoriesString);
if (categoriesList != null) {
Collections.addAll(categorySet, categoriesList);
}

View File

@ -284,7 +284,7 @@ public class RepoPersister {
final List<String> reasons = checker.getIncompatibleReasons(apk);
if (reasons.size() > 0) {
apk.compatible = false;
apk.incompatibleReasons = Utils.CommaSeparatedList.make(reasons);
apk.incompatibleReasons = reasons.toArray(new String[reasons.size()]);
} else {
apk.compatible = true;
apk.incompatibleReasons = null;

View File

@ -454,7 +454,7 @@ public final class LocalRepoManager {
private void tagFeatures(App app) throws IOException {
serializer.startTag("", "features");
if (app.installedApk.features != null) {
serializer.text(Utils.CommaSeparatedList.str(app.installedApk.features));
serializer.text(TextUtils.join(",", app.installedApk.features));
}
serializer.endTag("", "features");
}
@ -462,7 +462,7 @@ public final class LocalRepoManager {
private void tagNativecode(App app) throws IOException {
if (app.installedApk.nativecode != null) {
serializer.startTag("", "nativecode");
serializer.text(Utils.CommaSeparatedList.str(app.installedApk.nativecode));
serializer.text(TextUtils.join(",", app.installedApk.nativecode));
serializer.endTag("", "nativecode");
}
}

View File

@ -7,7 +7,6 @@ import android.net.Uri;
import org.fdroid.fdroid.Assert;
import org.fdroid.fdroid.BuildConfig;
import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.mock.MockApk;
import org.fdroid.fdroid.mock.MockApp;
import org.fdroid.fdroid.mock.MockRepo;
@ -410,7 +409,7 @@ public class ApkProviderTest extends FDroidProviderTest {
assertNull(apk.added);
assertNull(apk.hashType);
apk.features = Utils.CommaSeparatedList.make("one,two,three");
apk.features = new String[] {"one", "two", "three" };
long dateTimestamp = System.currentTimeMillis();
apk.added = new Date(dateTimestamp);
apk.hashType = "i'm a hash type";