From 9a7d6f01264a6167c246afcad540785ea59431ca Mon Sep 17 00:00:00 2001 From: Andrey Epin Date: Thu, 15 Dec 2022 14:51:54 -0800 Subject: Split ActionRow view into internface and implementation ActionRow widget is split into an interface, ActionRow, an implementation, ChooserActionRow. The view is continue to be referenced by the interface everywhere in the codae. Bug: 262278109 Test: manual testing for basic functionality Test: atest IntentResolverUnitTests Change-Id: Idadaaf80ea62f655719b40b10b9d23188c0e590d --- .../com/android/intentresolver/widget/ActionRow.kt | 64 +---------------- .../intentresolver/widget/ChooserActionRow.kt | 81 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 62 deletions(-) create mode 100644 java/src/com/android/intentresolver/widget/ChooserActionRow.kt (limited to 'java/src') diff --git a/java/src/com/android/intentresolver/widget/ActionRow.kt b/java/src/com/android/intentresolver/widget/ActionRow.kt index 1be48f34..6764d3ae 100644 --- a/java/src/com/android/intentresolver/widget/ActionRow.kt +++ b/java/src/com/android/intentresolver/widget/ActionRow.kt @@ -16,71 +16,11 @@ package com.android.intentresolver.widget -import android.annotation.LayoutRes -import android.content.Context import android.content.res.Resources.ID_NULL import android.graphics.drawable.Drawable -import android.os.Parcelable -import android.util.AttributeSet -import android.view.LayoutInflater -import android.widget.Button -import android.widget.LinearLayout -import com.android.intentresolver.R -// TODO: extract an interface out of the class, use it in layout hierarchy an have a layout inflater -// to instantiate the right view based on a flag value. -class ActionRow : LinearLayout { - constructor(context: Context) : this(context, null) - constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) - constructor( - context: Context, attrs: AttributeSet?, defStyleAttr: Int - ) : this(context, attrs, defStyleAttr, 0) - - constructor( - context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int - ) : super(context, attrs, defStyleAttr, defStyleRes) { - orientation = HORIZONTAL - } - - @LayoutRes - private val itemLayout = R.layout.chooser_action_button - private val itemMargin = - context.resources.getDimensionPixelSize(R.dimen.resolver_icon_margin) / 2 - private var actions: List = emptyList() - - override fun onRestoreInstanceState(state: Parcelable?) { - super.onRestoreInstanceState(state) - setActions(actions) - } - - fun setActions(actions: List) { - removeAllViews() - this.actions = ArrayList(actions) - for (action in actions) { - addAction(action) - } - } - - private fun addAction(action: Action) { - val b = LayoutInflater.from(context).inflate(itemLayout, null) as Button - if (action.icon != null) { - val size = resources - .getDimensionPixelSize(R.dimen.chooser_action_button_icon_size) - action.icon.setBounds(0, 0, size, size) - b.setCompoundDrawablesRelative(action.icon, null, null, null) - } - b.text = action.label ?: "" - b.setOnClickListener { - action.onClicked.run() - } - b.id = action.id - addView(b) - } - - override fun generateDefaultLayoutParams(): LayoutParams = - super.generateDefaultLayoutParams().apply { - setMarginsRelative(itemMargin, 0, itemMargin, 0) - } +interface ActionRow { + fun setActions(actions: List) class Action @JvmOverloads constructor( // TODO: apparently, IDs set to this field are used in unit tests only; evaluate whether we diff --git a/java/src/com/android/intentresolver/widget/ChooserActionRow.kt b/java/src/com/android/intentresolver/widget/ChooserActionRow.kt new file mode 100644 index 00000000..a4656bb5 --- /dev/null +++ b/java/src/com/android/intentresolver/widget/ChooserActionRow.kt @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2022 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.intentresolver.widget + +import android.annotation.LayoutRes +import android.content.Context +import android.os.Parcelable +import android.util.AttributeSet +import android.view.LayoutInflater +import android.widget.Button +import android.widget.LinearLayout +import com.android.intentresolver.R +import com.android.intentresolver.widget.ActionRow.Action + +class ChooserActionRow : LinearLayout, ActionRow { + constructor(context: Context) : this(context, null) + constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) + constructor( + context: Context, attrs: AttributeSet?, defStyleAttr: Int + ) : this(context, attrs, defStyleAttr, 0) + + constructor( + context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int + ) : super(context, attrs, defStyleAttr, defStyleRes) { + orientation = HORIZONTAL + } + + @LayoutRes + private val itemLayout = R.layout.chooser_action_button + private val itemMargin = + context.resources.getDimensionPixelSize(R.dimen.resolver_icon_margin) / 2 + private var actions: List = emptyList() + + override fun onRestoreInstanceState(state: Parcelable?) { + super.onRestoreInstanceState(state) + setActions(actions) + } + + override fun setActions(actions: List) { + removeAllViews() + this.actions = ArrayList(actions) + for (action in actions) { + addAction(action) + } + } + + private fun addAction(action: Action) { + val b = LayoutInflater.from(context).inflate(itemLayout, null) as Button + if (action.icon != null) { + val size = resources + .getDimensionPixelSize(R.dimen.chooser_action_button_icon_size) + action.icon.setBounds(0, 0, size, size) + b.setCompoundDrawablesRelative(action.icon, null, null, null) + } + b.text = action.label ?: "" + b.setOnClickListener { + action.onClicked.run() + } + b.id = action.id + addView(b) + } + + override fun generateDefaultLayoutParams(): LayoutParams = + super.generateDefaultLayoutParams().apply { + setMarginsRelative(itemMargin, 0, itemMargin, 0) + } +} -- cgit v1.2.3-59-g8ed1b