Closed Cornholio2108 closed 4 years ago
Add ringtone selection. Snippet from last version:
public void changeRingtone(final EditAlarmFragment editAlarmFragment) { final Dialog dialog = new Dialog(getContext()); dialog.setContentView(R.layout.choose_alarm_dialog); dialog.setTitle("Choose Ringtone Type"); if (dialog != null) { dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } TextView textViewTitle = dialog.findViewById(R.id.textViewTitle); textViewTitle.setText("Choose Ringtone Type"); Button btnSystemAlarms = dialog.findViewById(R.id.buttonSystemAlarms); Button btnMyMusic = dialog.findViewById(R.id.buttonMyMusic); ImageView dialogButtonOK = dialog.findViewById(R.id.imageViewOk); ImageView dialogButtonCancel = dialog.findViewById(R.id.imageViewCancel); btnSystemAlarms.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone"); intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null); editAlarmFragment.startActivityForResult(intent, 5); dialog.dismiss(); } }); btnMyMusic.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setNewRingtone(); dialog.dismiss(); } }); dialogButtonOK.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialogButtonCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); }
public void setNewRingtone() { final Dialog dialog = new Dialog(getContext()); dialog.setContentView(R.layout.ringtone_dialog); dialog.setTitle("Error"); if (dialog != null) { dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } TextView textViewTitle = dialog.findViewById(R.id.textViewTitle); ListView listView = dialog.findViewById(R.id.listView); textViewTitle.setText("Ringtones"); ContentResolver cr = getContext().getContentResolver(); Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0"; String sortOrder = MediaStore.Audio.Media.TITLE + " ASC"; Cursor cur = cr.query(uri, null, selection, null, sortOrder); int count = 0; final List<String> ringtoneArray = new ArrayList<>(); final List<String>ringtoneNames = new ArrayList<>(); final List<String>ringtoneTitles = new ArrayList<>(); if(cur != null) { count = cur.getCount(); if(count > 0) { while(cur.moveToNext()) { String data = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DATA)); // Add code to get more column here String title = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.TITLE)); String artist = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.ARTIST)); Log.d(TAG, "setNewRingtone: "+data); // Save to your list here ringtoneArray.add(data); ringtoneNames.add(artist+" - "+title); ringtoneTitles.add(title); } } } cur.close(); String[] array = ringtoneNames.toArray(new String[ringtoneNames.size()]); ArrayAdapter adapter = new ArrayAdapter<String>(getContext(),R.layout.ringtone_recycler_item,R.id.mytextview,array); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView<?>adapter,View v, int position, long test) { //Object item = adapter.getItemAtPosition(position); try { SharedPreferences myPrefs = getContext().getSharedPreferences(SP_TAG,0); SharedPreferences.Editor editor= myPrefs.edit(); editor.putString(SP_RINGTONE,ringtoneArray.get(position)); editor.putString(SP_RINGTONENAME,ringtoneTitles.get(position)); editor.apply(); } catch (Throwable t) { Log.e(TAG, "onOptionsItemSelected: ", t); } } }); ImageView dialogButtonOK = dialog.findViewById(R.id.imageViewOk); ImageView dialogButtonCancel = dialog.findViewById(R.id.imageViewCancel); dialogButtonOK.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { onNewRingtoneResult(); dialog.dismiss(); } }); dialogButtonCancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); }
public void onNewRingtoneResult() { Log.d(TAG, "onNewRingtoneResult: "+myPrefs.getString(SP_RINGTONENAME,"")); eTRingtone.setText(myPrefs.getString(SP_RINGTONENAME,"")); } @Override public void onActivityResult(final int requestCode, final int resultCode, final Intent intent) { if (resultCode == Activity.RESULT_OK && requestCode == 5) { Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if (uri != null) { SharedPreferences.Editor editor = myPrefs.edit(); editor.putString(SP_RINGTONE,uri.toString()); Ringtone ringtone = RingtoneManager.getRingtone(this.getContext(), uri); String title = ringtone.getTitle(this.getContext()); Log.d(TAG, "onActivityResult: "+title); editor.putString(SP_RINGTONENAME,title); editor.apply(); Log.d(TAG, "onActivityResult: "+uri.toString()); eTRingtone.setText(title); } else { //this.chosenRingtone = null; Log.d(TAG, "onActivityResult: null"); } } }
Done
Add ringtone selection. Snippet from last version: