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