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 | |
| 17 | package com.android.telecomm; |
| 18 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 19 | import android.media.AudioManager; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 20 | import android.media.Ringtone; |
| 21 | import android.media.RingtoneManager; |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame^] | 22 | import android.net.Uri; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 23 | import android.os.Handler; |
| 24 | import android.os.HandlerThread; |
| 25 | import android.os.Message; |
| 26 | import android.provider.Settings; |
| 27 | |
| 28 | import com.google.common.base.Preconditions; |
| 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 | */ |
| 34 | class AsyncRingtonePlayer { |
| 35 | // Message codes used with the ringtone thread. |
| 36 | static final int EVENT_PLAY = 1; |
| 37 | static final int EVENT_STOP = 2; |
| 38 | |
| 39 | /** Handler running on the ringtone thread. */ |
| 40 | private Handler mHandler; |
| 41 | |
| 42 | /** The current ringtone. Only used by the ringtone thread. */ |
| 43 | private Ringtone mRingtone; |
| 44 | |
| 45 | /** Plays the ringtone. */ |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame^] | 46 | void play(Uri ringtone) { |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 47 | Log.d(this, "Posting play."); |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame^] | 48 | postMessage(EVENT_PLAY, true /* shouldCreateHandler */, ringtone); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** Stops playing the ringtone. */ |
| 52 | void stop() { |
| 53 | Log.d(this, "Posting stop."); |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame^] | 54 | postMessage(EVENT_STOP, false /* shouldCreateHandler */, null); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Posts a message to the ringtone-thread handler. Creates the handler if specified by the |
| 59 | * parameter shouldCreateHandler. |
| 60 | * |
| 61 | * @param messageCode The message to post. |
| 62 | * @param shouldCreateHandler True when a handler should be created to handle this message. |
| 63 | */ |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame^] | 64 | private void postMessage(int messageCode, boolean shouldCreateHandler, Uri ringtone) { |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 65 | synchronized(this) { |
| 66 | if (mHandler == null && shouldCreateHandler) { |
| 67 | mHandler = getNewHandler(); |
| 68 | } |
| 69 | |
| 70 | if (mHandler == null) { |
| 71 | Log.d(this, "Message %d skipped because there is no handler.", messageCode); |
| 72 | } else { |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame^] | 73 | mHandler.obtainMessage(messageCode, ringtone).sendToTarget(); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Creates a new ringtone Handler running in its own thread. |
| 80 | */ |
| 81 | private Handler getNewHandler() { |
| 82 | Preconditions.checkState(mHandler == null); |
| 83 | |
| 84 | HandlerThread thread = new HandlerThread("ringtone-player"); |
| 85 | thread.start(); |
| 86 | |
| 87 | return new Handler(thread.getLooper()) { |
| 88 | @Override |
| 89 | public void handleMessage(Message msg) { |
| 90 | switch(msg.what) { |
| 91 | case EVENT_PLAY: |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame^] | 92 | handlePlay((Uri) msg.obj); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 93 | break; |
| 94 | case EVENT_STOP: |
| 95 | handleStop(); |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | }; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Starts the actual playback of the ringtone. Executes on ringtone-thread. |
| 104 | */ |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame^] | 105 | private void handlePlay(Uri ringtoneUri) { |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 106 | ThreadUtil.checkNotOnMainThread(); |
| 107 | Log.i(this, "Play ringtone."); |
| 108 | |
| 109 | if (mRingtone == null) { |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame^] | 110 | mRingtone = getRingtone(ringtoneUri); |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 111 | |
| 112 | // Cancel everything if there is no ringtone. |
| 113 | if (mRingtone == null) { |
| 114 | handleStop(); |
| 115 | return; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (mRingtone.isPlaying()) { |
| 120 | Log.d(this, "Ringtone already playing."); |
| 121 | } else { |
| 122 | mRingtone.play(); |
| 123 | Log.d(this, "Ringtone.play() invoked."); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Stops the playback of the ringtone. Executes on the ringtone-thread. |
| 129 | */ |
| 130 | private void handleStop() { |
| 131 | ThreadUtil.checkNotOnMainThread(); |
| 132 | Log.i(this, "Stop ringtone."); |
| 133 | |
| 134 | if (mRingtone != null) { |
| 135 | Log.d(this, "Ringtone.stop() invoked."); |
| 136 | mRingtone.stop(); |
| 137 | mRingtone = null; |
| 138 | } |
| 139 | |
| 140 | synchronized(this) { |
| 141 | if (mHandler.hasMessages(EVENT_PLAY)) { |
| 142 | Log.v(this, "Keeping alive ringtone thread for pending messages."); |
| 143 | } else { |
| 144 | mHandler.removeMessages(EVENT_STOP); |
| 145 | mHandler.getLooper().quitSafely(); |
| 146 | mHandler = null; |
| 147 | Log.v(this, "Handler cleared."); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
Santos Cordon | 5ba7f27 | 2014-05-28 13:59:49 -0700 | [diff] [blame^] | 152 | private Ringtone getRingtone(Uri ringtoneUri) { |
| 153 | if (ringtoneUri == null) { |
| 154 | ringtoneUri = Settings.System.DEFAULT_RINGTONE_URI; |
| 155 | } |
| 156 | |
| 157 | Ringtone ringtone = RingtoneManager.getRingtone(TelecommApp.getInstance(), ringtoneUri); |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 158 | ringtone.setStreamType(AudioManager.STREAM_RING); |
| 159 | return ringtone; |
Santos Cordon | 1ae2b85 | 2014-03-19 03:03:10 -0700 | [diff] [blame] | 160 | } |
| 161 | } |