public final class RingtonePickerActivity extends AlertActivity implements
AdapterView.OnItemSelectedListener, Runnable, DialogInterface.OnClickListener,
AlertController.AlertParams.OnPrepareListViewListener {
private static final String TAG = "RingtonePickerActivity";
private static final int DELAY_MS_SELECTION_PLAYED = 300;
private RingtoneManager mRingtoneManager;
private Cursor mCursor;
private Handler mHandler;
/** The position in the list of the 'Silent' item. */
private int mSilentPos = -1;
/** The position in the list of the 'Default' item. */
private int mDefaultRingtonePos = -1;
/** The position in the list of the last clicked item. */
private int mClickedPos = -1;
/** The position in the list of the ringtone to sample. */
private int mSampleRingtonePos = -1;
/** Whether this list has the 'Silent' item. */
private boolean mHasSilentItem;
/** The Uri to place a checkmark next to. */
private Uri mExistingUri;
/** The number of static items in the list. */
private int mStaticItemCount;
/** Whether this list has the 'Default' item. */
private boolean mHasDefaultItem;
/** The Uri to play when the 'Default' item is clicked. */
private Uri mUriForDefaultItem;
/**
* A Ringtone for the default ringtone. In most cases, the RingtoneManager
* will stop the previous ringtone. However, the RingtoneManager doesn't
* manage the default ringtone for us, so we should stop this one manually.
*/
private Ringtone mDefaultRingtone;
private DialogInterface.OnClickListener mRingtoneClickListener =
new DialogInterface.OnClickListener() {
/*
* On item clicked
*/
public void onClick(DialogInterface dialog, int which) {
// Save the position of most recently clicked item
mClickedPos = which;
// Play clip
playRingtone(which, 0);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG,"------onCreate----");
mHandler = new Handler();
Intent intent = getIntent();
/*
* Get whether to show the 'Default' item, and the URI to play when the
* default is clicked
*/
mHasDefaultItem = intent.getBooleanExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
Log.i(TAG,"------onCreate1----"+mHasDefaultItem);
mUriForDefaultItem = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI);
Log.i(TAG,"------onCreate2----"+mUriForDefaultItem.toString());
if (mUriForDefaultItem == null) {
mUriForDefaultItem = Settings.System.DEFAULT_RINGTONE_URI;//DEFAULT_RINGTONE_URI = content://settings/system/ringtone
}
Log.i(TAG,"------onCreate3----"+mUriForDefaultItem.toString());
// Get whether to show the 'Silent' item
mHasSilentItem = intent.getBooleanExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
Log.i(TAG,"------onCreate4----"+mHasSilentItem);
// Give the Activity so it can do managed queries
mRingtoneManager = new RingtoneManager(this);
// Get whether to include DRM ringtones
boolean includeDrm = intent.getBooleanExtra(RingtoneManager.EXTRA_RINGTONE_INCLUDE_DRM,
true);
mRingtoneManager.setIncludeDrm(includeDrm);
// Get the types of ringtones to show
int types = intent.getIntExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, -1);//ringtone
if (types != -1) {
mRingtoneManager.setType(types);
}
mCursor = mRingtoneManager.getCursor();
// The volume keys will control the stream that we are choosing a ringtone for
setVolumeControlStream(mRingtoneManager.inferStreamType());
// Get the URI whose list item should have a checkmark
mExistingUri = intent
.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI);
//add by me for T801T_P001087
Log.i(TAG,"------onCreate5----"+mExistingUri.toString());
// String absolutePath = getAbsoluteImagePath(mExistingUri);
// Log.i(TAG,"------onCreate6----"+absolutePath);
// File file = new File(absolutePath);
// if(!file.exists()) {
// int mRingtoneId = 0;
// Cursor cs = getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, null, null,
// null, null);
// if (cs!= null)
// while (!cs.isLast()) {
// cs.moveToNext();
// if(cs.getString(1).equals("/system/media/audio/ringtones/Bollywood.ogg"))
// mRingtoneId = cs.getInt(0);
// }
// Log.i(TAG,"------onCreate7----"+mRingtoneId);
// cs.close();
// Uri u = Uri.parse("content://media/internal/audio/media/");
// mExistingUri = ContentUris.withAppendedId(u, mRingtoneId);
// }
// Log.i(TAG,"------onCreate9----"+mExistingUri.toString());
//add end
final AlertController.AlertParams p = mAlertParams;
p.mCursor = mCursor;
p.mOnClickListener = mRingtoneClickListener;
p.mLabelColumn = MediaStore.Audio.Media.TITLE;
p.mIsSingleChoice = true;
p.mOnItemSelectedListener = this;
p.mPositiveButtonText = getString(com.android.internal.R.string.ok);
p.mPositiveButtonListener = this;
p.mNegativeButtonText = getString(com.android.internal.R.string.cancel);
p.mPositiveButtonListener = this;
p.mOnPrepareListViewListener = this;
p.mTitle = intent.getCharSequenceExtra(RingtoneManager.EXTRA_RINGTONE_TITLE);
if (p.mTitle == null) {
p.mTitle = getString(com.android.internal.R.string.ringtone_picker_title);
}
setupAlert();
}
//this method add by me
protected String getAbsoluteImagePath(Uri uri)
{
// can post image
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( uri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
//add end
public void onPrepareListView(ListView listView) {
Log.i(TAG,"------onPrepareListView----");
if (mHasDefaultItem) {//false
mDefaultRingtonePos = addDefaultRingtoneItem(listView);
Log.i(TAG,"------onPrepareListView1----"+mDefaultRingtonePos);
if (RingtoneManager.isDefault(mExistingUri)) {
Log.i(TAG,"------onPrepareListView4----");
mClickedPos = mDefaultRingtonePos;
}
}
Log.i(TAG,"------onPrepareListView2----"+mClickedPos); //-1
if (mHasSilentItem) {
mSilentPos = addSilentItem(listView);
// The 'Silent' item should use a null Uri
if (mExistingUri == null) { // not null
Log.i(TAG,"------onPrepareListView5----");
mClickedPos = mSilentPos;
}
}
if (mClickedPos == -1) { // yes,equals to -1
Log.i(TAG,"------onPrepareListView6----");
mClickedPos = getListPosition(mRingtoneManager.getRingtonePosition(mExistingUri));
}
//add by me
if (mClickedPos == -1) {
int mRingtoneId = 0;
Cursor cs = getContentResolver().query(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, null, null, null, null);
if (cs!= null)
while (!cs.isLast()) {
cs.moveToNext();
if(cs.getString(1).equals("/system/media/audio/ringtones/Bollywood.ogg"))
mRingtoneId = cs.getInt(0);
}
Log.i(TAG,"------onCreate7----"+mRingtoneId);
cs.close();
Uri u = Uri.parse("content://media/internal/audio/media/");
mExistingUri = ContentUris.withAppendedId(u, mRingtoneId);
mClickedPos = getListPosition(mRingtoneManager.getRingtonePosition(mExistingUri));
}
//add end
Log.i(TAG,"------onPrepareListView3----"+mClickedPos); // 1
// Put a checkmark next to an item.
mAlertParams.mCheckedItem = mClickedPos;
}
/**
* Adds a static item to the top of the list. A static item is one that is not from the
* RingtoneManager.
*
* @param listView The ListView to add to.
* @param textResId The resource ID of the text for the item.
* @return The position of the inserted item.
*/
private int addStaticItem(ListView listView, int textResId) {
Log.i(TAG,"------addStaticItem----");
TextView textView = (TextView) getLayoutInflater().inflate(
com.android.internal.R.layout.select_dialog_singlechoice, listView, false);
textView.setText(textResId);
Log.i(TAG,"------addStaticItem1----"+textResId);
listView.addHeaderView(textView);
mStaticItemCount++;
return listView.getHeaderViewsCount() - 1;
}
private int addDefaultRingtoneItem(ListView listView) {
Log.i(TAG,"------addDefaultRingtoneItem----");
return addStaticItem(listView, com.android.internal.R.string.ringtone_default);
}
private int addSilentItem(ListView listView) {
Log.i(TAG,"------addDefaultRingtoneItem----");
return addStaticItem(listView, com.android.internal.R.string.ringtone_silent);
}
/*
* On click of Ok/Cancel buttons
*/
public void onClick(DialogInterface dialog, int which) {
Log.i(TAG,"------onClick----");
boolean positiveResult = which == DialogInterface.BUTTON_POSITIVE;
// Stop playing the previous ringtone
mRingtoneManager.stopPreviousRingtone();
if (positiveResult) {
Intent resultIntent = new Intent();
Uri uri = null;
if (mClickedPos == mDefaultRingtonePos) {
// Set it to the default Uri that they originally gave us
uri = mUriForDefaultItem;
Log.i(TAG,"------onClick1----"+uri.toString());
} else if (mClickedPos == mSilentPos) {
// A null Uri is for the 'Silent' item
uri = null;
} else {
uri = mRingtoneManager.getRingtoneUri(getRingtoneManagerPosition(mClickedPos));
}
//Log.i(TAG,"------onClick2----"+uri.toString());
resultIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI, uri);
setResult(RESULT_OK, resultIntent);
} else {
setResult(RESULT_CANCELED);
}
getWindow().getDecorView().post(new Runnable() {
public void run() {
mCursor.deactivate();
}
});
finish();
}
/*
* On item selected via keys
*/
public void onItemSelected(AdapterView parent, View view, int position, long id) {
Log.i(TAG,"------onItemSelected----");
playRingtone(position, DELAY_MS_SELECTION_PLAYED);
}
public void onNothingSelected(AdapterView parent) {
Log.i(TAG,"------onNothingSelected----");
}
private void playRingtone(int position, int delayMs) {
Log.i(TAG,"------playRingtone----");
mHandler.removeCallbacks(this);
mSampleRingtonePos = position;
mHandler.postDelayed(this, delayMs);
}
public void run() {
Log.i(TAG,"------run----");
if (mSampleRingtonePos == mSilentPos) {
mRingtoneManager.stopPreviousRingtone();
return;
}
/*
* Stop the default ringtone, if it's playing (other ringtones will be
* stopped by the RingtoneManager when we get another Ringtone from it.
*/
if (mDefaultRingtone != null && mDefaultRingtone.isPlaying()) {
mDefaultRingtone.stop();
mDefaultRingtone = null;
}
Ringtone ringtone;
if (mSampleRingtonePos == mDefaultRingtonePos) {
if (mDefaultRingtone == null) {
mDefaultRingtone = RingtoneManager.getRingtone(this, mUriForDefaultItem);
}
ringtone = mDefaultRingtone;
/*
* Normally the non-static RingtoneManager.getRingtone stops the
* previous ringtone, but we're getting the default ringtone outside
* of the RingtoneManager instance, so let's stop the previous
* ringtone manually.
*/
mRingtoneManager.stopPreviousRingtone();
} else {
ringtone = mRingtoneManager.getRingtone(getRingtoneManagerPosition(mSampleRingtonePos));
}
if (ringtone != null) {
ringtone.play();
}
}
@Override
protected void onStop() {
Log.i(TAG,"------onStop----");
super.onStop();
stopAnyPlayingRingtone();
}
@Override
protected void onPause() {
Log.i(TAG,"------onPause----");
super.onPause();
stopAnyPlayingRingtone();
}
private void stopAnyPlayingRingtone() {
Log.i(TAG,"------stopAnyPlayingRingtone----");
if (mDefaultRingtone != null && mDefaultRingtone.isPlaying()) {
mDefaultRingtone.stop();
}
if (mRingtoneManager != null) {
mRingtoneManager.stopPreviousRingtone();
}
}
private int getRingtoneManagerPosition(int listPos) {
Log.i(TAG,"------getRingtoneManagerPosition----");
int i =listPos - mStaticItemCount;
Log.i(TAG,"------getRingtoneManagerPosition---- "+ i);
return listPos - mStaticItemCount;
}
private int getListPosition(int ringtoneManagerPos) {
Log.i(TAG,"------getListPosition----");
Log.i(TAG,"------getListPosition1----" + ringtoneManagerPos);
// If the manager position is -1 (for not found), return that
if (ringtoneManagerPos < 0) return ringtoneManagerPos;
int i = ringtoneManagerPos + mStaticItemCount;
Log.i(TAG,"------getListPosition2----" + i);
return ringtoneManagerPos + mStaticItemCount;
}
}
注意几个add by me,解决:音乐设置为铃声后,大容量存储模式下铃声列表界面所有铃声未选中