diff --git a/src/com/fsck/k9/K9.java b/src/com/fsck/k9/K9.java index 0c5da2007..60314d288 100644 --- a/src/com/fsck/k9/K9.java +++ b/src/com/fsck/k9/K9.java @@ -2,6 +2,7 @@ package com.fsck.k9; import java.io.File; +import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -22,6 +23,7 @@ import android.os.Environment; import android.os.Handler; import android.os.Looper; import android.text.format.Time; +import android.text.style.AbsoluteSizeSpan; import android.util.Log; import com.fsck.k9.activity.MessageCompose; @@ -66,6 +68,10 @@ public class K9 extends Application { */ private static List observers = new ArrayList(); + /** + * @see K9#createAbsoluteSizeSpan(int) + */ + private static Constructor sAbsoluteSizeSpanConstructor; public enum BACKGROUND_OPS { WHEN_CHECKED, ALWAYS, NEVER, WHEN_CHECKED_AUTO_SYNC @@ -992,4 +998,47 @@ public class K9 extends Application { K9.mAttachmentDefaultPath = attachmentDefaultPath; } + /** + * Creates an {@link AbsoluteSizeSpan} object. + * + *

+ * Android versions prior to 2.0 don't support the constructor with two parameters + * ({@link AbsoluteSizeSpan#AbsoluteSizeSpan(int, boolean)}). So we have to perform some + * reflection magic to dynamically load the new constructor on devices that support it. + * For devices with old Android versions we just use the size as pixels (instead of dip). + *

+ * + * @param size This is used as the {@code size} parameter for the AbsoluteSizeSpan constructor. + * @return a AbsoluteSizeSpan object with the specified text size. + */ + public static AbsoluteSizeSpan createAbsoluteSizeSpan(int size) { + if (Integer.parseInt(android.os.Build.VERSION.SDK) < 5) { + // For Android 1.5/1.6 simply use the constructor with only the size parameter. + // Yes, that will most likely look wrong! + return new AbsoluteSizeSpan(size); + } + + if (sAbsoluteSizeSpanConstructor == null) { + try { + sAbsoluteSizeSpanConstructor = AbsoluteSizeSpan.class.getConstructor(int.class, boolean.class); + } catch (Exception e) { + Log.e(K9.LOG_TAG, "Couldn't get the AbsoluteSizeSpan(int, boolean) constructor", e); + + // Fallback + return new AbsoluteSizeSpan(size); + } + } + + AbsoluteSizeSpan result; + try { + result = sAbsoluteSizeSpanConstructor.newInstance(size, true); + } catch (Exception e) { + Log.e(K9.LOG_TAG, "Couldn't call the AbsoluteSizeSpan(int, boolean) constructor", e); + + // Fallback + result = new AbsoluteSizeSpan(size); + } + + return result; + } } diff --git a/src/com/fsck/k9/activity/MessageList.java b/src/com/fsck/k9/activity/MessageList.java index c7069df0e..8e902223c 100644 --- a/src/com/fsck/k9/activity/MessageList.java +++ b/src/com/fsck/k9/activity/MessageList.java @@ -18,7 +18,6 @@ import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.Spannable; import android.text.SpannableStringBuilder; -import android.text.style.AbsoluteSizeSpan; import android.text.style.ForegroundColorSpan; import android.text.style.StyleSpan; import android.util.Log; @@ -2166,7 +2165,7 @@ public class MessageList 0, noSender.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - str.setSpan(new AbsoluteSizeSpan(mFontSizes.getMessageListSender(), true), + str.setSpan(K9.createAbsoluteSizeSpan(mFontSizes.getMessageListSender()), 0, noSender.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); @@ -2255,7 +2254,7 @@ public class MessageList 0, message.sender.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - str.setSpan(new AbsoluteSizeSpan(mFontSizes.getMessageListSender(), true), + str.setSpan(K9.createAbsoluteSizeSpan(mFontSizes.getMessageListSender()), 0, message.sender.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);