Skip to content

Commit c2b211f

Browse files
committed
Settings add
1 parent 63c8413 commit c2b211f

File tree

10 files changed

+470
-0
lines changed

10 files changed

+470
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.hackware.mormont.notebook
2+
3+
import android.content.res.Configuration
4+
import android.os.Bundle
5+
import android.preference.PreferenceActivity
6+
import androidx.annotation.LayoutRes
7+
import androidx.appcompat.app.ActionBar
8+
import androidx.appcompat.app.AppCompatDelegate
9+
import androidx.appcompat.widget.Toolbar
10+
import android.view.MenuInflater
11+
import android.view.View
12+
import android.view.ViewGroup
13+
14+
/**
15+
* A [android.preference.PreferenceActivity] which implements and proxies the necessary calls
16+
* to be used with AppCompat.
17+
*/
18+
abstract class AppCompatPreferenceActivity : PreferenceActivity() {
19+
20+
override fun onCreate(savedInstanceState: Bundle?) {
21+
delegate.installViewFactory()
22+
delegate.onCreate(savedInstanceState)
23+
super.onCreate(savedInstanceState)
24+
}
25+
26+
override fun onPostCreate(savedInstanceState: Bundle?) {
27+
super.onPostCreate(savedInstanceState)
28+
delegate.onPostCreate(savedInstanceState)
29+
}
30+
31+
val supportActionBar: ActionBar?
32+
get() = delegate.supportActionBar
33+
34+
fun setSupportActionBar(toolbar: Toolbar?) {
35+
delegate.setSupportActionBar(toolbar)
36+
}
37+
38+
override fun getMenuInflater(): MenuInflater {
39+
return delegate.menuInflater
40+
}
41+
42+
override fun setContentView(@LayoutRes layoutResID: Int) {
43+
delegate.setContentView(layoutResID)
44+
}
45+
46+
override fun setContentView(view: View) {
47+
delegate.setContentView(view)
48+
}
49+
50+
override fun setContentView(view: View, params: ViewGroup.LayoutParams) {
51+
delegate.setContentView(view, params)
52+
}
53+
54+
override fun addContentView(view: View, params: ViewGroup.LayoutParams) {
55+
delegate.addContentView(view, params)
56+
}
57+
58+
override fun onPostResume() {
59+
super.onPostResume()
60+
delegate.onPostResume()
61+
}
62+
63+
override fun onTitleChanged(title: CharSequence, color: Int) {
64+
super.onTitleChanged(title, color)
65+
delegate.setTitle(title)
66+
}
67+
68+
override fun onConfigurationChanged(newConfig: Configuration) {
69+
super.onConfigurationChanged(newConfig)
70+
delegate.onConfigurationChanged(newConfig)
71+
}
72+
73+
override fun onStop() {
74+
super.onStop()
75+
delegate.onStop()
76+
}
77+
78+
override fun onDestroy() {
79+
super.onDestroy()
80+
delegate.onDestroy()
81+
}
82+
83+
override fun invalidateOptionsMenu() {
84+
delegate.invalidateOptionsMenu()
85+
}
86+
87+
private val delegate: AppCompatDelegate by lazy {
88+
AppCompatDelegate.create(this, null)
89+
}
90+
}
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
package com.hackware.mormont.notebook
2+
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
10+
import android.os.Bundle
11+
import android.preference.ListPreference
12+
import android.preference.Preference
13+
import android.preference.PreferenceActivity
14+
import 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 androidx.core.app.NavUtils
20+
21+
/**
22+
* A [PreferenceActivity] that presents a set of application settings. On
23+
* handset devices, settings are presented as a single list. On tablets,
24+
* settings are split by category, with category headers shown to the left of
25+
* the list of settings.
26+
*
27+
* See [Android Design: Settings](http://developer.android.com/design/patterns/settings.html)
28+
* for design guidelines and the [Settings API Guide](http://developer.android.com/guide/topics/ui/settings.html)
29+
* for more information on developing a Settings UI.
30+
*/
31+
class SettingsActivity : AppCompatPreferenceActivity() {
32+
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+
supportActionBar?.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.
235+
236+
* @see .sBindPreferenceSummaryToValueListener
237+
*/
238+
private fun bindPreferenceSummaryToValue(preference: Preference) {
239+
// Set the listener to watch for value changes.
240+
preference.onPreferenceChangeListener = sBindPreferenceSummaryToValueListener
241+
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zm1,15h-2v-6h2v6zm0,-8h-2V7h2v2z"/>
9+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M11.5,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.9,2 2,2zm6.5,-6v-5.5c0,-3.07 -2.13,-5.64 -5,-6.32V3.5c0,-0.83 -0.67,-1.5 -1.5,-1.5S10,2.67 10,3.5v0.68c-2.87,0.68 -5,3.25 -5,6.32V16l-2,2v1h17v-1l-2,-2z"/>
9+
</vector>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24.0"
5+
android:viewportHeight="24.0">
6+
<path
7+
android:fillColor="#FF000000"
8+
android:pathData="M12 4V1L8 5l4 4V6c3.31 0 6 2.69 6 6 0 1.01,-.25 1.97,-.7 2.8l1.46 1.46C19.54 15.03 20 13.57 20 12c0,-4.42,-3.58,-8,-8,-8zm0 14c-3.31 0,-6,-2.69,-6,-6 0,-1.01.25,-1.97.7,-2.8L5.24 7.74C4.46 8.97 4 10.43 4 12c0 4.42 3.58 8 8 8v3l4,-4,-4,-4v3z"/>
9+
</vector>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
2+
3+
<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
4+
dismiss it. -->
5+
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
6+
<ListPreference
7+
android:key="sync_frequency"
8+
android:title="@string/pref_title_sync_frequency"
9+
android:entries="@array/pref_sync_frequency_titles"
10+
android:entryValues="@array/pref_sync_frequency_values"
11+
android:defaultValue="180"
12+
android:negativeButtonText="@null"
13+
android:positiveButtonText="@null"/>
14+
15+
<!-- This preference simply launches an intent when selected. Use this UI sparingly, per
16+
design guidelines. -->
17+
<Preference android:title="@string/pref_title_system_sync_settings">
18+
<intent android:action="android.settings.SYNC_SETTINGS"/>
19+
</Preference>
20+
21+
</PreferenceScreen>

0 commit comments

Comments
 (0)