Updating the WallpaperManagerCompatVL to use JobSchedular instead of intent service
Bug: 62065291 Change-Id: I5872cea1b110268e9593eeb6397e94ecea1bc03d
This commit is contained in:
parent
6d55202c9b
commit
8231596619
|
@ -89,7 +89,8 @@
|
|||
<service
|
||||
android:name="com.android.launcher3.compat.WallpaperManagerCompatVL$ColorExtractionService"
|
||||
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"
|
||||
android:enabled="@bool/notification_badging_enabled"
|
||||
|
|
|
@ -111,6 +111,7 @@ public final class Utilities {
|
|||
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();
|
||||
|
|
|
@ -19,23 +19,28 @@ import static android.app.WallpaperManager.FLAG_SYSTEM;
|
|||
|
||||
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 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
|
|||
|
||||
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 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
|
|||
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 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
|
|||
}
|
||||
|
||||
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 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
|
|||
/**
|
||||
* 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 @@ public class WallpaperManagerCompatVL extends WallpaperManagerCompat {
|
|||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue