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.contacts.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.R;
27import com.android.contacts.test.NeededForTesting;
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                throw new IllegalArgumentException("invalid call type: " + callType);
90        }
91    }
92
93    @Override
94    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
95        setMeasuredDimension(mWidth, mHeight);
96    }
97
98    @Override
99    protected void onDraw(Canvas canvas) {
100        int left = 0;
101        for (Integer callType : mCallTypes) {
102            final Drawable drawable = getCallTypeDrawable(callType);
103            final int right = left + drawable.getIntrinsicWidth();
104            drawable.setBounds(left, 0, right, drawable.getIntrinsicHeight());
105            drawable.draw(canvas);
106            left = right + mResources.iconMargin;
107        }
108    }
109
110    private static class Resources {
111        public final Drawable incoming;
112        public final Drawable outgoing;
113        public final Drawable missed;
114        public final Drawable voicemail;
115        public final int iconMargin;
116
117        public Resources(Context context) {
118            final android.content.res.Resources r = context.getResources();
119            incoming = r.getDrawable(R.drawable.ic_call_incoming_holo_dark);
120            outgoing = r.getDrawable(R.drawable.ic_call_outgoing_holo_dark);
121            missed = r.getDrawable(R.drawable.ic_call_missed_holo_dark);
122            voicemail = r.getDrawable(R.drawable.ic_call_voicemail_holo_dark);
123            iconMargin = r.getDimensionPixelSize(R.dimen.call_log_icon_margin);
124        }
125    }
126}
127