Android-x86
Fork
Donation

  • R/O
  • HTTP
  • SSH
  • HTTPS

packages-apps-Launcher3: Commit

packages/apps/Launcher3


Commit MetaInfo

Revisión629a67c5ac33409ebccf13284505a4cadc65656a (tree)
Tiempo2017-06-03 10:21:05
AutorTony <twickham@goog...>
CommiterTony

Log Message

Migrate ColorExtractionService to JobService

IntentService cannot be run in the background, so moving to
JobService prevents us from crashing if the wallpaper is
changed while we aren't running.

Bug: 62065291
Change-Id: Ie0c887e36d0ced43a0b9ab8136bf55eb37697489

Cambiar Resumen

Diferencia

--- a/AndroidManifest-common.xml
+++ b/AndroidManifest-common.xml
@@ -82,7 +82,8 @@
8282
8383 <service android:name="com.android.launcher3.dynamicui.ColorExtractionService"
8484 android:exported="false"
85- android:process=":wallpaper_chooser">
85+ android:process=":wallpaper_chooser"
86+ android:permission="android.permission.BIND_JOB_SERVICE">
8687 </service>
8788
8889 <service android:name="com.android.launcher3.notification.NotificationListener"
--- a/src/com/android/launcher3/Utilities.java
+++ b/src/com/android/launcher3/Utilities.java
@@ -61,7 +61,6 @@ import java.lang.reflect.Method;
6161 import java.util.Collection;
6262 import java.util.HashSet;
6363 import java.util.Locale;
64-import java.util.Set;
6564 import java.util.concurrent.Executor;
6665 import java.util.concurrent.LinkedBlockingQueue;
6766 import java.util.concurrent.ThreadPoolExecutor;
@@ -110,6 +109,8 @@ public final class Utilities {
110109 // An intent extra to indicate the horizontal scroll of the wallpaper.
111110 public static final String EXTRA_WALLPAPER_OFFSET = "com.android.launcher3.WALLPAPER_OFFSET";
112111
112+ public static final int COLOR_EXTRACTION_JOB_ID = 1;
113+
113114 // These values are same as that in {@link AsyncTask}.
114115 private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
115116 private static final int CORE_POOL_SIZE = CPU_COUNT + 1;
--- a/src/com/android/launcher3/dynamicui/ColorExtractionService.java
+++ b/src/com/android/launcher3/dynamicui/ColorExtractionService.java
@@ -17,15 +17,17 @@
1717 package com.android.launcher3.dynamicui;
1818
1919 import android.annotation.TargetApi;
20-import android.app.IntentService;
2120 import android.app.WallpaperManager;
22-import android.content.Intent;
21+import android.app.job.JobParameters;
22+import android.app.job.JobService;
2323 import android.graphics.Bitmap;
2424 import android.graphics.BitmapRegionDecoder;
2525 import android.graphics.Rect;
2626 import android.graphics.drawable.BitmapDrawable;
2727 import android.os.Build;
2828 import android.os.Bundle;
29+import android.os.Handler;
30+import android.os.HandlerThread;
2931 import android.os.ParcelFileDescriptor;
3032 import android.support.v7.graphics.Palette;
3133 import android.util.Log;
@@ -41,45 +43,76 @@ import java.io.IOException;
4143 /**
4244 * Extracts colors from the wallpaper, and saves results to {@link LauncherProvider}.
4345 */
44-public class ColorExtractionService extends IntentService {
46+public class ColorExtractionService extends JobService {
4547
4648 private static final String TAG = "ColorExtractionService";
49+ private static final boolean DEBUG = false;
4750
4851 /** The fraction of the wallpaper to extract colors for use on the hotseat. */
4952 private static final float HOTSEAT_FRACTION = 1f / 4;
5053
51- public ColorExtractionService() {
52- super("ColorExtractionService");
54+ private HandlerThread mWorkerThread;
55+ private Handler mWorkerHandler;
56+
57+ @Override
58+ public void onCreate() {
59+ super.onCreate();
60+ mWorkerThread = new HandlerThread("ColorExtractionService");
61+ mWorkerThread.start();
62+ mWorkerHandler = new Handler(mWorkerThread.getLooper());
5363 }
5464
5565 @Override
56- protected void onHandleIntent(Intent intent) {
57- WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
58- int wallpaperId = ExtractionUtils.getWallpaperId(wallpaperManager);
59-
60- ExtractedColors extractedColors = new ExtractedColors();
61- if (wallpaperManager.getWallpaperInfo() != null) {
62- // We can't extract colors from live wallpapers, so just use the default color always.
63- extractedColors.updateHotseatPalette(null);
64- } else {
65- // We extract colors for the hotseat and status bar separately,
66- // since they only consider part of the wallpaper.
67- extractedColors.updateHotseatPalette(getHotseatPalette());
68-
69- if (FeatureFlags.LIGHT_STATUS_BAR) {
70- extractedColors.updateStatusBarPalette(getStatusBarPalette());
66+ public void onDestroy() {
67+ super.onDestroy();
68+ mWorkerThread.quit();
69+ }
70+
71+ @Override
72+ public boolean onStartJob(final JobParameters jobParameters) {
73+ if (DEBUG) Log.d(TAG, "onStartJob");
74+ mWorkerHandler.post(new Runnable() {
75+ @Override
76+ public void run() {
77+ WallpaperManager wallpaperManager = WallpaperManager.getInstance(
78+ ColorExtractionService.this);
79+ int wallpaperId = ExtractionUtils.getWallpaperId(wallpaperManager);
80+
81+ ExtractedColors extractedColors = new ExtractedColors();
82+ if (wallpaperManager.getWallpaperInfo() != null) {
83+ // We can't extract colors from live wallpapers; always use the default color.
84+ extractedColors.updateHotseatPalette(null);
85+ } else {
86+ // We extract colors for the hotseat and status bar separately,
87+ // since they only consider part of the wallpaper.
88+ extractedColors.updateHotseatPalette(getHotseatPalette());
89+
90+ if (FeatureFlags.LIGHT_STATUS_BAR) {
91+ extractedColors.updateStatusBarPalette(getStatusBarPalette());
92+ }
93+ }
94+
95+ // Save the extracted colors and wallpaper id to LauncherProvider.
96+ String colorsString = extractedColors.encodeAsString();
97+ Bundle extras = new Bundle();
98+ extras.putInt(LauncherSettings.Settings.EXTRA_WALLPAPER_ID, wallpaperId);
99+ extras.putString(LauncherSettings.Settings.EXTRA_EXTRACTED_COLORS, colorsString);
100+ getContentResolver().call(
101+ LauncherSettings.Settings.CONTENT_URI,
102+ LauncherSettings.Settings.METHOD_SET_EXTRACTED_COLORS_AND_WALLPAPER_ID,
103+ null, extras);
104+ jobFinished(jobParameters, false /* needsReschedule */);
105+ if (DEBUG) Log.d(TAG, "job finished!");
71106 }
72- }
107+ });
108+ return true;
109+ }
73110
74- // Save the extracted colors and wallpaper id to LauncherProvider.
75- String colorsString = extractedColors.encodeAsString();
76- Bundle extras = new Bundle();
77- extras.putInt(LauncherSettings.Settings.EXTRA_WALLPAPER_ID, wallpaperId);
78- extras.putString(LauncherSettings.Settings.EXTRA_EXTRACTED_COLORS, colorsString);
79- getContentResolver().call(
80- LauncherSettings.Settings.CONTENT_URI,
81- LauncherSettings.Settings.METHOD_SET_EXTRACTED_COLORS_AND_WALLPAPER_ID,
82- null, extras);
111+ @Override
112+ public boolean onStopJob(JobParameters jobParameters) {
113+ if (DEBUG) Log.d(TAG, "onStopJob");
114+ mWorkerHandler.removeCallbacksAndMessages(null);
115+ return true;
83116 }
84117
85118 @TargetApi(Build.VERSION_CODES.N)
--- a/src/com/android/launcher3/dynamicui/ExtractionUtils.java
+++ b/src/com/android/launcher3/dynamicui/ExtractionUtils.java
@@ -18,8 +18,10 @@ package com.android.launcher3.dynamicui;
1818
1919 import android.annotation.TargetApi;
2020 import android.app.WallpaperManager;
21+import android.app.job.JobInfo;
22+import android.app.job.JobScheduler;
23+import android.content.ComponentName;
2124 import android.content.Context;
22-import android.content.Intent;
2325 import android.content.SharedPreferences;
2426 import android.graphics.Color;
2527 import android.os.Build;
@@ -58,7 +60,11 @@ public class ExtractionUtils {
5860
5961 /** Starts the {@link ColorExtractionService} without checking the wallpaper id */
6062 public static void startColorExtractionService(Context context) {
61- context.startService(new Intent(context, ColorExtractionService.class));
63+ JobScheduler jobScheduler = (JobScheduler) context.getSystemService(
64+ Context.JOB_SCHEDULER_SERVICE);
65+ jobScheduler.schedule(new JobInfo.Builder(Utilities.COLOR_EXTRACTION_JOB_ID,
66+ new ComponentName(context, ColorExtractionService.class))
67+ .setMinimumLatency(0).build());
6268 }
6369
6470 private static boolean hasWallpaperIdChanged(Context context) {
Show on old repository browser