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