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

Commit 82315966 authored by Sunny Goyal's avatar Sunny Goyal
Browse files

Updating the WallpaperManagerCompatVL to use JobSchedular instead of intent service

Bug: 62065291
Change-Id: I5872cea1b110268e9593eeb6397e94ecea1bc03d
parent 6d55202c
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -89,7 +89,8 @@
        <service
        <service
            android:name="com.android.launcher3.compat.WallpaperManagerCompatVL$ColorExtractionService"
            android:name="com.android.launcher3.compat.WallpaperManagerCompatVL$ColorExtractionService"
            android:exported="false"
            android:exported="false"
            android:process=":wallpaper_chooser" />
            android:process=":wallpaper_chooser"
            android:permission="android.permission.BIND_JOB_SERVICE" />


        <service android:name="com.android.launcher3.notification.NotificationListener"
        <service android:name="com.android.launcher3.notification.NotificationListener"
                 android:enabled="@bool/notification_badging_enabled"
                 android:enabled="@bool/notification_badging_enabled"
+1 −0
Original line number Original line Diff line number Diff line
@@ -111,6 +111,7 @@ public final class Utilities {
    public static final String EXTRA_WALLPAPER_OFFSET = "com.android.launcher3.WALLPAPER_OFFSET";
    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 COLOR_EXTRACTION_JOB_ID = 1;
    public static final int WALLPAPER_COMPAT_JOB_ID = 2;


    // These values are same as that in {@link AsyncTask}.
    // These values are same as that in {@link AsyncTask}.
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
+69 −21
Original line number Original line Diff line number Diff line
@@ -19,23 +19,28 @@ import static android.app.WallpaperManager.FLAG_SYSTEM;


import static com.android.launcher3.Utilities.getDevicePrefs;
import static com.android.launcher3.Utilities.getDevicePrefs;


import android.app.IntentService;
import android.app.WallpaperInfo;
import android.app.WallpaperInfo;
import android.app.WallpaperManager;
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.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.PermissionInfo;
import android.graphics.Bitmap;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Canvas;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.ParcelFileDescriptor;
import android.os.ParcelFileDescriptor;
import android.os.ResultReceiver;
import android.support.annotation.Nullable;
import android.support.annotation.Nullable;
import android.support.v7.graphics.Palette;
import android.support.v7.graphics.Palette;
import android.util.Log;
import android.util.Log;
@@ -53,7 +58,8 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {


    private static final String VERSION_PREFIX = "1,";
    private static final String VERSION_PREFIX = "1,";
    private static final String KEY_COLORS = "wallpaper_parsed_colors";
    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<>();
    private final ArrayList<OnColorsChangedListenerCompat> mListeners = new ArrayList<>();


@@ -80,6 +86,28 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
                reloadColors();
                reloadColors();
            }
            }
        }, new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED));
        }, 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
    @Nullable
@@ -94,15 +122,10 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
    }
    }


    private void reloadColors() {
    private void reloadColors() {
        ResultReceiver receiver = new ResultReceiver(new Handler()) {
        JobInfo job = new JobInfo.Builder(Utilities.WALLPAPER_COMPAT_JOB_ID,

                new ComponentName(mContext, ColorExtractionService.class))
            @Override
                .setMinimumLatency(0).build();
            protected void onReceiveResult(int resultCode, Bundle resultData) {
        ((JobScheduler) mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE)).schedule(job);
                handleResult(resultData.getString(KEY_COLORS));
            }
        };
        mContext.startService(new Intent(mContext, ColorExtractionService.class)
                .putExtra(EXTRA_RECEIVER, receiver));
    }
    }


    private void handleResult(String result) {
    private void handleResult(String result) {
@@ -141,18 +164,43 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
    /**
    /**
     * Intent service to handle color extraction
     * 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;
        private static final int MAX_WALLPAPER_EXTRACTION_AREA = 112 * 112;


        public ColorExtractionService() {
        private HandlerThread mWorkerThread;
            super("ColorExtractionService");
        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.
         * Extracts the wallpaper colors and sends the result back through the receiver.
         */
         */
        @Override
        @Override
        protected void onHandleIntent(@Nullable Intent intent) {
        public void run() {
            int wallpaperId = getWallpaperId(this);
            int wallpaperId = getWallpaperId(this);


            Bitmap bitmap = null;
            Bitmap bitmap = null;
@@ -223,10 +271,10 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
                value = builder.toString();
                value = builder.toString();
            }
            }


            ResultReceiver receiver = intent.getParcelableExtra(EXTRA_RECEIVER);
            // Send the result
            Bundle result = new Bundle();
            sendBroadcast(new Intent(ACTION_EXTRACTION_COMPLETE)
            result.putString(KEY_COLORS, value);
                    .setPackage(getPackageName())
            receiver.send(0, result);
                    .putExtra(KEY_COLORS, value));
        }
        }
    }
    }
}
}