Created
July 31, 2014 19:09
-
-
Save XinyueZ/1e219cfcf89e85da1366 to your computer and use it in GitHub Desktop.
Show list of tone on the device.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public final class ToneUtil { | |
private static final int DEFAULT_INDEX = 0; | |
private final static List<String> sTones = new ArrayList<String>(); | |
private final static List<Uri> sUris = new ArrayList<Uri>(); | |
public interface OnPostToneSearchingListener { | |
void onPostToneSearching(); | |
} | |
public interface OnToneSelectedListener { | |
void onToneSelectedListener(Uri _uri, String _name); | |
} | |
public interface OnNoToneSelectedListener { | |
void onNoToneSelected(); | |
} | |
private Context mContext; | |
public ToneUtil(Context _context) { | |
RingtoneManager ringtoneMgr = new RingtoneManager(_context); | |
ringtoneMgr.setType(RingtoneManager.TYPE_NOTIFICATION); | |
Cursor alarmsCursor = ringtoneMgr.getCursor(); | |
int alarmsCount = alarmsCursor.getCount(); | |
if (alarmsCount == 0 && !alarmsCursor.moveToFirst()) { | |
} else { | |
while (!alarmsCursor.isAfterLast() && alarmsCursor.moveToNext()) { | |
int currentPosition = alarmsCursor.getPosition(); | |
sTones.add(ringtoneMgr.getRingtone(currentPosition).getTitle(_context)); | |
sUris.add(ringtoneMgr.getRingtoneUri(currentPosition)); | |
} | |
alarmsCursor.close(); | |
} | |
mContext = _context; | |
} | |
public String getToneName(Uri _uri) { | |
return RingtoneManager.getRingtone(mContext, _uri).getTitle(mContext); | |
} | |
public int getIndex(Uri _uri) { | |
if (_uri == null) { | |
return DEFAULT_INDEX; | |
} else { | |
RingtoneManager ringtoneMgr = new RingtoneManager(mContext); | |
ringtoneMgr.setType(RingtoneManager.TYPE_NOTIFICATION); | |
return ringtoneMgr.getRingtonePosition(_uri); | |
} | |
} | |
public void showToneList(Context _context, int _preSelected, | |
OnPostToneSearchingListener _onPostToneSearchingListener, | |
final OnToneSelectedListener _onToneSelectedListener, | |
final OnNoToneSelectedListener _onNoToneSelectedListener) { | |
AlertDialog.Builder builder = new AlertDialog.Builder(_context); | |
ListView toneList = new ListView(_context); | |
TextView head = (TextView) View.inflate(_context, android.R.layout.simple_list_item_1, null); | |
head.setText("None"); | |
toneList.addHeaderView(head); | |
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(_context, android.R.layout.simple_list_item_1, | |
android.R.id.text1, sTones); | |
toneList.setAdapter(modeAdapter); | |
builder.setView(toneList); | |
final Dialog dialog = builder.create(); | |
dialog.show(); | |
_onPostToneSearchingListener.onPostToneSearching(); | |
toneList.setOnItemClickListener(new OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> _arg0, View _arg1, final int _location, long _arg3) { | |
if (_onToneSelectedListener != null) { | |
int location = _location - 1; | |
_onToneSelectedListener.onToneSelectedListener(sUris.get(location), sTones.get(location)); | |
} | |
dialog.dismiss(); | |
} | |
}); | |
head.setGravity(Gravity.CENTER); | |
head.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View _v) { | |
if (_onNoToneSelectedListener != null) { | |
_onNoToneSelectedListener.onNoToneSelected(); | |
} | |
dialog.dismiss(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment