Skip to content

Commit

Permalink
Oh sheesh I hope this thing won't impldoe and leak all my smartspace …
Browse files Browse the repository at this point in the history
…bizzare adventure !!!
  • Loading branch information
validcube committed Sep 21, 2024
1 parent 910da98 commit bb33d05
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 5 deletions.
45 changes: 45 additions & 0 deletions lawnchair/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,46 @@
<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>

<!-- Calendar -->
<string name="smartspace_calendar_now">now</string>
<string name="smartspace_calendar_in_time">in %1$s</string>
<plurals name="smartspace_calendar_minutes">
<item quantity="other">%d min</item>
<item quantity="one">%d min</item>
</plurals>
<plurals name="smartspace_calendar_hours">
<item quantity="other">%d hr</item>
<item quantity="one">%d hr</item>
</plurals>
<string name="smartspace_hours_mins">%1$s %2$s</string>
<string name="calendar_permission_needed">Warning: Lawnchair must have access to your calendar to show it</string>

<!-- Onboarding -->
<string name="onboarding">Onboarding</string>
<string name="onboarding_settings_title">Set Up %s</string>
<string name="onboarding_settings_summary">Touch &amp; hold on the Home Screen.</string>
<string name="onboarding_swipe_up">Swipe up to open the App Drawer.</string>

<!-- 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 +397,9 @@
<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>
<string name="smartspace_calendar_events">Calendar</string>
<string name="smartspace_onboarding_provider">Onboarding</string>

<!-- Smartspacer strings -->
<string name="maximum_number_of_targets">Maximum number of targets</string>
Expand All @@ -366,6 +409,8 @@
<!-- Setup settings -->
<string name="smartspace_requires_setup">Tap to set up</string>
<string name="event_provider_missing_notification_dots">To use <xliff:g example="My Provider" id="providerName">%1$s</xliff:g>, turn on Notification Dots.</string>
<string name="event_provider_missing_calendar">To use <xliff:g example="Calendar" id="providerName">%1$s</xliff:g> provider, allow Lawnchair to access your calendar.</string>
<string name="title_missing_calendar">Calendar access needed</string>

<!-- Toggle button for enabling Smartspace -->
<string name="smartspace_widget_toggle_label">Show on home screen</string>
Expand Down
15 changes: 15 additions & 0 deletions lawnchair/src/app/lawnchair/preferences2/PreferenceManager2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,21 @@ class PreferenceManager2 private constructor(private val context: Context) : Pre
defaultValue = true,
)

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

val smartspaceCalendarEvents = preference(
key = booleanPreferencesKey("enable_smartspace_calendar"),
defaultValue = true,
)

val smartspaceOnboardingProvider = preference(
key = booleanPreferencesKey("enable_smartspace_onboarding_assist"),
defaultValue = true,
)

val smartspaceShowDate = preference(
key = booleanPreferencesKey("smartspace_show_date"),
defaultValue = context.resources.getBoolean(R.bool.config_default_smartspace_show_date),
Expand Down
12 changes: 8 additions & 4 deletions lawnchair/src/app/lawnchair/smartspace/model/SmartspaceScores.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ 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_LOW_BATTERY = 10f
const val SCORE_PERSONALITY = 1f
const val SCORE_BATTERY = 2f
const val SCORE_MEDIA = 3f
const val SCORE_CALENDAR = 4f
const val SCORE_ONBOARDING = 10f

// Critical Information that must be displayed before anything else.
const val SCORE_LOW_BATTERY = 15f
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package app.lawnchair.smartspace.provider

import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.util.Log
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 TAG = "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)

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 reason = it.action
Log.d(TAG, "Event Triggered: $reason")

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
}

Log.d(TAG, "Greeting: $greeting at $randomIndex")

/* 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
)
}

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,7 +21,10 @@ class SmartspaceProvider private constructor(context: Context) {
SmartspaceWidgetReader(context),
BatteryStatusProvider(context),
NowPlayingProvider(context),
)
PersonalityProvider(context),
CalendarEventProvider(context),
OnboardingProvider(context),
).sortedBy { it.providerName } // TODO: Sort by SmartspaceScores? Should make it easier to make... ugh English SUCK

private val state = dataSources
.map { it.targets }
Expand Down

0 comments on commit bb33d05

Please sign in to comment.