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

Commit fd309fbf authored by Tony Wickham's avatar Tony Wickham
Browse files

Replaced Utilities.isNycOrAbove() with version code check.

- Instead of using reflection to check if N is present, we now use
  Build.VERSION.SDK_INT >= Build.VERSION_CODES.N.
- Other places that used reflection to use N APIs have also been
  cleaned up.

Bug: 22942492
Change-Id: Ia7b981cae375e800bcc8f0c54aec48e0c8c076da
parent d140f3c7
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -181,7 +181,7 @@ public class BitmapCropTask extends AsyncTask<Integer, Void, Boolean> {
                failure = true;
            }
            return !failure;
        } else if (mSetWallpaper && Utilities.isNycOrAbove()
        } else if (mSetWallpaper && Utilities.ATLEAST_N
                && mRotation == 0 && mOutWidth > 0 && mOutHeight > 0) {
            Rect hint = new Rect();
            mCropBounds.roundOut(hint);
@@ -404,7 +404,7 @@ public class BitmapCropTask extends AsyncTask<Integer, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Integer... params) {
        return cropBitmap(params.length == 0 ? NycWallpaperUtils.FLAG_SET_SYSTEM : params[0]);
        return cropBitmap(params.length == 0 ? WallpaperManager.FLAG_SYSTEM : params[0]);
    }

    @Override
