1deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon/*
2deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon * Copyright (C) 2014 The Android Open Source Project
3deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon *
4deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon * Licensed under the Apache License, Version 2.0 (the "License");
5deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon * you may not use this file except in compliance with the License.
6deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon * You may obtain a copy of the License at
7deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon *
8deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon *      http://www.apache.org/licenses/LICENSE-2.0
9deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon *
10deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon * Unless required by applicable law or agreed to in writing, software
11deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon * distributed under the License is distributed on an "AS IS" BASIS,
12deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon * See the License for the specific language governing permissions and
14deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon * limitations under the License.
15deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon */
16deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
177cc70b4f0ad1064a4a0dce6056ad82b205887160Tyler Gunnpackage com.android.server.telecom;
18deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
19deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordonimport android.app.StatusBarManager;
20deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordonimport android.content.Context;
21a3eccfee788c3ac3c831a443b085b141b39bb63dBrad Ebingerimport android.telecom.Log;
22deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
23f62630a57de0d52be2bdbc92a9bf8f305cc0892dHall Liuimport com.android.internal.annotations.VisibleForTesting;
24f62630a57de0d52be2bdbc92a9bf8f305cc0892dHall Liu
2591d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn// TODO: Needed for move to system service: import com.android.internal.R;
2691d43cf9c985cc5a83795f256ef5c46ebb8fbdc1Tyler Gunn
27deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon/**
28deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon * Manages the special status bar notifications used by the phone app.
29deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon */
30f62630a57de0d52be2bdbc92a9bf8f305cc0892dHall Liu@VisibleForTesting
31f62630a57de0d52be2bdbc92a9bf8f305cc0892dHall Liupublic class StatusBarNotifier extends CallsManagerListenerBase {
32deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    private static final String SLOT_MUTE = "mute";
33deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    private static final String SLOT_SPEAKERPHONE = "speakerphone";
34deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
35deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    private final Context mContext;
36deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    private final CallsManager mCallsManager;
37deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    private final StatusBarManager mStatusBarManager;
38deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
39deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    private boolean mIsShowingMute;
40deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    private boolean mIsShowingSpeakerphone;
41deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
42deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    StatusBarNotifier(Context context, CallsManager callsManager) {
43deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        mContext = context;
44deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        mCallsManager = callsManager;
45deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        mStatusBarManager = (StatusBarManager) context.getSystemService(Context.STATUS_BAR_SERVICE);
46deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    }
47deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
48deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    /** ${inheritDoc} */
49deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    @Override
50deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    public void onCallRemoved(Call call) {
51deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        if (!mCallsManager.hasAnyCalls()) {
52deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon            notifyMute(false);
53deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon            notifySpeakerphone(false);
54deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        }
55deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    }
56deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
57f62630a57de0d52be2bdbc92a9bf8f305cc0892dHall Liu    @VisibleForTesting
58f62630a57de0d52be2bdbc92a9bf8f305cc0892dHall Liu    public void notifyMute(boolean isMuted) {
59deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        // Never display anything if there are no calls.
60deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        if (!mCallsManager.hasAnyCalls()) {
61deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon            isMuted = false;
62deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        }
63deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
64deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        if (mIsShowingMute == isMuted) {
65deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon            return;
66deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        }
67deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
68deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        Log.d(this, "Mute status bar icon being set to %b", isMuted);
69deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
70deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        if (isMuted) {
71deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon            mStatusBarManager.setIcon(
72deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon                    SLOT_MUTE,
73deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon                    android.R.drawable.stat_notify_call_mute,
74deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon                    0,  /* iconLevel */
75deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon                    mContext.getString(R.string.accessibility_call_muted));
76deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        } else {
77deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon            mStatusBarManager.removeIcon(SLOT_MUTE);
78deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        }
79deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        mIsShowingMute = isMuted;
80deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    }
81deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
82f62630a57de0d52be2bdbc92a9bf8f305cc0892dHall Liu    @VisibleForTesting
83f62630a57de0d52be2bdbc92a9bf8f305cc0892dHall Liu    public void notifySpeakerphone(boolean isSpeakerphone) {
84deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        // Never display anything if there are no calls.
85deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        if (!mCallsManager.hasAnyCalls()) {
86deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon            isSpeakerphone = false;
87deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        }
88deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
89deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        if (mIsShowingSpeakerphone == isSpeakerphone) {
90deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon            return;
91deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        }
92deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
93deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        Log.d(this, "Speakerphone status bar icon being set to %b", isSpeakerphone);
94deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon
95deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        if (isSpeakerphone) {
96deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon            mStatusBarManager.setIcon(
97deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon                    SLOT_SPEAKERPHONE,
98deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon                    android.R.drawable.stat_sys_speakerphone,
99deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon                    0,  /* iconLevel */
100deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon                    mContext.getString(R.string.accessibility_speakerphone_enabled));
101deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        } else {
102deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon            mStatusBarManager.removeIcon(SLOT_SPEAKERPHONE);
103deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        }
104deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon        mIsShowingSpeakerphone = isSpeakerphone;
105deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon    }
106deb8c89707c604d4f9f32e476a58bd10a68293ffSantos Cordon}
107