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
diff --git a/WallpaperPicker/src/com/android/gallery3d/common/BitmapCropTask.java b/WallpaperPicker/src/com/android/gallery3d/common/BitmapCropTask.java
index 153fa90..b523812 100644
--- a/WallpaperPicker/src/com/android/gallery3d/common/BitmapCropTask.java
+++ b/WallpaperPicker/src/com/android/gallery3d/common/BitmapCropTask.java
@@ -181,7 +181,7 @@
                 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 @@
 
     @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 @@
     }
 
     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 abac830..d9d4293 100644
--- a/WallpaperPicker/src/com/android/launcher3/NycWallpaperUtils.java
+++ b/WallpaperPicker/src/com/android/launcher3/NycWallpaperUtils.java
@@ -9,15 +9,11 @@
 
 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 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 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 @@
                     .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 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 40f0544..3ecb53e 100644
--- a/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java
+++ b/WallpaperPicker/src/com/android/launcher3/WallpaperPickerActivity.java
@@ -353,7 +353,7 @@
         }
         @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 @@
                     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 @@
                                 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 @@
                 (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 b9a990f..00bed8b 100644
--- a/build.gradle
+++ b/build.gradle
@@ -3,7 +3,7 @@
         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 78ba22e..ba342b1 100644
--- a/src/com/android/launcher3/Utilities.java
+++ b/src/com/android/launcher3/Utilities.java
@@ -46,7 +46,6 @@
 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 @@
     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 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 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 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 978f922..186ab2a 100644
--- a/src/com/android/launcher3/compat/UserManagerCompat.java
+++ b/src/com/android/launcher3/compat/UserManagerCompat.java
@@ -32,7 +32,7 @@
     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 ae41e68..ed828a5 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 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 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 8117122..f7da596 100644
--- a/src/com/android/launcher3/model/GridSizeMigrationTask.java
+++ b/src/com/android/launcher3/model/GridSizeMigrationTask.java
@@ -39,7 +39,7 @@
  */
 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 08e8e86..5e60ed6 100644
--- a/src/com/android/launcher3/util/PackageManagerHelper.java
+++ b/src/com/android/launcher3/util/PackageManagerHelper.java
@@ -62,7 +62,7 @@
         // 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;