AnimatedAdapter.java revision fac92d71ad83a34eb200cd9bcb773642e5e58de2
1/*
2 * Copyright (C) 2012 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mail.ui;
19
20import android.animation.Animator;
21import android.animation.ObjectAnimator;
22import android.content.Context;
23import android.database.Cursor;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.SimpleCursorAdapter;
27
28import com.android.mail.browse.ConversationCursor;
29import com.android.mail.browse.ConversationItemView;
30import com.android.mail.providers.Account;
31import com.android.mail.providers.Conversation;
32import com.android.mail.providers.UIProvider;
33import com.android.mail.ui.ActionCompleteListener;
34import com.android.mail.ui.AnimatingItemView;
35
36import java.util.ArrayList;
37import java.util.Collection;
38import java.util.HashMap;
39import java.util.HashSet;
40
41public class AnimatedAdapter extends SimpleCursorAdapter implements
42        android.animation.Animator.AnimatorListener {
43    private HashSet<Integer> mDeletingItems = new HashSet<Integer>();
44    private Account mSelectedAccount;
45    private Context mContext;
46    private ConversationSelectionSet mBatchConversations;
47    private ActionCompleteListener mActionCompleteListener;
48    private HashMap<Integer, AnimatingItemView> mAnimatingViews =
49            new HashMap<Integer, AnimatingItemView>();
50
51    public AnimatedAdapter(Context context, int textViewResourceId, ConversationCursor cursor,
52            ConversationSelectionSet batch, Account account) {
53        super(context, textViewResourceId, cursor, UIProvider.CONVERSATION_PROJECTION, null, 0);
54        mContext = context;
55        mBatchConversations = batch;
56        mSelectedAccount = account;
57    }
58
59    @Override
60    public View newView(Context context, Cursor cursor, ViewGroup parent) {
61        ConversationItemView view = new ConversationItemView(context, mSelectedAccount.name);
62        return view;
63    }
64
65    @Override
66    public void bindView(View view, Context context, Cursor cursor) {
67        if (!isPositionAnimating(view)) {
68            ((ConversationItemView) view).bind(cursor, null, mSelectedAccount.name, null,
69                    new ViewMode(mContext), mBatchConversations);
70        }
71    }
72
73    private boolean isPositionAnimating(View view) {
74        return (view instanceof AnimatingItemView);
75    }
76
77    public void delete(Collection<Conversation> conversations,
78            ActionCompleteListener listener) {
79        // Animate out the positions.
80        // Call when all the animations are complete.
81        ArrayList<Integer> positions = new ArrayList<Integer>();
82        for (Conversation c : conversations) {
83            positions.add(c.position);
84        }
85        mActionCompleteListener = listener;
86        delete(positions);
87    }
88
89
90    public void delete(ArrayList<Integer> deletedRows) {
91        mDeletingItems.addAll(deletedRows);
92        notifyDataSetChanged();
93    }
94
95    @Override
96    public View getView(int position, View convertView, ViewGroup parent) {
97        if (isPositionAnimating(position)) {
98            return getAnimatingView(position, convertView, parent);
99        }
100        return super.getView(position, convertView, parent);
101    }
102
103    private View getAnimatingView(int position, View convertView, ViewGroup parent) {
104        AnimatingItemView view = mAnimatingViews.get(position);
105        if (view == null) {
106            Conversation conversation = Conversation.from((ConversationCursor) getItem(position));
107            conversation.position = position;
108            view = new AnimatingItemView(mContext, conversation, this);
109        }
110        return view;
111    }
112
113    private boolean isPositionAnimating(int position) {
114        return mDeletingItems.contains(position);
115    }
116
117    @Override
118    public void onAnimationStart(Animator animation) {
119        // TODO Auto-generated method stub
120    }
121
122    @Override
123    public void onAnimationEnd(Animator animation) {
124        if (!mDeletingItems.isEmpty()) {
125            // See if we have received all the animations we expected; if so,
126            // call the listener and reset it.
127            int position = ((AnimatingItemView)
128                    ((ObjectAnimator) animation).getTarget()).getData().position;
129            mDeletingItems.remove(position);
130            if (mDeletingItems.isEmpty()) {
131                mAnimatingViews.clear();
132                if (mActionCompleteListener != null) {
133                    mActionCompleteListener.onActionComplete();
134                    mActionCompleteListener = null;
135                }
136                notifyDataSetChanged();
137            }
138        }
139    }
140
141    @Override
142    public void onAnimationCancel(Animator animation) {
143        onAnimationEnd(animation);
144    }
145
146    @Override
147    public void onAnimationRepeat(Animator animation) {
148        // TODO Auto-generated method stub
149    }
150}
151