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