1 /* 2 * Copyright (C) 2016 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 androidx.core.content.ContextCompat; 21 import android.view.ContextMenu; 22 import android.view.LayoutInflater; 23 import android.view.Menu; 24 import android.view.View; 25 import android.view.ViewGroup; 26 import android.widget.ImageView; 27 import android.widget.TextView; 28 29 import com.android.deskclock.AnimatorUtils; 30 import com.android.deskclock.ItemAdapter; 31 import com.android.deskclock.R; 32 import com.android.deskclock.ThemeUtils; 33 import com.android.deskclock.Utils; 34 35 import static android.view.View.GONE; 36 import static android.view.View.OnClickListener; 37 import static android.view.View.OnCreateContextMenuListener; 38 import static android.view.View.VISIBLE; 39 40 final class RingtoneViewHolder extends ItemAdapter.ItemViewHolder<RingtoneHolder> 41 implements OnClickListener, OnCreateContextMenuListener { 42 43 static final int VIEW_TYPE_SYSTEM_SOUND = R.layout.ringtone_item_sound; 44 static final int VIEW_TYPE_CUSTOM_SOUND = -R.layout.ringtone_item_sound; 45 static final int CLICK_NORMAL = 0; 46 static final int CLICK_LONG_PRESS = -1; 47 static final int CLICK_NO_PERMISSIONS = -2; 48 49 private final View mSelectedView; 50 private final TextView mNameView; 51 private final ImageView mImageView; 52 RingtoneViewHolder(View itemView)53 private RingtoneViewHolder(View itemView) { 54 super(itemView); 55 itemView.setOnClickListener(this); 56 57 mSelectedView = itemView.findViewById(R.id.sound_image_selected); 58 mNameView = (TextView) itemView.findViewById(R.id.ringtone_name); 59 mImageView = (ImageView) itemView.findViewById(R.id.ringtone_image); 60 } 61 62 @Override onBindItemView(RingtoneHolder itemHolder)63 protected void onBindItemView(RingtoneHolder itemHolder) { 64 mNameView.setText(itemHolder.getName()); 65 final boolean opaque = itemHolder.isSelected() || !itemHolder.hasPermissions(); 66 mNameView.setAlpha(opaque ? 1f : .63f); 67 mImageView.setAlpha(opaque ? 1f : .63f); 68 mImageView.clearColorFilter(); 69 70 final int itemViewType = getItemViewType(); 71 if (itemViewType == VIEW_TYPE_CUSTOM_SOUND) { 72 if (!itemHolder.hasPermissions()) { 73 mImageView.setImageResource(R.drawable.ic_ringtone_not_found); 74 final int colorAccent = ThemeUtils.resolveColor(itemView.getContext(), 75 R.attr.colorAccent); 76 mImageView.setColorFilter(colorAccent, PorterDuff.Mode.SRC_ATOP); 77 } else { 78 mImageView.setImageResource(R.drawable.placeholder_album_artwork); 79 } 80 } else if (itemHolder.item == Utils.RINGTONE_SILENT) { 81 mImageView.setImageResource(R.drawable.ic_ringtone_silent); 82 } else if (itemHolder.isPlaying()) { 83 mImageView.setImageResource(R.drawable.ic_ringtone_active); 84 } else { 85 mImageView.setImageResource(R.drawable.ic_ringtone); 86 } 87 AnimatorUtils.startDrawableAnimation(mImageView); 88 89 mSelectedView.setVisibility(itemHolder.isSelected() ? VISIBLE : GONE); 90 91 final int bgColorId = itemHolder.isSelected() ? R.color.white_08p : R.color.transparent; 92 itemView.setBackgroundColor(ContextCompat.getColor(itemView.getContext(), bgColorId)); 93 94 if (itemViewType == VIEW_TYPE_CUSTOM_SOUND) { 95 itemView.setOnCreateContextMenuListener(this); 96 } 97 } 98 99 @Override onClick(View view)100 public void onClick(View view) { 101 if (getItemHolder().hasPermissions()) { 102 notifyItemClicked(RingtoneViewHolder.CLICK_NORMAL); 103 } else { 104 notifyItemClicked(RingtoneViewHolder.CLICK_NO_PERMISSIONS); 105 } 106 } 107 108 @Override onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo)109 public void onCreateContextMenu(ContextMenu contextMenu, View view, 110 ContextMenu.ContextMenuInfo contextMenuInfo) { 111 notifyItemClicked(RingtoneViewHolder.CLICK_LONG_PRESS); 112 contextMenu.add(Menu.NONE, 0, Menu.NONE, R.string.remove_sound); 113 } 114 115 public static class Factory implements ItemAdapter.ItemViewHolder.Factory { 116 117 private final LayoutInflater mInflater; 118 Factory(LayoutInflater inflater)119 Factory(LayoutInflater inflater) { 120 mInflater = inflater; 121 } 122 123 @Override createViewHolder(ViewGroup parent, int viewType)124 public ItemAdapter.ItemViewHolder<?> createViewHolder(ViewGroup parent, int viewType) { 125 final View itemView = mInflater.inflate(R.layout.ringtone_item_sound, parent, false); 126 return new RingtoneViewHolder(itemView); 127 } 128 } 129 } 130