|
| 1 | +package project.first.audiometrysimulator; |
| 2 | + |
| 3 | +import android.annotation.SuppressLint; |
| 4 | +import android.app.Activity; |
| 5 | +import android.content.ContentResolver; |
| 6 | +import android.content.ContentValues; |
| 7 | +import android.content.Context; |
| 8 | +import android.content.Intent; |
| 9 | +import android.content.res.AssetFileDescriptor; |
| 10 | +import android.media.MediaPlayer; |
| 11 | +import android.media.RingtoneManager; |
| 12 | +import android.net.Uri; |
| 13 | +import android.os.Environment; |
| 14 | +import android.provider.BaseColumns; |
| 15 | +import android.provider.ContactsContract; |
| 16 | +import android.provider.MediaStore; |
| 17 | +import android.util.AttributeSet; |
| 18 | +import android.view.MenuItem; |
| 19 | +import android.view.View; |
| 20 | +import android.widget.FrameLayout; |
| 21 | +import android.widget.ImageView; |
| 22 | +import android.widget.MediaController; |
| 23 | +import android.widget.PopupMenu; |
| 24 | +import android.widget.TextView; |
| 25 | +import android.widget.Toast; |
| 26 | +import android.widget.VideoView; |
| 27 | + |
| 28 | +import androidx.activity.result.ActivityResultLauncher; |
| 29 | +import androidx.activity.result.contract.ActivityResultContracts; |
| 30 | +import androidx.annotation.NonNull; |
| 31 | +import androidx.annotation.Nullable; |
| 32 | +import androidx.appcompat.app.AppCompatActivity; |
| 33 | +import androidx.core.content.res.ResourcesCompat; |
| 34 | + |
| 35 | +import java.io.File; |
| 36 | +import java.io.FileDescriptor; |
| 37 | +import java.io.FileOutputStream; |
| 38 | +import java.io.IOException; |
| 39 | +import java.io.InputStream; |
| 40 | +import java.io.OutputStream; |
| 41 | + |
| 42 | +public class AudioSection extends FrameLayout { |
| 43 | + private AssetFileDescriptor audio; |
| 44 | + public RingtoneSetter ringtoneSetter; |
| 45 | + public ImageView play,menu; |
| 46 | + public VideoView wave; |
| 47 | + public boolean isPlaying = false; |
| 48 | + |
| 49 | + public AudioSection(@NonNull Context context) { |
| 50 | + super(context); |
| 51 | + } |
| 52 | + |
| 53 | + public AudioSection(@NonNull Context context, @Nullable AttributeSet attrs) { |
| 54 | + super(context, attrs); |
| 55 | + init(context,attrs); |
| 56 | + } |
| 57 | + |
| 58 | + public AudioSection(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| 59 | + super(context, attrs, defStyleAttr); |
| 60 | + init(context,attrs); |
| 61 | + } |
| 62 | + |
| 63 | + public AudioSection(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 64 | + super(context, attrs, defStyleAttr, defStyleRes); |
| 65 | + init(context,attrs); |
| 66 | + } |
| 67 | + |
| 68 | + private void init(Context context,AttributeSet attrs){ |
| 69 | + var view = inflate(context, R.layout.audio_section,this); |
| 70 | + TextView title = view.findViewById(R.id.title); |
| 71 | + wave = view.findViewById(R.id.wave); |
| 72 | + menu = view.findViewById(R.id.menu); |
| 73 | + play = view.findViewById(R.id.play); |
| 74 | + |
| 75 | + wave.setVideoURI(Uri.parse("android.resource://" + context.getPackageName() + "/" + R.raw.waveform)); |
| 76 | + wave.setOnPreparedListener(mp -> mp.setLooping(true)); |
| 77 | + |
| 78 | + var attributes = context.obtainStyledAttributes(attrs,R.styleable.AudioSection); |
| 79 | + var titleStr = attributes.getString(R.styleable.AudioSection_title); |
| 80 | + var rawId = attributes.getResourceId(R.styleable.AudioSection_audio,0); |
| 81 | + audio = context.getResources().openRawResourceFd(rawId); |
| 82 | + title.setText(titleStr); |
| 83 | + |
| 84 | + menu.setOnClickListener(v -> { |
| 85 | + var pref = context.getSharedPreferences("ringtone",Context.MODE_PRIVATE); |
| 86 | + var menu = new PopupMenu(context,v); |
| 87 | + menu.getMenu().add(0,1,0,"Set as Ringtone"); |
| 88 | + menu.getMenu().add(0,0,1,"Restore ringtone"); |
| 89 | + menu.setOnMenuItemClickListener(item -> { |
| 90 | + if (item.getItemId() == 1){ |
| 91 | + String ringtone; |
| 92 | + if (!pref.getBoolean(titleStr,false)){ |
| 93 | + ringtone = copyRingtoneToSystem(rawId,titleStr,context).toString(); |
| 94 | + var editor = pref.edit(); |
| 95 | + editor.putString("ringtone", ringtone); |
| 96 | + editor.putBoolean(titleStr,true); |
| 97 | + editor.apply(); |
| 98 | + } else |
| 99 | + ringtone = pref.getString("ringtone",""); |
| 100 | + |
| 101 | + ringtoneSetter.setRingtone(Uri.parse(ringtone)); |
| 102 | + } else { |
| 103 | + var contact = pref.getString("lastContact_" + titleStr,null); |
| 104 | + var ringtone = RingtoneManager.getActualDefaultRingtoneUri(context,RingtoneManager.TYPE_RINGTONE); |
| 105 | + ringtoneSetter.restoreRingtone(Uri.parse(contact),ringtone); |
| 106 | + } |
| 107 | + return true; |
| 108 | + }); |
| 109 | + menu.show(); |
| 110 | + }); |
| 111 | + |
| 112 | + attributes.recycle(); |
| 113 | + } |
| 114 | + |
| 115 | + public void play(MediaPlayer player){ |
| 116 | + isPlaying = true; |
| 117 | + wave.setVisibility(View.VISIBLE); |
| 118 | + wave.start(); |
| 119 | + play.setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.paue,null)); |
| 120 | + try { |
| 121 | + player.setDataSource(audio); |
| 122 | + player.prepare(); |
| 123 | + player.start(); |
| 124 | + } catch (Exception e){ |
| 125 | + e.printStackTrace(); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + public void stop(MediaPlayer player){ |
| 130 | + isPlaying = false; |
| 131 | + player.stop(); |
| 132 | + player.reset(); |
| 133 | + wave.pause(); |
| 134 | + wave.setVisibility(View.INVISIBLE); |
| 135 | + play.setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.play,null)); |
| 136 | + } |
| 137 | + |
| 138 | + private Uri copyRingtoneToSystem(int rawFile,String title,Context context){ |
| 139 | + ContentResolver resolver = context.getContentResolver(); |
| 140 | + var values = new ContentValues(); |
| 141 | + values.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_RINGTONES); |
| 142 | + values.put(MediaStore.MediaColumns.DISPLAY_NAME,title + ".mp3"); |
| 143 | + values.put(MediaStore.MediaColumns.TITLE,title); |
| 144 | + values.put(MediaStore.MediaColumns.MIME_TYPE,"audio/mp3"); |
| 145 | + values.put(MediaStore.Audio.Media.IS_RINGTONE,true); |
| 146 | + var newUri = resolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,values); |
| 147 | + |
| 148 | + try { |
| 149 | + var inputStream = getResources().openRawResource(rawFile); |
| 150 | + OutputStream fileOutputStream = getContext().getContentResolver().openOutputStream(newUri); |
| 151 | + byte[] buffer = new byte[1024]; |
| 152 | + int length; |
| 153 | + while ((length = inputStream.read(buffer)) > 0) { |
| 154 | + fileOutputStream.write(buffer, 0, length); |
| 155 | + } |
| 156 | + inputStream.close(); |
| 157 | + fileOutputStream.close(); |
| 158 | + return newUri; |
| 159 | + } catch (IOException e) { |
| 160 | + e.printStackTrace(); |
| 161 | + Toast.makeText(context, "IOExcpetion : " + e.getMessage(), Toast.LENGTH_SHORT).show(); |
| 162 | + return null; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + public static void setRingtone(Context context,Uri contact,Uri ringtone){ |
| 167 | + var values = new ContentValues(); |
| 168 | + values.put(ContactsContract.Contacts.CUSTOM_RINGTONE,ringtone.toString()); |
| 169 | + context.getContentResolver().update(contact,values,null,null); |
| 170 | + |
| 171 | + context.getSharedPreferences("ringtone",Context.MODE_PRIVATE) |
| 172 | + .edit() |
| 173 | + .putString("lastContact_" + getFileName(ringtone,context),contact.toString()) |
| 174 | + .apply(); |
| 175 | + } |
| 176 | + |
| 177 | + public static String getFileName(Uri uri,Context context){ |
| 178 | + var cursor = context.getContentResolver().query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME},null,null,null); |
| 179 | + if (cursor != null && cursor.moveToFirst()){ |
| 180 | + @SuppressLint("Range") var name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); |
| 181 | + cursor.close(); |
| 182 | + return name; |
| 183 | + } |
| 184 | + return "default"; |
| 185 | + } |
| 186 | + |
| 187 | + public interface RingtoneSetter { |
| 188 | + void setRingtone(Uri ringtone); |
| 189 | + void restoreRingtone(Uri contact,Uri ringtone); |
| 190 | + } |
| 191 | +} |
0 commit comments