1 /** 2 * Copyright (c) 2012, Google Inc. 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 package com.android.mail.ui; 17 18 import android.content.Context; 19 import android.graphics.Color; 20 import android.graphics.drawable.ShapeDrawable; 21 import android.graphics.drawable.shapes.RoundRectShape; 22 import android.graphics.drawable.shapes.Shape; 23 import android.util.AttributeSet; 24 import android.view.View; 25 import android.widget.ImageView; 26 import android.widget.LinearLayout; 27 import android.widget.TextView; 28 29 import com.android.mail.R; 30 import com.android.mail.providers.Folder; 31 import com.android.mail.utils.FolderUri; 32 import com.android.mail.utils.LogTag; 33 import com.android.mail.utils.LogUtils; 34 import com.android.mail.utils.Utils; 35 36 /** 37 * The view for each folder in the folder list. 38 */ 39 public class FolderItemView extends LinearLayout { 40 private final String LOG_TAG = LogTag.getLogTag(); 41 42 private static float[] sUnseenCornerRadii; 43 44 private Folder mFolder; 45 private TextView mFolderTextView; 46 private TextView mUnreadCountTextView; 47 private TextView mUnseenCountTextView; 48 FolderItemView(Context context)49 public FolderItemView(Context context) { 50 super(context); 51 52 loadResources(context); 53 } 54 FolderItemView(Context context, AttributeSet attrs)55 public FolderItemView(Context context, AttributeSet attrs) { 56 super(context, attrs); 57 58 loadResources(context); 59 } 60 FolderItemView(Context context, AttributeSet attrs, int defStyle)61 public FolderItemView(Context context, AttributeSet attrs, int defStyle) { 62 super(context, attrs, defStyle); 63 64 loadResources(context); 65 } 66 loadResources(Context context)67 private void loadResources(Context context) { 68 if (sUnseenCornerRadii == null) { 69 final float cornerRadius = 70 context.getResources().getDimension(R.dimen.folder_rounded_corner_radius); 71 sUnseenCornerRadii = new float[] { 72 cornerRadius, cornerRadius, // top left 73 cornerRadius, cornerRadius, // top right 74 cornerRadius, cornerRadius, // bottom right 75 cornerRadius, cornerRadius // bottom left 76 }; 77 } 78 } 79 80 @Override onFinishInflate()81 protected void onFinishInflate() { 82 super.onFinishInflate(); 83 84 mFolderTextView = (TextView)findViewById(R.id.name); 85 mUnreadCountTextView = (TextView)findViewById(R.id.unread); 86 mUnseenCountTextView = (TextView)findViewById(R.id.unseen); 87 } 88 89 /** 90 * Returns true if the two folders lead to identical {@link FolderItemView} objects. 91 * @param a 92 * @param b 93 * @return true if the two folders would still lead to the same {@link FolderItemView}. 94 */ areSameViews(final Folder a, final Folder b)95 public static boolean areSameViews(final Folder a, final Folder b) { 96 if (a == null) { 97 return b == null; 98 } 99 if (b == null) { 100 // a is not null because it would have returned above. 101 return false; 102 } 103 return (a == b || (a.folderUri.equals(b.folderUri) 104 && a.name.equals(b.name) 105 && a.hasChildren == b.hasChildren 106 && a.unseenCount == b.unseenCount 107 && a.unreadCount == b.unreadCount)); 108 } 109 bind(final Folder folder, final FolderUri parentUri)110 public void bind(final Folder folder, final FolderUri parentUri) { 111 mFolder = folder; 112 113 mFolderTextView.setText(folder.name); 114 115 if (parentUri != null) { 116 final boolean isParent = folder.folderUri.equals(parentUri); 117 118 // If child folder, make spacer view visible, otherwise hide it away 119 findViewById(R.id.nested_folder_space).setVisibility( 120 isParent ? View.GONE : View.VISIBLE); 121 } 122 123 if (mFolder.isInbox() && mFolder.unseenCount > 0) { 124 mUnreadCountTextView.setVisibility(View.GONE); 125 setUnseenCount(mFolder.getBackgroundColor(Color.BLACK), mFolder.unseenCount); 126 } else { 127 mUnseenCountTextView.setVisibility(View.GONE); 128 setUnreadCount(Utils.getFolderUnreadDisplayCount(mFolder)); 129 } 130 } 131 132 /** 133 * Sets the icon, if any. 134 */ setIcon(final Folder folder)135 public void setIcon(final Folder folder) { 136 final ImageView folderIconView = (ImageView) findViewById(R.id.folder_icon); 137 Folder.setIcon(folder, folderIconView); 138 } 139 140 /** 141 * Sets the unread count, taking care to hide/show the textview if the count is zero/non-zero. 142 */ setUnreadCount(int count)143 private void setUnreadCount(int count) { 144 mUnreadCountTextView.setVisibility(count > 0 ? View.VISIBLE : View.GONE); 145 if (count > 0) { 146 mUnreadCountTextView.setText(Utils.getUnreadCountString(getContext(), count)); 147 } 148 } 149 150 /** 151 * Sets the unseen count, taking care to hide/show the textview if the count is zero/non-zero. 152 */ setUnseenCount(final int color, final int count)153 private void setUnseenCount(final int color, final int count) { 154 mUnseenCountTextView.setVisibility(count > 0 ? View.VISIBLE : View.GONE); 155 if (count > 0) { 156 final Shape shape = new RoundRectShape(sUnseenCornerRadii, null, null); 157 final ShapeDrawable drawable = new ShapeDrawable(shape); 158 drawable.getPaint().setColor(color); 159 mUnseenCountTextView.setBackgroundDrawable(drawable); 160 mUnseenCountTextView.setText(Utils.getUnseenCountString(getContext(), count)); 161 } 162 } 163 164 /** 165 * Used if we detect a problem with the unread count and want to force an override. 166 * @param count 167 */ overrideUnreadCount(int count)168 public final void overrideUnreadCount(int count) { 169 LogUtils.e(LOG_TAG, "FLF->FolderItem.getFolderView: unread count mismatch found (%s vs %d)", 170 mUnreadCountTextView.getText(), count); 171 setUnreadCount(count); 172 } 173 174 } 175