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.annotation.TargetApi; 20 import android.app.WallpaperManager; 21 import android.app.job.JobInfo; 22 import android.app.job.JobScheduler; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.SharedPreferences; 26 import android.graphics.Color; 27 import android.os.Build; 28 import android.support.v4.graphics.ColorUtils; 29 import android.support.v7.graphics.Palette; 30 31 import com.android.launcher3.Utilities; 32 import com.android.launcher3.config.FeatureFlags; 33 34 import java.util.List; 35 36 /** 37 * Contains helper fields and methods related to extracting colors from the wallpaper. 38 */ 39 public class ExtractionUtils { 40 public static final String EXTRACTED_COLORS_PREFERENCE_KEY = "pref_extractedColors"; 41 public static final String WALLPAPER_ID_PREFERENCE_KEY = "pref_wallpaperId"; 42 43 private static final float MIN_CONTRAST_RATIO = 2f; 44 45 /** 46 * Extract colors in the :wallpaper-chooser process, if the wallpaper id has changed. 47 * When the new colors are saved in the LauncherProvider, 48 * Launcher will be notified in Launcher#onSettingsChanged(String, String). 49 */ 50 public static void startColorExtractionServiceIfNecessary(final Context context) { 51 if (FeatureFlags.LAUNCHER3_GRADIENT_ALL_APPS) { 52 return; 53 } 54 // Run on a background thread, since the service is asynchronous anyway. 55 Utilities.THREAD_POOL_EXECUTOR.execute(new Runnable() { 56 @Override 57 public void run() { 58 if (hasWallpaperIdChanged(context)) { 59 startColorExtractionService(context); 60 } 61 } 62 }); 63 } 64 65 /** Starts the {@link ColorExtractionService} without checking the wallpaper id */ 66 public static void startColorExtractionService(Context context) { 67 if (FeatureFlags.LAUNCHER3_GRADIENT_ALL_APPS) { 68 return; 69 } 70 JobScheduler jobScheduler = (JobScheduler) context.getSystemService( 71 Context.JOB_SCHEDULER_SERVICE); 72 jobScheduler.schedule(new JobInfo.Builder(Utilities.COLOR_EXTRACTION_JOB_ID, 73 new ComponentName(context, ColorExtractionService.class)) 74 .setMinimumLatency(0).build()); 75 } 76 77 private static boolean hasWallpaperIdChanged(Context context) { 78 if (!Utilities.ATLEAST_NOUGAT) { 79 // TODO: update an id in sharedprefs in onWallpaperChanged broadcast, and read it here. 80 return false; 81 } 82 final SharedPreferences sharedPrefs = Utilities.getPrefs(context); 83 int wallpaperId = getWallpaperId(WallpaperManager.getInstance(context)); 84 int savedWallpaperId = sharedPrefs.getInt(ExtractionUtils.WALLPAPER_ID_PREFERENCE_KEY, -1); 85 return wallpaperId != savedWallpaperId; 86 } 87 88 @TargetApi(Build.VERSION_CODES.N) 89 public static int getWallpaperId(WallpaperManager wallpaperManager) { 90 return Utilities.ATLEAST_NOUGAT ? 91 wallpaperManager.getWallpaperId(WallpaperManager.FLAG_SYSTEM) : -1; 92 } 93 94 public static boolean isSuperLight(Palette p) { 95 return !isLegibleOnWallpaper(Color.WHITE, p.getSwatches()); 96 } 97 98 public static boolean isSuperDark(Palette p) { 99 return !isLegibleOnWallpaper(Color.BLACK, p.getSwatches()); 100 } 101 102 /** 103 * Given a color, returns true if that color is legible on 104 * the given wallpaper color swatches, else returns false. 105 */ 106 private static boolean isLegibleOnWallpaper(int color, List<Palette.Swatch> wallpaperSwatches) { 107 int legiblePopulation = 0; 108 int illegiblePopulation = 0; 109 for (Palette.Swatch swatch : wallpaperSwatches) { 110 if (isLegible(color, swatch.getRgb())) { 111 legiblePopulation += swatch.getPopulation(); 112 } else { 113 illegiblePopulation += swatch.getPopulation(); 114 } 115 } 116 return legiblePopulation > illegiblePopulation; 117 } 118 119 /** @return Whether the foreground color is legible on the background color. */ 120 private static boolean isLegible(int foreground, int background) { 121 background = ColorUtils.setAlphaComponent(background, 255); 122 return ColorUtils.calculateContrast(foreground, background) >= MIN_CONTRAST_RATIO; 123 } 124 125 } 126