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 android.annotation.Nullable; 20 import android.content.Context; 21 import android.graphics.Color; 22 import android.view.View; 23 import android.widget.ImageView; 24 25 import com.android.internal.util.ContrastColorUtil; 26 import com.android.systemui.res.R; 27 import com.android.systemui.statusbar.notification.collection.ListEntry; 28 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; 29 import com.android.systemui.util.Compile; 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 private static final boolean INCLUDE_HASH_CODE_IN_LIST_ENTRY_LOG_KEY = false; 80 81 /** Get the notification key, reformatted for logging, for the (optional) entry */ logKey(ListEntry entry)82 public static String logKey(ListEntry entry) { 83 if (entry == null) { 84 return "null"; 85 } 86 if (Compile.IS_DEBUG && INCLUDE_HASH_CODE_IN_LIST_ENTRY_LOG_KEY) { 87 return logKey(entry.getKey()) + "@" + Integer.toHexString(entry.hashCode()); 88 } else { 89 return logKey(entry.getKey()); 90 } 91 } 92 93 /** Get the notification key, reformatted for logging, for the (optional) row */ logKey(ExpandableNotificationRow row)94 public static String logKey(ExpandableNotificationRow row) { 95 return row == null ? "null" : logKey(row.getEntry()); 96 } 97 98 /** Removes newlines from the notification key to prettify apps that have these in the tag */ logKey(String key)99 public static String logKey(String key) { 100 if (key == null) { 101 return "null"; 102 } 103 return key.replace("\n", ""); 104 } 105 106 } 107