Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom sound #230

Merged
merged 3 commits into from
Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Change Log
All notable changes to this project will be documented in this file.

## 6.0.0 - 27/06/2020
* Support custom sound

## 5.1.3 - 06/06/2020
* Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion alerter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ apply from: rootProject.file('quality.gradle')

final String GROUP_ID = "com.tapadoo.android"

final String VERSION = "5.1.3"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @rezaiyan, thanks for the PR! Looks good, however I think it might be best to move to version 6.0.0, as this is a breaking change for those using the setSoundEnabled method!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure,
I agree but at first, I decided to create a new function, and next, I thought it's better to be easy to use and not confusing.
However, I update the version.

final String VERSION = "6.0.0"

final String DESCRIPTION = "An Android Alerting Library"
final String GITHUB_URL = "https://github.com/Tapadoo/Alerter"
Expand Down
25 changes: 10 additions & 15 deletions alerter/src/main/java/com/tapadoo/alerter/Alert.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.Context
import android.graphics.*
import android.graphics.drawable.Drawable
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.text.TextUtils
import android.util.AttributeSet
Expand Down Expand Up @@ -74,9 +75,9 @@ class Alert @JvmOverloads constructor(context: Context,
private var vibrationEnabled = true

/**
* Flag to enable / disable sound
* Uri to set sound
*/
private var soundEnabled = false
private var soundUri : Uri? = null

/**
* Sets the Layout Gravity of the Alert
Expand Down Expand Up @@ -219,15 +220,9 @@ class Alert @JvmOverloads constructor(context: Context,
if (vibrationEnabled) {
performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
}

if (soundEnabled) {
try {
val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val r = RingtoneManager.getRingtone(context, notification)
r.play()
} catch (e: Exception) {
e.printStackTrace()
}
soundUri?.let {
val r = RingtoneManager.getRingtone(context, soundUri)
r.play()
}

if (enableProgress) {
Expand Down Expand Up @@ -757,12 +752,12 @@ class Alert @JvmOverloads constructor(context: Context,
}

/**
* Enable or Disable sound
* Set sound Uri
*
* @param soundEnabled True to enable, false to disable
* @param soundUri To set sound Uri (raw folder)
*/
fun setSoundEnabled(soundEnabled: Boolean) {
this.soundEnabled = soundEnabled
fun setSound(soundUri: Uri?) {
this.soundUri = soundUri
}

/**
Expand Down
13 changes: 8 additions & 5 deletions alerter/src/main/java/com/tapadoo/alerter/Alerter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.graphics.ColorFilter
import android.graphics.PorterDuff
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.media.RingtoneManager
import android.net.Uri
import android.view.View
import android.view.ViewGroup
import android.view.animation.AnimationUtils
Expand Down Expand Up @@ -582,17 +584,18 @@ class Alerter private constructor() {
}

/**
* Enable or Disable Sound
* Set sound Uri
* if set null, sound will be disabled
*
* @param enable True to enable, False to disable
* @param uri To set sound Uri (raw folder)
* @return This Alerter
*/
fun enableSound(enable: Boolean): Alerter {
alert?.setSoundEnabled(enable)
@JvmOverloads
fun setSound(uri: Uri? = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)): Alerter {
alert?.setSound(uri)

return this
}

/**
* Disable touch events outside of the Alert
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tapadoo.alerter.demo;

import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
Expand Down Expand Up @@ -226,7 +227,7 @@ private void showAlertSound() {
.setTitle("Alert Title")
.setText("Alert text...")
.setBackgroundColorRes(R.color.colorAccent)
.enableSound(true)
.setSound(Uri.parse("android.resource://"+getPackageName()+"/raw/ringtone"))
.show();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.tapadoo.alerter.demo

import android.graphics.Typeface
import android.net.Uri
import android.os.Bundle
import android.view.Gravity
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.tapadoo.alerter.Alerter
import com.tapadoo.alerter.OnHideAlertListener
import com.tapadoo.alerter.OnShowAlertListener
Expand Down Expand Up @@ -159,7 +160,7 @@ class KotlinDemoActivity : AppCompatActivity() {
.setTitle("Alert Title")
.setText("Alert text...")
.setBackgroundColorRes(R.color.colorAccent)
.enableSound(true)
.setSound(Uri.parse("android.resource://$packageName/raw/ringtone"))
.show()
}

Expand Down
Binary file added app/src/main/res/raw/ringtone.mp3
Binary file not shown.