blob: 8a7be4dd0a5fdc8950ac2dfc9bae70cae4c9f508 [file] [log] [blame]
Santos Cordon1ae2b852014-03-19 03:03:10 -07001/*
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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Santos Cordon1ae2b852014-03-19 03:03:10 -070018
19import android.media.Ringtone;
Hall Liu8c89b202016-02-24 18:11:54 -080020import android.net.Uri;
Santos Cordon1ae2b852014-03-19 03:03:10 -070021import android.os.Handler;
22import android.os.HandlerThread;
23import android.os.Message;
Brad Ebingera3eccfe2016-10-05 15:45:22 -070024import android.telecom.Log;
Santos Cordon1ae2b852014-03-19 03:03:10 -070025
Brad Ebingerd931a012015-10-21 12:54:08 -070026import com.android.internal.annotations.VisibleForTesting;
Hall Liu8c89b202016-02-24 18:11:54 -080027import com.android.internal.os.SomeArgs;
Tyler Gunn91d43cf2014-09-17 12:19:39 -070028import com.android.internal.util.Preconditions;
Santos Cordon1ae2b852014-03-19 03:03:10 -070029
30/**
31 * Plays the default ringtone. Uses {@link Ringtone} in a separate thread so that this class can be
32 * used from the main thread.
33 */
Brad Ebingerd931a012015-10-21 12:54:08 -070034@VisibleForTesting
35public class AsyncRingtonePlayer {
Santos Cordon1ae2b852014-03-19 03:03:10 -070036 // Message codes used with the ringtone thread.
Santos Cordonb21c5da2014-09-12 04:52:02 -070037 private static final int EVENT_PLAY = 1;
38 private static final int EVENT_STOP = 2;
39 private static final int EVENT_REPEAT = 3;
40
41 // The interval in which to restart the ringer.
42 private static final int RESTART_RINGER_MILLIS = 3000;
Santos Cordon1ae2b852014-03-19 03:03:10 -070043
44 /** Handler running on the ringtone thread. */
45 private Handler mHandler;
46
47 /** The current ringtone. Only used by the ringtone thread. */
48 private Ringtone mRingtone;
49
50 /** Plays the ringtone. */
Brad Ebingerc9286f42016-03-10 16:02:54 -080051 public void play(RingtoneFactory factory, Call incomingCall) {
Santos Cordon1ae2b852014-03-19 03:03:10 -070052 Log.d(this, "Posting play.");
Hall Liu8c89b202016-02-24 18:11:54 -080053 SomeArgs args = SomeArgs.obtain();
54 args.arg1 = factory;
Brad Ebingerc9286f42016-03-10 16:02:54 -080055 args.arg2 = incomingCall;
Hall Liu8c89b202016-02-24 18:11:54 -080056 postMessage(EVENT_PLAY, true /* shouldCreateHandler */, args);
Santos Cordon1ae2b852014-03-19 03:03:10 -070057 }
58
59 /** Stops playing the ringtone. */
Brad Ebingerd931a012015-10-21 12:54:08 -070060 public void stop() {
Santos Cordon1ae2b852014-03-19 03:03:10 -070061 Log.d(this, "Posting stop.");
Santos Cordon5ba7f272014-05-28 13:59:49 -070062 postMessage(EVENT_STOP, false /* shouldCreateHandler */, null);
Santos Cordon1ae2b852014-03-19 03:03:10 -070063 }
64
65 /**
66 * Posts a message to the ringtone-thread handler. Creates the handler if specified by the
67 * parameter shouldCreateHandler.
68 *
69 * @param messageCode The message to post.
70 * @param shouldCreateHandler True when a handler should be created to handle this message.
71 */
Hall Liu8c89b202016-02-24 18:11:54 -080072 private void postMessage(int messageCode, boolean shouldCreateHandler, SomeArgs args) {
Santos Cordon1ae2b852014-03-19 03:03:10 -070073 synchronized(this) {
74 if (mHandler == null && shouldCreateHandler) {
75 mHandler = getNewHandler();
76 }
77
78 if (mHandler == null) {
79 Log.d(this, "Message %d skipped because there is no handler.", messageCode);
80 } else {
Hall Liu8c89b202016-02-24 18:11:54 -080081 mHandler.obtainMessage(messageCode, args).sendToTarget();
Santos Cordon1ae2b852014-03-19 03:03:10 -070082 }
83 }
84 }
85
86 /**
87 * Creates a new ringtone Handler running in its own thread.
88 */
89 private Handler getNewHandler() {
90 Preconditions.checkState(mHandler == null);
91
92 HandlerThread thread = new HandlerThread("ringtone-player");
93 thread.start();
94
Michael Wright76888bb2018-03-08 00:12:36 +000095 return new Handler(thread.getLooper(), null /*callback*/, true /*async*/) {
Santos Cordon1ae2b852014-03-19 03:03:10 -070096 @Override
97 public void handleMessage(Message msg) {
98 switch(msg.what) {
99 case EVENT_PLAY:
Hall Liu8c89b202016-02-24 18:11:54 -0800100 handlePlay((SomeArgs) msg.obj);
Santos Cordon1ae2b852014-03-19 03:03:10 -0700101 break;
Santos Cordonb21c5da2014-09-12 04:52:02 -0700102 case EVENT_REPEAT:
103 handleRepeat();
104 break;
Santos Cordon1ae2b852014-03-19 03:03:10 -0700105 case EVENT_STOP:
106 handleStop();
107 break;
108 }
109 }
110 };
111 }
112
113 /**
114 * Starts the actual playback of the ringtone. Executes on ringtone-thread.
115 */
Hall Liu8c89b202016-02-24 18:11:54 -0800116 private void handlePlay(SomeArgs args) {
117 RingtoneFactory factory = (RingtoneFactory) args.arg1;
Brad Ebingerc9286f42016-03-10 16:02:54 -0800118 Call incomingCall = (Call) args.arg2;
Hall Liu8c89b202016-02-24 18:11:54 -0800119 args.recycle();
Santos Cordonb21c5da2014-09-12 04:52:02 -0700120 // don't bother with any of this if there is an EVENT_STOP waiting.
121 if (mHandler.hasMessages(EVENT_STOP)) {
122 return;
123 }
124
Brad Ebingerad54e7f2016-05-23 13:33:09 -0700125 // If the Ringtone Uri is EMPTY, then the "None" Ringtone has been selected. Do not play
126 // anything.
127 if(Uri.EMPTY.equals(incomingCall.getRingtone())) {
128 mRingtone = null;
129 return;
130 }
131
Santos Cordon1ae2b852014-03-19 03:03:10 -0700132 ThreadUtil.checkNotOnMainThread();
133 Log.i(this, "Play ringtone.");
134
135 if (mRingtone == null) {
Brad Ebingerc9286f42016-03-10 16:02:54 -0800136 mRingtone = factory.getRingtone(incomingCall);
Hall Liu8c89b202016-02-24 18:11:54 -0800137 if (mRingtone == null) {
Brad Ebingerc9286f42016-03-10 16:02:54 -0800138 Uri ringtoneUri = incomingCall.getRingtone();
139 String ringtoneUriString = (ringtoneUri == null) ? "null" :
140 ringtoneUri.toSafeString();
Brad Ebingera3eccfe2016-10-05 15:45:22 -0700141 Log.addEvent(null, LogUtils.Events.ERROR_LOG, "Failed to get ringtone from " +
142 "factory. Skipping ringing. Uri was: " + ringtoneUriString);
Hall Liu8c89b202016-02-24 18:11:54 -0800143 return;
144 }
Santos Cordon1ae2b852014-03-19 03:03:10 -0700145 }
146
Santos Cordonb21c5da2014-09-12 04:52:02 -0700147 handleRepeat();
148 }
149
150 private void handleRepeat() {
151 if (mRingtone == null) {
152 return;
153 }
154
Santos Cordon1ae2b852014-03-19 03:03:10 -0700155 if (mRingtone.isPlaying()) {
156 Log.d(this, "Ringtone already playing.");
157 } else {
158 mRingtone.play();
Santos Cordonb21c5da2014-09-12 04:52:02 -0700159 Log.i(this, "Repeat ringtone.");
160 }
161
162 // Repost event to restart ringer in {@link RESTART_RINGER_MILLIS}.
163 synchronized(this) {
164 if (!mHandler.hasMessages(EVENT_REPEAT)) {
165 mHandler.sendEmptyMessageDelayed(EVENT_REPEAT, RESTART_RINGER_MILLIS);
166 }
Santos Cordon1ae2b852014-03-19 03:03:10 -0700167 }
168 }
169
170 /**
171 * Stops the playback of the ringtone. Executes on the ringtone-thread.
172 */
173 private void handleStop() {
174 ThreadUtil.checkNotOnMainThread();
175 Log.i(this, "Stop ringtone.");
176
177 if (mRingtone != null) {
178 Log.d(this, "Ringtone.stop() invoked.");
179 mRingtone.stop();
180 mRingtone = null;
181 }
182
183 synchronized(this) {
Santos Cordonb21c5da2014-09-12 04:52:02 -0700184 // At the time that STOP is handled, there should be no need for repeat messages in the
185 // queue.
186 mHandler.removeMessages(EVENT_REPEAT);
187
Santos Cordon1ae2b852014-03-19 03:03:10 -0700188 if (mHandler.hasMessages(EVENT_PLAY)) {
Santos Cordonb21c5da2014-09-12 04:52:02 -0700189 Log.v(this, "Keeping alive ringtone thread for subsequent play request.");
Santos Cordon1ae2b852014-03-19 03:03:10 -0700190 } else {
191 mHandler.removeMessages(EVENT_STOP);
192 mHandler.getLooper().quitSafely();
193 mHandler = null;
194 Log.v(this, "Handler cleared.");
195 }
196 }
197 }
Santos Cordon1ae2b852014-03-19 03:03:10 -0700198}