Commit 0e2442f3 authored by Simon Chae's avatar Simon Chae
Browse files

Closes #4790: Add UI for addon settings

parent cb972c84
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -63,6 +63,11 @@
                android:label="@string/add_ons"
                android:theme="@style/Theme.AppCompat.Light" />

        <activity
            android:name=".addons.AddOnSettingsActivity"
            android:label="@string/add_ons"
            android:theme="@style/Theme.AppCompat.Light" />

        <activity android:name=".IntentReceiverActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
+98 −0
Original line number Diff line number Diff line
/* 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.samples.browser.addons

import android.content.Context
import android.os.Bundle
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_add_on_settings.*
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.EngineView
import mozilla.components.feature.addons.AddOn
import org.mozilla.samples.browser.R
import org.mozilla.samples.browser.ext.components

/**
 * An activity to show the settings of an add-on.
 */
class AddOnSettingsActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_on_settings)

        val addOn = requireNotNull(intent.getParcelableExtra<AddOn>("add_on"))
        title = addOn.translatableName.translate()

        supportFragmentManager
            .beginTransaction()
            .replace(R.id.addOnSettingsContainer, AddOnSettingsFragment.create(addOn))
            .commit()
    }

    override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View? =
        when (name) {
            EngineView::class.java.name -> components.engine.createView(context, attrs).asView()
            else -> super.onCreateView(parent, name, context, attrs)
        }

    /**
     * A fragment to show the settings of an add-on with [EngineView].
     */
    class AddOnSettingsFragment : Fragment() {
        private lateinit var addOn: AddOn
        private lateinit var engineObserver: EngineSession.Observer
        private lateinit var engineSession: EngineSession

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            addOn = requireNotNull(arguments?.getParcelable("add_on"))
            engineSession = components.engine.createSession()
            engineObserver = object : EngineSession.Observer {
                override fun onLoadRequest(
                    url: String,
                    triggeredByRedirect: Boolean,
                    triggeredByWebContent: Boolean,
                    shouldLoadUri: (Boolean, String) -> Unit
                ) {
                    shouldLoadUri(true, "")
                }
            }
            engineSession.register(engineObserver)

            return inflater.inflate(R.layout.fragment_add_on_settings, container, false)
        }

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
            super.onViewCreated(view, savedInstanceState)

            addOnSettingsEngineView.render(engineSession)

            // update the url after add_on and web_extension link
            engineSession.loadUrl(addOn.siteUrl)
        }

        override fun onDestroyView() {
            engineSession.unregister(engineObserver)
            engineSession.close()
            super.onDestroyView()
        }

        companion object {
            /**
             * Create an [AddOnSettingsFragment] with add_on as a required parameter.
             */
            fun create(addOn: AddOn) = AddOnSettingsFragment().apply {
                arguments = Bundle().apply {
                    putParcelable("add_on", addOn)
                }
            }
        }
    }
}
+7 −3
Original line number Diff line number Diff line
@@ -30,7 +30,9 @@ class InstalledAddOnDetailsActivity : AppCompatActivity() {

        bindEnableSwitch(addOn.enabled)

        bindSettings()
        bindSettings(addOn)

        bindSettings(addOn)

        bindDetails(addOn)

@@ -50,9 +52,11 @@ class InstalledAddOnDetailsActivity : AppCompatActivity() {
        }
    }

    private fun bindSettings() {
    private fun bindSettings(addOn: AddOn) {
        findViewById<View>(R.id.settings).setOnClickListener {
            Toast.makeText(this, "Settings clicked", Toast.LENGTH_SHORT).show()
            val intent = Intent(this, AddOnSettingsActivity::class.java)
            intent.putExtra("add_on", addOn)
            this.startActivity(intent)
        }
    }

+11 −0
Original line number Diff line number Diff line
<?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/. -->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/addOnSettingsContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MergeRootFrame" />
+16 −0
Original line number Diff line number Diff line
<?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.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <mozilla.components.concept.engine.EngineView
        android:id="@+id/addOnSettingsEngineView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>