parent
477493e197
commit
346b4aa3fd
@ -0,0 +1,160 @@
|
||||
/* 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.tabtray
|
||||
|
||||
import android.view.View
|
||||
import android.widget.ImageButton
|
||||
import android.widget.ImageView
|
||||
import android.widget.TextView
|
||||
import androidx.appcompat.widget.AppCompatImageButton
|
||||
import androidx.core.content.ContextCompat
|
||||
import mozilla.components.browser.state.state.MediaState
|
||||
import mozilla.components.browser.tabstray.TabViewHolder
|
||||
import mozilla.components.browser.tabstray.thumbnail.TabThumbnailView
|
||||
import mozilla.components.concept.tabstray.Tab
|
||||
import mozilla.components.concept.tabstray.TabsTray
|
||||
import mozilla.components.feature.media.ext.pauseIfPlaying
|
||||
import mozilla.components.feature.media.ext.playIfPaused
|
||||
import mozilla.components.support.base.observer.Observable
|
||||
import mozilla.components.support.ktx.kotlin.tryGetHostFromUrl
|
||||
import org.mozilla.fenix.R
|
||||
import org.mozilla.fenix.components.metrics.Event
|
||||
import org.mozilla.fenix.ext.components
|
||||
import org.mozilla.fenix.ext.increaseTapArea
|
||||
import org.mozilla.fenix.ext.removeAndDisable
|
||||
import org.mozilla.fenix.ext.removeTouchDelegate
|
||||
import org.mozilla.fenix.ext.showAndEnable
|
||||
import org.mozilla.fenix.ext.toTab
|
||||
import org.mozilla.fenix.theme.ThemeManager
|
||||
|
||||
/**
|
||||
* A RecyclerView ViewHolder implementation for "tab" items.
|
||||
*/
|
||||
class TabTrayViewHolder(itemView: View) : TabViewHolder(itemView) {
|
||||
private val iconView: ImageView? = itemView.findViewById(R.id.mozac_browser_tabstray_icon)
|
||||
private val titleView: TextView = itemView.findViewById(R.id.mozac_browser_tabstray_title)
|
||||
private val closeView: AppCompatImageButton = itemView.findViewById(R.id.mozac_browser_tabstray_close)
|
||||
private val thumbnailView: TabThumbnailView = itemView.findViewById(R.id.mozac_browser_tabstray_thumbnail)
|
||||
private val urlView: TextView? = itemView.findViewById(R.id.mozac_browser_tabstray_url)
|
||||
private val playPauseButtonView: ImageButton = itemView.findViewById(R.id.play_pause_button)
|
||||
|
||||
override var tab: Tab? = null
|
||||
|
||||
/**
|
||||
* Displays the data of the given session and notifies the given observable about events.
|
||||
*/
|
||||
override fun bind(tab: Tab, isSelected: Boolean, observable: Observable<TabsTray.Observer>) {
|
||||
this.tab = tab
|
||||
|
||||
// Basic text
|
||||
updateTitle(tab)
|
||||
updateUrl(tab)
|
||||
updateCloseButtonDescription(tab.title)
|
||||
|
||||
// Drawables and theme
|
||||
updateBackgroundColor(isSelected)
|
||||
thumbnailView.setImageBitmap(tab.thumbnail)
|
||||
iconView?.setImageBitmap(tab.icon)
|
||||
|
||||
// Media state
|
||||
playPauseButtonView.increaseTapArea(PLAY_PAUSE_BUTTON_EXTRA_DPS)
|
||||
val session = itemView.context?.components?.core?.sessionManager?.findSessionById(tab.id)
|
||||
with(playPauseButtonView) {
|
||||
invalidate()
|
||||
when (session?.toTab(itemView.context)?.mediaState) {
|
||||
MediaState.State.PAUSED -> {
|
||||
showAndEnable()
|
||||
contentDescription =
|
||||
context.getString(R.string.mozac_feature_media_notification_action_play)
|
||||
setImageDrawable(
|
||||
androidx.appcompat.content.res.AppCompatResources.getDrawable(
|
||||
context,
|
||||
R.drawable.tab_tray_play_with_background
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
MediaState.State.PLAYING -> {
|
||||
showAndEnable()
|
||||
contentDescription =
|
||||
context.getString(R.string.mozac_feature_media_notification_action_pause)
|
||||
setImageDrawable(
|
||||
androidx.appcompat.content.res.AppCompatResources.getDrawable(
|
||||
context,
|
||||
R.drawable.tab_tray_pause_with_background
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
MediaState.State.NONE -> {
|
||||
removeTouchDelegate()
|
||||
removeAndDisable()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
playPauseButtonView.setOnClickListener {
|
||||
val mState = session?.toTab(itemView.context)?.mediaState
|
||||
when (mState) {
|
||||
MediaState.State.PLAYING -> {
|
||||
itemView.context.components.analytics.metrics.track(Event.TabMediaPause)
|
||||
itemView.context.components.core.store.state.media.pauseIfPlaying()
|
||||
}
|
||||
|
||||
MediaState.State.PAUSED -> {
|
||||
itemView.context.components.analytics.metrics.track(Event.TabMediaPlay)
|
||||
itemView.context.components.core.store.state.media.playIfPaused()
|
||||
}
|
||||
|
||||
MediaState.State.NONE -> throw AssertionError(
|
||||
"Play/Pause button clicked without play/pause state."
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
itemView.setOnClickListener {
|
||||
observable.notifyObservers { onTabSelected(tab) }
|
||||
}
|
||||
|
||||
closeView.setOnClickListener {
|
||||
observable.notifyObservers { onTabClosed(tab) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateTitle(tab: Tab) {
|
||||
val title = if (tab.title.isNotEmpty()) {
|
||||
tab.title
|
||||
} else {
|
||||
tab.url
|
||||
}
|
||||
titleView.text = title
|
||||
}
|
||||
private fun updateUrl(tab: Tab) {
|
||||
urlView?.text = tab.url.tryGetHostFromUrl()
|
||||
}
|
||||
|
||||
private fun updateBackgroundColor(isSelected: Boolean) {
|
||||
val itemBackground = if (isSelected) {
|
||||
R.attr.tabTrayItemSelectedBackground
|
||||
} else {
|
||||
R.attr.tabTrayItemBackground
|
||||
}
|
||||
itemView.setBackgroundColor(
|
||||
ContextCompat.getColor(
|
||||
itemView.context,
|
||||
ThemeManager.resolveAttribute(itemBackground, itemView.context)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
private fun updateCloseButtonDescription(title: String) {
|
||||
closeView.contentDescription =
|
||||
closeView.context.getString(R.string.close_tab_title, title)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val PLAY_PAUSE_BUTTON_EXTRA_DPS = 24
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?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/. -->
|
||||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item>
|
||||
<shape android:shape="oval">
|
||||
<size
|
||||
android:width="24dp"
|
||||
android:height="24dp" />
|
||||
<!-- Vertical pause lines -->
|
||||
<solid android:color="@color/tab_tray_item_media_stroke" />
|
||||
<!-- Outer circle border -->
|
||||
<stroke android:color="@color/tab_tray_item_media_stroke"
|
||||
android:width="2dp"/>
|
||||
</shape>
|
||||
</item>
|
||||
<item>
|
||||
<vector
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<!-- Background color of the circle -->
|
||||
<path
|
||||
android:fillColor="?tabTrayItemMediaBackground"
|
||||
android:pathData="M12,2a10,10 0,1 0,10 10A10,10 0,0 0,12 2zM10.5,16.125a0.375,0.375 0,0 1,-0.375 0.375h-2.25A0.375,0.375 0,0 1,7.5 16.125v-8.25A0.375,0.375 0,0 1,7.875 7.5h2.25A0.375,0.375 0,0 1,10.5 7.875zM16.5,16.125a0.375,0.375 0,0 1,-0.375 0.375h-2.25a0.375,0.375 0,0 1,-0.375 -0.375v-8.25A0.375,0.375 0,0 1,13.875 7.5h2.25A0.375,0.375 0,0 1,16.5 7.875z"/>
|
||||
</vector>
|
||||
</item>
|
||||
</layer-list>
|
@ -0,0 +1,30 @@
|
||||
<?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/. -->
|
||||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item>
|
||||
<shape android:shape="oval">
|
||||
<size
|
||||
android:width="24dp"
|
||||
android:height="24dp" />
|
||||
<!-- Vertical pause lines -->
|
||||
<solid android:color="@color/tab_tray_item_media_stroke" />
|
||||
<!-- Outer circle border -->
|
||||
<stroke android:color="@color/tab_tray_item_media_stroke"
|
||||
android:width="2dp"/>
|
||||
</shape>
|
||||
</item>
|
||||
<item>
|
||||
<vector
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24">
|
||||
<!-- Background color of the circle -->
|
||||
<path
|
||||
android:fillColor="?tabTrayItemMediaBackground"
|
||||
android:pathData="M12,22a10,10 0,1 1,10 -10,10 10,0 0,1 -10,10zM10,16.363l6,-3.5a1,1 0,0 0,0 -1.732l-6,-3.5A1,1 0,0 0,8.5 8.5v7a1,1 0,0 0,1.5 0.863z"/>
|
||||
</vector>
|
||||
</item>
|
||||
</layer-list>
|
Loading…
Reference in New Issue