Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 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 | |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 18 | |
| 19 | import android.media.Ringtone; |
Hall Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 20 | import android.net.Uri; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 21 | import android.os.Handler; |
| 22 | import android.os.HandlerThread; |
| 23 | import android.os.Message; |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 24 | import android.telecom.Log; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 25 | |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 26 | import com.android.internal.annotations.VisibleForTesting; |
Hall Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 27 | import com.android.internal.os.SomeArgs; |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 28 | import com.android.internal.util.Preconditions; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 29 | |
| 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 Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 34 | @VisibleForTesting |
| 35 | public class AsyncRingtonePlayer { |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 36 | // Message codes used with the ringtone thread. |
Santos Cordon | b21c5da | 2014-09-12 04:52:02 -0700 | [diff] [blame] | 37 | 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 Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 43 | |
| 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 Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 51 | public void play(RingtoneFactory factory, Call incomingCall) { |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 52 | Log.d(this, "Posting play."); |
Hall Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 53 | SomeArgs args = SomeArgs.obtain(); |
| 54 | args.arg1 = factory; |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 55 | args.arg2 = incomingCall; |
Hall Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 56 | postMessage(EVENT_PLAY, true /* shouldCreateHandler */, args); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** Stops playing the ringtone. */ |
Brad Ebinger | d931a01 | 2015-10-21 12:54:08 -0700 | [diff] [blame] | 60 | public void stop() { |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 61 | Log.d(this, "Posting stop."); |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame] | 62 | postMessage(EVENT_STOP, false /* shouldCreateHandler */, null); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 63 | } |
| 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 Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 72 | private void postMessage(int messageCode, boolean shouldCreateHandler, SomeArgs args) { |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 73 | 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 Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 81 | mHandler.obtainMessage(messageCode, args).sendToTarget(); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 82 | } |
| 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 Wright | 76888bb | 2018-03-08 00:12:36 +0000 | [diff] [blame] | 95 | return new Handler(thread.getLooper(), null /*callback*/, true /*async*/) { |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 96 | @Override |
| 97 | public void handleMessage(Message msg) { |
| 98 | switch(msg.what) { |
| 99 | case EVENT_PLAY: |
Hall Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 100 | handlePlay((SomeArgs) msg.obj); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 101 | break; |
Santos Cordon | b21c5da | 2014-09-12 04:52:02 -0700 | [diff] [blame] | 102 | case EVENT_REPEAT: |
| 103 | handleRepeat(); |
| 104 | break; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 105 | 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 Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 116 | private void handlePlay(SomeArgs args) { |
| 117 | RingtoneFactory factory = (RingtoneFactory) args.arg1; |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 118 | Call incomingCall = (Call) args.arg2; |
Hall Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 119 | args.recycle(); |
Santos Cordon | b21c5da | 2014-09-12 04:52:02 -0700 | [diff] [blame] | 120 | // 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 Ebinger | ad54e7f | 2016-05-23 13:33:09 -0700 | [diff] [blame] | 125 | // 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 Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 132 | ThreadUtil.checkNotOnMainThread(); |
| 133 | Log.i(this, "Play ringtone."); |
| 134 | |
| 135 | if (mRingtone == null) { |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 136 | mRingtone = factory.getRingtone(incomingCall); |
Hall Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 137 | if (mRingtone == null) { |
Brad Ebinger | c9286f4 | 2016-03-10 16:02:54 -0800 | [diff] [blame] | 138 | Uri ringtoneUri = incomingCall.getRingtone(); |
| 139 | String ringtoneUriString = (ringtoneUri == null) ? "null" : |
| 140 | ringtoneUri.toSafeString(); |
Brad Ebinger | a3eccfe | 2016-10-05 15:45:22 -0700 | [diff] [blame] | 141 | Log.addEvent(null, LogUtils.Events.ERROR_LOG, "Failed to get ringtone from " + |
| 142 | "factory. Skipping ringing. Uri was: " + ringtoneUriString); |
Hall Liu | 8c89b20 | 2016-02-24 18:11:54 -0800 | [diff] [blame] | 143 | return; |
| 144 | } |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Santos Cordon | b21c5da | 2014-09-12 04:52:02 -0700 | [diff] [blame] | 147 | handleRepeat(); |
| 148 | } |
| 149 | |
| 150 | private void handleRepeat() { |
| 151 | if (mRingtone == null) { |
| 152 | return; |
| 153 | } |
| 154 | |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 155 | if (mRingtone.isPlaying()) { |
| 156 | Log.d(this, "Ringtone already playing."); |
| 157 | } else { |
| 158 | mRingtone.play(); |
Santos Cordon | b21c5da | 2014-09-12 04:52:02 -0700 | [diff] [blame] | 159 | 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 Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 167 | } |
| 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 Cordon | b21c5da | 2014-09-12 04:52:02 -0700 | [diff] [blame] | 184 | // 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 Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 188 | if (mHandler.hasMessages(EVENT_PLAY)) { |
Santos Cordon | b21c5da | 2014-09-12 04:52:02 -0700 | [diff] [blame] | 189 | Log.v(this, "Keeping alive ringtone thread for subsequent play request."); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 190 | } else { |
| 191 | mHandler.removeMessages(EVENT_STOP); |
| 192 | mHandler.getLooper().quitSafely(); |
| 193 | mHandler = null; |
| 194 | Log.v(this, "Handler cleared."); |
| 195 | } |
| 196 | } |
| 197 | } |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 198 | } |