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.R;
27 
28 /**
29  * A util class for various reusable functions
30  */
31 public class NotificationUtils {
32     private static final int[] sLocationBase = new int[2];
33     private static final int[] sLocationOffset = new int[2];
34 
35     @Nullable private static Boolean sUseNewInterruptionModel = null;
36 
isGrayscale(ImageView v, ContrastColorUtil colorUtil)37     public static boolean isGrayscale(ImageView v, ContrastColorUtil colorUtil) {
38         Object isGrayscale = v.getTag(R.id.icon_is_grayscale);
39         if (isGrayscale != null) {
40             return Boolean.TRUE.equals(isGrayscale);
41         }
42         boolean grayscale = colorUtil.isGrayscaleIcon(v.getDrawable());
43         v.setTag(R.id.icon_is_grayscale, grayscale);
44         return grayscale;
45     }
46 
interpolate(float start, float end, float amount)47     public static float interpolate(float start, float end, float amount) {
48         return start * (1.0f - amount) + end * amount;
49     }
50 
interpolateColors(int startColor, int endColor, float amount)51     public static int interpolateColors(int startColor, int endColor, float amount) {
52         return Color.argb(
53                 (int) interpolate(Color.alpha(startColor), Color.alpha(endColor), amount),
54                 (int) interpolate(Color.red(startColor), Color.red(endColor), amount),
55                 (int) interpolate(Color.green(startColor), Color.green(endColor), amount),
56                 (int) interpolate(Color.blue(startColor), Color.blue(endColor), amount));
57     }
58 
getRelativeYOffset(View offsetView, View baseView)59     public static float getRelativeYOffset(View offsetView, View baseView) {
60         baseView.getLocationOnScreen(sLocationBase);
61         offsetView.getLocationOnScreen(sLocationOffset);
62         return sLocationOffset[1] - sLocationBase[1];
63     }
64 
65     /**
66      * @param dimenId the dimen to look up
67      * @return the font scaled dimen as if it were in sp but doesn't shrink sizes below dp
68      */
getFontScaledHeight(Context context, int dimenId)69     public static int getFontScaledHeight(Context context, int dimenId) {
70         int dimensionPixelSize = context.getResources().getDimensionPixelSize(dimenId);
71         float factor = Math.max(1.0f, context.getResources().getDisplayMetrics().scaledDensity /
72                 context.getResources().getDisplayMetrics().density);
73         return (int) (dimensionPixelSize * factor);
74     }
75 
76 }
77