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