1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 11:42:16 -05:00

Load AbsoluteSizeSpan(int,boolean) constructor via reflection

This will allow us to run on Android 1.5/1.6 devices.
This commit is contained in:
cketti 2011-05-27 05:38:31 +02:00 committed by Jesse Vincent
parent a9b28d62d7
commit c5342472f2
2 changed files with 51 additions and 3 deletions

View File

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

View File

@ -18,7 +18,6 @@ import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan; import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.util.Log; import android.util.Log;
@ -2166,7 +2165,7 @@ public class MessageList
0, 0,
noSender.length(), noSender.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new AbsoluteSizeSpan(mFontSizes.getMessageListSender(), true), str.setSpan(K9.createAbsoluteSizeSpan(mFontSizes.getMessageListSender()),
0, 0,
noSender.length(), noSender.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
@ -2255,7 +2254,7 @@ public class MessageList
0, 0,
message.sender.length() + 1, message.sender.length() + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new AbsoluteSizeSpan(mFontSizes.getMessageListSender(), true), str.setSpan(K9.createAbsoluteSizeSpan(mFontSizes.getMessageListSender()),
0, 0,
message.sender.length() + 1, message.sender.length() + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);