Add greetings as glance provider

This commit is contained in:
Tommaso Berlose 2020-10-18 12:51:02 +02:00
parent 16076dc145
commit c0e747a714
13 changed files with 266 additions and 14 deletions

View File

@ -25,6 +25,7 @@ import com.tommasoberlose.anotherwidget.global.Constants
import com.tommasoberlose.anotherwidget.global.Preferences
import com.tommasoberlose.anotherwidget.helpers.ActiveNotificationsHelper
import com.tommasoberlose.anotherwidget.helpers.AlarmHelper
import com.tommasoberlose.anotherwidget.helpers.GreetingsHelper
import com.tommasoberlose.anotherwidget.helpers.MediaPlayerHelper
import com.tommasoberlose.anotherwidget.receivers.ActivityDetectionReceiver
import com.tommasoberlose.anotherwidget.ui.activities.AppNotificationsFilterActivity
@ -95,12 +96,25 @@ class GlanceSettingsDialog(val context: Activity, val provider: Constants.Glance
/* NOTIFICATIONS */
view.action_filter_notifications_app.isVisible = provider == Constants.GlanceProviderId.NOTIFICATIONS
view.action_change_notification_timer.isVisible = provider == Constants.GlanceProviderId.NOTIFICATIONS
if (provider == Constants.GlanceProviderId.NOTIFICATIONS) {
checkLastNotificationsPermission(view)
val stringArray = context.resources.getStringArray(R.array.glance_notifications_timeout)
view.action_filter_notifications_app.setOnClickListener {
dismiss()
context.startActivityForResult(Intent(context, AppNotificationsFilterActivity::class.java), 0)
}
view.notification_timer_label.text = stringArray[Preferences.hideNotificationAfter]
view.action_change_notification_timer.setOnClickListener {
val dialog = BottomSheetMenu<Int>(context, header = context.getString(R.string.glance_notification_hide_timeout_title)).setSelectedValue(Preferences.hideNotificationAfter)
Constants.GlanceNotificationTimer.values().forEachIndexed { index, timeout ->
dialog.addItem(stringArray[index], timeout.value)
}
dialog.addOnSelectItemListener { value ->
Preferences.hideNotificationAfter = value
this.show()
}.show()
}
}
/* GREETINGS */
@ -146,6 +160,7 @@ class GlanceSettingsDialog(val context: Activity, val provider: Constants.Glance
}
Constants.GlanceProviderId.GREETINGS -> {
Preferences.showGreetings = isChecked
GreetingsHelper.toggleGreetings(context)
}
Constants.GlanceProviderId.GOOGLE_FIT_STEPS -> {
if (isChecked) {

View File

@ -13,4 +13,5 @@ object Actions {
const val ACTION_GO_TO_PREVIOUS_EVENT = "com.tommasoberlose.anotherwidget.action.GO_TO_PREVIOUS_EVENT"
const val ACTION_REPORT_CRASH = "com.tommasoberlose.anotherwidget.action.REPORT_CRASH"
const val ACTION_CLEAR_NOTIFICATION = "com.tommasoberlose.anotherwidget.action.CLEAR_NOTIFICATION"
const val ACTION_UPDATE_GREETINGS = "com.tommasoberlose.anotherwidget.action.UPDATE_GREETINGS"
}

View File

@ -61,6 +61,20 @@ object Constants {
}
}
enum class GlanceNotificationTimer(val value: Int) {
HALF_MINUTE(0),
ONE_MINUTE(1),
FIVE_MINUTES(2),
TEN_MINUTES(3),
FIFTEEN_MINUTES(4),
WHEN_DISMISSED(5);
companion object {
private val map = values().associateBy(GlanceNotificationTimer::value)
fun fromInt(type: Int) = map[type]
}
}
enum class WeatherIconPack(val value: Int) {
DEFAULT(0),
MINIMAL(1),

View File

@ -125,6 +125,7 @@ object Preferences : KotprefModel() {
var showDailySteps by booleanPref(default = false)
var showGreetings by booleanPref(default = false)
var showNotifications by booleanPref(default = false)
var hideNotificationAfter by intPref(default = Constants.GlanceNotificationTimer.ONE_MINUTE.value)
var lastNotificationId by intPref(default = -1)
var lastNotificationTitle by stringPref(default = "")

View File

@ -18,7 +18,6 @@ object GlanceProviderHelper {
.filter {
context.checkIfFitInstalled() || it != Constants.GlanceProviderId.GOOGLE_FIT_STEPS
}
.filterNot { it.id == Constants.GlanceProviderId.GREETINGS.id }
.toTypedArray()
providers.sortWith(Comparator { p1, p2 ->
@ -102,7 +101,8 @@ object GlanceProviderHelper {
(MediaPlayerHelper.isSomeonePlaying(context)) ||
(Preferences.showBatteryCharging && Preferences.isCharging || Preferences.isBatteryLevelLow) ||
(Preferences.customNotes.isNotEmpty()) ||
(Preferences.showDailySteps && Preferences.googleFitSteps > 0)
(Preferences.showDailySteps && Preferences.googleFitSteps > 0) ||
(Preferences.showGreetings && GreetingsHelper.showGreetings())
)
eventRepository.close()
return showGlance

View File

@ -0,0 +1,110 @@
package com.tommasoberlose.anotherwidget.helpers
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import com.tommasoberlose.anotherwidget.R
import com.tommasoberlose.anotherwidget.global.Actions
import com.tommasoberlose.anotherwidget.global.Preferences
import com.tommasoberlose.anotherwidget.receivers.UpdatesReceiver
import java.util.*
object GreetingsHelper {
private const val MORNING_TIME = 36
private const val MORNING_TIME_END = 37
private const val EVENING_TIME = 38
private const val NIGHT_TIME = 39
fun toggleGreetings(context: Context) {
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
val now = Calendar.getInstance().apply {
set(Calendar.SECOND, 0)
set(Calendar.MILLISECOND, 0)
set(Calendar.MINUTE, 0)
set(Calendar.HOUR_OF_DAY, 0)
}
if (Preferences.showGreetings) {
setRepeating(
AlarmManager.RTC,
now.apply {
set(Calendar.HOUR_OF_DAY, 5)
}.timeInMillis,
1000 * 60 * 60 * 24,
PendingIntent.getBroadcast(context,
MORNING_TIME,
Intent(context, UpdatesReceiver::class.java).apply {
action = Actions.ACTION_UPDATE_GREETINGS
},
0)
)
setRepeating(
AlarmManager.RTC,
now.apply {
set(Calendar.HOUR_OF_DAY, 9)
}.timeInMillis,
1000 * 60 * 60 * 24,
PendingIntent.getBroadcast(context,
MORNING_TIME_END,
Intent(context, UpdatesReceiver::class.java).apply {
action = Actions.ACTION_UPDATE_GREETINGS
},
0)
)
setRepeating(
AlarmManager.RTC,
now.apply {
set(Calendar.HOUR_OF_DAY, 19)
}.timeInMillis,
1000 * 60 * 60 * 24,
PendingIntent.getBroadcast(context,
EVENING_TIME,
Intent(context, UpdatesReceiver::class.java).apply {
action = Actions.ACTION_UPDATE_GREETINGS
},
0)
)
setRepeating(
AlarmManager.RTC,
now.apply {
set(Calendar.HOUR_OF_DAY, 22)
}.timeInMillis,
1000 * 60 * 60 * 24,
PendingIntent.getBroadcast(context,
NIGHT_TIME,
Intent(context, UpdatesReceiver::class.java).apply {
action = Actions.ACTION_UPDATE_GREETINGS
},
0)
)
} else {
listOf(MORNING_TIME, MORNING_TIME_END, EVENING_TIME, NIGHT_TIME).forEach {
cancel(PendingIntent.getBroadcast(context, it, Intent(context,
UpdatesReceiver::class.java).apply {
action = Actions.ACTION_UPDATE_GREETINGS
}, 0))
}
}
}
}
fun showGreetings(): Boolean {
val hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
return hour < 9 || hour >= 19
}
fun getRandomString(context: Context): String {
val hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY)
val array = when {
hour in 5..8 -> context.resources.getStringArray(R.array.morning_greetings)
hour in 19..21 -> context.resources.getStringArray(R.array.evening_greetings)
hour >= 22 && hour < 5 -> context.resources.getStringArray(R.array.night_greetings)
else -> emptyArray()
}
return if (array.isNotEmpty()) array[Random().nextInt(array.size)] else ""
}
}

View File

@ -9,6 +9,7 @@ import android.service.notification.NotificationListenerService
import android.service.notification.StatusBarNotification
import android.util.Log
import com.tommasoberlose.anotherwidget.global.Actions
import com.tommasoberlose.anotherwidget.global.Constants
import com.tommasoberlose.anotherwidget.global.Preferences
import com.tommasoberlose.anotherwidget.helpers.ActiveNotificationsHelper
import com.tommasoberlose.anotherwidget.helpers.MediaPlayerHelper
@ -78,16 +79,26 @@ class NotificationListener : NotificationListenerService() {
action = Actions.ACTION_CLEAR_NOTIFICATION
}
cancel(PendingIntent.getBroadcast(context, 28943, intent, 0))
setExact(
AlarmManager.RTC,
Calendar.getInstance().timeInMillis + 30 * 1000,
PendingIntent.getBroadcast(
context,
5,
intent,
0
val timeoutPref = Constants.GlanceNotificationTimer.fromInt(Preferences.hideNotificationAfter)
if (timeoutPref != Constants.GlanceNotificationTimer.WHEN_DISMISSED) {
setExact(
AlarmManager.RTC,
Calendar.getInstance().timeInMillis + when (timeoutPref) {
Constants.GlanceNotificationTimer.HALF_MINUTE -> 30 * 1000
Constants.GlanceNotificationTimer.ONE_MINUTE -> 60 * 1000
Constants.GlanceNotificationTimer.FIVE_MINUTES -> 5 * 60 * 1000
Constants.GlanceNotificationTimer.TEN_MINUTES -> 10 * 60 * 1000
Constants.GlanceNotificationTimer.FIFTEEN_MINUTES -> 15 * 60 * 1000
else -> 0
},
PendingIntent.getBroadcast(
context,
5,
intent,
0
)
)
)
}
}
}
}

View File

@ -52,6 +52,9 @@ class UpdatesReceiver : BroadcastReceiver() {
ActiveNotificationsHelper.clearLastNotification(context)
MainWidget.updateWidget(context)
}
Actions.ACTION_UPDATE_GREETINGS -> {
MainWidget.updateWidget(context)
}
}
}

View File

@ -262,7 +262,6 @@ class GlanceTabFragment : Fragment() {
list[toPos] = this
}
GlanceProviderHelper.saveGlanceProviderOrder(list)
Log.d("ciao", list.toString())
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y)
}

