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 17 package com.android.launcher3.dynamicui; 18 19 import android.app.IntentService; 20 import android.app.WallpaperManager; 21 import android.content.Intent; 22 import android.graphics.Bitmap; 23 import android.graphics.drawable.BitmapDrawable; 24 import android.os.Bundle; 25 import android.support.v7.graphics.Palette; 26 27 import com.android.launcher3.LauncherProvider; 28 import com.android.launcher3.LauncherSettings; 29 import com.android.launcher3.R; 30 import com.android.launcher3.config.FeatureFlags; 31 32 /** 33 * Extracts colors from the wallpaper, and saves results to {@link LauncherProvider}. 34 */ 35 public class ColorExtractionService extends IntentService { 36 37 /** The fraction of the wallpaper to extract colors for use on the hotseat. */ 38 private static final float HOTSEAT_FRACTION = 1f / 4; 39 40 public ColorExtractionService() { 41 super("ColorExtractionService"); 42 } 43 44 @Override 45 protected void onHandleIntent(Intent intent) { 46 WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 47 int wallpaperId = ExtractionUtils.getWallpaperId(wallpaperManager); 48 ExtractedColors extractedColors = new ExtractedColors(); 49 if (wallpaperManager.getWallpaperInfo() != null) { 50 // We can't extract colors from live wallpapers, so just use the default color always. 51 extractedColors.updatePalette(null); 52 extractedColors.updateHotseatPalette(null); 53 } else { 54 Bitmap wallpaper = ((BitmapDrawable) wallpaperManager.getDrawable()).getBitmap(); 55 Palette palette = Palette.from(wallpaper).generate(); 56 extractedColors.updatePalette(palette); 57 // We extract colors for the hotseat and status bar separately, 58 // since they only consider part of the wallpaper. 59 Palette hotseatPalette = Palette.from(wallpaper) 60 .setRegion(0, (int) (wallpaper.getHeight() * (1f - HOTSEAT_FRACTION)), 61 wallpaper.getWidth(), wallpaper.getHeight()) 62 .clearFilters() 63 .generate(); 64 extractedColors.updateHotseatPalette(hotseatPalette); 65 66 if (FeatureFlags.LIGHT_STATUS_BAR) { 67 int statusBarHeight = getResources() 68 .getDimensionPixelSize(R.dimen.status_bar_height); 69 Palette statusBarPalette = Palette.from(wallpaper) 70 .setRegion(0, 0, wallpaper.getWidth(), statusBarHeight) 71 .clearFilters() 72 .generate(); 73 extractedColors.updateStatusBarPalette(statusBarPalette); 74 } 75 } 76 77 // Save the extracted colors and wallpaper id to LauncherProvider. 78 String colorsString = extractedColors.encodeAsString(); 79 Bundle extras = new Bundle(); 80 extras.putInt(LauncherSettings.Settings.EXTRA_WALLPAPER_ID, wallpaperId); 81 extras.putString(LauncherSettings.Settings.EXTRA_EXTRACTED_COLORS, colorsString); 82 getContentResolver().call( 83 LauncherSettings.Settings.CONTENT_URI, 84 LauncherSettings.Settings.METHOD_SET_EXTRACTED_COLORS_AND_WALLPAPER_ID, 85 null, extras); 86 } 87 } 88