Home | History | Annotate | Download | only in incallui
      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 
     50   public ThemeColorManager(MaterialColorMapUtils colorMap) {
     51     this.colorMap = colorMap;
     52   }
     53 
     54   public void setPendingPhoneAccountHandle(@Nullable PhoneAccountHandle pendingPhoneAccountHandle) {
     55     this.pendingPhoneAccountHandle = pendingPhoneAccountHandle;
     56   }
     57 
     58   public void onForegroundCallChanged(Context context, @Nullable DialerCall newForegroundCall) {
     59     if (newForegroundCall == null) {
     60       updateThemeColors(context, pendingPhoneAccountHandle, false);
     61     } else {
     62       updateThemeColors(context, newForegroundCall.getAccountHandle(), newForegroundCall.isSpam());
     63     }
     64   }
     65 
     66   private void updateThemeColors(
     67       Context context, @Nullable PhoneAccountHandle handle, boolean isSpam) {
     68     MaterialPalette palette;
     69     if (isSpam) {
     70       palette =
     71           colorMap.calculatePrimaryAndSecondaryColor(R.color.incall_call_spam_background_color);
     72       backgroundColorTop = context.getColor(R.color.incall_background_gradient_spam_top);
     73       backgroundColorMiddle = context.getColor(R.color.incall_background_gradient_spam_middle);
     74       backgroundColorBottom = context.getColor(R.color.incall_background_gradient_spam_bottom);
     75       backgroundColorSolid = context.getColor(R.color.incall_background_multiwindow_spam);
     76     } else {
     77       @ColorInt int highlightColor = getHighlightColor(context, handle);
     78       palette = colorMap.calculatePrimaryAndSecondaryColor(highlightColor);
     79       backgroundColorTop = context.getColor(R.color.incall_background_gradient_top);
     80       backgroundColorMiddle = context.getColor(R.color.incall_background_gradient_middle);
     81       backgroundColorBottom = context.getColor(R.color.incall_background_gradient_bottom);
     82       backgroundColorSolid = context.getColor(R.color.incall_background_multiwindow);
     83       if (highlightColor != PhoneAccount.NO_HIGHLIGHT_COLOR) {
     84         // The default background gradient has a subtle alpha. We grab that alpha and apply it to
     85         // the phone account color.
     86         backgroundColorTop = applyAlpha(palette.mPrimaryColor, backgroundColorTop);
     87         backgroundColorMiddle = applyAlpha(palette.mPrimaryColor, backgroundColorMiddle);
     88         backgroundColorBottom = applyAlpha(palette.mPrimaryColor, backgroundColorBottom);
     89         backgroundColorSolid = applyAlpha(palette.mPrimaryColor, backgroundColorSolid);
     90       }
     91     }
     92 
     93     primaryColor = palette.mPrimaryColor;
     94     secondaryColor = palette.mSecondaryColor;
     95   }
     96 
     97   @ColorInt
     98   private static int getHighlightColor(Context context, @Nullable PhoneAccountHandle handle) {
     99     if (handle != null) {
    100       PhoneAccount account = context.getSystemService(TelecomManager.class).getPhoneAccount(handle);
    101       if (account != null) {
    102         return account.getHighlightColor();
    103       }
    104     }
    105     return PhoneAccount.NO_HIGHLIGHT_COLOR;
    106   }
    107 
    108   @ColorInt
    109   public int getPrimaryColor() {
    110     return primaryColor;
    111   }
    112 
    113   @ColorInt
    114   public int getSecondaryColor() {
    115     return secondaryColor;
    116   }
    117 
    118   @ColorInt
    119   public int getBackgroundColorTop() {
    120     return backgroundColorTop;
    121   }
    122 
    123   @ColorInt
    124   public int getBackgroundColorMiddle() {
    125     return backgroundColorMiddle;
    126   }
    127 
    128   @ColorInt
    129   public int getBackgroundColorBottom() {
    130     return backgroundColorBottom;
    131   }
    132 
    133   @ColorInt
    134   public int getBackgroundColorSolid() {
    135     return backgroundColorSolid;
    136   }
    137 
    138   @ColorInt
    139   private static int applyAlpha(@ColorInt int color, @ColorInt int sourceColorWithAlpha) {
    140     return ColorUtils.setAlphaComponent(color, Color.alpha(sourceColorWithAlpha));
    141   }
    142 }
    143