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 */
16package com.android.car.overview;
17
18import android.app.PendingIntent;
19import android.content.Context;
20import android.graphics.Color;
21import android.telecom.Call;
22import android.util.Log;
23import android.view.View;
24import android.widget.Chronometer;
25import android.widget.ImageButton;
26import android.widget.ImageView;
27import android.widget.TextView;
28import com.android.car.overview.utils.BitmapUtils;
29import com.android.car.stream.CurrentCallExtension;
30import com.android.car.stream.StreamCard;
31
32/**
33 * A {@link StreamViewHolder} that binds a {@link CurrentCallExtension} to
34 * an interactive in call UI.
35 */
36public class CurrentCallStreamViewHolder extends StreamViewHolder {
37    private static final String TAG = "CurrentCallStreamVH";
38
39    private final ImageView mBackgroundImage;
40    private final TextView mDisplayNameTextView;
41
42    private final TextView mCallStateTextView;
43    private final Chronometer mTimerView;
44
45    private final OverviewFabButton mCallActionButton;
46    private final ImageButton mMuteActionButton;
47
48    private PendingIntent mCallAction;
49    private PendingIntent mMuteAction;
50    private PendingIntent mContainerClickAction;
51
52    public CurrentCallStreamViewHolder(Context context, View itemView) {
53        super(context, itemView);
54
55        mBackgroundImage = (ImageView) itemView.findViewById(R.id.background_image);
56        mDisplayNameTextView = (TextView) itemView.findViewById(R.id.display_name);
57        mCallStateTextView = (TextView) itemView.findViewById(R.id.call_state);
58        mTimerView = (Chronometer) itemView.findViewById(R.id.timer);
59
60        mCallActionButton = (OverviewFabButton) itemView.findViewById(R.id.call_button);
61        mMuteActionButton = (ImageButton) itemView.findViewById(R.id.mute_button);
62
63        mCallActionButton.setAccentColor(Color.RED);
64        mCallActionButton.setOnClickListener(new View.OnClickListener() {
65            @Override
66            public void onClick(View v) {
67                if (mCallAction == null) {
68                    return;
69                }
70                try {
71                    mCallAction.send(mContext, 0 /* resultCode */, null /* intent */);
72                } catch (PendingIntent.CanceledException e) {
73                    Log.e(TAG, "Failed to send call action pending intent", e);
74                }
75            }
76        });
77
78        mMuteActionButton.setOnClickListener(new View.OnClickListener() {
79            @Override
80            public void onClick(View v) {
81                if (mMuteAction == null) {
82                    return;
83                }
84                try {
85                    mMuteAction.send(mContext, 0 /* resultCode */, null /* intent */);
86                } catch (PendingIntent.CanceledException e) {
87                    Log.e(TAG, "Failed to send mute action pending intent", e);
88                }
89            }
90        });
91
92        mActionContainer.setOnClickListener(new View.OnClickListener() {
93            @Override
94            public void onClick(View v) {
95                if (mContainerClickAction == null) {
96                    return;
97                }
98                try {
99                    mContainerClickAction.send(mContext, 0 /* resultCode */, null /* intent */);
100                } catch (PendingIntent.CanceledException e) {
101                    Log.e(TAG, "Failed to send call action pending intent", e);
102                }
103            }
104        });
105    }
106
107    @Override
108    public void bindStreamCard(StreamCard card) {
109        super.bindStreamCard(card);
110
111        if (!(card.getCardExtension() instanceof CurrentCallExtension)) {
112            Log.e(TAG, "StreamCard does not contain a CurrentCallExtension");
113            return;
114        }
115
116        mContainerClickAction = card.getContentPendingIntent();
117
118        CurrentCallExtension call = (CurrentCallExtension) card.getCardExtension();
119        int callState = call.getCallState();
120
121        mDisplayNameTextView.setText(call.getDisplayName());
122        mCallStateTextView.setText(getCallState(mContext, callState));
123
124        // For active calls set up mute button and timer view.
125        if (callState == Call.STATE_ACTIVE) {
126            mTimerView.setVisibility(View.VISIBLE);
127            mTimerView.setBase(call.getCallStartTime());
128            mTimerView.start();
129
130            int muteIconRes = call.isMuted() ? R.drawable.ic_mic_muted : R.drawable.ic_mic;
131            mMuteActionButton.setVisibility(View.VISIBLE);
132            mMuteActionButton.setImageResource(muteIconRes);
133            mMuteAction = call.isMuted() ? call.getUnMuteAction() : call.getMuteAction();
134        }
135
136        // Setup the call button.
137        if (callState == Call.STATE_DIALING || callState == Call.STATE_ACTIVE
138                || callState == Call.STATE_RINGING) {
139            mCallActionButton.setVisibility(View.VISIBLE);
140            mCallActionButton.setImageResource(R.drawable.ic_phone_hangup);
141
142            if (callState == Call.STATE_RINGING) {
143                mCallAction = call.getAcceptCallAction();
144            } else {
145                mCallAction = call.getHangupCallAction();
146            }
147        }
148
149        if (call.getContactPhoto() != null) {
150            mBackgroundImage
151                    .setImageBitmap(BitmapUtils.applySaturation(call.getContactPhoto(), 01.f));
152        }
153    }
154
155    private String getCallState(Context context, int state) {
156        switch (state) {
157            case Call.STATE_ACTIVE:
158                return context.getString(R.string.ongoing_call);
159            case Call.STATE_DIALING:
160                return context.getString(R.string.dialing_call);
161            case Call.STATE_DISCONNECTING:
162                return context.getString(R.string.disconnecting_call);
163            case Call.STATE_RINGING:
164                return context.getString(R.string.notification_incoming_call);
165            default:
166                return context.getString(R.string.unknown);
167        }
168    }
169
170    @Override
171    protected void resetViews() {
172        mBackgroundImage.setImageBitmap(null);
173        mDisplayNameTextView.setText(null);
174        mCallStateTextView.setText(null);
175
176        mTimerView.setText(null);
177        mTimerView.setVisibility(View.INVISIBLE);
178
179        mCallActionButton.setImageBitmap(null);
180        mCallActionButton.setVisibility(View.INVISIBLE);
181
182        mMuteActionButton.setImageBitmap(null);
183        mMuteActionButton.setVisibility(View.INVISIBLE);
184
185        mCallAction = null;
186        mMuteAction = null;
187    }
188}
189