1 /*
2  * Copyright 2019, 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 
17 package com.android.server.telecom.ui;
18 
19 import android.app.Notification;
20 import android.app.NotificationManager;
21 import android.content.Context;
22 import android.telecom.Log;
23 
24 import com.android.server.telecom.Call;
25 import com.android.server.telecom.CallState;
26 import com.android.server.telecom.CallsManagerListenerBase;
27 import com.android.server.telecom.R;
28 
29 /**
30  * Displays a persistent notification whenever there's a call in the AUDIO_PROCESSING state so that
31  * the user is aware that there's some app
32  */
33 public class AudioProcessingNotification extends CallsManagerListenerBase {
34 
35     private static final int AUDIO_PROCESSING_NOTIFICATION_ID = 2;
36     private static final String NOTIFICATION_TAG =
37             AudioProcessingNotification.class.getSimpleName();
38 
39     private final Context mContext;
40     private final NotificationManager mNotificationManager;
41     private Call mCallInAudioProcessing;
42 
AudioProcessingNotification(Context context)43     public AudioProcessingNotification(Context context) {
44         mContext = context;
45         mNotificationManager =
46                 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
47     }
48 
49     @Override
onCallStateChanged(Call call, int oldState, int newState)50     public void onCallStateChanged(Call call, int oldState, int newState) {
51         if (newState == CallState.AUDIO_PROCESSING && oldState != CallState.AUDIO_PROCESSING) {
52             showAudioProcessingNotification(call);
53         } else if (oldState == CallState.AUDIO_PROCESSING
54                 && newState != CallState.AUDIO_PROCESSING) {
55             cancelAudioProcessingNotification();
56         }
57     }
58 
59     @Override
onCallAdded(Call call)60     public void onCallAdded(Call call) {
61         if (call.getState() == CallState.AUDIO_PROCESSING) {
62             showAudioProcessingNotification(call);
63         }
64     }
65 
66     @Override
onCallRemoved(Call call)67     public void onCallRemoved(Call call) {
68         if (call == mCallInAudioProcessing) {
69             cancelAudioProcessingNotification();
70         }
71     }
72 
73     /**
74      * Create a system notification for the audio processing call.
75      *
76      * @param call The missed call.
77      */
showAudioProcessingNotification(Call call)78     private void showAudioProcessingNotification(Call call) {
79         Log.i(this, "showAudioProcessingNotification");
80         mCallInAudioProcessing = call;
81 
82         Notification.Builder builder = new Notification.Builder(mContext,
83                 NotificationChannelManager.CHANNEL_ID_AUDIO_PROCESSING);
84         builder.setSmallIcon(R.drawable.ic_phone)
85                 .setColor(mContext.getResources().getColor(R.color.theme_color))
86                 .setContentTitle(mContext.getText(R.string.notification_audioProcessing_title))
87                 .setStyle(new Notification.BigTextStyle()
88                         .bigText(mContext.getString(
89                                 R.string.notification_audioProcessing_body,
90                                 call.getAudioProcessingRequestingApp())))
91                 .setOngoing(true);
92 
93         Notification notification = builder.build();
94 
95         mNotificationManager.notify(
96                 NOTIFICATION_TAG, AUDIO_PROCESSING_NOTIFICATION_ID, notification);
97     }
98 
99     /** Cancels the audio processing notification. */
cancelAudioProcessingNotification()100     private void cancelAudioProcessingNotification() {
101         mNotificationManager.cancel(NOTIFICATION_TAG, AUDIO_PROCESSING_NOTIFICATION_ID);
102     }
103 }
104