Notification tone fix

This commit is contained in:
z3r0c00l-2k 2019-05-20 12:59:53 +05:30
parent e8eab3a127
commit 20b44214ef
6 changed files with 76 additions and 14 deletions

View file

@ -1,8 +1,12 @@
package io.github.z3r0c00l_2k.aquadroid.fragments
import android.app.Activity
import android.app.TimePickerDialog
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.media.RingtoneManager
import android.net.Uri
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
@ -11,6 +15,7 @@ import android.view.ViewGroup
import android.widget.Toast
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import io.github.z3r0c00l_2k.aquadroid.R
import io.github.z3r0c00l_2k.aquadroid.helpers.AlarmHelper
import io.github.z3r0c00l_2k.aquadroid.utils.AppUtils
import kotlinx.android.synthetic.main.bottom_sheet_fragment.*
import java.math.RoundingMode
@ -28,12 +33,14 @@ class BottomSheetFragment(val mCtx: Context) : BottomSheetDialogFragment() {
private var sleepingTime: String = ""
private var notificMsg: String = ""
private var notificFrequency: Int = 0
private var currentToneUri: String? = ""
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.bottom_sheet_fragment, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
@ -50,6 +57,11 @@ class BottomSheetFragment(val mCtx: Context) : BottomSheetDialogFragment() {
"Hey... Lets drink some water...."
)
)
currentToneUri = sharedPref.getString(
AppUtils.NOTIFICATION_TONE_URI_KEY,
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString()
)
etRingtone.editText!!.setText(RingtoneManager.getRingtone(mCtx, Uri.parse(currentToneUri)).getTitle(mCtx))
radioNotificItervel.setOnClickedButtonListener { button, position ->
notificFrequency = when (position) {
@ -70,6 +82,16 @@ class BottomSheetFragment(val mCtx: Context) : BottomSheetDialogFragment() {
}
}
etRingtone.editText!!.setOnClickListener {
val intent = Intent(RingtoneManager.ACTION_RINGTONE_PICKER)
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION)
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select ringtone for notifications:")
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false)
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true)
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentToneUri)
startActivityForResult(intent, 999)
}
etWakeUpTime.editText!!.setOnClickListener {
val hour: Int
@ -191,9 +213,24 @@ class BottomSheetFragment(val mCtx: Context) : BottomSheetDialogFragment() {
editor.putInt(AppUtils.TOTAL_INTAKE, df.format(totalIntake).toInt())
editor.apply()
Toast.makeText(mCtx, "Values updated successfully", Toast.LENGTH_SHORT).show()
val alarmHelper = AlarmHelper()
alarmHelper.cancelAlarm(mCtx)
alarmHelper.setAlarm(mCtx, sharedPref.getInt(AppUtils.NOTIFICATION_FREQUENCY_KEY, 30).toLong())
dismiss()
}
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && requestCode == 999) {
val uri = data!!.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI) as Uri
currentToneUri = uri.toString()
sharedPref.edit().putString(AppUtils.NOTIFICATION_TONE_URI_KEY, currentToneUri).apply()
val ringtone = RingtoneManager.getRingtone(mCtx, uri)
etRingtone.editText!!.setText(ringtone.getTitle(mCtx))
}
}
}

View file

@ -71,5 +71,6 @@ class AlarmHelper {
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
Log.i("AlarmHelper", "Cancelling alarms")
}
}

View file

