Home | History | Annotate | Download | only in dialer
      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 package com.android.car.dialer;
     17 
     18 import android.content.Context;
     19 import android.graphics.Canvas;
     20 import android.graphics.PorterDuff;
     21 import android.graphics.drawable.Drawable;
     22 import android.util.AttributeSet;
     23 import android.view.View;
     24 
     25 import com.android.car.dialer.telecom.PhoneLoader;
     26 
     27 import java.util.ArrayList;
     28 import java.util.List;
     29 
     30 /**
     31  * View that draws one or more symbols for different types of calls (missed calls, outgoing etc).
     32  * The symbols are set up horizontally. As this view doesn't create subviews, it is better suited
     33  * for ListView-recycling that a regular LinearLayout using ImageViews.
     34  *
     35  * TODO(mcrico): Move to shared.
     36  */
     37 public class CallTypeIconsView extends View {
     38     private List<Integer> mCallTypes = new ArrayList<>(MAX_CALL_TYPE_ICONS);
     39     private Resources mResources;
     40     private int mWidth;
     41     private int mHeight;
     42 
     43     public static final int MAX_CALL_TYPE_ICONS = 3;
     44 
     45     public CallTypeIconsView(Context context) {
     46         this(context, null);
     47     }
     48 
     49     public CallTypeIconsView(Context context, AttributeSet attrs) {
     50         super(context, attrs);
     51         mResources = new Resources(context);
     52         mResources.voicemail.setColorFilter(
     53                 getResources().getColor(R.color.car_tint), PorterDuff.Mode.SRC_IN);
     54     }
     55 
     56     public void clear() {
     57         mCallTypes.clear();
     58         mWidth = 0;
     59         mHeight = 0;
     60         requestLayout();
     61     }
     62 
     63     public void add(int callType) {
     64         mCallTypes.add(callType);
     65 
     66         final Drawable drawable = getCallTypeDrawable(callType);
     67         mWidth += drawable.getIntrinsicWidth() + mResources.iconMargin;
     68         mHeight = Math.max(mHeight, drawable.getIntrinsicHeight());
     69         requestLayout();
     70     }
     71 
     72     public int getCount() {
     73         return mCallTypes.size();
     74     }
     75 
     76     public int getCallType(int index) {
     77         return mCallTypes.get(index);
     78     }
     79 
     80     private Drawable getCallTypeDrawable(int callType) {
     81         switch (callType) {
     82             case PhoneLoader.INCOMING_TYPE:
     83                 return mResources.incoming;
     84             case PhoneLoader.OUTGOING_TYPE:
     85                 return mResources.outgoing;
     86             case PhoneLoader.MISSED_TYPE:
     87                 return mResources.missed;
     88             case PhoneLoader.VOICEMAIL_TYPE:
     89                 return mResources.voicemail;
     90             default:
     91                 // It is possible for users to end up with calls with unknown call types in their
     92                 // call history, possibly due to 3rd party call log implementations (e.g. to
     93                 // distinguish between rejected and missed calls). Instead of crashing, just
     94                 // assume that all unknown call types are missed calls.
     95                 return mResources.missed;
     96         }
     97     }
     98 
     99     @Override
    100     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    101         setMeasuredDimension(mWidth, mHeight);
    102     }
    103 
    104     @Override
    105     protected void onDraw(Canvas canvas) {
    106         int left = 0;
    107         for (Integer callType : mCallTypes) {
    108             final Drawable drawable = getCallTypeDrawable(callType);
    109             final int right = left + drawable.getIntrinsicWidth();
    110             drawable.setBounds(left, 0, right, drawable.getIntrinsicHeight());
    111             drawable.draw(canvas);
    112             left = right + mResources.iconMargin;
    113         }
    114     }
    115 
    116     private static class Resources {
    117         public final Drawable incoming;
    118         public final Drawable outgoing;
    119         public final Drawable missed;
    120         public final Drawable voicemail;
    121         public final int iconMargin;
    122 
    123         public Resources(Context context) {
    124             final android.content.res.Resources r = context.getResources();
    125             incoming = r.getDrawable(R.drawable.ic_call_received);
    126             outgoing = r.getDrawable(R.drawable.ic_call_made);
    127             missed = r.getDrawable(R.drawable.ic_call_missed);
    128             voicemail = r.getDrawable(R.drawable.ic_call_voicemail);
    129             iconMargin = r.getDimensionPixelSize(R.dimen.call_log_icon_margin);
    130         }
    131     }
    132 }
    133