Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c5342472 authored by cketti's avatar cketti Committed by Jesse Vincent
Browse files

Load AbsoluteSizeSpan(int,boolean) constructor via reflection

This will allow us to run on Android 1.5/1.6 devices.
parent a9b28d62
Loading
Loading
Loading
Loading
+49 −0
Original line number Diff line number Diff line
@@ -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<ApplicationAware> observers = new ArrayList<ApplicationAware>();

    /**
     * @see K9#createAbsoluteSizeSpan(int)
     */
    private static Constructor<AbsoluteSizeSpan> 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.
     *
     * <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;
    }
}
+2 −3
Original line number Diff line number Diff line
@@ -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);