1 /*
2  * Copyright (C) 2016 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.incallui;
18 
19 import android.content.Context;
20 import android.graphics.Color;
21 import android.support.annotation.ColorInt;
22 import android.support.annotation.Nullable;
23 import android.support.v4.graphics.ColorUtils;
24 import android.telecom.PhoneAccount;
25 import android.telecom.PhoneAccountHandle;
26 import android.telecom.TelecomManager;
27 import com.android.contacts.common.util.MaterialColorMapUtils;
28 import com.android.contacts.common.util.MaterialColorMapUtils.MaterialPalette;
29 import com.android.incallui.call.DialerCall;
30 
31 /**
32  * Calculates the background color for the in call window. The background color is based on the SIM
33  * and spam status.
34  */
35 public class ThemeColorManager {
36   private final MaterialColorMapUtils colorMap;
37   @ColorInt private int primaryColor;
38   @ColorInt private int secondaryColor;
39   @ColorInt private int backgroundColorTop;
40   @ColorInt private int backgroundColorMiddle;
41   @ColorInt private int backgroundColorBottom;
42   @ColorInt private int backgroundColorSolid;
43 
44   /**
45    * If there is no actual call currently in the call list, this will be used as a fallback to
46    * determine the theme color for InCallUI.
47    */
48   @Nullable private PhoneAccountHandle pendingPhoneAccountHandle;
49 
ThemeColorManager(MaterialColorMapUtils colorMap)50   public ThemeColorManager(MaterialColorMapUtils colorMap) {
51     this.colorMap = colorMap;
52   }
53 
setPendingPhoneAccountHandle(@ullable PhoneAccountHandle pendingPhoneAccountHandle)54   public void setPendingPhoneAccountHandle(@Nullable PhoneAccountHandle pendingPhoneAccountHandle) {
55     this.pendingPhoneAccountHandle = pendingPhoneAccountHandle;
56   }
57 
onForegroundCallChanged(Context context, @Nullable DialerCall newForegroundCall)58   public void onForegroundCallChanged(Context context, @Nullable DialerCall newForegroundCall) {
59     if (newForegroundCall == null) {
60       updateThemeColors(context, getHighlightColor(context, pendingPhoneAccountHandle), false);
61     } else {
62       updateThemeColors(
63           context,
64           getHighlightColor(context, newForegroundCall.getAccountHandle()),
65           newForegroundCall.isSpam());
66     }
67   }
68 
updateThemeColors(Context context, @ColorInt int highlightColor, boolean isSpam)69   private void updateThemeColors(Context context, @ColorInt int highlightColor, boolean isSpam) {
70     MaterialPalette palette;
71     if (isSpam) {
72       palette =
73           colorMap.calculatePrimaryAndSecondaryColor(R.color.incall_call_spam_background_color);
74       backgroundColorTop = context.getColor(R.color.incall_background_gradient_spam_top);
75       backgroundColorMiddle = context.getColor(R.color.incall_background_gradient_spam_middle);
76       backgroundColorBottom = context.getColor(R.color.incall_background_gradient_spam_bottom);
77       backgroundColorSolid = context.getColor(R.color.incall_background_multiwindow_spam);
78     } else {
79       palette = colorMap.calculatePrimaryAndSecondaryColor(highlightColor);
80       backgroundColorTop = context.getColor(R.color.incall_background_gradient_top);
81       backgroundColorMiddle = context.getColor(R.color.incall_background_gradient_middle);
82       backgroundColorBottom = context.getColor(R.color.incall_background_gradient_bottom);
83       backgroundColorSolid = context.getColor(R.color.incall_background_multiwindow);
84       if (highlightColor != PhoneAccount.NO_HIGHLIGHT_COLOR) {
85         // The default background gradient has a subtle alpha. We grab that alpha and apply it to
86         // the phone account color.
87         backgroundColorTop = applyAlpha(palette.mPrimaryColor, backgroundColorTop);
88         backgroundColorMiddle = applyAlpha(palette.mPrimaryColor, backgroundColorMiddle);
89         backgroundColorBottom = applyAlpha(palette.mPrimaryColor, backgroundColorBottom);
90         backgroundColorSolid = applyAlpha(palette.mPrimaryColor, backgroundColorSolid);
91       }
92     }
93 
94     primaryColor = palette.mPrimaryColor;
95     secondaryColor = palette.mSecondaryColor;
96   }
97 
98   @ColorInt
getHighlightColor(Context context, @Nullable PhoneAccountHandle handle)99   private int getHighlightColor(Context context, @Nullable PhoneAccountHandle handle) {
100     if (handle != null) {
101       PhoneAccount account = context.getSystemService(TelecomManager.class).getPhoneAccount(handle);
102       if (account != null) {
103         return account.getHighlightColor();
104       }
105     }
106     return PhoneAccount.NO_HIGHLIGHT_COLOR;
107   }
108 
109   @ColorInt
getPrimaryColor()110   public int getPrimaryColor() {
111     return primaryColor;
112   }
113 
114   @ColorInt
getSecondaryColor()115   public int getSecondaryColor() {
116     return secondaryColor;
117   }
118 
119   @ColorInt
getBackgroundColorTop()120   public int getBackgroundColorTop() {
121     return backgroundColorTop;
122   }
123 
124   @ColorInt
getBackgroundColorMiddle()125   public int getBackgroundColorMiddle() {
126     return backgroundColorMiddle;
127   }
128 
129   @ColorInt
getBackgroundColorBottom()130   public int getBackgroundColorBottom() {
131     return backgroundColorBottom;
132   }
133 
134   @ColorInt
getBackgroundColorSolid()135   public int getBackgroundColorSolid() {
136     return backgroundColorSolid;
137   }
138 
139   @ColorInt
applyAlpha(@olorInt int color, @ColorInt int sourceColorWithAlpha)140   private static int applyAlpha(@ColorInt int color, @ColorInt int sourceColorWithAlpha) {
141     return ColorUtils.setAlphaComponent(color, Color.alpha(sourceColorWithAlpha));
142   }
143 }
144