Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e9cb68b1 authored by Abhishek Aggarwal's avatar Abhishek Aggarwal
Browse files

tmp(widget): Clean

parent 587f2699
Loading
Loading
Loading
Loading
+24 −36
Original line number Diff line number Diff line
@@ -15,7 +15,6 @@ import android.appwidget.AppWidgetProviderInfo
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Insets
import android.graphics.Rect
import android.os.Bundle
import android.os.ServiceManager
@@ -24,7 +23,6 @@ import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.View.OnLayoutChangeListener
import android.view.ViewGroup
import android.view.WindowInsets
import android.widget.Button
@@ -62,41 +60,28 @@ class WidgetContainer(context: Context, attrs: AttributeSet?) : FrameLayout(cont
    private val mLauncher by lazy { Launcher.getLauncher(context) }

    private lateinit var mRemoveWidgetLayout: FrameLayout
    private lateinit var mWrapper: LinearLayout
    private lateinit var mResizeContainer: RelativeLayout

    private var mWrapperChildCount = 0
    private val mResizeContainerRect = Rect()
    private var mInsets: Insets? = null

    private val mInsetPadding =
        context.resources.getDimension(R.dimen.widget_page_inset_padding).toInt()

    private val layoutListener = OnLayoutChangeListener { view, _, _, _, _, _, _, _, _ ->
        val childCount = (view as LinearLayout).childCount
        if (mWrapperChildCount == childCount) return@OnLayoutChangeListener
        handleRemoveButtonVisibility(childCount)
    }

    override fun setPadding(left: Int, top: Int, right: Int, bottom: Int) {
        super.setPadding(0, 0, 0, 0)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        if (mResizeContainer.visibility == VISIBLE && ev.action == MotionEvent.ACTION_DOWN) {
            mResizeContainer.getHitRect(mResizeContainerRect)
        if (
            ev.action == MotionEvent.ACTION_DOWN &&
                !mResizeContainerRect.contains(ev.x.toInt(), ev.y.toInt())
        ) {
            if (!mResizeContainerRect.contains(ev.x.toInt(), ev.y.toInt())) {
                mLauncher.hideWidgetResizeContainer()
            }
        }
        return super.onInterceptTouchEvent(ev)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()

        mInsets = mLauncher.workspace.rootWindowInsets.getInsets(WindowInsets.Type.systemBars())
        val insets = mLauncher.workspace.rootWindowInsets.getInsets(WindowInsets.Type.systemBars())
        val insetPadding = context.resources.getDimension(R.dimen.widget_page_inset_padding).toInt()

        findViewById<Button>(R.id.manage_widgets).setOnClickListener {
            WidgetsFullSheet.show(mLauncher, true, true)
@@ -109,36 +94,39 @@ class WidgetContainer(context: Context, attrs: AttributeSet?) : FrameLayout(cont
        }

        mRemoveWidgetLayout = findViewById(R.id.remove_widget_parent)
        mWrapper =

        findViewWithTag<LinearLayout?>("wrapper_children").apply {
                addOnLayoutChangeListener(layoutListener)
                handleRemoveButtonVisibility(childCount)
            setOnHierarchyChangeListener(
                object : OnHierarchyChangeListener {
                    override fun onChildViewAdded(parent: View?, child: View?) {
                        handleRemoveButtonVisibility((parent as LinearLayout).childCount)
                    }

                    override fun onChildViewRemoved(parent: View?, child: View?) {
                        handleRemoveButtonVisibility((parent as LinearLayout).childCount)
                    }
                }
            )
        }

        mResizeContainer =
            findViewById<RelativeLayout?>(R.id.widget_resizer_container).apply {
                val layoutParams = this.layoutParams as LayoutParams
                layoutParams.bottomMargin = mInsetPadding + (mInsets?.bottom ?: 0)
                layoutParams.bottomMargin = insetPadding + (insets.bottom)
                this.layoutParams = layoutParams
            }

        findViewById<LinearLayout>(R.id.widget_linear_layout).apply {
            setPadding(
                this.paddingLeft,
                mInsetPadding + (mInsets?.top ?: 0),
                insetPadding + (insets.top),
                this.paddingRight,
                (mInsets?.bottom ?: 0),
                (insets.bottom),
            )
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        mWrapper.removeOnLayoutChangeListener(layoutListener)
    }

    private fun handleRemoveButtonVisibility(childCount: Int) {
        mWrapperChildCount = childCount
        CoroutineScope(Dispatchers.Main).launch {
            if (childCount == 0) {
                mRemoveWidgetLayout.visibility = View.GONE