당직근무 0.10.0 소스와 위젯 꾸미기 및 제작 매뉴얼 공개
위젯별 투명도와 밝은·어두운 색상 및 글자 크기와 표시 항목을 추가했다. 모든 위젯에 함께 적용하는 기본값과 개별 설정을 구분하고 크기에 따라 배치를 바꾼다. 포럼에 APK와 전체 소스 및 사용법과 제작 매뉴얼을 함께 배포한다. 앱 단위 검사 34개와 서버 검사 63개 및 웹 날짜·업데이트 검사 통과. 공개 소스에서 APK 빌드와 정적 검사 오류 0개 확인. 경고는 69개다. Android 16 가상 기기에서 기존 앱 위에 설치하고 투명 위젯과 큰 글자 및 RemoteViews 검사를 확인했다. 실제 휴대폰과 다른 홈 화면 앱의 절전·크기 동작은 별도 확인이 필요하다. 회원 DB와 인증 정보 및 배포 서명 키와 내부 운영 기록은 공개 자료에 포함하지 않았다.
This commit is contained in:
commit
8112721fa8
96 files changed
+7495
No files matched your search
@@ -0,0 +1,173 @@
|
||||
package kr.co.itosm.dutycalendar
|
||||
|
||||
import android.app.PendingIntent
|
||||
import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.text.SpannableString
|
||||
import android.text.Spanned
|
||||
import android.text.style.ForegroundColorSpan
|
||||
import android.text.style.StyleSpan
|
||||
import android.graphics.Typeface
|
||||
import android.text.style.RelativeSizeSpan
|
||||
import android.widget.RemoteViews
|
||||
import java.time.LocalDate
|
||||
import java.time.YearMonth
|
||||
|
||||
/**
|
||||
* 홈 화면 앱이 표시할 달력을 RemoteViews로 전달한다. 앱 화면을 계속 켜 둘 필요는 없다.
|
||||
* 위젯마다 보고 있는 달을 따로 저장하므로 여러 개를 배치해도 월 이동이 서로 영향을 주지 않는다.
|
||||
* 월을 고르지 않은 위젯은 현재 달을 따라간다. 직접 이동한 위젯은 오늘 버튼을 누를 때까지 선택을 유지한다.
|
||||
* 날짜 변경과 부팅 알림 및 주기 갱신으로 오늘 표시를 갱신한다. 실제 전달 시점은 운영체제의 영향을 받는다.
|
||||
*/
|
||||
class DutyWidget : AppWidgetProvider() {
|
||||
override fun onUpdate(context: Context, manager: AppWidgetManager, ids: IntArray) {
|
||||
Holidays.schedule(context)
|
||||
SyncClient.schedule(context)
|
||||
ids.forEach { update(context, manager, it) }
|
||||
}
|
||||
override fun onAppWidgetOptionsChanged(context: Context, manager: AppWidgetManager, id: Int, options: Bundle) { update(context, manager, id) }
|
||||
override fun onDeleted(context: Context, ids: IntArray) {
|
||||
val editor = context.getSharedPreferences("widget", Context.MODE_PRIVATE).edit()
|
||||
ids.forEach { editor.remove("month_$it") }
|
||||
editor.apply()
|
||||
context.getSharedPreferences("widget_style",0).edit().apply { ids.forEach { remove("style_$it") } }.apply()
|
||||
}
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
super.onReceive(context, intent)
|
||||
val manager = AppWidgetManager.getInstance(context)
|
||||
val action = intent.action ?: return
|
||||
if (action in listOf(PREVIOUS, NEXT, TODAY)) {
|
||||
val id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
|
||||
// 삭제된 위젯이나 이 앱에 속하지 않은 번호로 들어온 요청은 처리하지 않는다.
|
||||
if (id !in manager.getAppWidgetIds(ComponentName(context, DutyWidget::class.java))) return
|
||||
val prefs = context.getSharedPreferences("widget", Context.MODE_PRIVATE)
|
||||
val editor = prefs.edit()
|
||||
if (action == TODAY) editor.remove("month_$id")
|
||||
else editor.putString("month_$id", selectedMonth(context, id).plusMonths(if (action == NEXT) 1 else -1).toString())
|
||||
editor.apply()
|
||||
update(context, manager, id)
|
||||
} else if (action in listOf(Intent.ACTION_DATE_CHANGED, Intent.ACTION_TIME_CHANGED, Intent.ACTION_TIMEZONE_CHANGED, Intent.ACTION_BOOT_COMPLETED)) {
|
||||
Holidays.schedule(context)
|
||||
SyncClient.schedule(context)
|
||||
updateAll(context)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val PREVIOUS = "kr.co.itosm.dutycalendar.PREVIOUS"
|
||||
private const val NEXT = "kr.co.itosm.dutycalendar.NEXT"
|
||||
private const val TODAY = "kr.co.itosm.dutycalendar.TODAY"
|
||||
// 날짜 칸의 리소스 참조를 명시해 코드와 42칸 레이아웃의 순서를 맞춘다.
|
||||
// 문자열로 리소스를 찾지 않으므로 이름 변경이나 리소스 축소 때도 참조 관계를 추적할 수 있다.
|
||||
private val DAY_IDS = intArrayOf(
|
||||
R.id.day_0, R.id.day_1, R.id.day_2, R.id.day_3, R.id.day_4, R.id.day_5, R.id.day_6,
|
||||
R.id.day_7, R.id.day_8, R.id.day_9, R.id.day_10, R.id.day_11, R.id.day_12, R.id.day_13,
|
||||
R.id.day_14, R.id.day_15, R.id.day_16, R.id.day_17, R.id.day_18, R.id.day_19, R.id.day_20,
|
||||
R.id.day_21, R.id.day_22, R.id.day_23, R.id.day_24, R.id.day_25, R.id.day_26, R.id.day_27,
|
||||
R.id.day_28, R.id.day_29, R.id.day_30, R.id.day_31, R.id.day_32, R.id.day_33, R.id.day_34,
|
||||
R.id.day_35, R.id.day_36, R.id.day_37, R.id.day_38, R.id.day_39, R.id.day_40, R.id.day_41
|
||||
)
|
||||
// 저장된 월을 읽지 못해도 위젯을 비워 두지 않는다. 현재 달로 복구해 정상적인 표시를 유지한다.
|
||||
private fun selectedMonth(context: Context, id: Int): YearMonth = runCatching {
|
||||
YearMonth.parse(context.getSharedPreferences("widget", Context.MODE_PRIVATE).getString("month_$id", YearMonth.now().toString()))
|
||||
}.getOrDefault(YearMonth.now())
|
||||
|
||||
fun updateAll(context: Context) {
|
||||
val manager = AppWidgetManager.getInstance(context)
|
||||
manager.getAppWidgetIds(ComponentName(context, DutyWidget::class.java)).forEach { update(context, manager, it) }
|
||||
}
|
||||
private fun update(context: Context, manager: AppWidgetManager, id: Int) {
|
||||
manager.updateAppWidget(id, buildViews(context,id,manager.getAppWidgetOptions(id)))
|
||||
}
|
||||
internal fun buildViews(context:Context,id:Int,options:Bundle):RemoteViews {
|
||||
val style=WidgetStyle.load(context,id)
|
||||
val portrait=context.resources.configuration.orientation!=android.content.res.Configuration.ORIENTATION_LANDSCAPE
|
||||
val height=options.getInt(if(portrait)AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT else AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT,420).coerceIn(250,1200)
|
||||
val compact=style.compact(height)
|
||||
val density=context.resources.displayMetrics.density
|
||||
val month = selectedMonth(context, id)
|
||||
val schedule = ScheduleStore.load(context)
|
||||
val people = if(style.people&&!compact)People.all(context) else emptyList()
|
||||
val today = LocalDate.now()
|
||||
val configured = ScheduleStore.isConfigured(context)
|
||||
val view = RemoteViews(context.packageName, R.layout.duty_widget)
|
||||
// 배경만 투명하게 바꾸고 글자는 독립된 색으로 남긴다. 배경 교체 시 사라지는 안쪽 여백도 명시한다.
|
||||
view.setInt(R.id.widget_root,"setBackgroundColor",style.background)
|
||||
view.setViewPadding(R.id.widget_root,(12*density).toInt(),(10*density).toInt(),(12*density).toInt(),(10*density).toInt())
|
||||
for(control in listOf(R.id.widget_title,R.id.widget_today,R.id.widget_customize,R.id.widget_previous,R.id.widget_next))view.setTextColor(control,style.ink)
|
||||
for(control in listOf(R.id.widget_subtitle,R.id.widget_footer,R.id.widget_mon,R.id.widget_tue,R.id.widget_wed,R.id.widget_thu,R.id.widget_fri))view.setTextColor(control,style.muted)
|
||||
view.setTextColor(R.id.widget_sun,style.color(0xFFBD5163.toInt()))
|
||||
view.setTextColor(R.id.widget_saturday, style.color(schedule.saturdayColor))
|
||||
view.setTextViewTextSize(R.id.widget_title,android.util.TypedValue.COMPLEX_UNIT_SP,18f*style.scale/100)
|
||||
val customize=Intent(context,MainActivity::class.java).setData(android.net.Uri.parse("dutycalendar://widget/$id/style")).putExtra("widgetStyleId",id)
|
||||
view.setOnClickPendingIntent(R.id.widget_customize,PendingIntent.getActivity(context,id,customize,PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE))
|
||||
view.setTextViewText(R.id.widget_title, "${month.year}년 ${month.monthValue}월")
|
||||
view.setTextViewText(R.id.widget_subtitle, if (configured) "오늘 ${schedule.shiftOn(today).title} · ${schedule.pattern.size}일 반복" else "앱에서 근무 시작 날짜를 설정해 주세요")
|
||||
view.setTextViewText(R.id.widget_footer, if (month.year in Holidays.years(context)) "제작자 블리어 (이창현)" else "${month.year}년 공휴일 자료 확인 필요")
|
||||
val open = PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
|
||||
view.setOnClickPendingIntent(R.id.widget_root, open)
|
||||
// 같은 동작도 위젯 번호별로 다른 PendingIntent를 사용한다. 다른 위젯의 월을 바꾸지 않기 위해서다.
|
||||
// 명시적 수신 대상과 변경 불가 플래그를 사용해 홈 화면 앱이 전달 내용을 바꾸지 못하게 한다.
|
||||
listOf(R.id.widget_previous to PREVIOUS, R.id.widget_next to NEXT, R.id.widget_today to TODAY).forEach { (control, action) ->
|
||||
val intent = Intent(context, DutyWidget::class.java).setAction(action).putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id)
|
||||
view.setOnClickPendingIntent(control, PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE))
|
||||
}
|
||||
calendarDays(month).forEachIndexed { index, date ->
|
||||
val shift = schedule.shiftOn(date)
|
||||
val holiday = Holidays.name(context, date)
|
||||
val events = PlannerStore.eventsOn(context, date)
|
||||
val personalDays = Anniversaries.on(context, date)
|
||||
val number = date.dayOfMonth.toString() + if (style.eventMark && (events.isNotEmpty() || personalDays.isNotEmpty())) "+" else ""
|
||||
val holidayShort = holiday?.takeIf { style.holidays&&!compact }?.let { if (it.contains("대체공휴일")) "대체휴일" else it.replace(" 전날", "").replace(" 다음 날", "").replace("기독탄신일", "성탄절").replace("1월 1일", "신정") }
|
||||
val lunar = if (schedule.showLunar&&style.lunar&&!compact) LunarDates.label(context, date) else null
|
||||
val heading = number + (lunar?.let { "\n$it" } ?: "")
|
||||
val shiftStart = heading.length + 1
|
||||
val baseText = "$heading\n${shift.shortTitle}" + (holidayShort?.let { "\n$it" } ?: "")
|
||||
val colleagues = People.onDuty(people, date)
|
||||
// 작은 위젯에는 두 줄까지만 표시한다. 사람이 많아도 날짜의 높이와 정렬을 유지하고 전체 이름은 눌러서 확인한다.
|
||||
val names = if (colleagues.size > 2) listOf(colleagues.first().name.take(3), "외${colleagues.size - 1}명") else colleagues.map { it.name.take(3) }
|
||||
val cellText = baseText + names.joinToString("", transform = { "\n$it" }) + if (style.anniversaries&&!compact&&personalDays.isNotEmpty()) "\n" + personalDays.first().getJSONObject("data").getString("title").take(5) else ""
|
||||
// 하나의 날짜 칸 안에서 날짜와 근무 및 공휴일 색상을 따로 지정한다.
|
||||
// 전체 공휴일 이름은 접근성 설명에 남기고 화면에는 폭에 맞는 짧은 표기를 사용한다.
|
||||
val styled = SpannableString(cellText)
|
||||
val dateColor = style.color(schedule.dateColor(date, holiday != null))
|
||||
styled.setSpan(StyleSpan(Typeface.BOLD), 0, number.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
styled.setSpan(StyleSpan(Typeface.BOLD), shiftStart, shiftStart + shift.shortTitle.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
styled.setSpan(ForegroundColorSpan(dateColor), 0, number.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
styled.setSpan(ForegroundColorSpan(style.color(schedule.shiftColor(date))), shiftStart, shiftStart + shift.shortTitle.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
styled.setSpan(RelativeSizeSpan(0.85f), number.length + 1, cellText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
if (holidayShort != null) styled.setSpan(ForegroundColorSpan(style.color(0xFFBD5163.toInt())), shiftStart + 1 + shift.shortTitle.length, baseText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
if (lunar != null) {
|
||||
styled.setSpan(RelativeSizeSpan(0.75f), number.length + 1, heading.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
styled.setSpan(ForegroundColorSpan(style.muted), number.length + 1, heading.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
}
|
||||
var nameStart = baseText.length + 1
|
||||
names.forEachIndexed { i, name ->
|
||||
styled.setSpan(ForegroundColorSpan(if (colleagues.size > 2 && i == 1) style.muted else style.color(colleagues[i].color)), nameStart, nameStart + name.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
nameStart += name.length + 1
|
||||
}
|
||||
if (YearMonth.from(date) != month) {
|
||||
styled.setSpan(ForegroundColorSpan(0xFF9AA4B3.toInt()), 0, cellText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
||||
}
|
||||
val cellId = DAY_IDS[index]
|
||||
view.setTextViewText(cellId, styled)
|
||||
view.setInt(cellId, "setMaxLines", if(compact)2 else 7)
|
||||
// 여섯 주를 같은 높이로 유지한다. 작은 위젯의 자동 배치는 글줄을 줄이고 사용자 글자 배율을 적용한다.
|
||||
val baseSize=((height-120)/6f/(if(compact)2.6f else 5.4f)).coerceIn(11f,17f)
|
||||
view.setTextViewTextSize(cellId,android.util.TypedValue.COMPLEX_UNIT_SP,baseSize*style.scale/100)
|
||||
view.setInt(cellId, "setGravity", android.view.Gravity.TOP or android.view.Gravity.CENTER_HORIZONTAL)
|
||||
view.setViewPadding(cellId, 0, ((if(compact)2 else 5)*density).toInt(), 0, 0)
|
||||
view.setTextColor(cellId, if (YearMonth.from(date) == month) style.ink else 0xFF9AA4B3.toInt())
|
||||
view.setInt(cellId, "setBackgroundColor", if (date == today && style.today) (if(style.dark)0x554A6D96 else 0x44A7B5CB) else android.graphics.Color.TRANSPARENT)
|
||||
view.setContentDescription(cellId, "${date.monthValue}월 ${date.dayOfMonth}일 ${shift.title} ${lunar.orEmpty()} ${holiday.orEmpty()} 일정 ${events.size}개. 기념일: ${personalDays.joinToString { it.getJSONObject("data").getString("title") }}. 함께 당직: ${colleagues.joinToString { it.name }}")
|
||||
val dayIntent = Intent(context, MainActivity::class.java).setData(android.net.Uri.parse("dutycalendar://date/$date")).putExtra("selectedDate", date.toString())
|
||||
view.setOnClickPendingIntent(cellId, PendingIntent.getActivity(context, id * 1000 + index + 1, dayIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE))
|
||||
}
|
||||
return view
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user