Part of #288 - Set up basic settings menu
parent
4d804981f0
commit
8368f4fbb2
@ -0,0 +1,36 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.settings
|
||||
|
||||
import android.R.id.content
|
||||
import android.os.Bundle
|
||||
import android.view.MenuItem
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
|
||||
class SettingsActivity : AppCompatActivity(), SettingsFragment.ActionBarUpdater {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
if (savedInstanceState == null) {
|
||||
with(supportFragmentManager.beginTransaction()) {
|
||||
replace(content, SettingsFragment())
|
||||
commit()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
|
||||
android.R.id.home -> {
|
||||
onBackPressed()
|
||||
true
|
||||
}
|
||||
else -> super.onOptionsItemSelected(item)
|
||||
}
|
||||
|
||||
override fun updateTitle(titleResId: Int) {
|
||||
setTitle(titleResId)
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.settings
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.provider.Settings
|
||||
import android.widget.Toast
|
||||
import androidx.preference.Preference.OnPreferenceClickListener
|
||||
import androidx.preference.PreferenceFragmentCompat
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.R.string.pref_key_make_default_browser
|
||||
import org.mozilla.fenix.ext.getPreferenceKey
|
||||
|
||||
class SettingsFragment : PreferenceFragmentCompat() {
|
||||
|
||||
interface ActionBarUpdater {
|
||||
fun updateTitle(titleResId: Int)
|
||||
}
|
||||
|
||||
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||
setPreferencesFromResource(R.xml.preferences, rootKey)
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
setupPreferences()
|
||||
getActionBarUpdater().apply {
|
||||
updateTitle(R.string.settings)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("LongMethod") // Yep, this should be refactored.
|
||||
private fun setupPreferences() {
|
||||
val makeDefaultBrowserKey = context?.getPreferenceKey(pref_key_make_default_browser)
|
||||
|
||||
val preferenceMakeDefaultBrowser = findPreference(makeDefaultBrowserKey)
|
||||
|
||||
preferenceMakeDefaultBrowser.onPreferenceClickListener = getClickListenerForMakeDefaultBrowser()
|
||||
}
|
||||
|
||||
private val defaultClickListener = OnPreferenceClickListener { preference ->
|
||||
Toast.makeText(context, "${preference.title} Clicked", Toast.LENGTH_SHORT).show()
|
||||
true
|
||||
}
|
||||
|
||||
private fun getClickListenerForMakeDefaultBrowser(): OnPreferenceClickListener {
|
||||
return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
|
||||
OnPreferenceClickListener {
|
||||
val intent = Intent(
|
||||
Settings.ACTION_MANAGE_DEFAULT_APPS_SETTINGS
|
||||
)
|
||||
startActivity(intent)
|
||||
true
|
||||
}
|
||||
} else {
|
||||
defaultClickListener
|
||||
}
|
||||
}
|
||||
|
||||
private fun getActionBarUpdater() = activity as ActionBarUpdater
|
||||
}
|
@ -1,29 +1,54 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:id="@+id/nav_graph"
|
||||
app:startDestination="@id/homeFragment">
|
||||
|
||||
<fragment android:id="@+id/homeFragment" android:name="org.mozilla.fenix.home.HomeFragment"
|
||||
android:label="fragment_home" tools:layout="@layout/fragment_home">
|
||||
<action android:id="@+id/action_homeFragment_to_searchFragment" app:destination="@id/searchFragment"
|
||||
/>
|
||||
<action android:id="@+id/action_homeFragment_to_browserFragment" app:destination="@id/browserFragment"/>
|
||||
<fragment
|
||||
android:id="@+id/homeFragment"
|
||||
android:name="org.mozilla.fenix.home.HomeFragment"
|
||||
android:label="fragment_home"
|
||||
tools:layout="@layout/fragment_home">
|
||||
<action
|
||||
android:id="@+id/action_homeFragment_to_searchFragment"
|
||||
app:destination="@id/searchFragment" />
|
||||
<action
|
||||
android:id="@+id/action_homeFragment_to_browserFragment"
|
||||
app:destination="@id/browserFragment" />
|
||||
<action
|
||||
android:id="@+id/action_homeFragment_to_settingsActivity"
|
||||
app:destination="@id/settingsActivity" />
|
||||
</fragment>
|
||||
|
||||
<fragment android:id="@+id/searchFragment" android:name="org.mozilla.fenix.search.SearchFragment"
|
||||
android:label="fragment_search" tools:layout="@layout/fragment_search">
|
||||
<action android:id="@+id/action_searchFragment_to_browserFragment" app:destination="@id/browserFragment"
|
||||
app:popUpTo="@id/homeFragment"/>
|
||||
<fragment
|
||||
android:id="@+id/searchFragment"
|
||||
android:name="org.mozilla.fenix.search.SearchFragment"
|
||||
android:label="fragment_search"
|
||||
tools:layout="@layout/fragment_search">
|
||||
<action
|
||||
android:id="@+id/action_searchFragment_to_browserFragment"
|
||||
app:destination="@id/browserFragment"
|
||||
app:popUpTo="@id/homeFragment" />
|
||||
</fragment>
|
||||
|
||||
<fragment android:id="@+id/browserFragment" android:name="org.mozilla.fenix.browser.BrowserFragment"
|
||||
android:label="fragment_browser" tools:layout="@layout/fragment_browser">
|
||||
<fragment
|
||||
android:id="@+id/browserFragment"
|
||||
android:name="org.mozilla.fenix.browser.BrowserFragment"
|
||||
android:label="fragment_browser"
|
||||
tools:layout="@layout/fragment_browser">
|
||||
<action
|
||||
android:id="@+id/action_browserFragment_to_homeFragment"
|
||||
app:destination="@id/homeFragment" />
|
||||
<action
|
||||
android:id="@+id/action_browserFragment_to_searchFragment"
|
||||
app:destination="@id/searchFragment" />
|
||||
<action
|
||||
android:id="@+id/action_browserFragment_to_settingsActivity"
|
||||
app:destination="@id/settingsActivity" />
|
||||
</fragment>
|
||||
<activity
|
||||
android:id="@+id/settingsActivity"
|
||||
android:name="org.mozilla.fenix.settings.SettingsActivity"
|
||||
android:label="SettingsActivity" />
|
||||
</navigation>
|
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
<resources>
|
||||
<string name="pref_key_make_default_browser" translatable="false">pref_key_make_default_browser</string>
|
||||
</resources>
|
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- This Source Code Form is subject to the terms of the Mozilla Public
|
||||
- License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
|
||||
|
||||
<androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<androidx.preference.Preference
|
||||
android:key="@string/pref_key_make_default_browser"
|
||||
android:title="@string/preferences_make_default_browser" />
|
||||
|
||||
</androidx.preference.PreferenceScreen>
|
Loading…
Reference in New Issue