View File

@ -372,6 +372,12 @@ class MainWidget : AppWidgetProvider() {
} catch (ex: Exception) {}
}
}
Constants.GlanceProviderId.GREETINGS -> {
if (Preferences.showGreetings && GreetingsHelper.showGreetings() && GreetingsHelper.getRandomString(context).isNotBlank()) {
showSomething = true
break@loop
}
}
}
}
@ -749,6 +755,16 @@ class MainWidget : AppWidgetProvider() {
} catch (ex: Exception) {}
}
}
Constants.GlanceProviderId.GREETINGS -> {
val greetingsText = GreetingsHelper.getRandomString(context)
if (Preferences.showGreetings && GreetingsHelper.showGreetings() && greetingsText.isNotBlank()) {
v.next_event_date.text = greetingsText
v.next_event_date.maxLines = 2
v.second_row_icon.isVisible = false
showSomething = true
break@loop
}
}
}
}

View File

@ -206,6 +206,39 @@
style="@style/AnotherWidget.Settings.Subtitle"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
android:gravity="center_vertical"
android:id="@+id/action_change_notification_timer"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
style="@style/AnotherWidget.Settings.Title"
android:text="@string/glance_notification_hide_timeout_title"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:id="@+id/notification_timer_label"
style="@style/AnotherWidget.Settings.Subtitle"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="add_widget">Aggiungi</string>
<!-- Display -->
@ -211,6 +211,28 @@
<string name="action_disconnect">Scollega</string>
<string name="applications_filter_title">Applicazioni</string>
<string name="applications_filter_subtitle">Scegli le notifiche che vuoi vedere</string>
<string-array name="morning_greetings" tools:ignore="InconsistentArrays">
<item>Buona giornata!</item>
<item>Non dimenticare la colazione!</item>
</string-array>
<string-array name="evening_greetings" tools:ignore="InconsistentArrays">
<item>Buona serata!</item>
<item>Com\'è andata la giornata?</item>
</string-array>
<string-array name="night_greetings" tools:ignore="InconsistentArrays">
<item>Non fare troppo tardi!</item>
<item>A domani!</item>
<item>Buonanotte!</item>
</string-array>
<string name="glance_notification_hide_timeout_title">Nascondi la notifica dopo</string>
<string-array name="glance_notifications_timeout">
<item>30 secondi</item>
<item>1 minuto</item>
<item>5 minuti</item>
<item>10 minuti</item>
<item>15 minuti</item>
<item>che viene eliminata</item>
</string-array>
<!-- Settings -->
<string name="action_share">Condividi</string>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="app_name" translatable="false">Another Widget</string>
<string name="add_widget">Add Widget</string>
@ -228,6 +228,33 @@
<string name="action_disconnect">Disconnect</string>
<string name="applications_filter_title">Applications</string>
<string name="applications_filter_subtitle">Choose which notifications to view</string>
<string-array name="morning_greetings" tools:ignore="InconsistentArrays">
<item>Have a good day!</item>
<item>Good morning!</item>
<item>Ready to go?</item>
<item>Rise and shine.</item>
<item>Don\'t forget your breakfast!</item>
</string-array>
<string-array name="evening_greetings" tools:ignore="InconsistentArrays">
<item>Good evening!</item>
<item>How did your day go?</item>
<item>How was your day?</item>
</string-array>
<string-array name="night_greetings" tools:ignore="InconsistentArrays">
<item>Sleep well!</item>
<item>See you tomorrow!</item>
<item>Good night!</item>
<item>Don\'t stay up too late!</item>
</string-array>
<string name="glance_notification_hide_timeout_title">Hide the notification after</string>
<string-array name="glance_notifications_timeout">
<item>30 seconds</item>
<item>1 minute</item>
<item>5 minutes</item>
<item>10 minutes</item>
<item>15 minutes</item>
<item>it\'s dismissed</item>
</string-array>
<!-- Settings -->
<string name="action_share">Share</string>