diff options
9 files changed, 40 insertions, 73 deletions
diff --git a/WallpaperPicker/src/com/android/gallery3d/common/BitmapCropTask.java b/WallpaperPicker/src/com/android/gallery3d/common/BitmapCropTask.java index 153fa90587..b5238126ca 100644 --- a/WallpaperPicker/src/com/android/gallery3d/common/BitmapCropTask.java +++ b/WallpaperPicker/src/com/android/gallery3d/common/BitmapCropTask.java @@ -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); diff --git a/WallpaperPicker/src/com/android/launcher3/NycWallpaperUtils.java b/WallpaperPicker/src/com/android/launcher3/NycWallpaperUtils.java index abac830a2a..d9d4293a69 100644 --- a/WallpaperPicker/src/com/android/launcher3/NycWallpaperUtils.java +++ b/WallpaperPicker/src/com/android/launcher3/NycWallpaperUtils.java @@ -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(); } diff --git a/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java b/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java index 40f0544a2d..3ecb53eaa2 100644 --- a/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java +++ b/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java @@ -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; diff --git a/build.gradle b/build.gradle index b9a990f87e..00bed8bca4 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } } diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java index 78ba22eb66..ba342b1694 100644 --- a/src/com/android/launcher3/Utilities.java +++ b/src/com/android/launcher3/Utilities.java @@ -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); - Resources res = context.getResources(); - int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp - * res.getDisplayMetrics().densityDpi / originalDensity; - allowRotationPref = originalSmallestWidth >= 600; - } catch (Exception e) { - // Ignore - } + int originalDensity = DisplayMetrics.DENSITY_DEVICE_STABLE; + Resources res = context.getResources(); + int originalSmallestWidth = res.getConfiguration().smallestScreenWidthDp + * res.getDisplayMetrics().densityDpi / originalDensity; + allowRotationPref = originalSmallestWidth >= 600; } 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; } diff --git a/src/com/android/launcher3/compat/UserManagerCompat.java b/src/com/android/launcher3/compat/UserManagerCompat.java index 978f9229da..186ab2a2bc 100644 --- a/src/com/android/launcher3/compat/UserManagerCompat.java +++ b/src/com/android/launcher3/compat/UserManagerCompat.java @@ -32,7 +32,7 @@ public abstract class UserManagerCompat { public static UserManagerCompat getInstance(Context context) { synchronized (sInstanceLock) { if (sInstance == null) { - if (Utilities.isNycOrAbove()) { + if (Utilities.ATLEAST_N) { sInstance = new UserManagerCompatVN(context.getApplicationContext()); } else if (Utilities.ATLEAST_LOLLIPOP) { sInstance = new UserManagerCompatVL(context.getApplicationContext()); diff --git a/src/com/android/launcher3/compat/UserManagerCompatVN.java b/src/com/android/launcher3/compat/UserManagerCompatVN.java index ae41e68a36..ed828a5f9a 100644 --- a/src/com/android/launcher3/compat/UserManagerCompatVN.java +++ b/src/com/android/launcher3/compat/UserManagerCompatVN.java @@ -16,7 +16,9 @@ package com.android.launcher3.compat; +import android.annotation.TargetApi; import android.content.Context; +import android.os.Build; import android.os.UserHandle; import android.os.UserManager; import android.util.Log; @@ -24,7 +26,7 @@ import android.util.Log; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; -//TODO: Once gogole3 SDK is updated to N, add @TargetApi(Build.VERSION_CODES.N) +@TargetApi(Build.VERSION_CODES.N) public class UserManagerCompatVN extends UserManagerCompatVL { private static final String TAG = "UserManagerCompatVN"; @@ -37,13 +39,7 @@ public class UserManagerCompatVN extends UserManagerCompatVL { public boolean isQuietModeEnabled(UserHandleCompat user) { if (user != null) { try { - //TODO: Replace with proper API call once google3 SDK is updated. - Method isQuietModeEnabledMethod = UserManager.class.getMethod("isQuietModeEnabled", - UserHandle.class); - return (boolean) isQuietModeEnabledMethod.invoke(mUserManager, user.getUser()); - } catch (NoSuchMethodError | NoSuchMethodException | IllegalAccessException - | InvocationTargetException e) { - Log.e(TAG, "Running on N without isQuietModeEnabled", e); + return mUserManager.isQuietModeEnabled(user.getUser()); } catch (IllegalArgumentException e) { // TODO remove this when API is fixed to not throw this // when called on user that isn't a managed profile. diff --git a/src/com/android/launcher3/model/GridSizeMigrationTask.java b/src/com/android/launcher3/model/GridSizeMigrationTask.java index 8117122055..f7da596f81 100644 --- a/src/com/android/launcher3/model/GridSizeMigrationTask.java +++ b/src/com/android/launcher3/model/GridSizeMigrationTask.java @@ -39,7 +39,7 @@ import java.util.Locale; */ public class GridSizeMigrationTask { - public static boolean ENABLED = Utilities.isNycOrAbove(); + public static boolean ENABLED = Utilities.ATLEAST_N; private static final String TAG = "GridSizeMigrationTask"; private static final boolean DEBUG = true; diff --git a/src/com/android/launcher3/util/PackageManagerHelper.java b/src/com/android/launcher3/util/PackageManagerHelper.java index 08e8e869d5..5e60ed6ee8 100644 --- a/src/com/android/launcher3/util/PackageManagerHelper.java +++ b/src/com/android/launcher3/util/PackageManagerHelper.java @@ -62,7 +62,7 @@ public class PackageManagerHelper { // The value of FLAG_SUSPENDED was reused by a hidden constant // ApplicationInfo.FLAG_PRIVILEGED prior to N, so only check for suspended flag on N // or later. - if (Utilities.isNycOrAbove()) { + if (Utilities.ATLEAST_N) { return (info.flags & FLAG_SUSPENDED) != 0; } else { return false; |