1
0
Fork 0

Part of #288 - Set up basic settings menu

master
Emily Kager 2019-01-30 12:02:11 -08:00 committed by Emily Kager
parent 4d804981f0
commit 8368f4fbb2
11 changed files with 185 additions and 14 deletions

View File

@ -132,6 +132,7 @@ dependencies {
armImplementation Deps.geckoview_nightly_arm
x86Implementation Deps.geckoview_nightly_x86
implementation Deps.androidx_legacy
implementation Deps.androidx_preference
implementation Deps.android_arch_navigation
}

View File

@ -39,6 +39,12 @@
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<activity
android:name=".settings.SettingsActivity"
android:label="@string/settings"
android:parentActivityName=".HomeActivity"
android:theme="@style/AppTheme" />
</application>
</manifest>

View File

@ -42,6 +42,11 @@ class HomeFragment : Fragment() {
BitmapDrawable(resources, it.icon)
}
// Temporary so we can easily test settings
menuButton.setOnClickListener {
Navigation.findNavController(it).navigate(R.id.action_homeFragment_to_settingsActivity, null, null)
}
toolbar.setCompoundDrawablesWithIntrinsicBounds(searchIcon, null, null, null)
val roundToInt = (toolbarPaddingDp * Resources.getSystem().displayMetrics.density).roundToInt()
toolbar.compoundDrawablePadding = roundToInt

View File

@ -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)
}
}

View File

@ -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
}

View File

@ -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"
android:id="@+id/nav_graph"
app:startDestination="@id/homeFragment">
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>

View File

@ -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>

View File

@ -23,4 +23,10 @@
<string name="sessions_intro_description">Fenix will collect your browsing sessions on your Home Screen for you to retrieve later.</string>
<string name="search_scan_button">Scan</string>
<string name="search_shortcuts_button">Shortcuts</string>
<!-- Preference for settings related to changing the default browser -->
<string name="preferences_make_default_browser">Make default browser</string>
<!-- Menu option on the toolbar that takes you to settings page-->
<string name="settings">Settings</string>
</resources>

View File

@ -8,6 +8,12 @@
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>
<style name="search_pill" parent="Widget.AppCompat.Button.Borderless">

View File

@ -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>

View File

@ -13,6 +13,7 @@ private object Versions {
const val androidx_appcompat = "1.0.2"
const val androidx_constraint_layout = "2.0.0-alpha3"
const val androidx_preference = "1.0.0"
const val mozilla_android_components = "0.41.0-SNAPSHOT"
@ -86,6 +87,7 @@ object Deps {
const val tools_espresso_core = "com.android.support.test.espresso:espresso-core:${Versions.espresso_core}"
const val androidx_legacy = "androidx.legacy:legacy-support-v4:${Versions.androidx_legacy}"
const val androidx_preference = "androidx.preference:preference-ktx:${Versions.androidx_preference}"
const val android_arch_navigation = "android.arch.navigation:navigation-fragment:${Versions.android_arch_navigation}"
}