/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.media; import com.android.internal.database.SortCursor; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; import android.app.Activity; import android.content.ContentUris; import android.content.Context; import android.content.res.AssetFileDescriptor; import android.database.Cursor; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.provider.Settings; import android.provider.Settings.System; import android.util.Log; import java.util.ArrayList; import java.util.List; /** * RingtoneManager provides access to ringtones, notification, and other types * of sounds. It manages querying the different media providers and combines the * results into a single cursor. It also provides a {@link Ringtone} for each * ringtone. We generically call these sounds ringtones, however the * {@link #TYPE_RINGTONE} refers to the type of sounds that are suitable for the * phone ringer. *
* To show a ringtone picker to the user, use the
* {@link #ACTION_RINGTONE_PICKER} intent to launch the picker as a subactivity.
*
* @see Ringtone
*/
public class RingtoneManager {
private static final String TAG = "RingtoneManager";
// Make sure these are in sync with attrs.xml:
//
* Input: {@link #EXTRA_RINGTONE_EXISTING_URI}, * {@link #EXTRA_RINGTONE_SHOW_DEFAULT}, * {@link #EXTRA_RINGTONE_SHOW_SILENT}, {@link #EXTRA_RINGTONE_TYPE}, * {@link #EXTRA_RINGTONE_DEFAULT_URI}, {@link #EXTRA_RINGTONE_TITLE}, *
* Output: {@link #EXTRA_RINGTONE_PICKED_URI}. */ @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) public static final String ACTION_RINGTONE_PICKER = "android.intent.action.RINGTONE_PICKER"; /** * Given to the ringtone picker as a boolean. Whether to show an item for * "Default". * * @see #ACTION_RINGTONE_PICKER */ public static final String EXTRA_RINGTONE_SHOW_DEFAULT = "android.intent.extra.ringtone.SHOW_DEFAULT"; /** * Given to the ringtone picker as a boolean. Whether to show an item for * "Silent". If the "Silent" item is picked, * {@link #EXTRA_RINGTONE_PICKED_URI} will be null. * * @see #ACTION_RINGTONE_PICKER */ public static final String EXTRA_RINGTONE_SHOW_SILENT = "android.intent.extra.ringtone.SHOW_SILENT"; /** * Given to the ringtone picker as a boolean. Whether to include DRM ringtones. * @deprecated DRM ringtones are no longer supported */ @Deprecated public static final String EXTRA_RINGTONE_INCLUDE_DRM = "android.intent.extra.ringtone.INCLUDE_DRM"; /** * Given to the ringtone picker as a {@link Uri}. The {@link Uri} of the * current ringtone, which will be used to show a checkmark next to the item * for this {@link Uri}. If showing an item for "Default" (@see * {@link #EXTRA_RINGTONE_SHOW_DEFAULT}), this can also be one of * {@link System#DEFAULT_RINGTONE_URI}, * {@link System#DEFAULT_NOTIFICATION_URI}, or * {@link System#DEFAULT_ALARM_ALERT_URI} to have the "Default" item * checked. * * @see #ACTION_RINGTONE_PICKER */ public static final String EXTRA_RINGTONE_EXISTING_URI = "android.intent.extra.ringtone.EXISTING_URI"; /** * Given to the ringtone picker as a {@link Uri}. The {@link Uri} of the * ringtone to play when the user attempts to preview the "Default" * ringtone. This can be one of {@link System#DEFAULT_RINGTONE_URI}, * {@link System#DEFAULT_NOTIFICATION_URI}, or * {@link System#DEFAULT_ALARM_ALERT_URI} to have the "Default" point to * the current sound for the given default sound type. If you are showing a * ringtone picker for some other type of sound, you are free to provide any * {@link Uri} here. */ public static final String EXTRA_RINGTONE_DEFAULT_URI = "android.intent.extra.ringtone.DEFAULT_URI"; /** * Given to the ringtone picker as an int. Specifies which ringtone type(s) should be * shown in the picker. One or more of {@link #TYPE_RINGTONE}, * {@link #TYPE_NOTIFICATION}, {@link #TYPE_ALARM}, or {@link #TYPE_ALL} * (bitwise-ored together). */ public static final String EXTRA_RINGTONE_TYPE = "android.intent.extra.ringtone.TYPE"; /** * Given to the ringtone picker as a {@link CharSequence}. The title to * show for the ringtone picker. This has a default value that is suitable * in most cases. */ public static final String EXTRA_RINGTONE_TITLE = "android.intent.extra.ringtone.TITLE"; /** * Returned from the ringtone picker as a {@link Uri}. *
* It will be one of: *
* If this is false, make sure to {@link Ringtone#stop()} any previous * ringtones to free resources. * * @param stopPreviousRingtone If true, the previously retrieved * {@link Ringtone} will be stopped. */ public void setStopPreviousRingtone(boolean stopPreviousRingtone) { mStopPreviousRingtone = stopPreviousRingtone; } /** * @see #setStopPreviousRingtone(boolean) */ public boolean getStopPreviousRingtone() { return mStopPreviousRingtone; } /** * Stops playing the last {@link Ringtone} retrieved from this. */ public void stopPreviousRingtone() { if (mPreviousRingtone != null) { mPreviousRingtone.stop(); } } /** * Returns whether DRM ringtones will be included. * * @return Whether DRM ringtones will be included. * @see #setIncludeDrm(boolean) * Obsolete - always returns false * @deprecated DRM ringtones are no longer supported */ @Deprecated public boolean getIncludeDrm() { return false; } /** * Sets whether to include DRM ringtones. * * @param includeDrm Whether to include DRM ringtones. * Obsolete - no longer has any effect * @deprecated DRM ringtones are no longer supported */ @Deprecated public void setIncludeDrm(boolean includeDrm) { if (includeDrm) { Log.w(TAG, "setIncludeDrm no longer supported"); } } /** * Returns a {@link Cursor} of all the ringtones available. The returned * cursor will be the same cursor returned each time this method is called, * so do not {@link Cursor#close()} the cursor. The cursor can be * {@link Cursor#deactivate()} safely. *
* If {@link RingtoneManager#RingtoneManager(Activity)} was not used, the
* caller should manage the returned cursor through its activity's life
* cycle to prevent leaking the cursor.
*
* @return A {@link Cursor} of all the ringtones available.
* @see #ID_COLUMN_INDEX
* @see #TITLE_COLUMN_INDEX
* @see #URI_COLUMN_INDEX
*/
public Cursor getCursor() {
if (mCursor != null && mCursor.requery()) {
return mCursor;
}
final Cursor internalCursor = getInternalRingtones();
final Cursor mediaCursor = getMediaRingtones();
return mCursor = new SortCursor(new Cursor[] { internalCursor, mediaCursor },
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
}
/**
* Gets a {@link Ringtone} for the ringtone at the given position in the
* {@link Cursor}.
*
* @param position The position (in the {@link Cursor}) of the ringtone.
* @return A {@link Ringtone} pointing to the ringtone.
*/
public Ringtone getRingtone(int position) {
if (mStopPreviousRingtone && mPreviousRingtone != null) {
mPreviousRingtone.stop();
}
mPreviousRingtone = getRingtone(mContext, getRingtoneUri(position), inferStreamType());
return mPreviousRingtone;
}
/**
* Gets a {@link Uri} for the ringtone at the given position in the {@link Cursor}.
*
* @param position The position (in the {@link Cursor}) of the ringtone.
* @return A {@link Uri} pointing to the ringtone.
*/
public Uri getRingtoneUri(int position) {
// use cursor directly instead of requerying it, which could easily
// cause position to shuffle.
if (mCursor == null || !mCursor.moveToPosition(position)) {
return null;
}
return getUriFromCursor(mCursor);
}
private static Uri getUriFromCursor(Cursor cursor) {
return ContentUris.withAppendedId(Uri.parse(cursor.getString(URI_COLUMN_INDEX)), cursor
.getLong(ID_COLUMN_INDEX));
}
/**
* Gets the position of a {@link Uri} within this {@link RingtoneManager}.
*
* @param ringtoneUri The {@link Uri} to retreive the position of.
* @return The position of the {@link Uri}, or -1 if it cannot be found.
*/
public int getRingtonePosition(Uri ringtoneUri) {
if (ringtoneUri == null) return -1;
final Cursor cursor = getCursor();
final int cursorCount = cursor.getCount();
if (!cursor.moveToFirst()) {
return -1;
}
// Only create Uri objects when the actual URI changes
Uri currentUri = null;
String previousUriString = null;
for (int i = 0; i < cursorCount; i++) {
String uriString = cursor.getString(URI_COLUMN_INDEX);
if (currentUri == null || !uriString.equals(previousUriString)) {
currentUri = Uri.parse(uriString);
}
if (ringtoneUri.equals(ContentUris.withAppendedId(currentUri, cursor
.getLong(ID_COLUMN_INDEX)))) {
return i;
}
cursor.move(1);
previousUriString = uriString;
}
return -1;
}
/**
* Returns a valid ringtone URI. No guarantees on which it returns. If it
* cannot find one, returns null.
*
* @param context The context to use for querying.
* @return A ringtone URI, or null if one cannot be found.
*/
public static Uri getValidRingtoneUri(Context context) {
final RingtoneManager rm = new RingtoneManager(context);
Uri uri = getValidRingtoneUriFromCursorAndClose(context, rm.getInternalRingtones());
if (uri == null) {
uri = getValidRingtoneUriFromCursorAndClose(context, rm.getMediaRingtones());
}
return uri;
}
private static Uri getValidRingtoneUriFromCursorAndClose(Context context, Cursor cursor) {
if (cursor != null) {
Uri uri = null;
if (cursor.moveToFirst()) {
uri = getUriFromCursor(cursor);
}
cursor.close();
return uri;
} else {
return null;
}
}
private Cursor getInternalRingtones() {
return query(
MediaStore.Audio.Media.INTERNAL_CONTENT_URI, INTERNAL_COLUMNS,
constructBooleanTrueWhereClause(mFilterColumns),
null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
}
private Cursor getMediaRingtones() {
// Get the external media cursor. First check to see if it is mounted.
final String status = Environment.getExternalStorageState();
return (status.equals(Environment.MEDIA_MOUNTED) ||
status.equals(Environment.MEDIA_MOUNTED_READ_ONLY))
? query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, MEDIA_COLUMNS,
constructBooleanTrueWhereClause(mFilterColumns), null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER)
: null;
}
private void setFilterColumnsList(int type) {
List
* If the given URI cannot be opened for any reason, this method will
* attempt to fallback on another sound. If it cannot find any, it will
* return null.
*
* @param context A context used to query.
* @param ringtoneUri The {@link Uri} of a sound or ringtone.
* @return A {@link Ringtone} for the given URI, or null.
*/
public static Ringtone getRingtone(final Context context, Uri ringtoneUri) {
// Don't set the stream type
return getRingtone(context, ringtoneUri, -1);
}
/**
* Returns a {@link Ringtone} for a given sound URI on the given stream
* type. Normally, if you change the stream type on the returned
* {@link Ringtone}, it will re-create the {@link MediaPlayer}. This is just
* an optimized route to avoid that.
*
* @param streamType The stream type for the ringtone, or -1 if it should
* not be set (and the default used instead).
* @see #getRingtone(Context, Uri)
*/
private static Ringtone getRingtone(final Context context, Uri ringtoneUri, int streamType) {
try {
final Ringtone r = new Ringtone(context, true);
if (streamType >= 0) {
r.setStreamType(streamType);
}
r.setUri(ringtoneUri);
return r;
} catch (Exception ex) {
Log.e(TAG, "Failed to open ringtone " + ringtoneUri + ": " + ex);
}
return null;
}
/**
* Gets the current default sound's {@link Uri}. This will give the actual
* sound {@link Uri}, instead of using this, most clients can use
* {@link System#DEFAULT_RINGTONE_URI}.
*
* @param context A context used for querying.
* @param type The type whose default sound should be returned. One of
* {@link #TYPE_RINGTONE}, {@link #TYPE_NOTIFICATION}, or
* {@link #TYPE_ALARM}.
* @return A {@link Uri} pointing to the default sound for the sound type.
* @see #setActualDefaultRingtoneUri(Context, int, Uri)
*/
public static Uri getActualDefaultRingtoneUri(Context context, int type) {
String setting = getSettingForType(type);
if (setting == null) return null;
final String uriString = Settings.System.getString(context.getContentResolver(), setting);
return uriString != null ? Uri.parse(uriString) : null;
}
/**
* Sets the {@link Uri} of the default sound for a given sound type.
*
* @param context A context used for querying.
* @param type The type whose default sound should be set. One of
* {@link #TYPE_RINGTONE}, {@link #TYPE_NOTIFICATION}, or
* {@link #TYPE_ALARM}.
* @param ringtoneUri A {@link Uri} pointing to the default sound to set.
* @see #getActualDefaultRingtoneUri(Context, int)
*/
public static void setActualDefaultRingtoneUri(Context context, int type, Uri ringtoneUri) {
String setting = getSettingForType(type);
if (setting == null) return;
Settings.System.putString(context.getContentResolver(), setting,
ringtoneUri != null ? ringtoneUri.toString() : null);
}
private static String getSettingForType(int type) {
if ((type & TYPE_RINGTONE) != 0) {
return Settings.System.RINGTONE;
} else if ((type & TYPE_NOTIFICATION) != 0) {
return Settings.System.NOTIFICATION_SOUND;
} else if ((type & TYPE_ALARM) != 0) {
return Settings.System.ALARM_ALERT;
} else {
return null;
}
}
/**
* Returns whether the given {@link Uri} is one of the default ringtones.
*
* @param ringtoneUri The ringtone {@link Uri} to be checked.
* @return Whether the {@link Uri} is a default.
*/
public static boolean isDefault(Uri ringtoneUri) {
return getDefaultType(ringtoneUri) != -1;
}
/**
* Returns the type of a default {@link Uri}.
*
* @param defaultRingtoneUri The default {@link Uri}. For example,
* {@link System#DEFAULT_RINGTONE_URI},
* {@link System#DEFAULT_NOTIFICATION_URI}, or
* {@link System#DEFAULT_ALARM_ALERT_URI}.
* @return The type of the defaultRingtoneUri, or -1.
*/
public static int getDefaultType(Uri defaultRingtoneUri) {
if (defaultRingtoneUri == null) {
return -1;
} else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_RINGTONE_URI)) {
return TYPE_RINGTONE;
} else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_NOTIFICATION_URI)) {
return TYPE_NOTIFICATION;
} else if (defaultRingtoneUri.equals(Settings.System.DEFAULT_ALARM_ALERT_URI)) {
return TYPE_ALARM;
} else {
return -1;
}
}
/**
* Returns the {@link Uri} for the default ringtone of a particular type.
* Rather than returning the actual ringtone's sound {@link Uri}, this will
* return the symbolic {@link Uri} which will resolved to the actual sound
* when played.
*
* @param type The ringtone type whose default should be returned.
* @return The {@link Uri} of the default ringtone for the given type.
*/
public static Uri getDefaultUri(int type) {
if ((type & TYPE_RINGTONE) != 0) {
return Settings.System.DEFAULT_RINGTONE_URI;
} else if ((type & TYPE_NOTIFICATION) != 0) {
return Settings.System.DEFAULT_NOTIFICATION_URI;
} else if ((type & TYPE_ALARM) != 0) {
return Settings.System.DEFAULT_ALARM_ALERT_URI;
} else {
return null;
}
}
}