Replace TextDrawable with an implementation that supports tint
This commit is contained in:
@@ -40,7 +40,6 @@ dependencies {
|
||||
implementation(libs.androidx.core)
|
||||
implementation(libs.androidx.appcompat)
|
||||
|
||||
implementation(libs.textdrawable)
|
||||
|
||||
implementation(libs.koin.android)
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import android.Manifest
|
||||
import android.content.ContentUris
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.Color
|
||||
import android.graphics.Typeface
|
||||
import android.graphics.drawable.ColorDrawable
|
||||
@@ -16,29 +15,27 @@ import androidx.core.graphics.ColorUtils
|
||||
import androidx.core.graphics.blue
|
||||
import androidx.core.graphics.green
|
||||
import androidx.core.graphics.red
|
||||
import com.amulyakhare.textdrawable.TextDrawable
|
||||
import de.mm20.launcher2.calendar.R
|
||||
import de.mm20.launcher2.permissions.PermissionsManager
|
||||
import de.mm20.launcher2.graphics.TextDrawable
|
||||
import de.mm20.launcher2.icons.LauncherIcon
|
||||
import de.mm20.launcher2.ktx.checkPermission
|
||||
import de.mm20.launcher2.ktx.dp
|
||||
import de.mm20.launcher2.permissions.PermissionsManager
|
||||
import de.mm20.launcher2.preferences.LauncherPreferences
|
||||
import org.json.JSONObject
|
||||
import java.lang.NullPointerException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
class CalendarEvent(
|
||||
override val label: String,
|
||||
val id: Long,
|
||||
val color: Int,
|
||||
val startTime: Long,
|
||||
val endTime: Long,
|
||||
val allDay: Boolean,
|
||||
val location: String,
|
||||
val attendees: List<String>,
|
||||
val description: String,
|
||||
val calendar: Long
|
||||
override val label: String,
|
||||
val id: Long,
|
||||
val color: Int,
|
||||
val startTime: Long,
|
||||
val endTime: Long,
|
||||
val allDay: Boolean,
|
||||
val location: String,
|
||||
val attendees: List<String>,
|
||||
val description: String,
|
||||
val calendar: Long
|
||||
) : Searchable() {
|
||||
|
||||
override val key: String
|
||||
@@ -51,31 +48,27 @@ class CalendarEvent(
|
||||
df.applyPattern("MMM")
|
||||
val month = df.format(startTime)
|
||||
val fgLayers = arrayOf(
|
||||
TextDrawable
|
||||
.builder()
|
||||
.beginConfig()
|
||||
.textColor(Color.WHITE)
|
||||
.useFont(Typeface.DEFAULT_BOLD)
|
||||
.fontSize((36 * context.dp).toInt())
|
||||
.endConfig()
|
||||
.buildRect(day, 0),
|
||||
TextDrawable
|
||||
.builder()
|
||||
.beginConfig()
|
||||
.textColor(Color.WHITE)
|
||||
.bold()
|
||||
.fontSize((26 * context.dp).toInt())
|
||||
.endConfig()
|
||||
.buildRect(month, 0)
|
||||
TextDrawable(
|
||||
day,
|
||||
color = Color.WHITE,
|
||||
fontSize = 40 * context.dp,
|
||||
typeface = Typeface.DEFAULT_BOLD
|
||||
),
|
||||
TextDrawable(
|
||||
month,
|
||||
color = Color.WHITE,
|
||||
fontSize = 26 * context.dp,
|
||||
typeface = Typeface.DEFAULT_BOLD
|
||||
)
|
||||
)
|
||||
val foreground = LayerDrawable(fgLayers)
|
||||
foreground.setLayerInset(0, 0, 0, 0, (26 * context.dp).toInt())
|
||||
foreground.setLayerInset(1, 0, (36 * context.dp).toInt(), 0, 0)
|
||||
foreground.setLayerInset(1, 0, (40 * context.dp).toInt(), 0, 0)
|
||||
val background = ColorDrawable(getDisplayColor(context, color))
|
||||
return LauncherIcon(
|
||||
foreground = foreground,
|
||||
background = background,
|
||||
foregroundScale = 0.74f
|
||||
foreground = foreground,
|
||||
background = background,
|
||||
foregroundScale = 0.74f
|
||||
)
|
||||
}
|
||||
|
||||
@@ -84,14 +77,15 @@ class CalendarEvent(
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun search(context: Context,
|
||||
query: String,
|
||||
intervalStart: Long,
|
||||
intervalEnd: Long,
|
||||
limit: Int = 10,
|
||||
hideAllDayEvents: Boolean = false,
|
||||
unselectedCalendars: List<Long> = emptyList(),
|
||||
hiddenEvents: List<Long> = emptyList()
|
||||
fun search(
|
||||
context: Context,
|
||||
query: String,
|
||||
intervalStart: Long,
|
||||
intervalEnd: Long,
|
||||
limit: Int = 10,
|
||||
hideAllDayEvents: Boolean = false,
|
||||
unselectedCalendars: List<Long> = emptyList(),
|
||||
hiddenEvents: List<Long> = emptyList()
|
||||
): List<CalendarEvent> {
|
||||
val results = mutableListOf<CalendarEvent>()
|
||||
if (!query.isEmpty() && query.length < 3) return results
|
||||
@@ -104,15 +98,15 @@ class CalendarEvent(
|
||||
ContentUris.appendId(builder, intervalEnd)
|
||||
val uri = builder.build()
|
||||
val projection = arrayOf(
|
||||
CalendarContract.Instances.EVENT_ID,
|
||||
CalendarContract.Instances.TITLE,
|
||||
CalendarContract.Instances.BEGIN,
|
||||
CalendarContract.Instances.END,
|
||||
CalendarContract.Instances.ALL_DAY,
|
||||
CalendarContract.Instances.DISPLAY_COLOR,
|
||||
CalendarContract.Instances.EVENT_LOCATION,
|
||||
CalendarContract.Instances.CALENDAR_ID,
|
||||
CalendarContract.Instances.DESCRIPTION
|
||||
CalendarContract.Instances.EVENT_ID,
|
||||
CalendarContract.Instances.TITLE,
|
||||
CalendarContract.Instances.BEGIN,
|
||||
CalendarContract.Instances.END,
|
||||
CalendarContract.Instances.ALL_DAY,
|
||||
CalendarContract.Instances.DISPLAY_COLOR,
|
||||
CalendarContract.Instances.EVENT_LOCATION,
|
||||
CalendarContract.Instances.CALENDAR_ID,
|
||||
CalendarContract.Instances.DESCRIPTION
|
||||
)
|
||||
val selection = mutableListOf<String>()
|
||||
if (query.isNotEmpty()) selection.add("${CalendarContract.Instances.TITLE} LIKE ?")
|
||||
@@ -120,25 +114,32 @@ class CalendarEvent(
|
||||
if (unselectedCalendars.isNotEmpty()) selection.add("${CalendarContract.Instances.CALENDAR_ID} NOT IN (${unselectedCalendars.joinToString()})")
|
||||
if (hideAllDayEvents) selection.add("${CalendarContract.Instances.ALL_DAY} = 0")
|
||||
val selArgs = if (query.isBlank()) null else arrayOf("%$query%")
|
||||
val sort = "${CalendarContract.Instances.BEGIN} ASC" + if (limit > -1) " LIMIT $limit" else ""
|
||||
val cursor = context.contentResolver.query(uri, projection, selection.joinToString(separator = " AND "), selArgs, sort)
|
||||
?: return mutableListOf()
|
||||
val sort =
|
||||
"${CalendarContract.Instances.BEGIN} ASC" + if (limit > -1) " LIMIT $limit" else ""
|
||||
val cursor = context.contentResolver.query(
|
||||
uri,
|
||||
projection,
|
||||
selection.joinToString(separator = " AND "),
|
||||
selArgs,
|
||||
sort
|
||||
)
|
||||
?: return mutableListOf()
|
||||
val proj = arrayOf(
|
||||
CalendarContract.Attendees.EVENT_ID,
|
||||
CalendarContract.Attendees.ATTENDEE_NAME,
|
||||
CalendarContract.Attendees.ATTENDEE_EMAIL
|
||||
CalendarContract.Attendees.EVENT_ID,
|
||||
CalendarContract.Attendees.ATTENDEE_NAME,
|
||||
CalendarContract.Attendees.ATTENDEE_EMAIL
|
||||
)
|
||||
val s = "${CalendarContract.Attendees.ATTENDEE_NAME} COLLATE NOCASE ASC"
|
||||
while (cursor.moveToNext()) {
|
||||
val sel = "${CalendarContract.Attendees.EVENT_ID} = ${cursor.getLong(0)}"
|
||||
val cur = context.contentResolver.query(
|
||||
CalendarContract.Attendees.CONTENT_URI,
|
||||
proj, sel, null, s
|
||||
CalendarContract.Attendees.CONTENT_URI,
|
||||
proj, sel, null, s
|
||||
) ?: return mutableListOf()
|
||||
val attendees = mutableListOf<String>()
|
||||
while (cur.moveToNext()) {
|
||||
attendees.add(cur.getString(1).takeUnless { it.isNullOrBlank() }
|
||||
?: cur.getString(2))
|
||||
?: cur.getString(2))
|
||||
}
|
||||
cur.close()
|
||||
val allday = cursor.getInt(4) > 0
|
||||
@@ -150,17 +151,17 @@ class CalendarEvent(
|
||||
0
|
||||
}
|
||||
val event = CalendarEvent(
|
||||
label = cursor.getString(1) ?: "",
|
||||
id = cursor.getLong(0),
|
||||
color = cursor.getInt(5),
|
||||
startTime = begin - tzOffset,
|
||||
endTime = cursor.getLong(3) - tzOffset - if (allday) 1 else 0,
|
||||
allDay = allday,
|
||||
location = cursor.getString(6) ?: "",
|
||||
attendees = attendees,
|
||||
description = cursor.getStringOrNull(8)
|
||||
?: "",
|
||||
calendar = cursor.getLong(7)
|
||||
label = cursor.getString(1) ?: "",
|
||||
id = cursor.getLong(0),
|
||||
color = cursor.getInt(5),
|
||||
startTime = begin - tzOffset,
|
||||
endTime = cursor.getLong(3) - tzOffset - if (allday) 1 else 0,
|
||||
allDay = allday,
|
||||
location = cursor.getString(6) ?: "",
|
||||
attendees = attendees,
|
||||
description = cursor.getStringOrNull(8)
|
||||
?: "",
|
||||
calendar = cursor.getLong(7)
|
||||
)
|
||||
results.add(event)
|
||||
}
|
||||
@@ -173,24 +174,26 @@ class CalendarEvent(
|
||||
val calendars = mutableListOf<UserCalendar>()
|
||||
val uri = CalendarContract.Calendars.CONTENT_URI
|
||||
val proj = arrayOf(
|
||||
CalendarContract.Calendars._ID,
|
||||
CalendarContract.Calendars.NAME,
|
||||
CalendarContract.Calendars.ACCOUNT_NAME,
|
||||
CalendarContract.Calendars.CALENDAR_COLOR,
|
||||
CalendarContract.Calendars.VISIBLE,
|
||||
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
|
||||
CalendarContract.Calendars._ID,
|
||||
CalendarContract.Calendars.NAME,
|
||||
CalendarContract.Calendars.ACCOUNT_NAME,
|
||||
CalendarContract.Calendars.CALENDAR_COLOR,
|
||||
CalendarContract.Calendars.VISIBLE,
|
||||
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
|
||||
)
|
||||
if (!context.checkPermission(Manifest.permission.READ_CALENDAR)) return calendars
|
||||
val cursor = context.contentResolver.query(uri, proj, null, null, null)
|
||||
?: return emptyList()
|
||||
?: return emptyList()
|
||||
while (cursor.moveToNext()) {
|
||||
try {
|
||||
calendars.add(UserCalendar(
|
||||
calendars.add(
|
||||
UserCalendar(
|
||||
id = cursor.getLong(0),
|
||||
name = cursor.getString(5) ?: cursor.getString(1) ?: "",
|
||||
owner = cursor.getString(2),
|
||||
color = cursor.getInt(3)
|
||||
))
|
||||
)
|
||||
)
|
||||
} catch (e: NullPointerException) {
|
||||
continue
|
||||
}
|
||||
@@ -206,7 +209,13 @@ class CalendarEvent(
|
||||
it
|
||||
}
|
||||
return if (context.resources.getBoolean(R.bool.is_dark_theme)) {
|
||||
if (ColorUtils.calculateContrast(ContextCompat.getColor(context, R.color.calendar_foreground_color), color) < 2.5 || true) {
|
||||
if (ColorUtils.calculateContrast(
|
||||
ContextCompat.getColor(
|
||||
context,
|
||||
R.color.calendar_foreground_color
|
||||
), color
|
||||
) < 2.5 || true
|
||||
) {
|
||||
if (color.red == color.green && color.red == color.blue) {
|
||||
val level = 0xFF - ((0xFF - color.red) * 0.7f).toInt()
|
||||
Color.rgb(level, level, level)
|
||||
@@ -217,7 +226,13 @@ class CalendarEvent(
|
||||
}
|
||||
} else return color
|
||||
} else {
|
||||
if (ColorUtils.calculateContrast(ContextCompat.getColor(context, R.color.calendar_foreground_color), color) < 1.8) {
|
||||
if (ColorUtils.calculateContrast(
|
||||
ContextCompat.getColor(
|
||||
context,
|
||||
R.color.calendar_foreground_color
|
||||
), color
|
||||
) < 1.8
|
||||
) {
|
||||
if (color.red == color.green && color.red == color.blue) {
|
||||
val level = (color.red * 0.7f).toInt()
|
||||
Color.rgb(level, level, level)
|
||||
@@ -235,8 +250,8 @@ class CalendarEvent(
|
||||
}
|
||||
|
||||
data class UserCalendar(
|
||||
val id: Long,
|
||||
val name: String,
|
||||
val owner: String,
|
||||
val color: Int
|
||||
val id: Long,
|
||||
val name: String,
|
||||
val owner: String,
|
||||
val color: Int
|
||||
)
|
||||
Reference in New Issue
Block a user