diff --git a/res/layout/setting_holiday.xml b/res/layout/setting_holiday.xml index fdc1d9cda..02eaba448 100644 --- a/res/layout/setting_holiday.xml +++ b/res/layout/setting_holiday.xml @@ -13,7 +13,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" app:headerColor="#2a5a4c" - app:todayLabelColor="#0000ff" - app:highlightedDaysLabelsColor="#ff0000" + app:todayLabelColor="#ff0000" + app:type="many_days_picker" /> \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 8b195bb2a..ac46099a7 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -36,7 +36,7 @@ Repeat Exclude Holidays - Check holidays + Edit Holidays Delete diff --git a/src/com/android/deskclock/DeskClockApplication.kt b/src/com/android/deskclock/DeskClockApplication.kt index 452eb00da..0b0c6ebba 100644 --- a/src/com/android/deskclock/DeskClockApplication.kt +++ b/src/com/android/deskclock/DeskClockApplication.kt @@ -27,6 +27,7 @@ import com.best.deskclock.controller.Controller import com.best.deskclock.data.DataModel import com.best.deskclock.events.LogEventTracker import com.best.deskclock.uidata.UiDataModel +import com.best.deskclock.data.Holidays class DeskClockApplication : Application() { override fun onCreate() { @@ -39,6 +40,8 @@ class DeskClockApplication : Application() { UiDataModel.uiDataModel.init(applicationContext, prefs) Controller.getController().setContext(applicationContext) Controller.getController().addEventTracker(LogEventTracker(applicationContext)) + var holidays = Holidays() + holidays.init(applicationContext, prefs) } companion object { @@ -62,4 +65,5 @@ class DeskClockApplication : Application() { return PreferenceManager.getDefaultSharedPreferences(storageContext) } } -} \ No newline at end of file +} + diff --git a/src/com/android/deskclock/data/Holidays.kt b/src/com/android/deskclock/data/Holidays.kt index 13c05f8c7..f8cb37cff 100644 --- a/src/com/android/deskclock/data/Holidays.kt +++ b/src/com/android/deskclock/data/Holidays.kt @@ -15,11 +15,21 @@ */ package com.best.deskclock.data +import android.content.Context +import android.content.SharedPreferences +import androidx.core.text.buildSpannedString +import com.best.deskclock.DeskClockApplication +import com.best.deskclock.settings.MaterialCalendarActivity import java.util.* -public class Holidays { +class Holidays { + + companion object { - private val holidays = arrayOf( + private var mContext: Context? = null + private var mPrefs: SharedPreferences? = null + private val mHolidays = arrayListOf() + private val default_holidays = arrayOf( "2022/01/01", // Western New Year's Day "2022/01/02", "2022/01/03", @@ -39,31 +49,89 @@ public class Holidays { "2022/05/05", // Children's day "2022/07/18", // Marine day ) - } - fun isHoliday(time: Calendar): Boolean { - for (holiday in holidays) { - var h = holiday.split("/") - var month = h[1] - if (time[Calendar.YEAR].equals(h[0].toInt()) && - time[Calendar.MONTH].equals(h[1].toInt() - 1) && - time[Calendar.DAY_OF_MONTH].equals(h[2].toInt())) { - return true + /** Key to an extra that defines resource id to the title of this activity. */ + private const val KEY_HOLIDAYS = "holidays" + + fun getHolidays() :MutableList { + val calendars: MutableList = ArrayList() + for (holiday in mHolidays) { + var h = holiday.split("/") + var year = h[0].toInt() + var month = h[1].toInt() - 1 + var dayofMonth = h[2].toInt() + val calendar = Calendar.getInstance() + calendar[year, month] = dayofMonth + calendars.add(calendar) + } + return calendars + } + fun isHoliday(time: Calendar): Boolean { + for (holiday in mHolidays) { + var h = holiday.split("/") + var month = h[1] + if (time[Calendar.YEAR].equals(h[0].toInt()) && + time[Calendar.MONTH].equals(h[1].toInt() - 1) && + time[Calendar.DAY_OF_MONTH].equals(h[2].toInt())) { + return true + } + } + return false + } + } + fun init(context: Context, prefs: SharedPreferences) { + if (mContext != context) { + mContext = context + mPrefs = prefs + var prefsHolidays = prefs.getString(KEY_HOLIDAYS, null) + if (prefsHolidays == null) { + // 空っぽならば、新規につくる + mHolidays.clear() + for (holiday in default_holidays) { + mHolidays.add(holiday) + } + // prefsにmHolidays を変換して書き込む + writeHolidaysToPrefs() + } else { + mHolidays.clear() + for (holiday in prefsHolidays.split(",")) { + if (holiday != "") { + mHolidays.add(holiday) + } + } } } - return false } - fun getHolidays() :MutableList { - val calendars: MutableList = ArrayList() - for (holiday in holidays) { - var h = holiday.split("/") - var year = h[0].toInt() - var month = h[1].toInt() - 1 - var dayofMonth = h[2].toInt() - val calendar = Calendar.getInstance() - calendar[year, month] = dayofMonth - calendars.add(calendar) + + // XX writeHolidaysTo + private fun writeHolidaysToPrefs () { + var strHolidays = "" + for (holiday in mHolidays) { + strHolidays = strHolidays + holiday + "," + } + val editor = mPrefs!!.edit() + //editor.putString(KEY_HOLIDAYS, holidays.getHolidaysString) + editor.putString(KEY_HOLIDAYS, strHolidays) + //editor.commit(); + editor.apply() + } + + // XX + fun setHolidays (holidays: List) { + // XXholidaysをmHolidaysに設定する + if (holidays == null) { + return + } else { + mHolidays.clear() + for (holiday in holidays) { + var str = holiday[Calendar.YEAR].toString() + "/" + + (holiday[Calendar.MONTH] + 1).toString() + "/" + + holiday[Calendar.DAY_OF_MONTH].toString() + mHolidays.add(str) + } + writeHolidaysToPrefs() } - return calendars } + + } diff --git a/src/com/android/deskclock/data/Weekdays.kt b/src/com/android/deskclock/data/Weekdays.kt index 43d4333e5..3be2d422c 100644 --- a/src/com/android/deskclock/data/Weekdays.kt +++ b/src/com/android/deskclock/data/Weekdays.kt @@ -195,7 +195,6 @@ class Weekdays private constructor(bits: Int) { nextTime[Calendar.YEAR] = time[Calendar.YEAR] nextTime[Calendar.MONTH] = time[Calendar.MONTH] nextTime[Calendar.DAY_OF_MONTH] = time[Calendar.DAY_OF_MONTH] - var holidays = Holidays() var maxDays = 6 if (excludeHolidays) { maxDays = 365 @@ -210,12 +209,12 @@ class Weekdays private constructor(bits: Int) { LogUtils.i("false") } LogUtils.i("isHoliday") - if (holidays.isHoliday(nextTime)) { + if (Holidays.isHoliday(nextTime)) { LogUtils.i("true") } else { LogUtils.i("false") } - if (!excludeHolidays || holidays.isHoliday(nextTime) == false) { + if (!excludeHolidays || Holidays.isHoliday(nextTime) == false) { return count } } diff --git a/src/com/android/deskclock/settings/MaterialCalendarActivity.kt b/src/com/android/deskclock/settings/MaterialCalendarActivity.kt index bcb99f3ec..2d79cef48 100644 --- a/src/com/android/deskclock/settings/MaterialCalendarActivity.kt +++ b/src/com/android/deskclock/settings/MaterialCalendarActivity.kt @@ -16,11 +16,13 @@ import com.best.deskclock.R import java.util.* class MaterialCalendarActivity : AppCompatActivity() { + private var mCalendarView: CalendarView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.setting_holiday) val calendarView = findViewById(R.id.calendarView) + mCalendarView = calendarView val context: Context = getApplicationContext() val titleResourceId = intent.getIntExtra(EXTRA_TITLE, 0) @@ -30,25 +32,16 @@ class MaterialCalendarActivity : AppCompatActivity() { min.add(Calendar.DAY_OF_MONTH, -1); calendarView.setMinimumDate(min) - var holidays = Holidays() -// calendarView.setSelectedDates(holidays.getHolidays()) - calendarView.setHighlightedDays(holidays.getHolidays()) + calendarView.setSelectedDates(Holidays.getHolidays()) calendarView.setOnDayClickListener(OnDayClickListener { eventDay -> - //currently, do nothing(only show holidays) - //val nowCalendar = eventDay.calendar - + val nowCalendar = eventDay.calendar Toast.makeText( applicationContext, - "Even you select the holidays, it is not added to the holidays list(not yet implemented)." - ,Toast.LENGTH_LONG + nowCalendar.get(Calendar.YEAR).toString() + "-" + + (nowCalendar.get(Calendar.MONTH) + 1).toString() + "-" + + nowCalendar.get(Calendar.DATE).toString(), Toast.LENGTH_SHORT ).show() - //Toast.makeText( - // applicationContext, - // nowCalendar.get(Calendar.YEAR).toString() + "-" + - // (nowCalendar.get(Calendar.MONTH) + 1).toString() + "-" + - // nowCalendar.get(Calendar.DATE).toString(), Toast.LENGTH_SHORT - //).show() }) @@ -56,12 +49,20 @@ class MaterialCalendarActivity : AppCompatActivity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { when(item?.itemId){ android.R.id.home->{ + var holidays = Holidays() + mCalendarView?.getSelectedDates()?.let { holidays.setHolidays(it) } finish() } } return super.onOptionsItemSelected(item) } + override fun onDestroy() { + var holidays = Holidays() + mCalendarView?.getSelectedDates()?.let { holidays.setHolidays(it) } + super.onDestroy() + } + companion object { /** Key to an extra that defines resource id to the title of this activity. */ private const val EXTRA_TITLE = "extra_title"