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
17package android.car.cluster.demorenderer;
18
19import android.car.cluster.demorenderer.CallStateMonitor.PhoneStateListener;
20import android.content.Context;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.telephony.TelephonyManager;
24import android.util.Log;
25
26/**
27 * Demo for rendering phone status in instrument cluster.
28 */
29public class DemoPhoneRenderer implements PhoneStateListener {
30    private static final String TAG = DemoPhoneRenderer.class.getSimpleName();
31
32    private final DemoInstrumentClusterView mView;
33    private final Context mContext;
34
35    private static Bitmap sDefaultAvatar;
36
37    private int mCurrentState;
38    private String mCurrentNumber;
39
40    DemoPhoneRenderer(DemoInstrumentClusterView view) {
41        mView = view;
42        mContext = view.getContext();
43    }
44
45    @Override
46    public void onCallStateChanged(int state, String number) {
47        Log.d(TAG, "onCallStateChanged, state: " + state + ", number: " + number);
48        mCurrentState = state;
49        mCurrentNumber = PhoneBook.getFormattedNumber(number);
50
51        if (TelephonyManager.CALL_STATE_IDLE == state) {
52            mView.hidePhone();
53        } else {
54            mView.showPhone();
55            setPhoneTitleWithState(mCurrentNumber);
56            mView.setPhoneImage(getDefaultAvatar());
57        }
58    }
59
60    @Override
61    public void onContactDetailsUpdated(CharSequence name, CharSequence typeLabel,
62            boolean isVoiceMail) {
63        Log.d(TAG, "onContactDetailsUpdated, name: " + name + ", typeLabel: " + typeLabel
64                + ", isVoicemail: " + isVoiceMail);
65        setPhoneTitleWithState(name.toString());
66        mView.setPhoneSubtitle(mCurrentNumber);
67    }
68
69    private void setPhoneTitleWithState(String text) {
70        mView.setPhoneTitle(getCallStateToDisplay(mCurrentState) + " · " + text);
71    }
72
73    @Override
74    public void onContactPhotoUpdated(Bitmap picture) {
75        Log.d(TAG, "onContactPhotoUpdated, picture: " + picture);
76        if (picture != null) {
77            mView.setPhoneImage(picture);
78        }
79    }
80
81
82    private Bitmap getDefaultAvatar() {
83        if (sDefaultAvatar == null) {
84            sDefaultAvatar = BitmapFactory.decodeResource(mContext.getResources(),
85                    R.drawable.ic_contactavatar_large_light);
86        }
87        return sDefaultAvatar;
88    }
89
90    private String getCallStateToDisplay(int state) {
91        switch (state) {
92            case TelephonyManager.CALL_STATE_OFFHOOK:
93                return mContext.getString(R.string.call_state_active);
94            case TelephonyManager.CALL_STATE_RINGING:
95                return mContext.getString(R.string.call_state_ringing);
96            default:
97                Log.w(TAG, "Unexpected call state: " + state);
98                return "";
99        }
100    }
101}
102