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) { public static String getIconsDir(Context context) {
DisplayMetrics metrics = context.getResources().getDisplayMetrics(); DisplayMetrics metrics = context.getResources().getDisplayMetrics();
String iconsDir;
if (metrics.densityDpi >= 640) { if (metrics.densityDpi >= 640) {
iconsDir = "/icons-640/"; return "/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 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) public static void copy(InputStream input, OutputStream output)
@ -224,15 +225,13 @@ public final class Utils {
XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml"); XmlResourceParser xml = am.openXmlResourceParser("AndroidManifest.xml");
int eventType = xml.getEventType(); int eventType = xml.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) { while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) { if (eventType == XmlPullParser.START_TAG && xml.getName().equals("uses-sdk")) {
if (xml.getName().equals("uses-sdk")) {
for (int j = 0; j < xml.getAttributeCount(); j++) { for (int j = 0; j < xml.getAttributeCount(); j++) {
if (xml.getAttributeName(j).equals("minSdkVersion")) { if (xml.getAttributeName(j).equals("minSdkVersion")) {
return Integer.parseInt(xml.getAttributeValue(j)); return Integer.parseInt(xml.getAttributeValue(j));
} }
} }
} }
}
eventType = xml.nextToken(); eventType = xml.nextToken();
} }
} catch (PackageManager.NameNotFoundException | IOException | XmlPullParserException e) { } catch (PackageManager.NameNotFoundException | IOException | XmlPullParserException e) {

View File

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

View File

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

View File

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

View File

@ -7,9 +7,8 @@ public abstract class LayoutCompat extends Compatibility {
public static LayoutCompat create() { public static LayoutCompat create() {
if (hasApi(17)) { if (hasApi(17)) {
return new JellyBeanMr1LayoutCompatImpl(); return new JellyBeanMr1LayoutCompatImpl();
} else {
return new OldLayoutCompatImpl();
} }
return new OldLayoutCompatImpl();
} }
private static final LayoutCompat impl = LayoutCompat.create(); 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 * Cryptography Architecture primitives. A good place to invoke them is in the
* application's {@code onCreate}. * 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 = 16;
private static final int VERSION_CODE_JELLY_BEAN_MR2 = 18; 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. * @throws SecurityException if the fix is needed but could not be applied.
*/ */
private static void applyOpenSSLFix() throws SecurityException { private static void applyOpenSSLFix() throws SecurityException {
if ((Build.VERSION.SDK_INT < VERSION_CODE_JELLY_BEAN) if (getApi() < VERSION_CODE_JELLY_BEAN || getApi() > VERSION_CODE_JELLY_BEAN_MR2) {
|| (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2)) {
// No need to apply the fix // No need to apply the fix
return; return;
} }
@ -99,7 +98,7 @@ public final class PRNGFixes {
*/ */
private static void installLinuxPRNGSecureRandom() private static void installLinuxPRNGSecureRandom()
throws SecurityException { 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 // No need to apply the fix
return; return;
} }

View File

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

View File

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