@ -7,9 +7,9 @@ import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.media.AudioAttributes
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.preference.PreferenceManager
import android.util.Log
import io.github.z3r0c00l_2k.aquadroid.R
import io.github.z3r0c00l_2k.aquadroid.utils.AppUtils
@ -25,7 +25,11 @@ class NotificationHelper(val ctx: Context) {
private fun createChannels() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val prefs = ctx.getSharedPreferences(AppUtils.USERS_SHARED_PREF, AppUtils.PRIVATE_MODE)
val notificationsNewMessageRingtone = prefs.getString("notifications_new_message_ringtone", "")
val notificationsNewMessageRingtone = prefs.getString(
AppUtils.NOTIFICATION_TONE_URI_KEY, RingtoneManager.getDefaultUri(
RingtoneManager.TYPE_NOTIFICATION
).toString()
)
val notificationChannel = NotificationChannel(
CHANNEL_ONE_ID,
CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH
@ -49,7 +53,8 @@ class NotificationHelper(val ctx: Context) {
fun getNotification(
title: String,
body: String
body: String,
notificationsTone: String?
): Notification.Builder {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
return Notification.Builder(ctx.applicationContext, CHANNEL_ONE_ID)
@ -67,8 +72,6 @@ class NotificationHelper(val ctx: Context) {
.setSmallIcon(R.drawable.ic_small_logo)
.setAutoCancel(true)
} else {
val prefs = PreferenceManager.getDefaultSharedPreferences(ctx)
val notificationsNewMessageRingtone = prefs.getString("notifications_new_message_ringtone", "")
val notification = Notification.Builder(ctx.applicationContext)
.setContentTitle(title)
@ -82,14 +85,10 @@ class NotificationHelper(val ctx: Context) {
.setSmallIcon(R.drawable.ic_small_logo)
.setAutoCancel(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
notification.setShowWhen(true)
}
if (notificationsNewMessageRingtone!!.length > 0) {
notification.setSound(Uri.parse(notificationsTone))
notification.setSound(Uri.parse(notificationsNewMessageRingtone))
}
return notification
}
}

View file

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.media.RingtoneManager
import io.github.z3r0c00l_2k.aquadroid.R
import io.github.z3r0c00l_2k.aquadroid.helpers.NotificationHelper
import io.github.z3r0c00l_2k.aquadroid.utils.AppUtils
@ -13,6 +14,11 @@ class NotifierReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val prefs = context.getSharedPreferences(AppUtils.USERS_SHARED_PREF, AppUtils.PRIVATE_MODE)
val notificationsTone = prefs.getString(
AppUtils.NOTIFICATION_TONE_URI_KEY, RingtoneManager.getDefaultUri(
RingtoneManager.TYPE_NOTIFICATION
).toString()
)
val title = context.resources.getString(R.string.app_name)
val messageToShow = prefs.getString(
@ -23,7 +29,7 @@ class NotifierReceiver : BroadcastReceiver() {
/* Notify */
val nHelper = NotificationHelper(context)
@SuppressLint("ResourceType") val nBuilder = nHelper
.getNotification(title, messageToShow)
.getNotification(title, messageToShow, notificationsTone)
nHelper.notify(1, nBuilder)
}

View file

@ -36,5 +36,6 @@ class AppUtils {
val NOTIFICATION_MSG_KEY = "notificationmsg"
val SLEEPING_TIME_KEY = "sleepingtime"
val WAKEUP_TIME = "wakeuptime"
val NOTIFICATION_TONE_URI_KEY = "notificationtone"
}
}

View file

@ -126,7 +126,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/textView8"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="28dp" android:layout_marginTop="16dp"
android:layout_marginStart="16dp" android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/etSleepTime"/>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
@ -145,6 +145,24 @@
android:background="@null"
android:hint="Notification messgae" android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:theme="@style/TextInputLayoutAppearance"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="16dp"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="@+id/etNotificationText"
android:id="@+id/etRingtone">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="45dp"
android:paddingStart="10dp"
android:clickable="true"
android:paddingBottom="10dp"
android:background="@null"
android:hint="Notification tone" android:inputType="text" android:focusable="false"/>
</com.google.android.material.textfield.TextInputLayout>
<co.ceryle.radiorealbutton.RadioRealButtonGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -156,7 +174,7 @@
app:rrbg_selectorColor="@color/colorSkyBlue"
app:rrbg_selectorSize="6dp"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/etNotificationText"
app:layout_constraintTop_toBottomOf="@+id/etRingtone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"