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