1 /* 2 * Copyright (C) 2020 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 17 package com.android.deskclock.settings 18 19 import android.annotation.TargetApi 20 import android.os.Build 21 import android.os.Bundle 22 import android.view.MenuItem 23 import androidx.appcompat.app.AppCompatActivity 24 import androidx.preference.ListPreference 25 import androidx.preference.Preference 26 import androidx.preference.PreferenceFragmentCompat 27 28 import com.android.deskclock.R 29 import com.android.deskclock.Utils 30 31 /** 32 * Settings for Clock screen saver 33 */ 34 class ScreensaverSettingsActivity : AppCompatActivity() { 35 onCreatenull36 override fun onCreate(savedInstanceState: Bundle?) { 37 super.onCreate(savedInstanceState) 38 setContentView(R.layout.screensaver_settings) 39 } 40 onOptionsItemSelectednull41 override fun onOptionsItemSelected(item: MenuItem): Boolean { 42 when (item.getItemId()) { 43 android.R.id.home -> { 44 finish() 45 return true 46 } 47 else -> { 48 } 49 } 50 return super.onOptionsItemSelected(item) 51 } 52 53 class PrefsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener { 54 55 @TargetApi(Build.VERSION_CODES.N) onCreatenull56 override fun onCreate(savedInstanceState: Bundle?) { 57 super.onCreate(savedInstanceState) 58 if (Utils.isNOrLater) { 59 getPreferenceManager().setStorageDeviceProtected() 60 } 61 } 62 onCreatePreferencesnull63 override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { 64 addPreferencesFromResource(R.xml.screensaver_settings) 65 } 66 onResumenull67 override fun onResume() { 68 super.onResume() 69 refresh() 70 } 71 onPreferenceChangenull72 override fun onPreferenceChange(pref: Preference, newValue: Any?): Boolean { 73 if (KEY_CLOCK_STYLE == pref.getKey()) { 74 val clockStylePref: ListPreference = pref as ListPreference 75 val index: Int = clockStylePref.findIndexOfValue(newValue as String?) 76 clockStylePref.setSummary(clockStylePref.getEntries().get(index)) 77 } 78 return true 79 } 80 refreshnull81 private fun refresh() { 82 val clockStylePref = findPreference<ListPreference>(KEY_CLOCK_STYLE) as ListPreference 83 clockStylePref.setSummary(clockStylePref.getEntry()) 84 clockStylePref.setOnPreferenceChangeListener(this) 85 } 86 } 87 88 companion object { 89 const val KEY_CLOCK_STYLE = "screensaver_clock_style" 90 const val KEY_NIGHT_MODE = "screensaver_night_mode" 91 } 92 } 93