1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.calendar
17 
18 import android.app.Activity
19 import android.app.Dialog
20 import android.app.TimePickerDialog
21 import android.content.ComponentName
22 import android.content.Intent
23 import android.content.SharedPreferences
24 import android.os.Bundle
25 import android.preference.CheckBoxPreference
26 import android.preference.ListPreference
27 import android.preference.Preference
28 import android.preference.Preference.OnPreferenceChangeListener
29 import android.preference.PreferenceFragment
30 import android.preference.PreferenceManager
31 import android.preference.PreferenceScreen
32 import android.text.format.DateFormat
33 import android.text.format.Time
34 import android.util.Log
35 import android.widget.TimePicker
36 
37 class OtherPreferences : PreferenceFragment(), OnPreferenceChangeListener {
38     private var mCopyDb: Preference? = null
39     private var mQuietHours: CheckBoxPreference? = null
40     private var mQuietHoursStart: Preference? = null
41     private var mQuietHoursEnd: Preference? = null
42     private var mTimePickerDialog: TimePickerDialog? = null
43     private var mQuietHoursStartListener: TimeSetListener? = null
44     private var mQuietHoursStartDialog: TimePickerDialog? = null
45     private var mQuietHoursEndListener: TimeSetListener? = null
46     private var mQuietHoursEndDialog: TimePickerDialog? = null
47     private var mIs24HourMode = false
48 
49     @Override
onCreatenull50     override fun onCreate(icicle: Bundle?) {
51         super.onCreate(icicle)
52         val manager: PreferenceManager = getPreferenceManager()
53         manager.setSharedPreferencesName(SHARED_PREFS_NAME)
54         val prefs: SharedPreferences = manager.getSharedPreferences()
55         addPreferencesFromResource(R.xml.other_preferences)
56         mCopyDb = findPreference(KEY_OTHER_COPY_DB)
57         val activity: Activity = getActivity()
58         if (activity == null) {
59             Log.d(TAG, "Activity was null")
60         }
61         mIs24HourMode = DateFormat.is24HourFormat(activity)
62         mQuietHours = findPreference(KEY_OTHER_QUIET_HOURS) as CheckBoxPreference?
63         val startHour: Int = prefs.getInt(KEY_OTHER_QUIET_HOURS_START_HOUR,
64                 QUIET_HOURS_DEFAULT_START_HOUR)
65         val startMinute: Int = prefs.getInt(KEY_OTHER_QUIET_HOURS_START_MINUTE,
66                 QUIET_HOURS_DEFAULT_START_MINUTE)
67         mQuietHoursStart = findPreference(KEY_OTHER_QUIET_HOURS_START)
68         mQuietHoursStartListener = TimeSetListener(START_LISTENER)
69         mQuietHoursStartDialog = TimePickerDialog(
70                 activity, mQuietHoursStartListener,
71                 startHour, startMinute, mIs24HourMode)
72         mQuietHoursStart?.setSummary(formatTime(startHour, startMinute))
73         val endHour: Int = prefs.getInt(KEY_OTHER_QUIET_HOURS_END_HOUR,
74                 QUIET_HOURS_DEFAULT_END_HOUR)
75         val endMinute: Int = prefs.getInt(KEY_OTHER_QUIET_HOURS_END_MINUTE,
76                 QUIET_HOURS_DEFAULT_END_MINUTE)
77         mQuietHoursEnd = findPreference(KEY_OTHER_QUIET_HOURS_END)
78         mQuietHoursEndListener = TimeSetListener(END_LISTENER)
79         mQuietHoursEndDialog = TimePickerDialog(
80                 activity, mQuietHoursEndListener,
81                 endHour, endMinute, mIs24HourMode)
82         mQuietHoursEnd?.setSummary(formatTime(endHour, endMinute))
83     }
84 
85     @Override
onPreferenceChangenull86     override fun onPreferenceChange(preference: Preference?, objValue: Any?): Boolean {
87         return true
88     }
89 
90     @Override
onPreferenceTreeClicknull91     override fun onPreferenceTreeClick(screen: PreferenceScreen?, preference: Preference): Boolean {
92         if (preference === mCopyDb) {
93             val intent = Intent(Intent.ACTION_MAIN)
94             intent.setComponent(ComponentName("com.android.providers.calendar",
95                     "com.android.providers.calendar.CalendarDebugActivity"))
96             startActivity(intent)
97         } else if (preference === mQuietHoursStart) {
98             if (mTimePickerDialog == null) {
99                 mTimePickerDialog = mQuietHoursStartDialog
100                 mTimePickerDialog?.show()
101             } else {
102                 Log.v(TAG, "not null")
103             }
104         } else if (preference === mQuietHoursEnd) {
105             if (mTimePickerDialog == null) {
106                 mTimePickerDialog = mQuietHoursEndDialog
107                 mTimePickerDialog?.show()
108             } else {
109                 Log.v(TAG, "not null")
110             }
111         } else {
112             return super.onPreferenceTreeClick(screen, preference)
113         }
114         return true
115     }
116 
117     private inner class TimeSetListener(private val mListenerId: Int) :
118             TimePickerDialog.OnTimeSetListener {
119         @Override
onTimeSetnull120         override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
121             mTimePickerDialog = null
122             val prefs: SharedPreferences = getPreferenceManager().getSharedPreferences()
123             val editor: SharedPreferences.Editor = prefs.edit()
124             val summary = formatTime(hourOfDay, minute)
125             when (mListenerId) {
126                 START_LISTENER -> {
127                     mQuietHoursStart?.setSummary(summary)
128                     editor.putInt(KEY_OTHER_QUIET_HOURS_START_HOUR, hourOfDay)
129                     editor.putInt(KEY_OTHER_QUIET_HOURS_START_MINUTE, minute)
130                 }
131                 END_LISTENER -> {
132                     mQuietHoursEnd?.setSummary(summary)
133                     editor.putInt(KEY_OTHER_QUIET_HOURS_END_HOUR, hourOfDay)
134                     editor.putInt(KEY_OTHER_QUIET_HOURS_END_MINUTE, minute)
135                 }
136                 else -> Log.d(TAG, "Set time for unknown listener: $mListenerId")
137             }
138             editor.commit()
139         }
140     }
141 
142     /**
143      * @param hourOfDay the hour of the day (0-24)
144      * @param minute
145      * @return human-readable string formatted based on 24-hour mode.
146      */
formatTimenull147     private fun formatTime(hourOfDay: Int, minute: Int): String {
148         val time = Time()
149         time.hour = hourOfDay
150         time.minute = minute
151         val format = if (mIs24HourMode) format24Hour else format12Hour
152         return time.format(format)
153     }
154 
155     companion object {
156         private const val TAG = "CalendarOtherPreferences"
157 
158         // The name of the shared preferences file. This name must be maintained for
159         // historical reasons, as it's what PreferenceManager assigned the first
160         // time the file was created.
161         const val SHARED_PREFS_NAME = "com.android.calendar_preferences"
162 
163         // Must be the same keys that are used in the other_preferences.xml file.
164         const val KEY_OTHER_COPY_DB = "preferences_copy_db"
165         const val KEY_OTHER_QUIET_HOURS = "preferences_reminders_quiet_hours"
166         const val KEY_OTHER_REMINDERS_RESPONDED = "preferences_reminders_responded"
167         const val KEY_OTHER_QUIET_HOURS_START = "preferences_reminders_quiet_hours_start"
168         const val KEY_OTHER_QUIET_HOURS_START_HOUR = "preferences_reminders_quiet_hours_start_hour"
169         const val KEY_OTHER_QUIET_HOURS_START_MINUTE =
170                 "preferences_reminders_quiet_hours_start_minute"
171         const val KEY_OTHER_QUIET_HOURS_END = "preferences_reminders_quiet_hours_end"
172         const val KEY_OTHER_QUIET_HOURS_END_HOUR = "preferences_reminders_quiet_hours_end_hour"
173         const val KEY_OTHER_QUIET_HOURS_END_MINUTE = "preferences_reminders_quiet_hours_end_minute"
174         const val KEY_OTHER_1 = "preferences_tardis_1"
175         const val QUIET_HOURS_DEFAULT_START_HOUR = 22
176         const val QUIET_HOURS_DEFAULT_START_MINUTE = 0
177         const val QUIET_HOURS_DEFAULT_END_HOUR = 8
178         const val QUIET_HOURS_DEFAULT_END_MINUTE = 0
179         private const val START_LISTENER = 1
180         private const val END_LISTENER = 2
181         private const val format24Hour = "%H:%M"
182         private const val format12Hour = "%I:%M%P"
183     }
184 }