Some more small code refactors

This commit is contained in:
Daniel Martí 2015-04-07 12:08:22 +02:00
parent 0f18a0979d
commit c54c905d2e
8 changed files with 53 additions and 59 deletions

View File

@ -79,21 +79,22 @@ public final class Utils {
public static String getIconsDir(Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
String iconsDir;
if (metrics.densityDpi >= 640) {
iconsDir = "/icons-640/";
} else if (metrics.densityDpi >= 480) {
iconsDir = "/icons-480/";
} else if (metrics.densityDpi >= 320) {
iconsDir = "/icons-320/";
} else if (metrics.densityDpi >= 240) {
iconsDir = "/icons-240/";
} else if (metrics.densityDpi >= 160) {
iconsDir = "/icons-160/";
} else {
iconsDir = "/icons-120/";
return "/icons-640/";
}
return iconsDir;
if (metrics.densityDpi >= 480) {
return "/icons-480/";
}
if (metrics.densityDpi >= 320) {
return "/icons-320/";
}
if (metrics.densityDpi >= 240) {
return "/icons-240/";
}
if (metrics.densityDpi >= 160) {
return "/icons-160/";
}
return "/icons-120/";
}
public static void copy(InputStream input, OutputStream output)
@ -224,12 +225,10 @@ public final class Utils {
XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");
int eventType = xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xml.getName().equals("uses-sdk")) {
for (int j = 0; j < xml.getAttributeCount(); j++) {
if (xml.getAttributeName(j).equals("minSdkVersion")) {
return Integer.parseInt(xml.getAttributeValue(j));
}
if (eventType == XmlPullParser.START_TAG && xml.getName().equals("uses-sdk")) {
for (int j = 0; j < xml.getAttributeCount(); j++) {
if (xml.getAttributeName(j).equals("minSdkVersion")) {
return Integer.parseInt(xml.getAttributeValue(j));
}
}
}
@ -254,9 +253,9 @@ public final class Utils {
for (char c : buffer) {
if (c == substring.charAt(currentSubstringIndex)) {
currentSubstringIndex ++;
currentSubstringIndex++;
if (currentSubstringIndex == substring.length()) {
count ++;
count++;
currentSubstringIndex = 0;
}
} else {

View File

@ -1,16 +1,15 @@
package org.fdroid.fdroid.compat;
import android.annotation.TargetApi;
import android.os.Build;
import android.widget.ArrayAdapter;
import java.util.List;
public class ArrayAdapterCompat {
public class ArrayAdapterCompat extends Compatibility {
@TargetApi(11)
public static <T> void addAll(ArrayAdapter<T> adapter, List<T> list) {
if (Build.VERSION.SDK_INT >= 11) {
if (hasApi(11)) {
adapter.addAll(list);
} else {
for (T category : list) {

View File

@ -5,16 +5,16 @@ import android.os.Build;
public abstract class Compatibility {
// like minSdkVersion
protected static boolean hasApi(int apiLevel) {
protected static final boolean hasApi(int apiLevel) {
return getApi() >= apiLevel;
}
// like maxSdkVersion
protected static boolean upToApi(int apiLevel) {
protected static final boolean upToApi(int apiLevel) {
return (apiLevel < 1 || getApi() <= apiLevel);
}
protected static int getApi() {
protected static final int getApi() {
return Build.VERSION.SDK_INT;
}

View File

@ -11,15 +11,15 @@ import org.fdroid.fdroid.data.SanitizedFile;
import java.io.IOException;
import java.lang.reflect.Method;
public class FileCompat {
public class FileCompat extends Compatibility {
private static final String TAG = "fdroid.FileCompat";
public static boolean symlink(SanitizedFile source, SanitizedFile dest) {
if (Compatibility.hasApi(21)) {
if (hasApi(21)) {
symlinkOs(source, dest);
} else if (Compatibility.hasApi(15)) {
} else if (hasApi(15)) {
symlinkLibcore(source, dest);
} else {
symlinkRuntime(source, dest);
@ -82,17 +82,16 @@ public class FileCompat {
@TargetApi(9)
public static boolean setReadable(SanitizedFile file, boolean readable, boolean ownerOnly) {
if (Compatibility.hasApi(9)) {
if (hasApi(9)) {
return file.setReadable(readable, ownerOnly);
} else {
String mode;
if (readable) {
mode = ownerOnly ? "0600" : "0644";
} else {
mode = "0000";
}
return setMode(file, mode);
}
String mode;
if (readable) {
mode = ownerOnly ? "0600" : "0644";
} else {
mode = "0000";
}
return setMode(file, mode);
}
@ -121,17 +120,16 @@ public class FileCompat {
@TargetApi(9)
public static boolean setExecutable(SanitizedFile file, boolean readable, boolean ownerOnly) {
if (Compatibility.hasApi(9)) {
if (hasApi(9)) {
return file.setExecutable(readable, ownerOnly);
} else {
String mode;
if (readable) {
mode = ownerOnly ? "0700" : "0711";
} else {
mode = ownerOnly ? "0600" : "0600";
}
return setMode(file, mode);
}
String mode;
if (readable) {
mode = ownerOnly ? "0700" : "0711";
} else {
mode = ownerOnly ? "0600" : "0600";
}
return setMode(file, mode);
}

View File

@ -7,9 +7,8 @@ public abstract class LayoutCompat extends Compatibility {
public static LayoutCompat create() {
if (hasApi(17)) {
return new JellyBeanMr1LayoutCompatImpl();
} else {
return new OldLayoutCompatImpl();
}
return new OldLayoutCompatImpl();
}
private static final LayoutCompat impl = LayoutCompat.create();

View File

@ -36,7 +36,7 @@ import java.security.Security;
* Cryptography Architecture primitives. A good place to invoke them is in the
* application's {@code onCreate}.
*/
public final class PRNGFixes {
public final class PRNGFixes extends Compatibility {
private static final int VERSION_CODE_JELLY_BEAN = 16;
private static final int VERSION_CODE_JELLY_BEAN_MR2 = 18;
@ -63,8 +63,7 @@ public final class PRNGFixes {
* @throws SecurityException if the fix is needed but could not be applied.
*/
private static void applyOpenSSLFix() throws SecurityException {
if ((Build.VERSION.SDK_INT < VERSION_CODE_JELLY_BEAN)
|| (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2)) {
if (getApi() < VERSION_CODE_JELLY_BEAN || getApi() > VERSION_CODE_JELLY_BEAN_MR2) {
// No need to apply the fix
return;
}
@ -99,7 +98,7 @@ public final class PRNGFixes {
*/
private static void installLinuxPRNGSecureRandom()
throws SecurityException {
if (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2) {
if (getApi() > VERSION_CODE_JELLY_BEAN_MR2) {
// No need to apply the fix
return;
}

View File

@ -6,25 +6,25 @@ import android.os.Build;
public class SupportedArchitectures extends Compatibility {
@SuppressWarnings("deprecation")
private static String[] getAbisDonut() {
private static final String[] getAbisDonut() {
return new String[]{Build.CPU_ABI};
}
@SuppressWarnings("deprecation")
@TargetApi(8)
private static String[] getAbisFroyo() {
private static final String[] getAbisFroyo() {
return new String[]{Build.CPU_ABI, Build.CPU_ABI2};
}
@TargetApi(21)
private static String[] getAbisLollipop() {
private static final String[] getAbisLollipop() {
return Build.SUPPORTED_ABIS;
}
/**
* The most preferred ABI is the first element in the list.
*/
public static String[] getAbis() {
public static final String[] getAbis() {
if (hasApi(21)) {
return getAbisLollipop();
}

View File

@ -929,7 +929,7 @@ public class AppProvider extends FDroidProvider {
private void updateIconUrls() {
Log.d(TAG, "Updating icon paths for apps belonging to repos with version >= " + Repo.VERSION_DENSITY_SPECIFIC_ICONS);
String iconsDir = Utils.getIconsDir(getContext());
final String iconsDir = Utils.getIconsDir(getContext());
Log.d(TAG, "Using icon dir '"+iconsDir+"'");
String repoVersion = Integer.toString(Repo.VERSION_DENSITY_SPECIFIC_ICONS);
String query = getIconUpdateQuery();