@@ -418,7 +418,7 @@ public class BitmapCropTask extends AsyncTask<Integer, Void, Boolean> {
    }

    private void setWallpaper(InputStream in, Rect crop, int whichWallpaper) throws IOException {
        if (!Utilities.isNycOrAbove()) {
        if (!Utilities.ATLEAST_N) {
            WallpaperManager.getInstance(mContext.getApplicationContext()).setStream(in);
        } else {
            NycWallpaperUtils.setStream(mContext, in, crop, true, whichWallpaper);
+12 −21
Original line number Diff line number Diff line
@@ -9,15 +9,11 @@ import android.os.AsyncTask;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Utility class used to help set lockscreen wallpapers on N+.
 */
public class NycWallpaperUtils {
    public static final int FLAG_SET_SYSTEM = 1 << 0; // TODO: use WallpaperManager.FLAG_SET_SYSTEM
    public static final int FLAG_SET_LOCK = 1 << 1; // TODO: use WallpaperManager.FLAG_SET_LOCK

    /**
     * Calls cropTask.execute(), once the user has selected which wallpaper to set. On pre-N
@@ -26,7 +22,7 @@ public class NycWallpaperUtils {
    public static void executeCropTaskAfterPrompt(
            Context context, final AsyncTask<Integer, ?, ?> cropTask,
            DialogInterface.OnCancelListener onCancelListener) {
        if (Utilities.isNycOrAbove()) {
        if (Utilities.ATLEAST_N) {
            new AlertDialog.Builder(context)
                    .setTitle(R.string.wallpaper_instructions)
                    .setItems(R.array.which_wallpaper_options, new DialogInterface.OnClickListener() {
@@ -34,11 +30,12 @@ public class NycWallpaperUtils {
                        public void onClick(DialogInterface dialog, int selectedItemIndex) {
                            int whichWallpaper;
                            if (selectedItemIndex == 0) {
                                whichWallpaper = FLAG_SET_SYSTEM;
                                whichWallpaper = WallpaperManager.FLAG_SYSTEM;
                            } else if (selectedItemIndex == 1) {
                                whichWallpaper = FLAG_SET_LOCK;
                                whichWallpaper = WallpaperManager.FLAG_LOCK;
                            } else {
                                whichWallpaper = FLAG_SET_SYSTEM | FLAG_SET_LOCK;
                                whichWallpaper = WallpaperManager.FLAG_SYSTEM
                                        | WallpaperManager.FLAG_LOCK;
                            }
                            cropTask.execute(whichWallpaper);
                        }
@@ -46,20 +43,16 @@ public class NycWallpaperUtils {
                    .setOnCancelListener(onCancelListener)
                    .show();
        } else {
            cropTask.execute(FLAG_SET_SYSTEM);
            cropTask.execute(WallpaperManager.FLAG_SYSTEM);
        }
    }

    public static void setStream(Context context, final InputStream data, Rect visibleCropHint,
            boolean allowBackup, int whichWallpaper) throws IOException {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
        try {
            // TODO: use mWallpaperManager.setStream(data, visibleCropHint, allowBackup, which)
            // without needing reflection.
            Method setStream = WallpaperManager.class.getMethod("setStream", InputStream.class,
                    Rect.class, boolean.class, int.class);
            setStream.invoke(wallpaperManager, data, visibleCropHint, allowBackup, whichWallpaper);
        } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
        if (Utilities.ATLEAST_N) {
            wallpaperManager.setStream(data, visibleCropHint, allowBackup, whichWallpaper);
        } else {
            // Fall back to previous implementation (set system)
            wallpaperManager.setStream(data);
        }
@@ -67,11 +60,9 @@ public class NycWallpaperUtils {

    public static void clear(Context context, int whichWallpaper) throws IOException {
        WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
        try {
            // TODO: use mWallpaperManager.clear(whichWallpaper) without needing reflection.
            Method clear = WallpaperManager.class.getMethod("clear", int.class);
            clear.invoke(wallpaperManager, whichWallpaper);
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        if (Utilities.ATLEAST_N) {
            wallpaperManager.clear(whichWallpaper);
        } else {
            // Fall back to previous implementation (clear system)
            wallpaperManager.clear();
        }
+4 −4
Original line number Diff line number Diff line
@@ -353,7 +353,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {
        }
        @Override
        public void onSave(final WallpaperPickerActivity a) {
            if (!Utilities.isNycOrAbove()) {
            if (!Utilities.ATLEAST_N) {
                try {
                    WallpaperManager.getInstance(a.getContext()).clear();
                    a.setResult(Activity.RESULT_OK);
@@ -393,7 +393,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {
                    int whichWallpaper = params[0];
                    boolean succeeded = true;
                    try {
                        if (whichWallpaper == NycWallpaperUtils.FLAG_SET_LOCK) {
                        if (whichWallpaper == WallpaperManager.FLAG_LOCK) {
                            Bitmap defaultWallpaper = ((BitmapDrawable) WallpaperManager
                                    .getInstance(a.getApplicationContext()).getBuiltInDrawable())
                                    .getBitmap();
@@ -403,7 +403,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {
                                byte[] outByteArray = tmpOut.toByteArray();
                                NycWallpaperUtils.setStream(a.getApplicationContext(),
                                        new ByteArrayInputStream(outByteArray), null, true,
                                        NycWallpaperUtils.FLAG_SET_LOCK);
                                        WallpaperManager.FLAG_LOCK);
                            }
                        } else {
                            NycWallpaperUtils.clear(a, whichWallpaper);
@@ -944,7 +944,7 @@ public class WallpaperPickerActivity extends WallpaperCropActivity {
                (int) rotatedBounds[0], (int) rotatedBounds[1], width, height, leftAligned);
        cropTask.setCropBounds(cropRect);

        if (cropTask.cropBitmap(NycWallpaperUtils.FLAG_SET_SYSTEM)) {
        if (cropTask.cropBitmap(WallpaperManager.FLAG_SYSTEM)) {
            return cropTask.getCroppedBitmap();
        } else {
            return null;
+1 −1
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ buildscript {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.google.protobuf:protobuf-gradle-plugin:0.7.0'
    }
}
+13 −33
Original line number Diff line number Diff line
@@ -46,7 +46,6 @@ import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.Process;
@@ -101,8 +100,11 @@ public final class Utilities {
    private static final int[] sLoc0 = new int[2];
    private static final int[] sLoc1 = new int[2];

    // TODO: use Build.VERSION_CODES when available
    public static final boolean ATLEAST_MARSHMALLOW = Build.VERSION.SDK_INT >= 23;
    // TODO: use the full N name (e.g. ATLEAST_N*****) when available
    public static final boolean ATLEAST_N = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;

    public static final boolean ATLEAST_MARSHMALLOW =
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;

    public static final boolean ATLEAST_LOLLIPOP_MR1 =
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1;
@@ -119,18 +121,6 @@ public final class Utilities {
    public static final boolean ATLEAST_JB_MR2 =
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;

    public static boolean isNycOrAbove() {
        // TODO: Replace using reflection with looking at the API version once
        // Build.VERSION.SDK_INT gets bumped to 24. b/22942492.
        try {
            View.class.getDeclaredField("DRAG_FLAG_OPAQUE");
            // View.DRAG_FLAG_OPAQUE doesn't exist in M-release, so it's an indication of N+.
            return true;
        } catch (NoSuchFieldException e) {
            return false;
        }
    }

    // These values are same as that in {@link AsyncTask}.
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
@@ -151,20 +141,14 @@ public final class Utilities {

    public static boolean isAllowRotationPrefEnabled(Context context) {
        boolean allowRotationPref = false;
        if (isNycOrAbove()) {
        if (ATLEAST_N) {
            // If the device was scaled, used the original dimensions to determine if rotation
            // is allowed of not.
            try {
                // TODO: Use the actual field when the API is finalized.
                int originalDensity =
                        DisplayMetrics.class.getField("DENSITY_DEVICE_STABLE").getInt(null);
            int originalDensity = DisplayMetrics.DENSITY_DEVICE_STABLE;
            Resources res = context.getResources();
            int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp
                    * res.getDisplayMetrics().densityDpi / originalDensity;
            allowRotationPref = originalSmallestWidth >= 600;
            } catch (Exception e) {
                // Ignore
            }
        }
        return getPrefs(context).getBoolean(ALLOW_ROTATION_PREFERENCE_KEY, allowRotationPref);
    }
@@ -824,12 +808,8 @@ public final class Utilities {
    }

    public static boolean isWallapaperAllowed(Context context) {
        if (isNycOrAbove()) {
            try {
                WallpaperManager wm = context.getSystemService(WallpaperManager.class);
                return (Boolean) wm.getClass().getDeclaredMethod("isSetWallpaperAllowed")
                        .invoke(wm);
            } catch (Exception e) { }
        if (ATLEAST_N) {
            return context.getSystemService(WallpaperManager.class).isSetWallpaperAllowed();
        }
        return true;
    }
Loading