1 /* 2 * Copyright (C) 2017 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 package com.android.launcher3.compat; 17 18 import android.annotation.TargetApi; 19 import android.app.WallpaperColors; 20 import android.app.WallpaperManager; 21 import android.app.WallpaperManager.OnColorsChangedListener; 22 import android.content.Context; 23 import android.graphics.Color; 24 import android.support.annotation.Nullable; 25 import android.util.Log; 26 27 import java.lang.reflect.Method; 28 29 30 @TargetApi(27) 31 public class WallpaperManagerCompatVOMR1 extends WallpaperManagerCompat { 32 33 private static final String TAG = "WMCompatVOMR1"; 34 35 private final WallpaperManager mWm; 36 private Method mWCColorHintsMethod; 37 WallpaperManagerCompatVOMR1(Context context)38 WallpaperManagerCompatVOMR1(Context context) throws Throwable { 39 mWm = context.getSystemService(WallpaperManager.class); 40 String className = WallpaperColors.class.getName(); 41 try { 42 mWCColorHintsMethod = WallpaperColors.class.getDeclaredMethod("getColorHints"); 43 } catch (Exception exc) { 44 Log.e(TAG, "getColorHints not available", exc); 45 } 46 } 47 48 @Nullable 49 @Override getWallpaperColors(int which)50 public WallpaperColorsCompat getWallpaperColors(int which) { 51 return convertColorsObject(mWm.getWallpaperColors(which)); 52 } 53 54 @Override addOnColorsChangedListener(final OnColorsChangedListenerCompat listener)55 public void addOnColorsChangedListener(final OnColorsChangedListenerCompat listener) { 56 OnColorsChangedListener onChangeListener = new OnColorsChangedListener() { 57 @Override 58 public void onColorsChanged(WallpaperColors colors, int which) { 59 listener.onColorsChanged(convertColorsObject(colors), which); 60 } 61 }; 62 mWm.addOnColorsChangedListener(onChangeListener, null); 63 } 64 convertColorsObject(WallpaperColors colors)65 private WallpaperColorsCompat convertColorsObject(WallpaperColors colors) { 66 if (colors == null) { 67 return null; 68 } 69 Color primary = colors.getPrimaryColor(); 70 Color secondary = colors.getSecondaryColor(); 71 Color tertiary = colors.getTertiaryColor(); 72 int primaryVal = primary != null ? primary.toArgb() : 0; 73 int secondaryVal = secondary != null ? secondary.toArgb() : 0; 74 int tertiaryVal = tertiary != null ? tertiary.toArgb() : 0; 75 int colorHints = 0; 76 try { 77 if (mWCColorHintsMethod != null) { 78 colorHints = (Integer) mWCColorHintsMethod.invoke(colors); 79 } 80 } catch (Exception exc) { 81 Log.e(TAG, "error calling color hints", exc); 82 } 83 return new WallpaperColorsCompat(primaryVal, secondaryVal, tertiaryVal, colorHints); 84 } 85 } 86