Skip to content

Commit 68de157

Browse files
author
Todd Kennedy
committed
Use RingtonePlayer to get ringtone title
Instead of requiring every application that calls Rintone.getTitle() to request android.permission.READ_EXTERNAL_STORAGE, pass the call through to the system UI process. We only do this for media store URIs. Bug: 22067670 Change-Id: I38cf3fb8d769ef6984c41a7b04afbbd4c57175ce
1 parent 2dcfc7a commit 68de157

3 files changed

Lines changed: 44 additions & 14 deletions

File tree

media/java/android/media/IRingtonePlayer.aidl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ interface IRingtonePlayer {
3333
/** Used for Notification sound playback. */
3434
void playAsync(in Uri uri, in UserHandle user, boolean looping, in AudioAttributes aa);
3535
void stopAsync();
36+
37+
/** Return the title of the media. */
38+
String getTitle(in Uri uri);
3639
}

media/java/android/media/Ringtone.java

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import android.os.RemoteException;
2828
import android.provider.MediaStore;
2929
import android.provider.Settings;
30+
import android.provider.MediaStore.MediaColumns;
3031
import android.util.Log;
3132

3233
import java.io.IOException;
@@ -50,6 +51,8 @@ public class Ringtone {
5051
MediaStore.Audio.Media.DATA,
5152
MediaStore.Audio.Media.TITLE
5253
};
54+
/** Selection that limits query results to just audio files */
55+
private static final String MEDIA_SELECTION = MediaColumns.MIME_TYPE + " LIKE 'audio/%'";
5356

5457
// keep references on active Ringtones until stopped or completion listener called.
5558
private static final ArrayList<Ringtone> sActiveRingtones = new ArrayList<Ringtone>();
@@ -193,11 +196,14 @@ private void applyPlaybackProperties_sync() {
193196
*/
194197
public String getTitle(Context context) {
195198
if (mTitle != null) return mTitle;
196-
return mTitle = getTitle(context, mUri, true);
199+
return mTitle = getTitle(context, mUri, true /*followSettingsUri*/, mAllowRemote);
197200
}
198201

199-
private static String getTitle(Context context, Uri uri, boolean followSettingsUri) {
200-
Cursor cursor = null;
202+
/**
203+
* @hide
204+
*/
205+
public static String getTitle(
206+
Context context, Uri uri, boolean followSettingsUri, boolean allowRemote) {
201207
ContentResolver res = context.getContentResolver();
202208

203209
String title = null;
@@ -209,31 +215,45 @@ private static String getTitle(Context context, Uri uri, boolean followSettingsU
209215
if (followSettingsUri) {
210216
Uri actualUri = RingtoneManager.getActualDefaultRingtoneUri(context,
211217
RingtoneManager.getDefaultType(uri));
212-
String actualTitle = getTitle(context, actualUri, false);
218+
String actualTitle = getTitle(
219+
context, actualUri, false /*followSettingsUri*/, allowRemote);
213220
title = context
214221
.getString(com.android.internal.R.string.ringtone_default_with_actual,
215222
actualTitle);
216223
}
217224
} else {
225+
Cursor cursor = null;
218226
try {
219227
if (MediaStore.AUTHORITY.equals(authority)) {
220-
cursor = res.query(uri, MEDIA_COLUMNS, null, null, null);
228+
final String mediaSelection = allowRemote ? null : MEDIA_SELECTION;
229+
cursor = res.query(uri, MEDIA_COLUMNS, mediaSelection, null, null);
230+
if (cursor != null && cursor.getCount() == 1) {
231+
cursor.moveToFirst();
232+
return cursor.getString(2);
233+
}
234+
// missing cursor is handled below
221235
}
222236
} catch (SecurityException e) {
223-
// missing cursor is handled below
224-
}
225-
226-
try {
227-
if (cursor != null && cursor.getCount() == 1) {
228-
cursor.moveToFirst();
229-
return cursor.getString(2);
230-
} else {
231-
title = uri.getLastPathSegment();
237+
IRingtonePlayer mRemotePlayer = null;
238+
if (allowRemote) {
239+
AudioManager audioManager =
240+
(AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
241+
mRemotePlayer = audioManager.getRingtonePlayer();
242+
}
243+
if (mRemotePlayer != null) {
244+
try {
245+
title = mRemotePlayer.getTitle(uri);
246+
} catch (RemoteException re) {
247+
}
232248
}
233249
} finally {
234250
if (cursor != null) {
235251
cursor.close();
236252
}
253+
cursor = null;
254+
}
255+
if (title == null) {
256+
title = uri.getLastPathSegment();
237257
}
238258
}
239259
}

packages/SystemUI/src/com/android/systemui/media/RingtonePlayer.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,13 @@ public void stopAsync() {
171171
}
172172
mAsyncPlayer.stop();
173173
}
174+
175+
@Override
176+
public String getTitle(Uri uri) {
177+
final UserHandle user = Binder.getCallingUserHandle();
178+
return Ringtone.getTitle(getContextForUser(user), uri,
179+
false /*followSettingsUri*/, false /*allowRemote*/);
180+
}
174181
};
175182

176183
private Context getContextForUser(UserHandle user) {

0 commit comments

Comments
 (0)