1 /*
2  * Copyright 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 
17 package com.android.server.telecom;
18 
19 import android.content.Context;
20 import android.media.AudioManager;
21 import android.media.ToneGenerator;
22 import android.os.Handler;
23 import android.os.HandlerThread;
24 import android.os.Looper;
25 import android.os.Message;
26 import android.provider.Settings;
27 import android.telecom.Log;
28 import android.telecom.Logging.Session;
29 
30 import com.android.internal.annotations.VisibleForTesting;
31 
32 // TODO: Needed for move to system service: import com.android.internal.R;
33 
34 /**
35  * Plays DTMF tones locally for the caller to hear. In order to reduce (1) the amount of times we
36  * check the "play local tones" setting and (2) the length of time we keep the tone generator, this
37  * class employs a concept of a call "session" that starts and stops when the foreground call
38  * changes.
39  */
40 public class DtmfLocalTonePlayer {
41     public static class ToneGeneratorProxy {
42         /** Generator used to actually play the tone. */
43         private ToneGenerator mToneGenerator;
44 
45         public void create() {
46             if (mToneGenerator == null) {
47                 try {
48                     mToneGenerator = new ToneGenerator(AudioManager.STREAM_DTMF, 80);
49                 } catch (RuntimeException e) {
50                     Log.e(this, e, "Error creating local tone generator.");
51                     mToneGenerator = null;
52                 }
53             }
54         }
55 
56         public void release() {
57             if (mToneGenerator != null) {
58                 mToneGenerator.release();
59                 mToneGenerator = null;
60             }
61         }
62 
63         public boolean isPresent() {
64             return mToneGenerator != null;
65         }
66 
67         public void startTone(int toneType, int durationMs) {
68             if (mToneGenerator != null) {
69                 mToneGenerator.startTone(toneType, durationMs);
70             }
71         }
72 
73         public void stopTone() {
74             if (mToneGenerator != null) {
75                 mToneGenerator.stopTone();
76             }
77         }
78     }
79 
80     private final class ToneHandler extends Handler {
81 
82         public ToneHandler(Looper looper) {
83             super(looper);
84         }
85 
86         @Override
87         public void handleMessage(Message msg) {
88             try {
89                 if (msg.obj instanceof Session) {
90                     Log.continueSession((Session) msg.obj, "DLTP.TH");
91                 }
92 
93                 switch (msg.what) {
94                     case EVENT_START_SESSION:
95                         mToneGeneratorProxy.create();
96                         break;
97                     case EVENT_END_SESSION:
98                         mToneGeneratorProxy.release();
99                         break;
100                     case EVENT_PLAY_TONE:
101                         char c = (char) msg.arg1;
102                         if (!mToneGeneratorProxy.isPresent()) {
103                             Log.d(this, "playTone: no tone generator, %c.", c);
104                         } else {
105                             Log.d(this, "starting local tone: %c.", c);
106                             int tone = getMappedTone(c);
107                             if (tone != ToneGenerator.TONE_UNKNOWN) {
108                                 mToneGeneratorProxy.startTone(tone, -1 /* toneDuration */);
109                             }
110                         }
111                         break;
112                     case EVENT_STOP_TONE:
113                         if (mToneGeneratorProxy.isPresent()) {
114                             mToneGeneratorProxy.stopTone();
115                         }
116                         break;
117                     default:
118                         Log.w(this, "Unknown message: %d", msg.what);
119                         break;
120                 }
121             } finally {
122                 Log.endSession();
123             }
124         }
125     }
126 
127     /** The current call associated with an existing dtmf session. */
128     private Call mCall;
129 
130     /**
131      * Message codes to be used for creating and deleting ToneGenerator object in the tonegenerator
132      * thread, as well as for actually playing the tones.
133      */
134     private static final int EVENT_START_SESSION = 1;
135     private static final int EVENT_END_SESSION = 2;
136     private static final int EVENT_PLAY_TONE = 3;
137     private static final int EVENT_STOP_TONE = 4;
138 
139     /** Handler running on the tonegenerator thread. */
140     private ToneHandler mHandler;
141 
142     private final ToneGeneratorProxy mToneGeneratorProxy;
143 
144     public DtmfLocalTonePlayer(ToneGeneratorProxy toneGeneratorProxy) {
145         mToneGeneratorProxy = toneGeneratorProxy;
146     }
147 
148     public void onForegroundCallChanged(Call oldForegroundCall, Call newForegroundCall) {
149         endDtmfSession(oldForegroundCall);
150         startDtmfSession(newForegroundCall);
151     }
152 
153     /**
154      * Starts playing the dtmf tone specified by c.
155      *
156      * @param call The associated call.
157      * @param c The digit to play.
158      */
159     public void playTone(Call call, char c) {
160         // Do nothing if it is not the right call.
161         if (mCall != call) {
162             return;
163         }
164 
165         getHandler().sendMessage(
166                 getHandler().obtainMessage(EVENT_PLAY_TONE, (int) c, 0, Log.createSubsession()));
167     }
168 
169     /**
170      * Stops any currently playing dtmf tone.
171      *
172      * @param call The associated call.
173      */
174     public void stopTone(Call call) {
175         // Do nothing if it's not the right call.
176         if (mCall != call) {
177             return;
178         }
179 
180         getHandler().sendMessage(
181                 getHandler().obtainMessage(EVENT_STOP_TONE, Log.createSubsession()));
182     }
183 
184     /**
185      * Runs initialization requires to play local tones during a call.
186      *
187      * @param call The call associated with this dtmf session.
188      */
189     private void startDtmfSession(Call call) {
190         if (call == null) {
191             return;
192         }
193         final Context context = call.getContext();
194         final boolean areLocalTonesEnabled;
195         if (context.getResources().getBoolean(R.bool.allow_local_dtmf_tones)) {
196             areLocalTonesEnabled = Settings.System.getIntForUser(
197                     context.getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING, 1,
198                     context.getUserId()) == 1;
199         } else {
200             areLocalTonesEnabled = false;
201         }
202 
203         mCall = call;
204 
205         if (areLocalTonesEnabled) {
206             Log.d(this, "Posting create.");
207             getHandler().sendMessage(
208                     getHandler().obtainMessage(EVENT_START_SESSION, Log.createSubsession()));
209         }
210     }
211 
212     /**
213      * Releases resources needed for playing local dtmf tones.
214      *
215      * @param call The call associated with the session to end.
216      */
217     private void endDtmfSession(Call call) {
218         if (call != null && mCall == call) {
219             // Do a stopTone() in case the sessions ends before we are told to stop the tone.
220             stopTone(call);
221 
222             mCall = null;
223             Log.d(this, "Posting delete.");
224             getHandler().sendMessage(
225                     getHandler().obtainMessage(EVENT_END_SESSION, Log.createSubsession()));
226         }
227     }
228 
229     /**
230      * Creates a new ToneHandler on a separate thread if none exists, and returns it.
231      * No need for locking, since everything that calls this is protected by the Telecom lock.
232      */
233     @VisibleForTesting
234     public ToneHandler getHandler() {
235         if (mHandler == null) {
236             HandlerThread thread = new HandlerThread("tonegenerator-dtmf");
237             thread.start();
238             mHandler = new ToneHandler(thread.getLooper());
239         }
240         return mHandler;
241     }
242 
243     private static int getMappedTone(char digit) {
244         if (digit >= '0' && digit <= '9') {
245             return ToneGenerator.TONE_DTMF_0 + digit - '0';
246         } else if (digit == '#') {
247             return ToneGenerator.TONE_DTMF_P;
248         } else if (digit == '*') {
249             return ToneGenerator.TONE_DTMF_S;
250         }
251         return ToneGenerator.TONE_UNKNOWN;
252     }
253 }
254