1 /* 2 * Copyright (C) 2015 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.systemui.statusbar.notification; 18 19 import static android.provider.Settings.Secure.NOTIFICATION_NEW_INTERRUPTION_MODEL; 20 21 import android.annotation.Nullable; 22 import android.content.Context; 23 import android.graphics.Color; 24 import android.provider.Settings; 25 import android.view.View; 26 import android.widget.ImageView; 27 28 import com.android.internal.util.ContrastColorUtil; 29 import com.android.systemui.R; 30 31 /** 32 * A util class for various reusable functions 33 */ 34 public class NotificationUtils { 35 private static final int[] sLocationBase = new int[2]; 36 private static final int[] sLocationOffset = new int[2]; 37 38 @Nullable private static Boolean sUseNewInterruptionModel = null; 39 isGrayscale(ImageView v, ContrastColorUtil colorUtil)40 public static boolean isGrayscale(ImageView v, ContrastColorUtil colorUtil) { 41 Object isGrayscale = v.getTag(R.id.icon_is_grayscale); 42 if (isGrayscale != null) { 43 return Boolean.TRUE.equals(isGrayscale); 44 } 45 boolean grayscale = colorUtil.isGrayscaleIcon(v.getDrawable()); 46 v.setTag(R.id.icon_is_grayscale, grayscale); 47 return grayscale; 48 } 49 interpolate(float start, float end, float amount)50 public static float interpolate(float start, float end, float amount) { 51 return start * (1.0f - amount) + end * amount; 52 } 53 interpolateColors(int startColor, int endColor, float amount)54 public static int interpolateColors(int startColor, int endColor, float amount) { 55 return Color.argb( 56 (int) interpolate(Color.alpha(startColor), Color.alpha(endColor), amount), 57 (int) interpolate(Color.red(startColor), Color.red(endColor), amount), 58 (int) interpolate(Color.green(startColor), Color.green(endColor), amount), 59 (int) interpolate(Color.blue(startColor), Color.blue(endColor), amount)); 60 } 61 getRelativeYOffset(View offsetView, View baseView)62 public static float getRelativeYOffset(View offsetView, View baseView) { 63 baseView.getLocationOnScreen(sLocationBase); 64 offsetView.getLocationOnScreen(sLocationOffset); 65 return sLocationOffset[1] - sLocationBase[1]; 66 } 67 68 /** 69 * @param dimenId the dimen to look up 70 * @return the font scaled dimen as if it were in sp but doesn't shrink sizes below dp 71 */ getFontScaledHeight(Context context, int dimenId)72 public static int getFontScaledHeight(Context context, int dimenId) { 73 int dimensionPixelSize = context.getResources().getDimensionPixelSize(dimenId); 74 float factor = Math.max(1.0f, context.getResources().getDisplayMetrics().scaledDensity / 75 context.getResources().getDisplayMetrics().density); 76 return (int) (dimensionPixelSize * factor); 77 } 78 79 /** 80 * Returns the value of the new interruption model setting. This result is cached and cannot 81 * change except through reboots/process restarts. 82 */ useNewInterruptionModel(Context context)83 public static boolean useNewInterruptionModel(Context context) { 84 if (sUseNewInterruptionModel == null) { 85 sUseNewInterruptionModel = Settings.Secure.getInt(context.getContentResolver(), 86 NOTIFICATION_NEW_INTERRUPTION_MODEL, 1) != 0; 87 } 88 return sUseNewInterruptionModel; 89 } 90 } 91