1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3.dynamicui;
18
19import android.annotation.TargetApi;
20import android.app.WallpaperManager;
21import android.app.job.JobParameters;
22import android.app.job.JobService;
23import android.graphics.Bitmap;
24import android.graphics.BitmapFactory;
25import android.graphics.BitmapRegionDecoder;
26import android.graphics.Rect;
27import android.graphics.drawable.BitmapDrawable;
28import android.os.Build;
29import android.os.Bundle;
30import android.os.Handler;
31import android.os.HandlerThread;
32import android.os.ParcelFileDescriptor;
33import android.support.v7.graphics.Palette;
34import android.util.Log;
35
36import com.android.launcher3.LauncherProvider;
37import com.android.launcher3.LauncherSettings;
38import com.android.launcher3.R;
39import com.android.launcher3.Utilities;
40import com.android.launcher3.config.FeatureFlags;
41
42import java.io.IOException;
43
44/**
45 * Extracts colors from the wallpaper, and saves results to {@link LauncherProvider}.
46 */
47public class ColorExtractionService extends JobService {
48
49    private static final String TAG = "ColorExtractionService";
50    private static final boolean DEBUG = false;
51
52    /** The fraction of the wallpaper to extract colors for use on the hotseat. */
53    private static final float HOTSEAT_FRACTION = 1f / 4;
54
55    private HandlerThread mWorkerThread;
56    private Handler mWorkerHandler;
57
58    @Override
59    public void onCreate() {
60        super.onCreate();
61        mWorkerThread = new HandlerThread("ColorExtractionService");
62        mWorkerThread.start();
63        mWorkerHandler = new Handler(mWorkerThread.getLooper());
64    }
65
66    @Override
67    public void onDestroy() {
68        super.onDestroy();
69        mWorkerThread.quit();
70    }
71
72    @Override
73    public boolean onStartJob(final JobParameters jobParameters) {
74        if (DEBUG) Log.d(TAG, "onStartJob");
75        mWorkerHandler.post(new Runnable() {
76            @Override
77            public void run() {
78                WallpaperManager wallpaperManager = WallpaperManager.getInstance(
79                        ColorExtractionService.this);
80                int wallpaperId = ExtractionUtils.getWallpaperId(wallpaperManager);
81
82                ExtractedColors extractedColors = new ExtractedColors();
83                if (wallpaperManager.getWallpaperInfo() != null) {
84                    // We can't extract colors from live wallpapers; always use the default color.
85                    extractedColors.updateHotseatPalette(null);
86
87                    if (FeatureFlags.QSB_IN_HOTSEAT || FeatureFlags.LAUNCHER3_GRADIENT_ALL_APPS) {
88                        extractedColors.updateWallpaperThemePalette(null);
89                    }
90                } else {
91                    // We extract colors for the hotseat and status bar separately,
92                    // since they only consider part of the wallpaper.
93                    extractedColors.updateHotseatPalette(getHotseatPalette());
94
95                    if (FeatureFlags.LIGHT_STATUS_BAR) {
96                        extractedColors.updateStatusBarPalette(getStatusBarPalette());
97                    }
98
99                    if (FeatureFlags.QSB_IN_HOTSEAT || FeatureFlags.LAUNCHER3_GRADIENT_ALL_APPS) {
100                        extractedColors.updateWallpaperThemePalette(getWallpaperPalette());
101                    }
102                }
103
104                // Save the extracted colors and wallpaper id to LauncherProvider.
105                String colorsString = extractedColors.encodeAsString();
106                Bundle extras = new Bundle();
107                extras.putInt(LauncherSettings.Settings.EXTRA_WALLPAPER_ID, wallpaperId);
108                extras.putString(LauncherSettings.Settings.EXTRA_EXTRACTED_COLORS, colorsString);
109                getContentResolver().call(
110                        LauncherSettings.Settings.CONTENT_URI,
111                        LauncherSettings.Settings.METHOD_SET_EXTRACTED_COLORS_AND_WALLPAPER_ID,
112                        null, extras);
113                jobFinished(jobParameters, false /* needsReschedule */);
114                if (DEBUG) Log.d(TAG, "job finished!");
115            }
116        });
117        return true;
118    }
119
120    @Override
121    public boolean onStopJob(JobParameters jobParameters) {
122        if (DEBUG) Log.d(TAG, "onStopJob");
123        mWorkerHandler.removeCallbacksAndMessages(null);
124        return true;
125    }
126
127    @TargetApi(Build.VERSION_CODES.N)
128    private Palette getHotseatPalette() {
129        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
130        if (Utilities.ATLEAST_NOUGAT) {
131            try (ParcelFileDescriptor fd = wallpaperManager
132                    .getWallpaperFile(WallpaperManager.FLAG_SYSTEM)) {
133                BitmapRegionDecoder decoder = BitmapRegionDecoder
134                        .newInstance(fd.getFileDescriptor(), false);
135                int height = decoder.getHeight();
136                Rect decodeRegion = new Rect(0, (int) (height * (1f - HOTSEAT_FRACTION)),
137                        decoder.getWidth(), height);
138                Bitmap bitmap = decoder.decodeRegion(decodeRegion, null);
139                decoder.recycle();
140                if (bitmap != null) {
141                    return Palette.from(bitmap).clearFilters().generate();
142                }
143            } catch (IOException | NullPointerException e) {
144                Log.e(TAG, "Fetching partial bitmap failed, trying old method", e);
145            }
146        }
147
148        Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap();
149        return Palette.from(wallpaper)
150                .setRegion(0, (int) (wallpaper.getHeight() * (1f - HOTSEAT_FRACTION)),
151                        wallpaper.getWidth(), wallpaper.getHeight())
152                .clearFilters()
153                .generate();
154    }
155
156    @TargetApi(Build.VERSION_CODES.N)
157    private Palette getStatusBarPalette() {
158        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
159        int statusBarHeight = getResources()
160                .getDimensionPixelSize(R.dimen.status_bar_height);
161
162        if (Utilities.ATLEAST_NOUGAT) {
163            try (ParcelFileDescriptor fd = wallpaperManager
164                    .getWallpaperFile(WallpaperManager.FLAG_SYSTEM)) {
165                BitmapRegionDecoder decoder = BitmapRegionDecoder
166                        .newInstance(fd.getFileDescriptor(), false);
167                Rect decodeRegion = new Rect(0, 0,
168                        decoder.getWidth(), statusBarHeight);
169                Bitmap bitmap = decoder.decodeRegion(decodeRegion, null);
170                decoder.recycle();
171                if (bitmap != null) {
172                    return Palette.from(bitmap).clearFilters().generate();
173                }
174            } catch (IOException | NullPointerException e) {
175                Log.e(TAG, "Fetching partial bitmap failed, trying old method", e);
176            }
177        }
178
179        Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap();
180        return Palette.from(wallpaper)
181                .setRegion(0, 0, wallpaper.getWidth(), statusBarHeight)
182                .clearFilters()
183                .generate();
184    }
185
186    @TargetApi(Build.VERSION_CODES.N)
187    private Palette getWallpaperPalette() {
188        WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
189        if (Utilities.ATLEAST_NOUGAT) {
190            try (ParcelFileDescriptor fd = wallpaperManager
191                    .getWallpaperFile(WallpaperManager.FLAG_SYSTEM)) {
192                Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
193                if (bitmap != null) {
194                    return Palette.from(bitmap).clearFilters().generate();
195                }
196            } catch (IOException | NullPointerException e) {
197                Log.e(TAG, "Fetching partial bitmap failed, trying old method", e);
198            }
199        }
200
201        Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap();
202        return Palette.from(wallpaper).clearFilters().generate();
203    }
204}
205