From 17b2b7a21b1a8bb2e7afdbff90293009f1b26057 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Sat, 19 Aug 2017 23:18:47 -0700 Subject: Feature/ringtone selector (#200) * Update changelog * Create and test ringtone selector * Update changelog * Update text --- .../frost/activities/SettingsActivity.kt | 29 ++++++++++++++++ .../frost/activities/WebOverlayActivity.kt | 1 - .../pitchedapps/frost/enums/MainActivityLayout.kt | 2 +- .../com/pitchedapps/frost/fragments/WebFragment.kt | 2 +- .../pitchedapps/frost/intro/IntroFragmentTheme.kt | 2 +- .../frost/services/FrostNotifications.kt | 40 +++++++++++++--------- .../frost/services/NotificationService.kt | 4 +-- .../pitchedapps/frost/settings/Notifications.kt | 38 ++++++++++++++++++-- .../com/pitchedapps/frost/utils/Downloader.kt | 6 ++-- .../kotlin/com/pitchedapps/frost/utils/Prefs.kt | 6 +++- .../main/res/values/strings_pref_notifications.xml | 7 +++- app/src/main/res/values/strings_preferences.xml | 2 +- app/src/main/res/xml/frost_changelog.xml | 10 ++++-- 13 files changed, 116 insertions(+), 33 deletions(-) (limited to 'app') diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt index 7023fbc2..497ee375 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/SettingsActivity.kt @@ -3,6 +3,8 @@ package com.pitchedapps.frost.activities import android.annotation.SuppressLint import android.app.Activity import android.content.Intent +import android.media.RingtoneManager +import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem @@ -32,12 +34,39 @@ class SettingsActivity : KPrefActivity(), FrostBilling by IABSettings() { var resultFlag = Activity.RESULT_CANCELED + companion object { + private const val REQUEST_RINGTONE = 0b10111 shl 5 + const val REQUEST_NOTIFICATION_RINGTONE = REQUEST_RINGTONE or 1 + const val REQUEST_MESSAGE_RINGTONE = REQUEST_RINGTONE or 2 + } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (fetchRingtone(requestCode, resultCode, data)) return if (!onActivityResultBilling(requestCode, resultCode, data)) super.onActivityResult(requestCode, resultCode, data) reloadList() } + /** + * Fetch ringtone and save uri + * Returns [true] if consumed, [false] otherwise + */ + private fun fetchRingtone(requestCode: Int, resultCode: Int, data: Intent?): Boolean { + if (requestCode and REQUEST_RINGTONE != REQUEST_RINGTONE || resultCode != Activity.RESULT_OK) return false + val uri: String = data?.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI)?.toString() ?: "" + when (requestCode) { + REQUEST_NOTIFICATION_RINGTONE -> { + Prefs.notificationRingtone = uri + reloadByTitle(R.string.notification_ringtone) + } + REQUEST_MESSAGE_RINGTONE -> { + Prefs.messageRingtone = uri + reloadByTitle(R.string.message_ringtone) + } + } + return true + } + override fun kPrefCoreAttributes(): CoreAttributeContract.() -> Unit = { textColor = { Prefs.textColor } accentColor = { Prefs.accentColor } diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt index 51aedae3..756d5d35 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/WebOverlayActivity.kt @@ -13,7 +13,6 @@ import android.webkit.WebChromeClient import ca.allanwang.kau.internal.KauBaseActivity import ca.allanwang.kau.swipe.kauSwipeOnCreate import ca.allanwang.kau.swipe.kauSwipeOnDestroy -import ca.allanwang.kau.swipe.kauSwipeOnPostCreate import ca.allanwang.kau.utils.* import com.mikepenz.community_material_typeface_library.CommunityMaterial import com.mikepenz.google_material_typeface_library.GoogleMaterial diff --git a/app/src/main/kotlin/com/pitchedapps/frost/enums/MainActivityLayout.kt b/app/src/main/kotlin/com/pitchedapps/frost/enums/MainActivityLayout.kt index b3d71cfc..79b11752 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/enums/MainActivityLayout.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/enums/MainActivityLayout.kt @@ -23,7 +23,7 @@ enum class MainActivityLayout( { Prefs.textColor }); companion object { - val values = MainActivityLayout.values() //save one instance + val values = values() //save one instance operator fun invoke(index: Int) = values[index] } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt index 1a640ba5..fd2b6cc1 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/fragments/WebFragment.kt @@ -8,8 +8,8 @@ import android.view.View import android.view.ViewGroup import ca.allanwang.kau.utils.withArguments import com.pitchedapps.frost.activities.MainActivity -import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.enums.FeedSort +import com.pitchedapps.frost.facebook.FbItem import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.web.FrostWebView import com.pitchedapps.frost.web.FrostWebViewCore diff --git a/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroFragmentTheme.kt b/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroFragmentTheme.kt index 3134ff11..2e7d3572 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroFragmentTheme.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/intro/IntroFragmentTheme.kt @@ -6,8 +6,8 @@ import ca.allanwang.kau.utils.bindViewResettable import ca.allanwang.kau.utils.scaleXY import com.pitchedapps.frost.R import com.pitchedapps.frost.activities.IntroActivity -import com.pitchedapps.frost.utils.Prefs import com.pitchedapps.frost.enums.Theme +import com.pitchedapps.frost.utils.Prefs /** * Created by Allan Wang on 2017-07-28. diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt index d3dfe79c..810e5f97 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/FrostNotifications.kt @@ -34,27 +34,34 @@ import org.jetbrains.anko.runOnUiThread * Logic for build notifications, scheduling notifications, and showing notifications */ -/** - * Wrap the default builder with our icon and accent color - */ + val Context.frostNotification: NotificationCompat.Builder - get() = NotificationCompat.Builder(this, BuildConfig.APPLICATION_ID).apply { - setSmallIcon(R.drawable.frost_f_24) - setAutoCancel(true) - color = color(R.color.frost_notification_accent) - } + get() = frostNotification() /** - * Assign global changes to the notification after it is built + * Wrap the default builder with our icon and accent color */ -@Suppress("DEPRECATION") - //The update feature is for Android O and seems to still be in beta -fun Notification.frostConfig() = apply { +fun Context.frostNotification(ringtone: String = Prefs.notificationRingtone): NotificationCompat.Builder + = NotificationCompat.Builder(this, BuildConfig.APPLICATION_ID).apply { + setSmallIcon(R.drawable.frost_f_24) + setAutoCancel(true) + color = color(R.color.frost_notification_accent) + var defaults = 0 if (Prefs.notificationVibrate) defaults = defaults or Notification.DEFAULT_VIBRATE - if (Prefs.notificationSound) defaults = defaults or Notification.DEFAULT_SOUND + if (Prefs.notificationSound) { + if (ringtone.isNotBlank()) setSound(Uri.parse(ringtone)) + else defaults = defaults or Notification.DEFAULT_SOUND + } if (Prefs.notificationLights) defaults = defaults or Notification.DEFAULT_LIGHTS + setDefaults(defaults) } +val NotificationCompat.Builder.quiet + get() = apply { setDefaults(0) } + +val NotificationCompat.Builder.messageRingtone + get() = apply { } + val NotificationCompat.Builder.withBigText: NotificationCompat.BigTextStyle get() = NotificationCompat.BigTextStyle(this) @@ -72,7 +79,7 @@ class FrostNotificationTarget(val context: Context, override fun onResourceReady(resource: Bitmap, transition: Transition) { builder.setLargeIcon(resource) - NotificationManagerCompat.from(context).notify(notifTag, notifId, builder.withBigText.build().frostConfig()) + NotificationManagerCompat.from(context).notify(notifTag, notifId, builder.withBigText.build()) } } @@ -99,7 +106,8 @@ data class NotificationContent(val data: CookieModel, intent.putExtra(ARG_USER_ID, data.id) val group = "${groupPrefix}_${data.id}" val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0) - val notifBuilder = context.frostNotification + val ringtone = if (groupPrefix == FROST_MESSAGE_NOTIFICATION_GROUP) Prefs.messageRingtone else Prefs.notificationRingtone + val notifBuilder = context.frostNotification(ringtone) .setContentTitle(title ?: context.string(R.string.frost_name)) .setContentText(text) .setContentIntent(pendingIntent) @@ -109,7 +117,7 @@ data class NotificationContent(val data: CookieModel, if (timestamp != -1L) notifBuilder.setWhen(timestamp * 1000) L.v("Notif load", this.toString()) - NotificationManagerCompat.from(context).notify(group, notifId, notifBuilder.withBigText.build().frostConfig()) + NotificationManagerCompat.from(context).notify(group, notifId, notifBuilder.withBigText.build()) if (profileUrl.isNotBlank()) { context.runOnUiThread { diff --git a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt index 5859a306..9321c42f 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/services/NotificationService.kt @@ -233,7 +233,7 @@ class NotificationService : JobService() { val notifBuilder = frostNotification .setContentTitle(string(R.string.frost_name)) .setContentText(text) - NotificationManagerCompat.from(this).notify(999, notifBuilder.build().frostConfig()) + NotificationManagerCompat.from(this).notify(999, notifBuilder.build()) } private fun summaryNotification(userId: Long, count: Int, contentRes: Int, pendingUrl: String, groupPrefix: String) { @@ -250,7 +250,7 @@ class NotificationService : JobService() { .setContentIntent(pendingIntent) .setCategory(Notification.CATEGORY_SOCIAL) - NotificationManagerCompat.from(this).notify("${groupPrefix}_$userId", userId.toInt(), notifBuilder.build().frostConfig()) + NotificationManagerCompat.from(this).notify("${groupPrefix}_$userId", userId.toInt(), notifBuilder.build()) } } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/settings/Notifications.kt b/app/src/main/kotlin/com/pitchedapps/frost/settings/Notifications.kt index 8fcb3594..429b74fb 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/settings/Notifications.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/settings/Notifications.kt @@ -1,7 +1,12 @@ package com.pitchedapps.frost.settings +import android.content.Intent +import android.media.RingtoneManager +import android.net.Uri import ca.allanwang.kau.kpref.activity.KPrefAdapterBuilder +import ca.allanwang.kau.kpref.activity.items.KPrefText import ca.allanwang.kau.utils.minuteToText +import ca.allanwang.kau.utils.string import com.pitchedapps.frost.R import com.pitchedapps.frost.activities.SettingsActivity import com.pitchedapps.frost.services.fetchNotifications @@ -11,6 +16,7 @@ import com.pitchedapps.frost.utils.frostSnackbar import com.pitchedapps.frost.utils.materialDialogThemed import com.pitchedapps.frost.views.Keywords + /** * Created by Allan Wang on 2017-06-29. */ @@ -59,7 +65,36 @@ fun SettingsActivity.getNotificationPrefs(): KPrefAdapterBuilder.() -> Unit = { descRes = R.string.notification_messages_desc } - checkbox(R.string.notification_sound, { Prefs.notificationSound }, { Prefs.notificationSound = it }) + checkbox(R.string.notification_sound, { Prefs.notificationSound }, { Prefs.notificationSound = it; reloadByTitle(R.string.notification_ringtone, R.string.message_ringtone) }) + + fun KPrefText.KPrefTextContract.ringtone(code: Int) { + enabler = { Prefs.notificationSound } + textGetter = { + if (it.isBlank()) string(R.string.kau_default) + else RingtoneManager.getRingtone(this@getNotificationPrefs, Uri.parse(it)).getTitle(this@getNotificationPrefs) + } + onClick = { + _, _, item -> + val intent = Intent(RingtoneManager.ACTION_RINGTONE_PICKER).apply { + putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, string(R.string.select_ringtone)) + putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false) + putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true) + putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION) + if (item.pref.isNotBlank()) + putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(item.pref)) + } + startActivityForResult(intent, code) + true + } + } + + text(R.string.notification_ringtone, { Prefs.notificationRingtone }, { Prefs.notificationRingtone = it }) { + ringtone(SettingsActivity.REQUEST_NOTIFICATION_RINGTONE) + } + + text(R.string.message_ringtone, { Prefs.messageRingtone }, { Prefs.messageRingtone = it }) { + ringtone(SettingsActivity.REQUEST_MESSAGE_RINGTONE) + } checkbox(R.string.notification_vibrate, { Prefs.notificationVibrate }, { Prefs.notificationVibrate = it }) @@ -75,5 +110,4 @@ fun SettingsActivity.getNotificationPrefs(): KPrefAdapterBuilder.() -> Unit = { } } - } \ No newline at end of file diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt index 35f69bca..60d709fb 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Downloader.kt @@ -13,9 +13,9 @@ import ca.allanwang.kau.utils.copyFromInputStream import ca.allanwang.kau.utils.string import com.pitchedapps.frost.BuildConfig import com.pitchedapps.frost.R -import com.pitchedapps.frost.services.frostConfig import com.pitchedapps.frost.services.frostNotification import com.pitchedapps.frost.services.getNotificationPendingCancelIntent +import com.pitchedapps.frost.services.quiet import okhttp3.MediaType import okhttp3.OkHttpClient import okhttp3.Request @@ -66,7 +66,7 @@ private fun AnkoAsyncContext.frostDownloadImpl(url: String, type: Downl val notifId = Math.abs(url.hashCode() + System.currentTimeMillis().toInt()) var notifBuilderAttempt: NotificationCompat.Builder? = null weakRef.get()?.apply { - notifBuilderAttempt = frostNotification + notifBuilderAttempt = frostNotification.quiet .setContentTitle(string(type.downloadingRes)) .setCategory(Notification.CATEGORY_PROGRESS) .setWhen(System.currentTimeMillis()) @@ -133,7 +133,7 @@ private fun OkHttpClient.cancel(url: String) { private fun NotificationCompat.Builder.show(weakRef: WeakReference, notifId: Int) { val c = weakRef.get() ?: return - NotificationManagerCompat.from(c).notify(DOWNLOAD_GROUP, notifId, build().frostConfig()) + NotificationManagerCompat.from(c).notify(DOWNLOAD_GROUP, notifId, build()) } private class ProgressResponseBody( diff --git a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt index 3ad90652..af605388 100644 --- a/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt +++ b/app/src/main/kotlin/com/pitchedapps/frost/utils/Prefs.kt @@ -7,9 +7,9 @@ import ca.allanwang.kau.kpref.StringSet import ca.allanwang.kau.kpref.kpref import ca.allanwang.kau.utils.isColorVisibleOn import com.pitchedapps.frost.enums.FACEBOOK_BLUE +import com.pitchedapps.frost.enums.FeedSort import com.pitchedapps.frost.enums.MainActivityLayout import com.pitchedapps.frost.enums.Theme -import com.pitchedapps.frost.enums.FeedSort import com.pitchedapps.frost.injectors.InjectorContract /** @@ -104,6 +104,10 @@ object Prefs : KPref() { var notificationSound: Boolean by kpref("notification_sound", true) + var notificationRingtone: String by kpref("notification_ringtone", "") + + var messageRingtone: String by kpref("message_ringtone", "") + var notificationLights: Boolean by kpref("notification_lights", true) var messageScrollToBottom: Boolean by kpref("message_scroll_to_bottom", false) diff --git a/app/src/main/res/values/strings_pref_notifications.xml b/app/src/main/res/values/strings_pref_notifications.xml index 3cd953f6..6d8c5d5e 100644 --- a/app/src/main/res/values/strings_pref_notifications.xml +++ b/app/src/main/res/values/strings_pref_notifications.xml @@ -12,10 +12,15 @@ Enable message notifications Get instant message notifications for your current account. Fetch Notifications Now - Trigger the notification fetcher one time. + Trigger the notification fetcher once. Note that fetching instant messages takes time. Fetching Notifications… Couldn\'t fetch notifications Notification sound + + Notification Ringtone + Message Ringtone + Selet Ringtone + Notification vibration Notification lights diff --git a/app/src/main/res/values/strings_preferences.xml b/app/src/main/res/values/strings_preferences.xml index 05335345..590e8fe5 100644 --- a/app/src/main/res/values/strings_preferences.xml +++ b/app/src/main/res/values/strings_preferences.xml @@ -2,7 +2,7 @@ Appearance Theme, Items to display, etc - Frequency, filters, etc + Frequency, filters, ringtones, etc News Feed Define what items appear in the newsfeed diff --git a/app/src/main/res/xml/frost_changelog.xml b/app/src/main/res/xml/frost_changelog.xml index e1db7e36..5cbc6791 100644 --- a/app/src/main/res/xml/frost_changelog.xml +++ b/app/src/main/res/xml/frost_changelog.xml @@ -11,7 +11,13 @@ - + + + + + + + @@ -19,8 +25,6 @@ - - -- cgit v1.2.3