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
17package com.android.server.telecom;
18
19import android.content.Context;
20import android.media.AudioManager;
21import android.media.ToneGenerator;
22import android.os.Handler;
23import android.os.HandlerThread;
24import android.os.Looper;
25import android.os.Message;
26import android.provider.Settings;
27import android.telecom.Log;
28import android.telecom.Logging.Session;
29
30import 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 */
40public 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.getInt(
197                    context.getContentResolver(), Settings.System.DTMF_TONE_WHEN_DIALING, 1) == 1;
198        } else {
199            areLocalTonesEnabled = false;
200        }
201
202        mCall = call;
203
204        if (areLocalTonesEnabled) {
205            Log.d(this, "Posting create.");
206            getHandler().sendMessage(
207                    getHandler().obtainMessage(EVENT_START_SESSION, Log.createSubsession()));
208        }
209    }
210
211    /**
212     * Releases resources needed for playing local dtmf tones.
213     *
214     * @param call The call associated with the session to end.
215     */
216    private void endDtmfSession(Call call) {
217        if (call != null && mCall == call) {
218            // Do a stopTone() in case the sessions ends before we are told to stop the tone.
219            stopTone(call);
220
221            mCall = null;
222            Log.d(this, "Posting delete.");
223            getHandler().sendMessage(
224                    getHandler().obtainMessage(EVENT_END_SESSION, Log.createSubsession()));
225        }
226    }
227
228    /**
229     * Creates a new ToneHandler on a separate thread if none exists, and returns it.
230     * No need for locking, since everything that calls this is protected by the Telecom lock.
231     */
232    @VisibleForTesting
233    public ToneHandler getHandler() {
234        if (mHandler == null) {
235            HandlerThread thread = new HandlerThread("tonegenerator-dtmf");
236            thread.start();
237            mHandler = new ToneHandler(thread.getLooper());
238        }
239        return mHandler;
240    }
241
242    private static int getMappedTone(char digit) {
243        if (digit >= '0' && digit <= '9') {
244            return ToneGenerator.TONE_DTMF_0 + digit - '0';
245        } else if (digit == '#') {
246            return ToneGenerator.TONE_DTMF_P;
247        } else if (digit == '*') {
248            return ToneGenerator.TONE_DTMF_S;
249        }
250        return ToneGenerator.TONE_UNKNOWN;
251    }
252}
253