11package tk.superl2.xwifi
22
3- import android.annotation.TargetApi
4- import android.content.Context
5- import android.content.Intent
6- import android.content.res.Configuration
7- import android.media.RingtoneManager
8- import android.net.Uri
9- import android.os.Build
103import android.os.Bundle
11- import android.preference.ListPreference
12- import android.preference.Preference
134import android.preference.PreferenceActivity
5+ import android.R.xml
146import android.preference.PreferenceFragment
15- import android.preference.PreferenceManager
16- import android.preference.RingtonePreference
17- import android.text.TextUtils
18- import android.view.MenuItem
19- import android.support.v4.app.NavUtils
7+
8+
209
2110/* *
2211 * A [PreferenceActivity] that presents a set of application settings. On
@@ -29,224 +18,19 @@ import android.support.v4.app.NavUtils
2918 * for more information on developing a Settings UI.
3019 */
3120class SettingsActivity : PreferenceActivity () {
21+ override fun onCreate (savedInstanceState : Bundle ? ) {
22+ super .onCreate(savedInstanceState)
3223
33- override fun onCreate (savedInstanceState : Bundle ? ) {
34- super .onCreate(savedInstanceState)
35- setupActionBar()
36- }
37-
38- /* *
39- * Set up the [android.app.ActionBar], if the API is available.
40- */
41- private fun setupActionBar () {
42- actionBar?.setDisplayHomeAsUpEnabled(true )
43- }
44-
45- override fun onMenuItemSelected (featureId : Int , item : MenuItem ): Boolean {
46- val id = item.itemId
47- if (id == android.R .id.home) {
48- if (! super .onMenuItemSelected(featureId, item)) {
49- NavUtils .navigateUpFromSameTask(this )
50- }
51- return true
52- }
53- return super .onMenuItemSelected(featureId, item)
54- }
55-
56- /* *
57- * {@inheritDoc}
58- */
59- override fun onIsMultiPane (): Boolean {
60- return isXLargeTablet(this )
61- }
62-
63- /* *
64- * {@inheritDoc}
65- */
66- @TargetApi(Build .VERSION_CODES .HONEYCOMB )
67- override fun onBuildHeaders (target : List <PreferenceActivity .Header >) {
68- loadHeadersFromResource(R .xml.pref_headers, target)
69- }
70-
71- /* *
72- * This method stops fragment injection in malicious applications.
73- * Make sure to deny any unknown fragments here.
74- */
75- override fun isValidFragment (fragmentName : String ): Boolean {
76- return PreferenceFragment ::class .java.name == fragmentName
77- || GeneralPreferenceFragment ::class .java.name == fragmentName
78- || DataSyncPreferenceFragment ::class .java.name == fragmentName
79- || NotificationPreferenceFragment ::class .java.name == fragmentName
80- }
81-
82- /* *
83- * This fragment shows general preferences only. It is used when the
84- * activity is showing a two-pane settings UI.
85- */
86- @TargetApi(Build .VERSION_CODES .HONEYCOMB )
87- class GeneralPreferenceFragment : PreferenceFragment () {
88- override fun onCreate (savedInstanceState : Bundle ? ) {
89- super .onCreate(savedInstanceState)
90- addPreferencesFromResource(R .xml.pref_general)
91- setHasOptionsMenu(true )
92-
93- // Bind the summaries of EditText/List/Dialog/Ringtone preferences
94- // to their values. When their values change, their summaries are
95- // updated to reflect the new value, per the Android Design
96- // guidelines.
97- bindPreferenceSummaryToValue(findPreference(" example_text" ))
98- bindPreferenceSummaryToValue(findPreference(" example_list" ))
99- }
100-
101- override fun onOptionsItemSelected (item : MenuItem ): Boolean {
102- val id = item.itemId
103- if (id == android.R .id.home) {
104- startActivity(Intent (activity, SettingsActivity ::class .java))
105- return true
106- }
107- return super .onOptionsItemSelected(item)
108- }
109- }
110-
111- /* *
112- * This fragment shows notification preferences only. It is used when the
113- * activity is showing a two-pane settings UI.
114- */
115- @TargetApi(Build .VERSION_CODES .HONEYCOMB )
116- class NotificationPreferenceFragment : PreferenceFragment () {
117- override fun onCreate (savedInstanceState : Bundle ? ) {
118- super .onCreate(savedInstanceState)
119- addPreferencesFromResource(R .xml.pref_notification)
120- setHasOptionsMenu(true )
121-
122- // Bind the summaries of EditText/List/Dialog/Ringtone preferences
123- // to their values. When their values change, their summaries are
124- // updated to reflect the new value, per the Android Design
125- // guidelines.
126- bindPreferenceSummaryToValue(findPreference(" notifications_new_message_ringtone" ))
127- }
128-
129- override fun onOptionsItemSelected (item : MenuItem ): Boolean {
130- val id = item.itemId
131- if (id == android.R .id.home) {
132- startActivity(Intent (activity, SettingsActivity ::class .java))
133- return true
134- }
135- return super .onOptionsItemSelected(item)
136- }
137- }
138-
139- /* *
140- * This fragment shows data and sync preferences only. It is used when the
141- * activity is showing a two-pane settings UI.
142- */
143- @TargetApi(Build .VERSION_CODES .HONEYCOMB )
144- class DataSyncPreferenceFragment : PreferenceFragment () {
145- override fun onCreate (savedInstanceState : Bundle ? ) {
146- super .onCreate(savedInstanceState)
147- addPreferencesFromResource(R .xml.pref_data_sync)
148- setHasOptionsMenu(true )
149-
150- // Bind the summaries of EditText/List/Dialog/Ringtone preferences
151- // to their values. When their values change, their summaries are
152- // updated to reflect the new value, per the Android Design
153- // guidelines.
154- bindPreferenceSummaryToValue(findPreference(" sync_frequency" ))
155- }
156-
157- override fun onOptionsItemSelected (item : MenuItem ): Boolean {
158- val id = item.itemId
159- if (id == android.R .id.home) {
160- startActivity(Intent (activity, SettingsActivity ::class .java))
161- return true
162- }
163- return super .onOptionsItemSelected(item)
164- }
165- }
166-
167- companion object {
168-
169- /* *
170- * A preference value change listener that updates the preference's summary
171- * to reflect its new value.
172- */
173- private val sBindPreferenceSummaryToValueListener = Preference .OnPreferenceChangeListener { preference, value ->
174- val stringValue = value.toString()
175-
176- if (preference is ListPreference ) {
177- // For list preferences, look up the correct display value in
178- // the preference's 'entries' list.
179- val listPreference = preference
180- val index = listPreference.findIndexOfValue(stringValue)
181-
182- // Set the summary to reflect the new value.
183- preference.setSummary(
184- if (index >= 0 )
185- listPreference.entries[index]
186- else
187- null
188- )
189-
190- } else if (preference is RingtonePreference ) {
191- // For ringtone preferences, look up the correct display value
192- // using RingtoneManager.
193- if (TextUtils .isEmpty(stringValue)) {
194- // Empty values correspond to 'silent' (no ringtone).
195- preference.setSummary(R .string.pref_ringtone_silent)
196-
197- } else {
198- val ringtone = RingtoneManager .getRingtone(
199- preference.getContext(), Uri .parse(stringValue)
200- )
201-
202- if (ringtone == null ) {
203- // Clear the summary if there was a lookup error.
204- preference.setSummary(null )
205- } else {
206- // Set the summary to reflect the new ringtone display
207- // name.
208- val name = ringtone.getTitle(preference.getContext())
209- preference.setSummary(name)
210- }
211- }
212-
213- } else {
214- // For all other preferences, set the summary to the value's
215- // simple string representation.
216- preference.summary = stringValue
217- }
218- true
219- }
220-
221- /* *
222- * Helper method to determine if the device has an extra-large screen. For
223- * example, 10" tablets are extra-large.
224- */
225- private fun isXLargeTablet (context : Context ): Boolean {
226- return context.resources.configuration.screenLayout and Configuration .SCREENLAYOUT_SIZE_MASK >= Configuration .SCREENLAYOUT_SIZE_XLARGE
227- }
228-
229- /* *
230- * Binds a preference's summary to its value. More specifically, when the
231- * preference's value is changed, its summary (line of text below the
232- * preference title) is updated to reflect the value. The summary is also
233- * immediately updated upon calling this method. The exact display format is
234- * dependent on the type of preference.
24+ // Display the fragment as the main context
25+ fragmentManager.beginTransaction().replace(android.R .id.content, SettingsFragment ()).commit()
26+ }
27+ }
23528
236- * @see .sBindPreferenceSummaryToValueListener
237- */
238- private fun bindPreferenceSummaryToValue (preference : Preference ) {
239- // Set the listener to watch for value changes.
240- preference.onPreferenceChangeListener = sBindPreferenceSummaryToValueListener
29+ class SettingsFragment : PreferenceFragment () {
30+ override fun onCreate (savedInstanceState : Bundle ? ) {
31+ super .onCreate(savedInstanceState)
24132
242- // Trigger the listener immediately with the preference's
243- // current value.
244- sBindPreferenceSummaryToValueListener.onPreferenceChange(
245- preference,
246- PreferenceManager
247- .getDefaultSharedPreferences(preference.context)
248- .getString(preference.key, " " )
249- )
250- }
251- }
252- }
33+ // Load the preferences from an XML resource
34+ addPreferencesFromResource(R .xml.preferences)
35+ }
36+ }
0 commit comments