Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.media; |
| 18 | |
Yiwen Chen | eac542e | 2019-02-10 14:23:25 -0800 | [diff] [blame] | 19 | import android.annotation.Nullable; |
Jeff Sharkey | 783ee0c | 2016-03-05 17:23:28 -0700 | [diff] [blame] | 20 | import android.content.ContentResolver; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 22 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | 783ee0c | 2016-03-05 17:23:28 -0700 | [diff] [blame] | 23 | import android.database.Cursor; |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 24 | import android.media.AudioAttributes; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 25 | import android.media.IAudioService; |
| 26 | import android.media.IRingtonePlayer; |
| 27 | import android.media.Ringtone; |
Yiwen Chen | eac542e | 2019-02-10 14:23:25 -0800 | [diff] [blame] | 28 | import android.media.VolumeShaper; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
| 30 | import android.os.Binder; |
| 31 | import android.os.IBinder; |
Jeff Sharkey | 783ee0c | 2016-03-05 17:23:28 -0700 | [diff] [blame] | 32 | import android.os.ParcelFileDescriptor; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 33 | import android.os.Process; |
| 34 | import android.os.RemoteException; |
| 35 | import android.os.ServiceManager; |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 36 | import android.os.UserHandle; |
Jeff Sharkey | 783ee0c | 2016-03-05 17:23:28 -0700 | [diff] [blame] | 37 | import android.provider.MediaStore; |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 38 | import android.util.Log; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 39 | |
| 40 | import com.android.systemui.SystemUI; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 41 | |
| 42 | import java.io.FileDescriptor; |
Jeff Sharkey | 783ee0c | 2016-03-05 17:23:28 -0700 | [diff] [blame] | 43 | import java.io.IOException; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 44 | import java.io.PrintWriter; |
| 45 | import 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 | */ |
| 51 | public class RingtonePlayer extends SystemUI { |
| 52 | private static final String TAG = "RingtonePlayer"; |
Jeff Sharkey | b6e404a | 2012-05-15 11:36:11 -0700 | [diff] [blame] | 53 | private static final boolean LOGD = false; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 54 | |
| 55 | // TODO: support Uri switching under same IBinder |
| 56 | |
| 57 | private IAudioService mAudioService; |
| 58 | |
| 59 | private final NotificationPlayer mAsyncPlayer = new NotificationPlayer(TAG); |
John Spurlock | b8baccc | 2013-06-05 12:59:01 -0400 | [diff] [blame] | 60 | private final HashMap<IBinder, Client> mClients = new HashMap<IBinder, Client>(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 61 | |
| 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 Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 71 | Log.e(TAG, "Problem registering RingtonePlayer: " + e); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 72 | } |
| 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 Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 82 | public Client(IBinder token, Uri uri, UserHandle user, AudioAttributes aa) { |
Yiwen Chen | eac542e | 2019-02-10 14:23:25 -0800 | [diff] [blame] | 83 | this(token, uri, user, aa, null); |
| 84 | } |
| 85 | |
| 86 | Client(IBinder token, Uri uri, UserHandle user, AudioAttributes aa, |
| 87 | @Nullable VolumeShaper.Configuration volumeShaperConfig) { |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 88 | mToken = token; |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 89 | |
| 90 | mRingtone = new Ringtone(getContextForUser(user), false); |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 91 | mRingtone.setAudioAttributes(aa); |
Yiwen Chen | eac542e | 2019-02-10 14:23:25 -0800 | [diff] [blame] | 92 | mRingtone.setUri(uri, volumeShaperConfig); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void binderDied() { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 97 | if (LOGD) Log.d(TAG, "binderDied() token=" + mToken); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 98 | synchronized (mClients) { |
| 99 | mClients.remove(mToken); |
| 100 | } |
| 101 | mRingtone.stop(); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | private IRingtonePlayer mCallback = new IRingtonePlayer.Stub() { |
| 106 | @Override |
Jean-Michel Trivi | 462045e | 2015-06-30 12:34:48 -0700 | [diff] [blame] | 107 | public void play(IBinder token, Uri uri, AudioAttributes aa, float volume, boolean looping) |
| 108 | throws RemoteException { |
Yiwen Chen | eac542e | 2019-02-10 14:23:25 -0800 | [diff] [blame] | 109 | playWithVolumeShaping(token, uri, aa, volume, looping, null); |
| 110 | } |
| 111 | @Override |
| 112 | public void playWithVolumeShaping(IBinder token, Uri uri, AudioAttributes aa, float volume, |
| 113 | boolean looping, @Nullable VolumeShaper.Configuration volumeShaperConfig) |
| 114 | throws RemoteException { |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 115 | if (LOGD) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 116 | Log.d(TAG, "play(token=" + token + ", uri=" + uri + ", uid=" |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 117 | + Binder.getCallingUid() + ")"); |
| 118 | } |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 119 | Client client; |
| 120 | synchronized (mClients) { |
| 121 | client = mClients.get(token); |
| 122 | if (client == null) { |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 123 | final UserHandle user = Binder.getCallingUserHandle(); |
Yiwen Chen | eac542e | 2019-02-10 14:23:25 -0800 | [diff] [blame] | 124 | client = new Client(token, uri, user, aa, volumeShaperConfig); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 125 | token.linkToDeath(client, 0); |
| 126 | mClients.put(token, client); |
| 127 | } |
| 128 | } |
Jean-Michel Trivi | 462045e | 2015-06-30 12:34:48 -0700 | [diff] [blame] | 129 | client.mRingtone.setLooping(looping); |
| 130 | client.mRingtone.setVolume(volume); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 131 | client.mRingtone.play(); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void stop(IBinder token) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 136 | if (LOGD) Log.d(TAG, "stop(token=" + token + ")"); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 137 | Client client; |
| 138 | synchronized (mClients) { |
| 139 | client = mClients.remove(token); |
| 140 | } |
| 141 | if (client != null) { |
| 142 | client.mToken.unlinkToDeath(client, 0); |
| 143 | client.mRingtone.stop(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public boolean isPlaying(IBinder token) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 149 | if (LOGD) Log.d(TAG, "isPlaying(token=" + token + ")"); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 150 | Client client; |
| 151 | synchronized (mClients) { |
| 152 | client = mClients.get(token); |
| 153 | } |
| 154 | if (client != null) { |
| 155 | return client.mRingtone.isPlaying(); |
| 156 | } else { |
| 157 | return false; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | @Override |
Jean-Michel Trivi | 462045e | 2015-06-30 12:34:48 -0700 | [diff] [blame] | 162 | public void setPlaybackProperties(IBinder token, float volume, boolean looping) { |
| 163 | Client client; |
| 164 | synchronized (mClients) { |
| 165 | client = mClients.get(token); |
| 166 | } |
| 167 | if (client != null) { |
| 168 | client.mRingtone.setVolume(volume); |
| 169 | client.mRingtone.setLooping(looping); |
| 170 | } |
| 171 | // else no client for token when setting playback properties but will be set at play() |
| 172 | } |
| 173 | |
| 174 | @Override |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 175 | public void playAsync(Uri uri, UserHandle user, boolean looping, AudioAttributes aa) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 176 | if (LOGD) Log.d(TAG, "playAsync(uri=" + uri + ", user=" + user + ")"); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 177 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 178 | throw new SecurityException("Async playback only available from system UID."); |
| 179 | } |
Sungmin Choi | 4a05bbcd | 2015-08-07 18:12:19 -0700 | [diff] [blame] | 180 | if (UserHandle.ALL.equals(user)) { |
Xiaohui Chen | e4de5a0 | 2015-09-22 15:33:31 -0700 | [diff] [blame] | 181 | user = UserHandle.SYSTEM; |
Sungmin Choi | 4a05bbcd | 2015-08-07 18:12:19 -0700 | [diff] [blame] | 182 | } |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 183 | mAsyncPlayer.play(getContextForUser(user), uri, looping, aa); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void stopAsync() { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 188 | if (LOGD) Log.d(TAG, "stopAsync()"); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 189 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 190 | throw new SecurityException("Async playback only available from system UID."); |
| 191 | } |
| 192 | mAsyncPlayer.stop(); |
| 193 | } |
Todd Kennedy | 68de157 | 2015-07-20 13:32:40 -0700 | [diff] [blame] | 194 | |
| 195 | @Override |
| 196 | public String getTitle(Uri uri) { |
| 197 | final UserHandle user = Binder.getCallingUserHandle(); |
| 198 | return Ringtone.getTitle(getContextForUser(user), uri, |
| 199 | false /*followSettingsUri*/, false /*allowRemote*/); |
| 200 | } |
Jeff Sharkey | 783ee0c | 2016-03-05 17:23:28 -0700 | [diff] [blame] | 201 | |
| 202 | @Override |
| 203 | public ParcelFileDescriptor openRingtone(Uri uri) { |
| 204 | final UserHandle user = Binder.getCallingUserHandle(); |
| 205 | final ContentResolver resolver = getContextForUser(user).getContentResolver(); |
| 206 | |
| 207 | // Only open the requested Uri if it's a well-known ringtone or |
| 208 | // other sound from the platform media store, otherwise this opens |
| 209 | // up arbitrary access to any file on external storage. |
| 210 | if (uri.toString().startsWith(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.toString())) { |
| 211 | try (Cursor c = resolver.query(uri, new String[] { |
| 212 | MediaStore.Audio.AudioColumns.IS_RINGTONE, |
| 213 | MediaStore.Audio.AudioColumns.IS_ALARM, |
| 214 | MediaStore.Audio.AudioColumns.IS_NOTIFICATION |
| 215 | }, null, null, null)) { |
| 216 | if (c.moveToFirst()) { |
| 217 | if (c.getInt(0) != 0 || c.getInt(1) != 0 || c.getInt(2) != 0) { |
| 218 | try { |
| 219 | return resolver.openFileDescriptor(uri, "r"); |
| 220 | } catch (IOException e) { |
| 221 | throw new SecurityException(e); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | throw new SecurityException("Uri is not ringtone, alarm, or notification: " + uri); |
| 228 | } |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 229 | }; |
| 230 | |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 231 | private Context getContextForUser(UserHandle user) { |
| 232 | try { |
| 233 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 234 | } catch (NameNotFoundException e) { |
| 235 | throw new RuntimeException(e); |
| 236 | } |
| 237 | } |
| 238 | |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 239 | @Override |
| 240 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 241 | pw.println("Clients:"); |
| 242 | synchronized (mClients) { |
| 243 | for (Client client : mClients.values()) { |
| 244 | pw.print(" mToken="); |
| 245 | pw.print(client.mToken); |
| 246 | pw.print(" mUri="); |
| 247 | pw.println(client.mRingtone.getUri()); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |