Enabling async inflation for AppWidgets

Bug: 22839968
Change-Id: Id622053c2882f478b3a3ab3918a9f76750ead8c6
diff --git a/src/com/android/launcher3/LauncherAppWidgetHostView.java b/src/com/android/launcher3/LauncherAppWidgetHostView.java
index b3db092..b34d1ff 100644
--- a/src/com/android/launcher3/LauncherAppWidgetHostView.java
+++ b/src/com/android/launcher3/LauncherAppWidgetHostView.java
@@ -22,6 +22,7 @@
 import android.graphics.Rect;
 import android.os.Handler;
 import android.os.SystemClock;
+import android.util.Log;
 import android.util.SparseBooleanArray;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
@@ -36,13 +37,17 @@
 
 import com.android.launcher3.dragndrop.DragLayer.TouchCompleteListener;
 
+import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.concurrent.Executor;
 
 /**
  * {@inheritDoc}
  */
 public class LauncherAppWidgetHostView extends AppWidgetHostView implements TouchCompleteListener {
 
+    private static final String TAG = "LauncherWidgetHostView";
+
     // Related to the auto-advancing of widgets
     private static final long ADVANCE_INTERVAL = 20000;
     private static final long ADVANCE_STAGGER = 250;
@@ -75,6 +80,16 @@
         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         setAccessibilityDelegate(Launcher.getLauncher(context).getAccessibilityDelegate());
         setBackgroundResource(R.drawable.widget_internal_focus_bg);
+
+        if (Utilities.isAtLeastO()) {
+            try {
+                Method asyncMethod = AppWidgetHostView.class
+                        .getMethod("setAsyncExecutor", Executor.class);
+                asyncMethod.invoke(this, Utilities.THREAD_POOL_EXECUTOR);
+            } catch (Exception e) {
+                Log.e(TAG, "Unable to set async executor", e);
+            }
+        }
     }
 
     @Override
diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java
index 13a69f2..be8ff00 100644
--- a/src/com/android/launcher3/Utilities.java
+++ b/src/com/android/launcher3/Utilities.java
@@ -84,6 +84,12 @@
     private static final int[] sLoc0 = new int[2];
     private static final int[] sLoc1 = new int[2];
 
+    public static boolean isAtLeastO() {
+        // TODO: Clean this up: b/32610406
+        return !"REL".equals(Build.VERSION.CODENAME)
+                && "O".compareTo(Build.VERSION.CODENAME) <= 0;
+    }
+
     public static final boolean ATLEAST_NOUGAT_MR1 =
         Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1;