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 | |
| 19 | import android.content.Context; |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 20 | import android.content.pm.PackageManager.NameNotFoundException; |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 21 | import android.media.AudioAttributes; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 22 | import android.media.IAudioService; |
| 23 | import android.media.IRingtonePlayer; |
| 24 | import android.media.Ringtone; |
| 25 | import android.net.Uri; |
| 26 | import android.os.Binder; |
| 27 | import android.os.IBinder; |
| 28 | import android.os.Process; |
| 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 31 | import android.os.UserHandle; |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 32 | import android.util.Log; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 33 | |
| 34 | import com.android.systemui.SystemUI; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 35 | |
| 36 | import java.io.FileDescriptor; |
| 37 | import java.io.PrintWriter; |
| 38 | import java.util.HashMap; |
| 39 | |
| 40 | /** |
| 41 | * Service that offers to play ringtones by {@link Uri}, since our process has |
| 42 | * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE}. |
| 43 | */ |
| 44 | public class RingtonePlayer extends SystemUI { |
| 45 | private static final String TAG = "RingtonePlayer"; |
Jeff Sharkey | b6e404a | 2012-05-15 11:36:11 -0700 | [diff] [blame] | 46 | private static final boolean LOGD = false; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 47 | |
| 48 | // TODO: support Uri switching under same IBinder |
| 49 | |
| 50 | private IAudioService mAudioService; |
| 51 | |
| 52 | private final NotificationPlayer mAsyncPlayer = new NotificationPlayer(TAG); |
John Spurlock | b8baccc | 2013-06-05 12:59:01 -0400 | [diff] [blame] | 53 | private final HashMap<IBinder, Client> mClients = new HashMap<IBinder, Client>(); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 54 | |
| 55 | @Override |
| 56 | public void start() { |
| 57 | mAsyncPlayer.setUsesWakeLock(mContext); |
| 58 | |
| 59 | mAudioService = IAudioService.Stub.asInterface( |
| 60 | ServiceManager.getService(Context.AUDIO_SERVICE)); |
| 61 | try { |
| 62 | mAudioService.setRingtonePlayer(mCallback); |
| 63 | } catch (RemoteException e) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 64 | Log.e(TAG, "Problem registering RingtonePlayer: " + e); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Represents an active remote {@link Ringtone} client. |
| 70 | */ |
| 71 | private class Client implements IBinder.DeathRecipient { |
| 72 | private final IBinder mToken; |
| 73 | private final Ringtone mRingtone; |
| 74 | |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 75 | public Client(IBinder token, Uri uri, UserHandle user, AudioAttributes aa) { |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 76 | mToken = token; |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 77 | |
| 78 | mRingtone = new Ringtone(getContextForUser(user), false); |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 79 | mRingtone.setAudioAttributes(aa); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 80 | mRingtone.setUri(uri); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void binderDied() { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 85 | if (LOGD) Log.d(TAG, "binderDied() token=" + mToken); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 86 | synchronized (mClients) { |
| 87 | mClients.remove(mToken); |
| 88 | } |
| 89 | mRingtone.stop(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private IRingtonePlayer mCallback = new IRingtonePlayer.Stub() { |
| 94 | @Override |
Jean-Michel Trivi | 462045e | 2015-06-30 12:34:48 -0700 | [diff] [blame^] | 95 | public void play(IBinder token, Uri uri, AudioAttributes aa, float volume, boolean looping) |
| 96 | throws RemoteException { |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 97 | if (LOGD) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 98 | Log.d(TAG, "play(token=" + token + ", uri=" + uri + ", uid=" |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 99 | + Binder.getCallingUid() + ")"); |
| 100 | } |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 101 | Client client; |
| 102 | synchronized (mClients) { |
| 103 | client = mClients.get(token); |
| 104 | if (client == null) { |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 105 | final UserHandle user = Binder.getCallingUserHandle(); |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 106 | client = new Client(token, uri, user, aa); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 107 | token.linkToDeath(client, 0); |
| 108 | mClients.put(token, client); |
| 109 | } |
| 110 | } |
Jean-Michel Trivi | 462045e | 2015-06-30 12:34:48 -0700 | [diff] [blame^] | 111 | client.mRingtone.setLooping(looping); |
| 112 | client.mRingtone.setVolume(volume); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 113 | client.mRingtone.play(); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public void stop(IBinder token) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 118 | if (LOGD) Log.d(TAG, "stop(token=" + token + ")"); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 119 | Client client; |
| 120 | synchronized (mClients) { |
| 121 | client = mClients.remove(token); |
| 122 | } |
| 123 | if (client != null) { |
| 124 | client.mToken.unlinkToDeath(client, 0); |
| 125 | client.mRingtone.stop(); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public boolean isPlaying(IBinder token) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 131 | if (LOGD) Log.d(TAG, "isPlaying(token=" + token + ")"); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 132 | Client client; |
| 133 | synchronized (mClients) { |
| 134 | client = mClients.get(token); |
| 135 | } |
| 136 | if (client != null) { |
| 137 | return client.mRingtone.isPlaying(); |
| 138 | } else { |
| 139 | return false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | @Override |
Jean-Michel Trivi | 462045e | 2015-06-30 12:34:48 -0700 | [diff] [blame^] | 144 | public void setPlaybackProperties(IBinder token, float volume, boolean looping) { |
| 145 | Client client; |
| 146 | synchronized (mClients) { |
| 147 | client = mClients.get(token); |
| 148 | } |
| 149 | if (client != null) { |
| 150 | client.mRingtone.setVolume(volume); |
| 151 | client.mRingtone.setLooping(looping); |
| 152 | } |
| 153 | // else no client for token when setting playback properties but will be set at play() |
| 154 | } |
| 155 | |
| 156 | @Override |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 157 | public void playAsync(Uri uri, UserHandle user, boolean looping, AudioAttributes aa) { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 158 | if (LOGD) Log.d(TAG, "playAsync(uri=" + uri + ", user=" + user + ")"); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 159 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 160 | throw new SecurityException("Async playback only available from system UID."); |
| 161 | } |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 162 | |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 163 | mAsyncPlayer.play(getContextForUser(user), uri, looping, aa); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public void stopAsync() { |
John Spurlock | cd686b5 | 2013-06-05 10:13:46 -0400 | [diff] [blame] | 168 | if (LOGD) Log.d(TAG, "stopAsync()"); |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 169 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 170 | throw new SecurityException("Async playback only available from system UID."); |
| 171 | } |
| 172 | mAsyncPlayer.stop(); |
| 173 | } |
| 174 | }; |
| 175 | |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 176 | private Context getContextForUser(UserHandle user) { |
| 177 | try { |
| 178 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 179 | } catch (NameNotFoundException e) { |
| 180 | throw new RuntimeException(e); |
| 181 | } |
| 182 | } |
| 183 | |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 184 | @Override |
| 185 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 186 | pw.println("Clients:"); |
| 187 | synchronized (mClients) { |
| 188 | for (Client client : mClients.values()) { |
| 189 | pw.print(" mToken="); |
| 190 | pw.print(client.mToken); |
| 191 | pw.print(" mUri="); |
| 192 | pw.println(client.mRingtone.getUri()); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |