Updating the WallpaperManagerCompatVL to use JobSchedular instead of intent service

Bug: 62065291
Change-Id: I5872cea1b110268e9593eeb6397e94ecea1bc03d
diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java
index d48b9ab..c2987f8 100644
--- a/src/com/android/launcher3/Utilities.java
+++ b/src/com/android/launcher3/Utilities.java
@@ -111,6 +111,7 @@
     public static final String EXTRA_WALLPAPER_OFFSET = "com.android.launcher3.WALLPAPER_OFFSET";
 
     public static final int COLOR_EXTRACTION_JOB_ID = 1;
+    public static final int WALLPAPER_COMPAT_JOB_ID = 2;
 
     // These values are same as that in {@link AsyncTask}.
     private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
diff --git a/src/com/android/launcher3/compat/WallpaperManagerCompatVL.java b/src/com/android/launcher3/compat/WallpaperManagerCompatVL.java
index d61b564..8bdcedb 100644
--- a/src/com/android/launcher3/compat/WallpaperManagerCompatVL.java
+++ b/src/com/android/launcher3/compat/WallpaperManagerCompatVL.java
@@ -19,23 +19,28 @@
 
 import static com.android.launcher3.Utilities.getDevicePrefs;
 
-import android.app.IntentService;
 import android.app.WallpaperInfo;
 import android.app.WallpaperManager;
+import android.app.job.JobInfo;
+import android.app.job.JobParameters;
+import android.app.job.JobScheduler;
+import android.app.job.JobService;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.content.pm.PackageManager;
+import android.content.pm.PermissionInfo;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.BitmapRegionDecoder;
 import android.graphics.Canvas;
 import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
-import android.os.Bundle;
 import android.os.Handler;
+import android.os.HandlerThread;
 import android.os.ParcelFileDescriptor;
-import android.os.ResultReceiver;
 import android.support.annotation.Nullable;
 import android.support.v7.graphics.Palette;
 import android.util.Log;
@@ -53,7 +58,8 @@
 
     private static final String VERSION_PREFIX = "1,";
     private static final String KEY_COLORS = "wallpaper_parsed_colors";
-    private static final String EXTRA_RECEIVER = "receiver";
+    private static final String ACTION_EXTRACTION_COMPLETE =
+            "com.android.launcher3.compat.WallpaperManagerCompatVL.EXTRACTION_COMPLETE";
 
     private final ArrayList<OnColorsChangedListenerCompat> mListeners = new ArrayList<>();
 
@@ -80,6 +86,28 @@
                 reloadColors();
             }
         }, new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED));
+
+        // Register a receiver for results
+        String permission = null;
+        // Find a permission which only we can use.
+        try {
+            for (PermissionInfo info : context.getPackageManager().getPackageInfo(
+                    context.getPackageName(),
+                    PackageManager.GET_PERMISSIONS).permissions) {
+                if ((info.protectionLevel & PermissionInfo.PROTECTION_SIGNATURE) != 0) {
+                    permission = info.name;
+                }
+            }
+        } catch (PackageManager.NameNotFoundException e) {
+            // Something went wrong. ignore
+            Log.d(TAG, "Unable to get permission info", e);
+        }
+        mContext.registerReceiver(new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                handleResult(intent.getStringExtra(KEY_COLORS));
+            }
+        }, new IntentFilter(ACTION_EXTRACTION_COMPLETE), permission, new Handler());
     }
 
     @Nullable
@@ -94,15 +122,10 @@
     }
 
     private void reloadColors() {
-        ResultReceiver receiver = new ResultReceiver(new Handler()) {
-
-            @Override
-            protected void onReceiveResult(int resultCode, Bundle resultData) {
-                handleResult(resultData.getString(KEY_COLORS));
-            }
-        };
-        mContext.startService(new Intent(mContext, ColorExtractionService.class)
-                .putExtra(EXTRA_RECEIVER, receiver));
+        JobInfo job = new JobInfo.Builder(Utilities.WALLPAPER_COMPAT_JOB_ID,
+                new ComponentName(mContext, ColorExtractionService.class))
+                .setMinimumLatency(0).build();
+        ((JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(job);
     }
 
     private void handleResult(String result) {
@@ -141,18 +164,43 @@
     /**
      * Intent service to handle color extraction
      */
-    public static class ColorExtractionService extends IntentService {
+    public static class ColorExtractionService extends JobService implements Runnable {
         private static final int MAX_WALLPAPER_EXTRACTION_AREA = 112 * 112;
 
-        public ColorExtractionService() {
-            super("ColorExtractionService");
+        private HandlerThread mWorkerThread;
+        private Handler mWorkerHandler;
+
+        @Override
+        public void onCreate() {
+            super.onCreate();
+            mWorkerThread = new HandlerThread("ColorExtractionService");
+            mWorkerThread.start();
+            mWorkerHandler = new Handler(mWorkerThread.getLooper());
+        }
+
+        @Override
+        public void onDestroy() {
+            super.onDestroy();
+            mWorkerThread.quit();
+        }
+
+        @Override
+        public boolean onStartJob(final JobParameters jobParameters) {
+            mWorkerHandler.post(this);
+            return true;
+        }
+
+        @Override
+        public boolean onStopJob(JobParameters jobParameters) {
+            mWorkerHandler.removeCallbacksAndMessages(null);
+            return true;
         }
 
         /**
          * Extracts the wallpaper colors and sends the result back through the receiver.
          */
         @Override
-        protected void onHandleIntent(@Nullable Intent intent) {
+        public void run() {
             int wallpaperId = getWallpaperId(this);
 
             Bitmap bitmap = null;
@@ -223,10 +271,10 @@
                 value = builder.toString();
             }
 
-            ResultReceiver receiver = intent.getParcelableExtra(EXTRA_RECEIVER);
-            Bundle result = new Bundle();
-            result.putString(KEY_COLORS, value);
-            receiver.send(0, result);
+            // Send the result
+            sendBroadcast(new Intent(ACTION_EXTRACTION_COMPLETE)
+                    .setPackage(getPackageName())
+                    .putExtra(KEY_COLORS, value));
         }
     }
 }