blob: ab612dd93dd135d45daa17a8949a37aad5545668 [file] [log] [blame]
Jeff Sharkey098d5802012-04-26 17:30:34 -07001/*
2 * Copyright (C) 2012 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.systemui.media;
18
Jeff Sharkey783ee0c2016-03-05 17:23:28 -070019import android.content.ContentResolver;
Jeff Sharkey098d5802012-04-26 17:30:34 -070020import android.content.Context;
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -070021import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkey783ee0c2016-03-05 17:23:28 -070022import android.database.Cursor;
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -070023import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070024import android.media.IAudioService;
25import android.media.IRingtonePlayer;
26import android.media.Ringtone;
27import android.net.Uri;
28import android.os.Binder;
29import android.os.IBinder;
Jeff Sharkey783ee0c2016-03-05 17:23:28 -070030import android.os.ParcelFileDescriptor;
Jeff Sharkey098d5802012-04-26 17:30:34 -070031import android.os.Process;
32import android.os.RemoteException;
33import android.os.ServiceManager;
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -070034import android.os.UserHandle;
Jeff Sharkey783ee0c2016-03-05 17:23:28 -070035import android.provider.MediaStore;
36import android.provider.MediaStore.Audio.AudioColumns;
John Spurlockcd686b52013-06-05 10:13:46 -040037import android.util.Log;
Jeff Sharkey098d5802012-04-26 17:30:34 -070038
Jeff Sharkey783ee0c2016-03-05 17:23:28 -070039import com.android.internal.util.Preconditions;
Jeff Sharkey098d5802012-04-26 17:30:34 -070040import com.android.systemui.SystemUI;
Jeff Sharkey098d5802012-04-26 17:30:34 -070041
42import java.io.FileDescriptor;
Jeff Sharkey783ee0c2016-03-05 17:23:28 -070043import java.io.IOException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070044import java.io.PrintWriter;
45import java.util.HashMap;
46
47/**
48 * Service that offers to play ringtones by {@link Uri}, since our process has
49 * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE}.
50 */
51public class RingtonePlayer extends SystemUI {
52 private static final String TAG = "RingtonePlayer";
Jeff Sharkeyb6e404a2012-05-15 11:36:11 -070053 private static final boolean LOGD = false;
Jeff Sharkey098d5802012-04-26 17:30:34 -070054
55 // TODO: support Uri switching under same IBinder
56
57 private IAudioService mAudioService;
58
59 private final NotificationPlayer mAsyncPlayer = new NotificationPlayer(TAG);
John Spurlockb8baccc2013-06-05 12:59:01 -040060 private final HashMap<IBinder, Client> mClients = new HashMap<IBinder, Client>();
Jeff Sharkey098d5802012-04-26 17:30:34 -070061
62 @Override
63 public void start() {
64 mAsyncPlayer.setUsesWakeLock(mContext);
65
66 mAudioService = IAudioService.Stub.asInterface(
67 ServiceManager.getService(Context.AUDIO_SERVICE));
68 try {
69 mAudioService.setRingtonePlayer(mCallback);
70 } catch (RemoteException e) {
John Spurlockcd686b52013-06-05 10:13:46 -040071 Log.e(TAG, "Problem registering RingtonePlayer: " + e);
Jeff Sharkey098d5802012-04-26 17:30:34 -070072 }
73 }
74
75 /**
76 * Represents an active remote {@link Ringtone} client.
77 */
78 private class Client implements IBinder.DeathRecipient {
79 private final IBinder mToken;
80 private final Ringtone mRingtone;
81
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -070082 public Client(IBinder token, Uri uri, UserHandle user, AudioAttributes aa) {
Jeff Sharkey098d5802012-04-26 17:30:34 -070083 mToken = token;
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -070084
85 mRingtone = new Ringtone(getContextForUser(user), false);
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -070086 mRingtone.setAudioAttributes(aa);
Jeff Sharkey098d5802012-04-26 17:30:34 -070087 mRingtone.setUri(uri);
88 }
89
90 @Override
91 public void binderDied() {
John Spurlockcd686b52013-06-05 10:13:46 -040092 if (LOGD) Log.d(TAG, "binderDied() token=" + mToken);
Jeff Sharkey098d5802012-04-26 17:30:34 -070093 synchronized (mClients) {
94 mClients.remove(mToken);
95 }
96 mRingtone.stop();
97 }
98 }
99
100 private IRingtonePlayer mCallback = new IRingtonePlayer.Stub() {
101 @Override
Jean-Michel Trivi462045e2015-06-30 12:34:48 -0700102 public void play(IBinder token, Uri uri, AudioAttributes aa, float volume, boolean looping)
103 throws RemoteException {
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700104 if (LOGD) {
John Spurlockcd686b52013-06-05 10:13:46 -0400105 Log.d(TAG, "play(token=" + token + ", uri=" + uri + ", uid="
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700106 + Binder.getCallingUid() + ")");
107 }
Jeff Sharkey098d5802012-04-26 17:30:34 -0700108 Client client;
109 synchronized (mClients) {
110 client = mClients.get(token);
111 if (client == null) {
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700112 final UserHandle user = Binder.getCallingUserHandle();
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700113 client = new Client(token, uri, user, aa);
Jeff Sharkey098d5802012-04-26 17:30:34 -0700114 token.linkToDeath(client, 0);
115 mClients.put(token, client);
116 }
117 }
Jean-Michel Trivi462045e2015-06-30 12:34:48 -0700118 client.mRingtone.setLooping(looping);
119 client.mRingtone.setVolume(volume);
Jeff Sharkey098d5802012-04-26 17:30:34 -0700120 client.mRingtone.play();
121 }
122
123 @Override
124 public void stop(IBinder token) {
John Spurlockcd686b52013-06-05 10:13:46 -0400125 if (LOGD) Log.d(TAG, "stop(token=" + token + ")");
Jeff Sharkey098d5802012-04-26 17:30:34 -0700126 Client client;
127 synchronized (mClients) {
128 client = mClients.remove(token);
129 }
130 if (client != null) {
131 client.mToken.unlinkToDeath(client, 0);
132 client.mRingtone.stop();
133 }
134 }
135
136 @Override
137 public boolean isPlaying(IBinder token) {
John Spurlockcd686b52013-06-05 10:13:46 -0400138 if (LOGD) Log.d(TAG, "isPlaying(token=" + token + ")");
Jeff Sharkey098d5802012-04-26 17:30:34 -0700139 Client client;
140 synchronized (mClients) {
141 client = mClients.get(token);
142 }
143 if (client != null) {
144 return client.mRingtone.isPlaying();
145 } else {
146 return false;
147 }
148 }
149
150 @Override
Jean-Michel Trivi462045e2015-06-30 12:34:48 -0700151 public void setPlaybackProperties(IBinder token, float volume, boolean looping) {
152 Client client;
153 synchronized (mClients) {
154 client = mClients.get(token);
155 }
156 if (client != null) {
157 client.mRingtone.setVolume(volume);
158 client.mRingtone.setLooping(looping);
159 }
160 // else no client for token when setting playback properties but will be set at play()
161 }
162
163 @Override
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700164 public void playAsync(Uri uri, UserHandle user, boolean looping, AudioAttributes aa) {
John Spurlockcd686b52013-06-05 10:13:46 -0400165 if (LOGD) Log.d(TAG, "playAsync(uri=" + uri + ", user=" + user + ")");
Jeff Sharkey098d5802012-04-26 17:30:34 -0700166 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
167 throw new SecurityException("Async playback only available from system UID.");
168 }
Sungmin Choi4a05bbcd2015-08-07 18:12:19 -0700169 if (UserHandle.ALL.equals(user)) {
Xiaohui Chene4de5a02015-09-22 15:33:31 -0700170 user = UserHandle.SYSTEM;
Sungmin Choi4a05bbcd2015-08-07 18:12:19 -0700171 }
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700172 mAsyncPlayer.play(getContextForUser(user), uri, looping, aa);
Jeff Sharkey098d5802012-04-26 17:30:34 -0700173 }
174
175 @Override
176 public void stopAsync() {
John Spurlockcd686b52013-06-05 10:13:46 -0400177 if (LOGD) Log.d(TAG, "stopAsync()");
Jeff Sharkey098d5802012-04-26 17:30:34 -0700178 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
179 throw new SecurityException("Async playback only available from system UID.");
180 }
181 mAsyncPlayer.stop();
182 }
Todd Kennedy68de1572015-07-20 13:32:40 -0700183
184 @Override
185 public String getTitle(Uri uri) {
186 final UserHandle user = Binder.getCallingUserHandle();
187 return Ringtone.getTitle(getContextForUser(user), uri,
188 false /*followSettingsUri*/, false /*allowRemote*/);
189 }
Jeff Sharkey783ee0c2016-03-05 17:23:28 -0700190
191 @Override
192 public ParcelFileDescriptor openRingtone(Uri uri) {
193 final UserHandle user = Binder.getCallingUserHandle();
194 final ContentResolver resolver = getContextForUser(user).getContentResolver();
195
196 // Only open the requested Uri if it's a well-known ringtone or
197 // other sound from the platform media store, otherwise this opens
198 // up arbitrary access to any file on external storage.
199 if (uri.toString().startsWith(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString())) {
200 try (Cursor c = resolver.query(uri, new String[] {
201 MediaStore.Audio.AudioColumns.IS_RINGTONE,
202 MediaStore.Audio.AudioColumns.IS_ALARM,
203 MediaStore.Audio.AudioColumns.IS_NOTIFICATION
204 }, null, null, null)) {
205 if (c.moveToFirst()) {
206 if (c.getInt(0) != 0 || c.getInt(1) != 0 || c.getInt(2) != 0) {
207 try {
208 return resolver.openFileDescriptor(uri, "r");
209 } catch (IOException e) {
210 throw new SecurityException(e);
211 }
212 }
213 }
214 }
215 }
216 throw new SecurityException("Uri is not ringtone, alarm, or notification: " + uri);
217 }
Jeff Sharkey098d5802012-04-26 17:30:34 -0700218 };
219
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700220 private Context getContextForUser(UserHandle user) {
221 try {
222 return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
223 } catch (NameNotFoundException e) {
224 throw new RuntimeException(e);
225 }
226 }
227
Jeff Sharkey098d5802012-04-26 17:30:34 -0700228 @Override
229 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
230 pw.println("Clients:");
231 synchronized (mClients) {
232 for (Client client : mClients.values()) {
233 pw.print(" mToken=");
234 pw.print(client.mToken);
235 pw.print(" mUri=");
236 pw.println(client.mRingtone.getUri());
237 }
238 }
239 }
240}