diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6791da6..ce2a603 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -17,7 +17,6 @@ - - + + + + + android:turnScreenOn="true" /> @@ -68,6 +75,7 @@ android:theme="@style/Theme.MyApplication"> + @@ -79,10 +87,10 @@ android:theme="@style/Theme.MyApplication"> + - \ No newline at end of file diff --git a/app/src/main/java/com/rooster/rooster/Alarm.kt b/app/src/main/java/com/rooster/rooster/Alarm.kt index 8b537dd..35d1a4a 100644 --- a/app/src/main/java/com/rooster/rooster/Alarm.kt +++ b/app/src/main/java/com/rooster/rooster/Alarm.kt @@ -10,6 +10,7 @@ class AlarmCreation( val label: String, val enabled: Boolean, val mode: String, + var ringtoneUri: String, var relative1: String, var relative2: String, var time1: Long, @@ -22,6 +23,7 @@ class Alarm( var label: String, var enabled: Boolean, var mode: String, + var ringtoneUri: String, var relative1: String, var relative2: String, var time1: Long, diff --git a/app/src/main/java/com/rooster/rooster/AlarmActivity.kt b/app/src/main/java/com/rooster/rooster/AlarmActivity.kt index ec4f662..af69d85 100644 --- a/app/src/main/java/com/rooster/rooster/AlarmActivity.kt +++ b/app/src/main/java/com/rooster/rooster/AlarmActivity.kt @@ -26,6 +26,7 @@ import java.util.Calendar import java.util.Date class AlarmActivity : FragmentActivity() { + private var alarmId: Long = 0 private var alarmIsRunning = false private var isVibrating = false @@ -33,8 +34,15 @@ class AlarmActivity : FragmentActivity() { private var mediaPlayer: MediaPlayer? = null private var wakeLock: PowerManager.WakeLock? = null + val alarmDbHelper = AlarmDbHelper(this) + + override fun onCreate(savedInstanceState: Bundle?) { - Log.e("Alarm", "Alarm Activity Start") + val alarm = alarmDbHelper.getAlarm(alarmId) + Log.e("Alarm", "Alarm Activity Start\n" + "Alarm id: $alarmId") + alarmId = intent.getStringExtra("alarm_id")!! + .toLong() // -1 is a default value if "alarm_id" is not found + Log.e("Alarmclock Reciever", "Alarm id: $alarmId") alarmIsRunning = true super.setShowWhenLocked(true) super.setTurnScreenOn(true) @@ -55,7 +63,9 @@ class AlarmActivity : FragmentActivity() { override fun onStopTrackingTouch(seekBar: SeekBar?) {} }) refreshCycle() - alarmRing() + if (alarm != null) { + alarmRing(alarm.ringtoneUri) + } //TODO Release wake lock val alarmHandler = AlarmHandler() alarmHandler.setNextAlarm(applicationContext) @@ -93,7 +103,7 @@ class AlarmActivity : FragmentActivity() { handler.post(updateRunnable) } - private fun alarmRing() { + private fun alarmRing(ringtoneUri: String) { // Wake Phone val powerManager: PowerManager = getSystemService(Context.POWER_SERVICE) as PowerManager window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) @@ -108,9 +118,13 @@ class AlarmActivity : FragmentActivity() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // it is safe to cancel other vibrations currently taking place vibrator!!.cancel() - val soundUri = + val soundUri = if (ringtoneUri == "default") { + // Fallback to a default sound if the URI is null or empty Uri.parse("android.resource://${applicationContext.packageName}/raw/alarmclock") - + } else { + // Use the provided URI + Uri.parse(ringtoneUri) + } mediaPlayer = MediaPlayer().apply { setDataSource(applicationContext, soundUri) setAudioAttributes( diff --git a/app/src/main/java/com/rooster/rooster/AlarmAdapter.kt b/app/src/main/java/com/rooster/rooster/AlarmAdapter.kt index 916830f..59ba3fe 100644 --- a/app/src/main/java/com/rooster/rooster/AlarmAdapter.kt +++ b/app/src/main/java/com/rooster/rooster/AlarmAdapter.kt @@ -3,6 +3,11 @@ package com.rooster.rooster import android.app.AlertDialog import android.app.TimePickerDialog import android.content.Context +import android.content.Intent +import android.media.RingtoneManager +import android.net.Uri +import android.os.Build +import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View @@ -126,6 +131,21 @@ class AlarmAdapter(private val alarmList: List, val alarmDbHelper: AlarmD setButtonState(dynamicButton, dynamicButton.isSelected) } + val ringtoneTitle = holder.alarmContainer.findViewById(R.id.ringtoneTitle) + val title = getRingtoneTitleFromUri(holder.alarmContainer.context, alarm.ringtoneUri) + ringtoneTitle.text = title + val ringtoneButton = holder.alarmContainer.findViewById(R.id.layoutRingtone) + ringtoneButton.setOnClickListener { + val ringtoneActivity = Intent(context, RingtoneActivity::class.java) + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.M || Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + ringtoneActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + val b = Bundle() + b.putLong("alarm_id", alarm.id) + ringtoneActivity.putExtras(b); + context.applicationContext.startActivity(ringtoneActivity); + } + // Vibrate var vibrateButton = holder.alarmContainer.findViewById(R.id.checkBoxVibrate) vibrateButton.setOnClickListener{ @@ -474,4 +494,16 @@ class AlarmAdapter(private val alarmList: List, val alarmDbHelper: AlarmD } button?.setBackgroundResource(bgDrawable) } + fun getRingtoneTitleFromUri(context: Context, ringtoneUri: String?): String { + if (ringtoneUri == null) return "Default Ringtone" // Return a default or an indication that no custom ringtone is set + + try { + val uri = Uri.parse(ringtoneUri) + val ringtone = RingtoneManager.getRingtone(context, uri) + return ringtone.getTitle(context) + } catch (e: Exception) { + e.printStackTrace() + return "Unknown Ringtone" // In case of any error, return a default name + } + } } \ No newline at end of file diff --git a/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt b/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt index 920095a..cee7572 100644 --- a/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt +++ b/app/src/main/java/com/rooster/rooster/AlarmDbHelper.kt @@ -16,6 +16,10 @@ import androidx.appcompat.app.AppCompatActivity import java.util.Date class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", null, 1) { + companion object { + const val DATABASE_VERSION = 2 // Increment database version + const val DATABASE_NAME = "alarm_db" + } private val alarmHandler = AlarmHandler() val context = context @@ -27,6 +31,7 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, label TEXT, mode TEXT, + ringtoneUri TEXT, relative1 TEXT, relative2 TEXT, time1 INTEGER, @@ -45,7 +50,9 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu } override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { - // TODO: Implement database migration if needed + if (oldVersion < 2) { + db.execSQL("ALTER TABLE alarms ADD COLUMN ringtoneUri TEXT") + } } fun insertAlarm(alarm: AlarmCreation) { @@ -53,6 +60,7 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu val values = ContentValues().apply { put("label", alarm.label) put("mode", alarm.mode) + put("ringtoneUri", alarm.ringtoneUri) put("relative1", alarm.relative1) put("relative2", alarm.relative2) put("time1", alarm.time1) @@ -89,6 +97,7 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu id = cursor.getLong(cursor.getColumnIndex("id")), label = cursor.getString(cursor.getColumnIndex("label")), mode = cursor.getString(cursor.getColumnIndex("mode")), + ringtoneUri = cursor.getString(cursor.getColumnIndex("ringtoneUri")), relative1 = cursor.getString(cursor.getColumnIndex("relative1")), relative2 = cursor.getString(cursor.getColumnIndex("relative2")), time1 = cursor.getLong(cursor.getColumnIndex("time1")), @@ -123,6 +132,7 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu val values = ContentValues().apply { put("label", alarm.label) put("mode", alarm.mode) + put("ringtoneUri", alarm.ringtoneUri) put("relative1", alarm.relative1) put("relative2", alarm.relative2) put("time1", alarm.time1) @@ -285,13 +295,14 @@ class AlarmDbHelper(context: Context) : SQLiteOpenHelper(context, "alarm_db", nu fun getAllAlarms(): List { val db = readableDatabase - val cursor = db.query("alarms", arrayOf("id", "label", "mode", "relative1", "relative2", "time1", "time2", "calculated_time", "enabled", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"), null, null, null, null, null) + val cursor = db.query("alarms", arrayOf("id", "label", "mode", "ringtoneUri", "relative1", "relative2", "time1", "time2", "calculated_time", "enabled", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"), null, null, null, null, null) val alarms = mutableListOf() while (cursor.moveToNext()) { alarms.add(Alarm( id = cursor.getLong(cursor.getColumnIndex("id")), label = cursor.getString(cursor.getColumnIndex("label")), mode = cursor.getString(cursor.getColumnIndex("mode")), + ringtoneUri = cursor.getString(cursor.getColumnIndex("ringtoneUri")), relative1 = cursor.getString(cursor.getColumnIndex("relative1")), relative2 = cursor.getString(cursor.getColumnIndex("relative2")), time1 = cursor.getLong(cursor.getColumnIndex("time1")), diff --git a/app/src/main/java/com/rooster/rooster/AlarmListActivity.kt b/app/src/main/java/com/rooster/rooster/AlarmListActivity.kt index c297b36..b23737b 100644 --- a/app/src/main/java/com/rooster/rooster/AlarmListActivity.kt +++ b/app/src/main/java/com/rooster/rooster/AlarmListActivity.kt @@ -22,7 +22,7 @@ class AlarmListActivity() : ComponentActivity() { private fun linkButtons() { val addAlarmButton = findViewById