Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Emojis for the following are chosen based on [gitmoji](https://gitmoji.dev/).

### ✨ New Features

- The "Regularly update data" switch is saved and defaults to off. When it is on, the app checks Scribe-Server for newer language data immediately and once during the second week of each month, then posts a local notification. It does not download the data ([#768](https://github.com/scribe-org/Scribe-Android/issues/768)).
- Keyboard extensions that can be used in any app.
- Annotation of words in the command bar including the genders of nouns and cases that follow prepositions ([#135](https://github.com/scribe-org/Scribe-Android/issues/135), [#136](https://github.com/scribe-org/Scribe-Android/issues/136), [#275](https://github.com/scribe-org/Scribe-Android/issues/275)).
- The Scribe command UI can be displayed by pressing the Scribe Key ([#16](https://github.com/scribe-org/Scribe-Android/issues/16), [#89](https://github.com/scribe-org/Scribe-Android/issues/89), [#137](https://github.com/scribe-org/Scribe-Android/issues/137)).
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG_CONJUGATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Emojis for the following are chosen based on [gitmoji](https://gitmoji.dev/).

### ✨ New Features

- The "Regularly update data" switch is saved and defaults to off. When it is on, the app checks Scribe-Server for newer verb data immediately and once during the second week of each month, then posts a local notification. It does not download the data ([#768](https://github.com/scribe-org/Scribe-Android/issues/768)).
- A verb Conjugation tab was added to the application ([#563](https://github.com/scribe-org/Scribe-Android/issues/563)).
- Users can search for verbs across languages ([#566](https://github.com/scribe-org/Scribe-Android/issues/566)).
- A reactive conjugation selection UI was developed to easily copy desired conjugations ([#567](https://github.com/scribe-org/Scribe-Android/issues/567), [#570](https://github.com/scribe-org/Scribe-Android/issues/570)).
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ dependencies {
api("com.github.bumptech.glide:glide:4.16.0")
ksp("com.github.bumptech.glide:ksp:4.16.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.2")
implementation("androidx.work:work-runtime-ktx:2.11.2")
implementation("com.charleskorn.kaml:kaml:0.57.0")
implementation("org.jetbrains.kotlinx:kotlinx-collections-immutable:0.3.7")
}
Expand Down
5 changes: 5 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@
-keep class be.scri.inputmethod.keyboard.** { *; }
-keep class be.scri.inputmethod.latin.** { *; }
-keep class be.scri.latin.** { *; }

# WorkManager looks this class up by name. Keep the worker constructor.
-keep class be.scri.helpers.dataupdate.DataUpdateCheckWorker {
public <init>(android.content.Context, androidx.work.WorkerParameters);
}
20 changes: 20 additions & 0 deletions app/src/conjugate/java/be/scri/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package be.scri.activities

import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
Expand All @@ -24,6 +25,10 @@ import androidx.navigation.compose.rememberNavController
import be.scri.ScribeApp
import be.scri.helpers.PreferencesHelper
import be.scri.helpers.PreferencesHelper.setLightDarkModePreference
import be.scri.helpers.dataupdate.DataUpdateNotifier
import be.scri.helpers.dataupdate.DataUpdateScheduler
import be.scri.helpers.dataupdate.DownloadNavEffect
import be.scri.helpers.dataupdate.DownloadNavEvents
import be.scri.ui.common.bottombar.BottomBarScreen
import be.scri.ui.theme.ScribeTheme

Expand All @@ -32,12 +37,16 @@ import be.scri.ui.theme.ScribeTheme
* Initializes theme settings, navigation, and sets up the main UI using Jetpack Compose.
*/
class MainActivity : ComponentActivity() {
private val downloadNavEvents = DownloadNavEvents()

/**
* Initializes the app on launch. Sets the theme based on user preferences, sets up edge-to-edge
* layout, and builds the UI using Compose.
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
DataUpdateScheduler.ensureScheduled(this)
downloadNavEvents.publish(intent)
AppCompatDelegate.setDefaultNightMode(PreferencesHelper.getUserDarkModePreference(this))

val isDark = PreferencesHelper.getUserDarkModePreference(this) == AppCompatDelegate.MODE_NIGHT_YES
Expand Down Expand Up @@ -69,6 +78,11 @@ class MainActivity : ComponentActivity() {
}

val navController = rememberNavController()
DownloadNavEffect(
events = downloadNavEvents,
navController = navController,
route = DataUpdateNotifier.ROUTE_CONJUGATE,
)

val isHintChangedMap = remember { mutableStateMapOf<Int, Boolean>() }

Expand Down Expand Up @@ -121,6 +135,12 @@ class MainActivity : ComponentActivity() {
}
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
downloadNavEvents.publish(intent)
}

private fun applyNavigationBarStyle(isDark: Boolean) {
enableEdgeToEdge(
navigationBarStyle =
Expand Down
28 changes: 20 additions & 8 deletions app/src/keyboards/java/be/scri/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

package be.scri.activities

import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
Expand All @@ -14,7 +15,6 @@ import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -25,6 +25,10 @@ import androidx.navigation.compose.rememberNavController
import be.scri.ScribeApp
import be.scri.helpers.PreferencesHelper
import be.scri.helpers.PreferencesHelper.setLightDarkModePreference
import be.scri.helpers.dataupdate.DataUpdateNotifier
import be.scri.helpers.dataupdate.DataUpdateScheduler
import be.scri.helpers.dataupdate.DownloadNavEffect
import be.scri.helpers.dataupdate.DownloadNavEvents
import be.scri.ui.common.bottombar.BottomBarScreen
import be.scri.ui.theme.ScribeTheme

Expand All @@ -33,12 +37,16 @@ import be.scri.ui.theme.ScribeTheme
* Initializes theme settings, navigation, and sets up the main UI using Jetpack Compose.
*/
class MainActivity : ComponentActivity() {
private val downloadNavEvents = DownloadNavEvents()

/**
* Initializes the app on launch. Sets the theme based on user preferences, sets up edge-to-edge
* layout, and builds the UI using Compose.
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
DataUpdateScheduler.ensureScheduled(this)
downloadNavEvents.publish(intent)
AppCompatDelegate.setDefaultNightMode(PreferencesHelper.getUserDarkModePreference(this))

val isDark = PreferencesHelper.getUserDarkModePreference(this) == AppCompatDelegate.MODE_NIGHT_YES
Expand Down Expand Up @@ -71,13 +79,11 @@ class MainActivity : ComponentActivity() {
}

val navController = rememberNavController()

val navigateTo = intent.getStringExtra("navigate_to")
LaunchedEffect(navigateTo) {
if (navigateTo == "download_data") {
navController.navigate("download_data")
}
}
DownloadNavEffect(
events = downloadNavEvents,
navController = navController,
route = DataUpdateNotifier.ROUTE_KEYBOARDS,
)

val isHintChangedMap = remember { mutableStateMapOf<Int, Boolean>() }

Expand Down Expand Up @@ -128,6 +134,12 @@ class MainActivity : ComponentActivity() {
}
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent)
downloadNavEvents.publish(intent)
}

private fun applyNavigationBarStyle(isDark: Boolean) {
enableEdgeToEdge(
navigationBarStyle =
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
android:installLocation="auto">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:allowBackup="true"
android:appCategory="productivity"
Expand Down
21 changes: 21 additions & 0 deletions app/src/main/java/be/scri/helpers/PreferencesHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ object PreferencesHelper {
private const val DEFAULT_KEYBOARD_LAYOUT = "default_keyboard_layout"
private const val HOLD_FOR_ALT_KEYS = "hold_for_alt_keys"
private const val INCREASE_TEXT_SIZE = "increase_text_size"
private const val REGULARLY_CHECK_FOR_DATA_UPDATES = "regularly_check_for_data_updates"

/**
* Sets the translation source language for a given language.
Expand Down Expand Up @@ -651,6 +652,26 @@ object PreferencesHelper {
return sharedPref.getBoolean(INCREASE_TEXT_SIZE, false)
}

/**
* Saves whether this install should check for new language data in the background.
* The default is off. The check notifies the user and does not download the data.
*/
fun setRegularlyCheckForDataUpdates(
context: Context,
enabled: Boolean,
) {
val sharedPref = context.getSharedPreferences(SCRIBE_PREFS, Context.MODE_PRIVATE)
sharedPref.edit { putBoolean(REGULARLY_CHECK_FOR_DATA_UPDATES, enabled) }
}

/**
* Returns whether background data checks are enabled. Missing values are off.
*/
fun getRegularlyCheckForDataUpdates(context: Context): Boolean {
val sharedPref = context.getSharedPreferences(SCRIBE_PREFS, Context.MODE_PRIVATE)
return sharedPref.getBoolean(REGULARLY_CHECK_FOR_DATA_UPDATES, false)
}

// MARK: Floating Keyboard Preferences

private const val FLOATING_MODE_ENABLED = "floating_mode_enabled"
Expand Down
121 changes: 121 additions & 0 deletions app/src/main/java/be/scri/helpers/dataupdate/DataUpdateCalendar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package be.scri.helpers.dataupdate

import android.content.Context
import androidx.core.content.edit
import be.scri.helpers.PreferencesHelper
import java.time.Duration
import java.time.LocalDateTime
import kotlin.random.Random

/**
* The day and minute, stored once per install, when a monthly data check should run.
* The day is always in the second week of the month.
*/
data class DataCheckSlot(
val dayOfMonth: Int,
val minuteOfDay: Int,
)

/**
* Picks and remembers a monthly check slot, and computes how long to wait for it.
*/
object DataUpdateCalendar {
const val FIRST_DAY = 8
const val LAST_DAY = 14
const val GIVE_UP_DAY = 21
const val POSTPONE_HOURS = 12L

private const val KEY_DAY = "data_check_day_of_month"
private const val KEY_MINUTE = "data_check_minute_of_day"
private const val UNSET_DAY = 0
private const val UNSET_MINUTE = -1
private const val MINUTES_PER_HOUR = 60
private const val MINUTES_PER_DAY = 24 * MINUTES_PER_HOUR

/**
* Returns the saved slot, or creates one and saves it.
*/
fun ensureSlot(
context: Context,
random: Random = Random.Default,
): DataCheckSlot {
val prefs = context.getSharedPreferences(PreferencesHelper.SCRIBE_PREFS, Context.MODE_PRIVATE)
val day = prefs.getInt(KEY_DAY, UNSET_DAY)
val minute = prefs.getInt(KEY_MINUTE, UNSET_MINUTE)
if (day in FIRST_DAY..LAST_DAY && minute in 0 until MINUTES_PER_DAY) {
return DataCheckSlot(day, minute)
}
val slot = newSlot(random)
prefs.edit {
putInt(KEY_DAY, slot.dayOfMonth)
putInt(KEY_MINUTE, slot.minuteOfDay)
}
return slot
}

/**
* A random day from the 8th through the 14th, and a random minute of that day.
*/
fun newSlot(random: Random): DataCheckSlot =
DataCheckSlot(
dayOfMonth = random.nextInt(FIRST_DAY, LAST_DAY + 1),
minuteOfDay = random.nextInt(MINUTES_PER_DAY),
)

/**
* Delay from [now] until the next time [slot] occurs. Never zero or negative.
*/
fun delayUntilNextSlot(
now: LocalDateTime,
slot: DataCheckSlot,
): Duration = Duration.between(now, nextOccurrence(now, slot))

/**
* Delay until the next saved slot, creating the slot if this install does not have one yet.
*/
fun delayUntilNextSlot(
context: Context,
now: LocalDateTime = LocalDateTime.now(),
): Duration = delayUntilNextSlot(now, ensureSlot(context))

/**
* While battery saver is on before the 21st, wait twelve hours. On the 21st or later, wait
* until next month's slot instead of checking outside the intended window.
*/
fun batterySaverDelay(
now: LocalDateTime,
slot: DataCheckSlot,
): Duration =
if (now.dayOfMonth >= GIVE_UP_DAY) {
delayUntilNextSlot(now, slot)
} else {
Duration.ofHours(POSTPONE_HOURS)
}

/**
* Same rule as [batterySaverDelay], using the slot saved for this install.
*/
fun batterySaverDelay(
context: Context,
now: LocalDateTime = LocalDateTime.now(),
): Duration = batterySaverDelay(now, ensureSlot(context))

/**
* The next local date-time at which [slot] occurs strictly after [now].
*/
fun nextOccurrence(
now: LocalDateTime,
slot: DataCheckSlot,
): LocalDateTime {
val thisMonth =
now
.withDayOfMonth(slot.dayOfMonth)
.withHour(slot.minuteOfDay / MINUTES_PER_HOUR)
.withMinute(slot.minuteOfDay % MINUTES_PER_HOUR)
.withSecond(0)
.withNano(0)
return if (thisMonth.isAfter(now)) thisMonth else thisMonth.plusMonths(1)
}
}
Loading
Loading