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

Port Personality Provider from Lawnchair Legacy #4848

Open
wants to merge 9 commits into
base: 14-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
21 changes: 21 additions & 0 deletions lawnchair/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,26 @@
<string name="smartspace_widget">At a Glance</string>
<string name="smartspace_widget_description">What to show</string>

<!-- Personality provider -->
<string name="personality_provider">Contextual messages</string>
<string-array name="smartspace_personality_greetings_morning">
<item>Good morning!</item>
<item>Have a great day!</item>
<item>It’s a beautiful day outside!</item>
</string-array>
<string-array name="smartspace_personality_greetings_evening">
<item>What’s up?</item>
<item>Good evening!</item>
<item>How did your day go?</item>
<item>How was your day?</item>
</string-array>
<string-array name="smartspace_personality_greetings_night">
<item>Sleep well!</item>
<item>See you tomorrow!</item>
<item>Good night!</item>
<item>Don’t stay up too late!</item>
</string-array>
validcube marked this conversation as resolved.
Show resolved Hide resolved

<!-- Data and time format settings -->
<string name="smartspace_calendar">Calendar</string>
<string name="smartspace_date_and_time">Date &amp; time</string>
Expand All @@ -357,6 +377,7 @@
<string name="smartspace_weather">Weather</string>
<string name="smartspace_battery_status">Battery status</string>
<string name="smartspace_now_playing">Now Playing</string>
<string name="smartspace_personality_provider">Greetings</string>

<!-- Smartspacer strings -->
<string name="maximum_number_of_targets">Maximum number of targets</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ class PreferenceManager2 private constructor(private val context: Context) : Pre
defaultValue = true,
)

val smartspacePersonalityProvider = preference(
key = booleanPreferencesKey("enable_smartspace_personality_provider"),
defaultValue = false,
)

val smartspaceShowDate = preference(
key = booleanPreferencesKey("smartspace_show_date"),
defaultValue = context.resources.getBoolean(R.bool.config_default_smartspace_show_date),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package app.lawnchair.smartspace.model

object SmartspaceScores {
const val SCORE_WEATHER = 0f
const val SCORE_BATTERY = 1f
const val SCORE_MEDIA = 2f
const val SCORE_CALENDAR = 3f
const val SCORE_PERSONALITY = 1f
const val SCORE_BATTERY = 2f
const val SCORE_MEDIA = 3f

// Critical Information that must be displayed before anything else.
validcube marked this conversation as resolved.
Show resolved Hide resolved
const val SCORE_LOW_BATTERY = 10f
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package app.lawnchair.smartspace.provider

import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import app.lawnchair.smartspace.model.SmartspaceAction
import app.lawnchair.smartspace.model.SmartspaceScores
import app.lawnchair.smartspace.model.SmartspaceTarget
import app.lawnchair.util.broadcastReceiverFlow
import com.android.launcher3.R
import java.util.Calendar
import kotlin.math.abs
import kotlin.random.Random
import kotlinx.coroutines.flow.map

class PersonalityProvider(context: Context) : SmartspaceDataSource(
context,
R.string.smartspace_personality_provider,
{ smartspacePersonalityProvider },
) {
private val morningStrings = context.resources.getStringArray(R.array.smartspace_personality_greetings_morning)
private val eveningStrings = context.resources.getStringArray(R.array.smartspace_personality_greetings_evening)
private val nightStrings = context.resources.getStringArray(R.array.smartspace_personality_greetings_night)
validcube marked this conversation as resolved.
Show resolved Hide resolved

override val internalTargets = broadcastReceiverFlow(
context,
IntentFilter().apply {
addAction(Intent.ACTION_DATE_CHANGED)
addAction(Intent.ACTION_TIME_CHANGED)
addAction(Intent.ACTION_TIMEZONE_CHANGED)

// Difficulty testing? I know you do, uncomment this.
// addAction(Intent.ACTION_SCREEN_ON)
},
).map {
val time = Calendar.getInstance()
listOfNotNull(getSmartspaceTarget(time))
}

private fun getSmartspaceTarget(time: Calendar): SmartspaceTarget? {
val randomIndex = abs(Random(time.dayOfYear).nextInt())

val greeting = when {
isMorning(time) -> morningStrings[randomIndex % morningStrings.size]
isEvening(time) -> eveningStrings[randomIndex % eveningStrings.size]
isNight(time) -> nightStrings[randomIndex % nightStrings.size]
else -> return null
}

/* TODO: We really need target's expiration time which isn't supported on new Smartspace
* ImplRef: LawnchairSmartspaceController.kt @ 10-dev */
return SmartspaceTarget(
id = "personalityGreeting",
headerAction = SmartspaceAction(
id = "personalityGreetingAction",
title = greeting,
),
score = SmartspaceScores.SCORE_PERSONALITY,
featureType = SmartspaceTarget.FeatureType.FEATURE_REMINDER,
)
}
validcube marked this conversation as resolved.
Show resolved Hide resolved

private fun isMorning(time: Calendar) = time.hourOfDay in 5 until 9
private fun isEvening(time: Calendar) = time.hourOfDay in 19 until 21
private fun isNight(time: Calendar) = time.hourOfDay in 22 until 24 || time.hourOfDay in 0 until 4

private val Calendar.dayOfYear: Int get() = get(Calendar.DAY_OF_YEAR)
private val Calendar.hourOfDay: Int get() = get(Calendar.HOUR_OF_DAY)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class SmartspaceProvider private constructor(context: Context) {
SmartspaceWidgetReader(context),
BatteryStatusProvider(context),
NowPlayingProvider(context),
PersonalityProvider(context),
)

private val state = dataSources
Expand Down
Loading