1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.deskclock.ringtone 18 19 import android.graphics.PorterDuff 20 import android.view.ContextMenu 21 import android.view.LayoutInflater 22 import android.view.Menu 23 import android.view.View 24 import android.view.ViewGroup 25 import android.view.ContextMenu.ContextMenuInfo 26 import android.view.View.OnCreateContextMenuListener 27 import android.widget.ImageView 28 import android.widget.TextView 29 import androidx.core.content.ContextCompat 30 31 import com.android.deskclock.AnimatorUtils 32 import com.android.deskclock.ItemAdapter.ItemViewHolder 33 import com.android.deskclock.R 34 import com.android.deskclock.ThemeUtils 35 import com.android.deskclock.Utils 36 37 internal class RingtoneViewHolder private constructor(itemView: View) 38 : ItemViewHolder<RingtoneHolder>(itemView), View.OnClickListener, OnCreateContextMenuListener { 39 private val mSelectedView: View = itemView.findViewById(R.id.sound_image_selected) 40 private val mNameView: TextView = itemView.findViewById<View>(R.id.ringtone_name) as TextView 41 private val mImageView: ImageView = 42 itemView.findViewById<View>(R.id.ringtone_image) as ImageView 43 44 init { 45 itemView.setOnClickListener(this) 46 } 47 onBindItemViewnull48 override fun onBindItemView(itemHolder: RingtoneHolder) { 49 mNameView.text = itemHolder.name 50 val opaque = itemHolder.isSelected || !itemHolder.hasPermissions() 51 mNameView.alpha = if (opaque) 1f else .63f 52 mImageView.alpha = if (opaque) 1f else .63f 53 mImageView.clearColorFilter() 54 55 val itemViewType: Int = getItemViewType() 56 if (itemViewType == VIEW_TYPE_CUSTOM_SOUND) { 57 if (!itemHolder.hasPermissions()) { 58 mImageView.setImageResource(R.drawable.ic_ringtone_not_found) 59 val colorAccent = ThemeUtils.resolveColor(itemView.getContext(), 60 R.attr.colorAccent) 61 mImageView.setColorFilter(colorAccent, PorterDuff.Mode.SRC_ATOP) 62 } else { 63 mImageView.setImageResource(R.drawable.placeholder_album_artwork) 64 } 65 } else if (itemHolder.item == Utils.RINGTONE_SILENT) { 66 mImageView.setImageResource(R.drawable.ic_ringtone_silent) 67 } else if (itemHolder.isPlaying) { 68 mImageView.setImageResource(R.drawable.ic_ringtone_active) 69 } else { 70 mImageView.setImageResource(R.drawable.ic_ringtone) 71 } 72 AnimatorUtils.startDrawableAnimation(mImageView) 73 74 mSelectedView.visibility = if (itemHolder.isSelected) View.VISIBLE else View.GONE 75 76 val bgColorId = if (itemHolder.isSelected) R.color.white_08p else R.color.transparent 77 itemView.setBackgroundColor(ContextCompat.getColor(itemView.getContext(), bgColorId)) 78 79 if (itemViewType == VIEW_TYPE_CUSTOM_SOUND) { 80 itemView.setOnCreateContextMenuListener(this) 81 } 82 } 83 onClicknull84 override fun onClick(view: View) { 85 if (itemHolder!!.hasPermissions()) { 86 notifyItemClicked(CLICK_NORMAL) 87 } else { 88 notifyItemClicked(CLICK_NO_PERMISSIONS) 89 } 90 } 91 onCreateContextMenunull92 override fun onCreateContextMenu( 93 contextMenu: ContextMenu, 94 view: View, 95 contextMenuInfo: ContextMenuInfo 96 ) { 97 notifyItemClicked(CLICK_LONG_PRESS) 98 contextMenu.add(Menu.NONE, 0, Menu.NONE, R.string.remove_sound) 99 } 100 101 class Factory internal constructor(private val mInflater: LayoutInflater) 102 : ItemViewHolder.Factory { createViewHoldernull103 override fun createViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder<*> { 104 val itemView = mInflater.inflate(R.layout.ringtone_item_sound, parent, false) 105 return RingtoneViewHolder(itemView) 106 } 107 } 108 109 companion object { 110 const val VIEW_TYPE_SYSTEM_SOUND = R.layout.ringtone_item_sound 111 const val VIEW_TYPE_CUSTOM_SOUND = -R.layout.ringtone_item_sound 112 const val CLICK_NORMAL = 0 113 const val CLICK_LONG_PRESS = -1 114 const val CLICK_NO_PERMISSIONS = -2 115 } 116 }