Fields Summary |
---|
private static final String | TAG |
private static final boolean | LOGD |
private static final String[] | MEDIA_COLUMNS |
private final android.content.Context | mContext |
private final AudioManager | mAudioManager |
private final boolean | mAllowRemoteFlag indicating if we're allowed to fall back to remote playback using
{@link #mRemotePlayer}. Typically this is false when we're the remote
player and there is nobody else to delegate to. |
private final IRingtonePlayer | mRemotePlayer |
private final android.os.Binder | mRemoteToken |
private MediaPlayer | mLocalPlayer |
private android.net.Uri | mUri |
private String | mTitle |
private AudioAttributes | mAudioAttributes |
Methods Summary |
---|
private void | destroyLocalPlayer()
if (mLocalPlayer != null) {
mLocalPlayer.reset();
mLocalPlayer.release();
mLocalPlayer = null;
}
|
public AudioAttributes | getAudioAttributes()Returns the {@link AudioAttributes} used by this object.
return mAudioAttributes;
|
public int | getStreamType()Gets the stream type where this ringtone will be played.
return AudioAttributes.toLegacyStreamType(mAudioAttributes);
|
public java.lang.String | getTitle(android.content.Context context)Returns a human-presentable title for ringtone. Looks in media
content provider. If not in either, uses the filename
if (mTitle != null) return mTitle;
return mTitle = getTitle(context, mUri, true);
|
private static java.lang.String | getTitle(android.content.Context context, android.net.Uri uri, boolean followSettingsUri)
Cursor cursor = null;
ContentResolver res = context.getContentResolver();
String title = null;
if (uri != null) {
String authority = uri.getAuthority();
if (Settings.AUTHORITY.equals(authority)) {
if (followSettingsUri) {
Uri actualUri = RingtoneManager.getActualDefaultRingtoneUri(context,
RingtoneManager.getDefaultType(uri));
String actualTitle = getTitle(context, actualUri, false);
title = context
.getString(com.android.internal.R.string.ringtone_default_with_actual,
actualTitle);
}
} else {
try {
if (MediaStore.AUTHORITY.equals(authority)) {
cursor = res.query(uri, MEDIA_COLUMNS, null, null, null);
}
} catch (SecurityException e) {
// missing cursor is handled below
}
try {
if (cursor != null && cursor.getCount() == 1) {
cursor.moveToFirst();
return cursor.getString(2);
} else {
title = uri.getLastPathSegment();
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}
if (title == null) {
title = context.getString(com.android.internal.R.string.ringtone_unknown);
if (title == null) {
title = "";
}
}
return title;
|
public android.net.Uri | getUri(){@hide}
return mUri;
|
public boolean | isPlaying()Whether this ringtone is currently playing.
if (mLocalPlayer != null) {
return mLocalPlayer.isPlaying();
} else if (mAllowRemote && (mRemotePlayer != null)) {
try {
return mRemotePlayer.isPlaying(mRemoteToken);
} catch (RemoteException e) {
Log.w(TAG, "Problem checking ringtone: " + e);
return false;
}
} else {
Log.w(TAG, "Neither local nor remote playback available");
return false;
}
|
public void | play()Plays the ringtone.
if (mLocalPlayer != null) {
// do not play ringtones if stream volume is 0
// (typically because ringer mode is silent).
if (mAudioManager.getStreamVolume(
AudioAttributes.toLegacyStreamType(mAudioAttributes)) != 0) {
mLocalPlayer.start();
}
} else if (mAllowRemote && (mRemotePlayer != null)) {
final Uri canonicalUri = mUri.getCanonicalUri();
try {
mRemotePlayer.play(mRemoteToken, canonicalUri, mAudioAttributes);
} catch (RemoteException e) {
if (!playFallbackRingtone()) {
Log.w(TAG, "Problem playing ringtone: " + e);
}
}
} else {
if (!playFallbackRingtone()) {
Log.w(TAG, "Neither local nor remote playback available");
}
}
|
private boolean | playFallbackRingtone()
if (mAudioManager.getStreamVolume(AudioAttributes.toLegacyStreamType(mAudioAttributes))
!= 0) {
int ringtoneType = RingtoneManager.getDefaultType(mUri);
if (ringtoneType == -1 ||
RingtoneManager.getActualDefaultRingtoneUri(mContext, ringtoneType) != null) {
// Default ringtone, try fallback ringtone.
try {
AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(
com.android.internal.R.raw.fallbackring);
if (afd != null) {
mLocalPlayer = new MediaPlayer();
if (afd.getDeclaredLength() < 0) {
mLocalPlayer.setDataSource(afd.getFileDescriptor());
} else {
mLocalPlayer.setDataSource(afd.getFileDescriptor(),
afd.getStartOffset(),
afd.getDeclaredLength());
}
mLocalPlayer.setAudioAttributes(mAudioAttributes);
mLocalPlayer.prepare();
mLocalPlayer.start();
afd.close();
return true;
} else {
Log.e(TAG, "Could not load fallback ringtone");
}
} catch (IOException ioe) {
destroyLocalPlayer();
Log.e(TAG, "Failed to open fallback ringtone");
} catch (NotFoundException nfe) {
Log.e(TAG, "Fallback ringtone does not exist");
}
} else {
Log.w(TAG, "not playing fallback for " + mUri);
}
}
return false;
|
public void | setAudioAttributes(AudioAttributes attributes)Sets the {@link AudioAttributes} for this ringtone.
if (attributes == null) {
throw new IllegalArgumentException("Invalid null AudioAttributes for Ringtone");
}
mAudioAttributes = attributes;
// The audio attributes have to be set before the media player is prepared.
// Re-initialize it.
setUri(mUri);
|
public void | setStreamType(int streamType)Sets the stream type where this ringtone will be played.
setAudioAttributes(new AudioAttributes.Builder()
.setInternalLegacyStreamType(streamType)
.build());
|
void | setTitle(java.lang.String title)
mTitle = title;
|
public void | setUri(android.net.Uri uri)Set {@link Uri} to be used for ringtone playback. Attempts to open
locally, otherwise will delegate playback to remote
{@link IRingtonePlayer}.
destroyLocalPlayer();
mUri = uri;
if (mUri == null) {
return;
}
// TODO: detect READ_EXTERNAL and specific content provider case, instead of relying on throwing
// try opening uri locally before delegating to remote player
mLocalPlayer = new MediaPlayer();
try {
mLocalPlayer.setDataSource(mContext, mUri);
mLocalPlayer.setAudioAttributes(mAudioAttributes);
mLocalPlayer.prepare();
} catch (SecurityException | IOException e) {
destroyLocalPlayer();
if (!mAllowRemote) {
Log.w(TAG, "Remote playback not allowed: " + e);
}
}
if (LOGD) {
if (mLocalPlayer != null) {
Log.d(TAG, "Successfully created local player");
} else {
Log.d(TAG, "Problem opening; delegating to remote player");
}
}
|
public void | stop()Stops a playing ringtone.
if (mLocalPlayer != null) {
destroyLocalPlayer();
} else if (mAllowRemote && (mRemotePlayer != null)) {
try {
mRemotePlayer.stop(mRemoteToken);
} catch (RemoteException e) {
Log.w(TAG, "Problem stopping ringtone: " + e);
}
}
|