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.EntryAdapter; 28 import com.android.systemui.statusbar.notification.collection.ListEntry; 29 import com.android.systemui.statusbar.notification.collection.PipelineEntry; 30 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; 31 import com.android.systemui.util.Compile; 32 33 /** 34 * A util class for various reusable functions 35 */ 36 public class NotificationUtils { 37 private static final int[] sLocationBase = new int[2]; 38 private static final int[] sLocationOffset = new int[2]; 39 40 @Nullable private static Boolean sUseNewInterruptionModel = null; 41 isGrayscale(ImageView v, ContrastColorUtil colorUtil)42 public static boolean isGrayscale(ImageView v, ContrastColorUtil colorUtil) { 43 Object isGrayscale = v.getTag(R.id.icon_is_grayscale); 44 if (isGrayscale != null) { 45 return Boolean.TRUE.equals(isGrayscale); 46 } 47 boolean grayscale = colorUtil.isGrayscaleIcon(v.getDrawable()); 48 v.setTag(R.id.icon_is_grayscale, grayscale); 49 return grayscale; 50 } 51 interpolate(float start, float end, float amount)52 public static float interpolate(float start, float end, float amount) { 53 return start * (1.0f - amount) + end * amount; 54 } 55 interpolateColors(int startColor, int endColor, float amount)56 public static int interpolateColors(int startColor, int endColor, float amount) { 57 return Color.argb( 58 (int) interpolate(Color.alpha(startColor), Color.alpha(endColor), amount), 59 (int) interpolate(Color.red(startColor), Color.red(endColor), amount), 60 (int) interpolate(Color.green(startColor), Color.green(endColor), amount), 61 (int) interpolate(Color.blue(startColor), Color.blue(endColor), amount)); 62 } 63 getRelativeYOffset(View offsetView, View baseView)64 public static float getRelativeYOffset(View offsetView, View baseView) { 65 baseView.getLocationOnScreen(sLocationBase); 66 offsetView.getLocationOnScreen(sLocationOffset); 67 return sLocationOffset[1] - sLocationBase[1]; 68 } 69 70 /** 71 * @param dimenId the dimen to look up 72 * @return the font scaled dimen as if it were in sp but doesn't shrink sizes below dp 73 */ getFontScaledHeight(Context context, int dimenId)74 public static int getFontScaledHeight(Context context, int dimenId) { 75 int dimensionPixelSize = context.getResources().getDimensionPixelSize(dimenId); 76 float factor = Math.max(1.0f, context.getResources().getDisplayMetrics().scaledDensity / 77 context.getResources().getDisplayMetrics().density); 78 return (int) (dimensionPixelSize * factor); 79 } 80 81 private static final boolean INCLUDE_HASH_CODE_IN_LIST_ENTRY_LOG_KEY = false; 82 83 /** Get the notification key, reformatted for logging, for the (optional) entry */ logKey(PipelineEntry entry)84 public static String logKey(PipelineEntry entry) { 85 if (entry == null) { 86 return "null"; 87 } 88 if (Compile.IS_DEBUG && INCLUDE_HASH_CODE_IN_LIST_ENTRY_LOG_KEY) { 89 return logKey(entry.getKey()) + "@" + Integer.toHexString(entry.hashCode()); 90 } else { 91 return logKey(entry.getKey()); 92 } 93 } 94 95 /** Get the notification key, reformatted for logging, for the (optional) row */ logKey(ExpandableNotificationRow row)96 public static String logKey(ExpandableNotificationRow row) { 97 return row == null ? "null" : row.getLoggingKey(); 98 } 99 100 /** Removes newlines from the notification key to prettify apps that have these in the tag */ logKey(String key)101 public static String logKey(String key) { 102 if (key == null) { 103 return "null"; 104 } 105 return key.replace("\n", ""); 106 } 107 108 } 109