1/*
2 * Copyright (C) 2014 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.server.telecom;
18
19import android.app.StatusBarManager;
20import android.content.Context;
21
22import com.android.internal.annotations.VisibleForTesting;
23
24// TODO: Needed for move to system service: import com.android.internal.R;
25
26/**
27 * Manages the special status bar notifications used by the phone app.
28 */
29@VisibleForTesting
30public class StatusBarNotifier extends CallsManagerListenerBase {
31    private static final String SLOT_MUTE = "mute";
32    private static final String SLOT_SPEAKERPHONE = "speakerphone";
33
34    private final Context mContext;
35    private final CallsManager mCallsManager;
36    private final StatusBarManager mStatusBarManager;
37
38    private boolean mIsShowingMute;
39    private boolean mIsShowingSpeakerphone;
40
41    StatusBarNotifier(Context context, CallsManager callsManager) {
42        mContext = context;
43        mCallsManager = callsManager;
44        mStatusBarManager = (StatusBarManager) context.getSystemService(Context.STATUS_BAR_SERVICE);
45    }
46
47    /** ${inheritDoc} */
48    @Override
49    public void onCallRemoved(Call call) {
50        if (!mCallsManager.hasAnyCalls()) {
51            notifyMute(false);
52            notifySpeakerphone(false);
53        }
54    }
55
56    @VisibleForTesting
57    public void notifyMute(boolean isMuted) {
58        // Never display anything if there are no calls.
59        if (!mCallsManager.hasAnyCalls()) {
60            isMuted = false;
61        }
62
63        if (mIsShowingMute == isMuted) {
64            return;
65        }
66
67        Log.d(this, "Mute status bar icon being set to %b", isMuted);
68
69        if (isMuted) {
70            mStatusBarManager.setIcon(
71                    SLOT_MUTE,
72                    android.R.drawable.stat_notify_call_mute,
73                    0,  /* iconLevel */
74                    mContext.getString(R.string.accessibility_call_muted));
75        } else {
76            mStatusBarManager.removeIcon(SLOT_MUTE);
77        }
78        mIsShowingMute = isMuted;
79    }
80
81    @VisibleForTesting
82    public void notifySpeakerphone(boolean isSpeakerphone) {
83        // Never display anything if there are no calls.
84        if (!mCallsManager.hasAnyCalls()) {
85            isSpeakerphone = false;
86        }
87
88        if (mIsShowingSpeakerphone == isSpeakerphone) {
89            return;
90        }
91
92        Log.d(this, "Speakerphone status bar icon being set to %b", isSpeakerphone);
93
94        if (isSpeakerphone) {
95            mStatusBarManager.setIcon(
96                    SLOT_SPEAKERPHONE,
97                    android.R.drawable.stat_sys_speakerphone,
98                    0,  /* iconLevel */
99                    mContext.getString(R.string.accessibility_speakerphone_enabled));
100        } else {
101            mStatusBarManager.removeIcon(SLOT_SPEAKERPHONE);
102        }
103        mIsShowingSpeakerphone = isSpeakerphone;
104    }
105}
106