Fields Summary |
---|
private static final String | TAG |
private static final int | DELAY_MS_SELECTION_PLAYED |
private android.media.RingtoneManager | mRingtoneManager |
private android.database.Cursor | mCursor |
private android.os.Handler | mHandler |
private int | mSilentPosThe position in the list of the 'Silent' item. |
private int | mDefaultRingtonePosThe position in the list of the 'Default' item. |
private int | mClickedPosThe position in the list of the last clicked item. |
private int | mSampleRingtonePosThe position in the list of the ringtone to sample. |
private boolean | mHasSilentItemWhether this list has the 'Silent' item. |
private android.net.Uri | mExistingUriThe Uri to place a checkmark next to. |
private int | mStaticItemCountThe number of static items in the list. |
private boolean | mHasDefaultItemWhether this list has the 'Default' item. |
private android.net.Uri | mUriForDefaultItemThe Uri to play when the 'Default' item is clicked. |
private android.media.Ringtone | mDefaultRingtoneA 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 DialogInterface.OnClickListener | mRingtoneClickListener |
Methods Summary |
---|
private int | addDefaultRingtoneItem(android.widget.ListView listView)
return addStaticItem(listView, com.android.internal.R.string.ringtone_default);
|
private int | addSilentItem(android.widget.ListView listView)
return addStaticItem(listView, com.android.internal.R.string.ringtone_silent);
|
private int | addStaticItem(android.widget.ListView listView, int textResId)Adds a static item to the top of the list. A static item is one that is not from the
RingtoneManager.
TextView textView = (TextView) getLayoutInflater().inflate(
com.android.internal.R.layout.select_dialog_singlechoice, listView, false);
textView.setText(textResId);
listView.addHeaderView(textView);
mStaticItemCount++;
return listView.getHeaderViewsCount() - 1;
|
private int | getListPosition(int ringtoneManagerPos)
// If the manager position is -1 (for not found), return that
if (ringtoneManagerPos < 0) return ringtoneManagerPos;
return ringtoneManagerPos + mStaticItemCount;
|
private int | getRingtoneManagerPosition(int listPos)
return listPos - mStaticItemCount;
|
public void | onClick(android.content.DialogInterface dialog, int which)
boolean positiveResult = which == BUTTON1;
// 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;
} else if (mClickedPos == mSilentPos) {
// A null Uri is for the 'Silent' item
uri = null;
} else {
uri = mRingtoneManager.getRingtoneUri(getRingtoneManagerPosition(mClickedPos));
}
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();
|
protected void | onCreate(android.os.Bundle savedInstanceState)
super.onCreate(savedInstanceState);
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);
mUriForDefaultItem = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI);
if (mUriForDefaultItem == null) {
mUriForDefaultItem = Settings.System.DEFAULT_RINGTONE_URI;
}
// Get whether to show the 'Silent' item
mHasSilentItem = intent.getBooleanExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
// 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);
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);
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();
|
public void | onItemSelected(android.widget.AdapterView parent, android.view.View view, int position, long id)
playRingtone(position, DELAY_MS_SELECTION_PLAYED);
|
public void | onNothingSelected(android.widget.AdapterView parent)
|
protected void | onPause()
super.onPause();
stopAnyPlayingRingtone();
|
public void | onPrepareListView(android.widget.ListView listView)
if (mHasDefaultItem) {
mDefaultRingtonePos = addDefaultRingtoneItem(listView);
if (RingtoneManager.isDefault(mExistingUri)) {
mClickedPos = mDefaultRingtonePos;
}
}
if (mHasSilentItem) {
mSilentPos = addSilentItem(listView);
// The 'Silent' item should use a null Uri
if (mExistingUri == null) {
mClickedPos = mSilentPos;
}
}
if (mClickedPos == -1) {
mClickedPos = getListPosition(mRingtoneManager.getRingtonePosition(mExistingUri));
}
// Put a checkmark next to an item.
mAlertParams.mCheckedItem = mClickedPos;
|
protected void | onStop()
super.onStop();
stopAnyPlayingRingtone();
|
private void | playRingtone(int position, int delayMs)
mHandler.removeCallbacks(this);
mSampleRingtonePos = position;
mHandler.postDelayed(this, delayMs);
|
public void | run()
if (mSampleRingtonePos == mSilentPos) {
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();
}
|
private void | stopAnyPlayingRingtone()
if (mDefaultRingtone != null && mDefaultRingtone.isPlaying()) {
mDefaultRingtone.stop();
}
if (mRingtoneManager != null) {
mRingtoneManager.stopPreviousRingtone();
}
|