PhoneFavoritesTileAdapter.java revision 72418af9f84b7a97ff8cb08b99aa68b2db9ee1a9
1dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee/*
2dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * Copyright (C) 2013 The Android Open Source Project
3dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee *
4dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * Licensed under the Apache License, Version 2.0 (the "License");
5dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * you may not use this file except in compliance with the License.
6dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * You may obtain a copy of the License at
7dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee *
8dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee *      http://www.apache.org/licenses/LICENSE-2.0
9dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee *
10dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * Unless required by applicable law or agreed to in writing, software
11dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * distributed under the License is distributed on an "AS IS" BASIS,
12dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * See the License for the specific language governing permissions and
14dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * limitations under the License.
15dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee */
16dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leepackage com.android.dialer.list;
17dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
18316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chenimport android.animation.ObjectAnimator;
19dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.content.ContentUris;
2055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leeimport android.content.ContentValues;
21dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.content.Context;
22dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.content.res.Resources;
23dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.database.Cursor;
24316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chenimport android.graphics.Rect;
25dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.net.Uri;
26dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.provider.ContactsContract.CommonDataKinds.Phone;
27dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.provider.ContactsContract.Contacts;
2855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leeimport android.provider.ContactsContract.PinnedPositions;
294e741192ec40278d9189b46cf2ae6bc547d9071dYorke Leeimport android.text.TextUtils;
30dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.util.Log;
314e741192ec40278d9189b46cf2ae6bc547d9071dYorke Leeimport android.util.LongSparseArray;
3211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Leeimport android.view.MotionEvent;
33dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.view.View;
3411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Leeimport android.view.ViewConfiguration;
35dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.view.ViewGroup;
36dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.widget.BaseAdapter;
37dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport android.widget.FrameLayout;
38dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
39dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport com.android.contacts.common.ContactPhotoManager;
40dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport com.android.contacts.common.ContactTileLoaderFactory;
41dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport com.android.contacts.common.R;
42dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport com.android.contacts.common.list.ContactEntry;
4355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leeimport com.android.contacts.common.list.ContactTileAdapter.DisplayType;
44dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport com.android.contacts.common.list.ContactTileView;
4511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Leeimport com.android.dialer.list.SwipeHelper.OnItemGestureListener;
4611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Leeimport com.android.dialer.list.SwipeHelper.SwipeHelperCallback;
4755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leeimport com.android.internal.annotations.VisibleForTesting;
4855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
4955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leeimport com.google.common.collect.ComparisonChain;
50dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
51dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Leeimport java.util.ArrayList;
5255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leeimport java.util.Comparator;
53c36684277aa45085999284bfe71cb8be71b3a464Yorke Leeimport java.util.HashMap;
5455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leeimport java.util.LinkedList;
5555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leeimport java.util.List;
5655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Leeimport java.util.PriorityQueue;
57dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
58dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee/**
59dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * Also allows for a configurable number of columns as well as a maximum row of tiled contacts.
60dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee *
61dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee * This adapter has been rewritten to only support a maximum of one row for favorites.
62dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee *
63dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee */
6411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Leepublic class PhoneFavoritesTileAdapter extends BaseAdapter implements
6511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        SwipeHelper.OnItemGestureListener {
66316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    private static final String TAG = PhoneFavoritesTileAdapter.class.getSimpleName();
67316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    private static final boolean DEBUG = false;
68dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
696a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee    public static final int ROW_LIMIT_DEFAULT = 1;
70dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
71dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private ContactTileView.Listener mListener;
72c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    private OnDataSetChangedForAnimationListener mDataSetChangedListener;
73c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee
74dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private Context mContext;
75dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private Resources mResources;
76316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
7718e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee    private View mEmptyView;
7818e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee
79316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /** Contact data stored in cache. This is used to populate the associated view. */
80316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    protected ArrayList<ContactEntry> mContactEntries = null;
81316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /** Back up of the temporarily removed Contact during dragging. */
82316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    private ContactEntry mDraggedEntry = null;
83316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /** Position of the temporarily removed contact in the cache. */
84316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    private int mDraggedEntryIndex = -1;
85316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /** New position of the temporarily removed contact in the cache. */
86316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    private int mDropEntryIndex = -1;
8772418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    /** New position of the temporarily entered contact in the cache. */
8872418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    private int mDragEnteredEntryIndex = -1;
89316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /** Position of the contact pending removal. */
90316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    private int mPotentialRemoveEntryIndex = -1;
91c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    private long mIdToKeepInPlace = -1;
92316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
935a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee    private boolean mAwaitingRemove = false;
945a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee
95dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private ContactPhotoManager mPhotoManager;
96dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected int mNumFrequents;
976a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee    protected int mNumStarred;
98dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
99dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected int mColumnCount;
1006a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee    private int mMaxTiledRows = ROW_LIMIT_DEFAULT;
101dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private int mStarredIndex;
102dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
103dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected int mIdIndex;
104dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected int mLookupIndex;
105dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected int mPhotoUriIndex;
106dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected int mNameIndex;
107dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected int mPresenceIndex;
108dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected int mStatusIndex;
109dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
110dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    /**
111dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Only valid when {@link DisplayType#STREQUENT_PHONE_ONLY} is true
1124e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee     *
1134e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee     * TODO krelease: Remove entirely if not needed
114dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     */
115dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private int mPhoneNumberIndex;
116dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private int mPhoneNumberTypeIndex;
117dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private int mPhoneNumberLabelIndex;
11855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    protected int mPinnedIndex;
11955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    protected int mContactIdForFrequentIndex;
120dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
121dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private final int mPaddingInPixels;
122dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
123316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /** Indicates whether a drag is in process. */
124316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    private boolean mInDragging = false;
125316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
12672418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    public static final int PIN_LIMIT = 20;
12755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
128d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang    /**
129d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang     * The soft limit on how many contact tiles to show.
130d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang     * NOTE This soft limit would not restrict the number of starred contacts to show, rather
131d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang     * 1. If the count of starred contacts is less than this limit, show 20 tiles total.
132d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang     * 2. If the count of starred contacts is more than or equal to this limit,
133d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang     * show all starred tiles and no frequents.
134d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang     */
135d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang    private static final int TILES_SOFT_LIMIT = 20;
136d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang
13755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    final Comparator<ContactEntry> mContactEntryComparator = new Comparator<ContactEntry>() {
13855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        @Override
13955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        public int compare(ContactEntry lhs, ContactEntry rhs) {
14055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            return ComparisonChain.start()
14155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                    .compare(lhs.pinned, rhs.pinned)
14255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                    .compare(lhs.name, rhs.name)
14355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                    .result();
14455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        }
14555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    };
14655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
147c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    public interface OnDataSetChangedForAnimationListener {
148c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee        public void onDataSetChangedForAnimation(long... idsInPlace);
149c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee        public void cacheOffsetsForDatasetChange();
150c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    };
151dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
152316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    public PhoneFavoritesTileAdapter(Context context, ContactTileView.Listener listener,
153c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            OnDataSetChangedForAnimationListener dataSetChangedListener,
154316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            int numCols, int maxTiledRows) {
155c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee        mDataSetChangedListener = dataSetChangedListener;
156dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mListener = listener;
157dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mContext = context;
158dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mResources = context.getResources();
159dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mColumnCount = numCols;
160dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mNumFrequents = 0;
161dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mMaxTiledRows = maxTiledRows;
162316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        mContactEntries = new ArrayList<ContactEntry>();
163dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        // Converting padding in dips to padding in pixels
164dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mPaddingInPixels = mContext.getResources()
165d6a16d364de95c8eb0d9c4f5a07d5ad6387c8470Yorke Lee                .getDimensionPixelSize(R.dimen.contact_tile_divider_padding);
166316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
167dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        bindColumnIndices();
168dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
169dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
170dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public void setPhotoLoader(ContactPhotoManager photoLoader) {
171dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mPhotoManager = photoLoader;
172dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
173dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
174dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public void setMaxRowCount(int maxRows) {
175dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mMaxTiledRows = maxRows;
176dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
177dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
178dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public void setColumnCount(int columnCount) {
179dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mColumnCount = columnCount;
180dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
181dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
182dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    /**
183316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * Indicates whether a drag is in process.
184316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     *
185316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * @param inDragging Boolean variable indicating whether there is a drag in process.
186316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     */
187316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    public void setInDragging(boolean inDragging) {
188316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        mInDragging = inDragging;
189316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
190316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
191316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /** Gets whether the drag is in process. */
192316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    public boolean getInDragging() {
193316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        return mInDragging;
194316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
195316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
196316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /**
197dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Sets the column indices for expected {@link Cursor}
198dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * based on {@link DisplayType}.
199dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     */
200dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected void bindColumnIndices() {
201dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mIdIndex = ContactTileLoaderFactory.CONTACT_ID;
202dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mLookupIndex = ContactTileLoaderFactory.LOOKUP_KEY;
203dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mPhotoUriIndex = ContactTileLoaderFactory.PHOTO_URI;
204dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mNameIndex = ContactTileLoaderFactory.DISPLAY_NAME;
205dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mStarredIndex = ContactTileLoaderFactory.STARRED;
206dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mPresenceIndex = ContactTileLoaderFactory.CONTACT_PRESENCE;
207dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mStatusIndex = ContactTileLoaderFactory.CONTACT_STATUS;
208dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
209dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mPhoneNumberIndex = ContactTileLoaderFactory.PHONE_NUMBER;
210dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mPhoneNumberTypeIndex = ContactTileLoaderFactory.PHONE_NUMBER_TYPE;
211dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        mPhoneNumberLabelIndex = ContactTileLoaderFactory.PHONE_NUMBER_LABEL;
21255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        mPinnedIndex = ContactTileLoaderFactory.PINNED;
21355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        mContactIdForFrequentIndex = ContactTileLoaderFactory.CONTACT_ID_FOR_FREQUENT;
214dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
215dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
216dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    /**
217dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Gets the number of frequents from the passed in cursor.
218dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     *
219dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * This methods is needed so the GroupMemberTileAdapter can override this.
220dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     *
221dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * @param cursor The cursor to get number of frequents from.
222dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     */
223dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected void saveNumFrequentsFromCursor(Cursor cursor) {
2246a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee        mNumFrequents = cursor.getCount() - mNumStarred;
225dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
226dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
227dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    /**
228dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Creates {@link ContactTileView}s for each item in {@link Cursor}.
229dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     *
230dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Else use {@link ContactTileLoaderFactory}
231dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     */
232dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public void setContactCursor(Cursor cursor) {
233316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        if (cursor != null && !cursor.isClosed()) {
234316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            mNumStarred = getNumStarredContacts(cursor);
235c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            if (mAwaitingRemove) {
236c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee                mDataSetChangedListener.cacheOffsetsForDatasetChange();
237c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            }
238c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee
239316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            saveNumFrequentsFromCursor(cursor);
240316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            saveCursorToCache(cursor);
241316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            // cause a refresh of any views that rely on this data
242316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            notifyDataSetChanged();
243c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            // about to start redraw
244c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            if (mIdToKeepInPlace != -1) {
245c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee                mDataSetChangedListener.onDataSetChangedForAnimation(mIdToKeepInPlace);
246c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            } else {
247c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee                mDataSetChangedListener.onDataSetChangedForAnimation();
248c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            }
249c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            mIdToKeepInPlace = -1;
250316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        }
251316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
252dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
253316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /**
254316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * Saves the cursor data to the cache, to speed up UI changes.
255316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     *
256316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * @param cursor Returned cursor with data to populate the view.
257316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     */
258316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    private void saveCursorToCache(Cursor cursor) {
259316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        mContactEntries.clear();
26055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
261443d758d53d1f7ebb12e8ed39fc6195c9d5ed18dYorke Lee        cursor.moveToPosition(-1);
26255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
2634e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee        final LongSparseArray<Object> duplicates = new LongSparseArray<Object>(cursor.getCount());
264443d758d53d1f7ebb12e8ed39fc6195c9d5ed18dYorke Lee
2654e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee        // Dummy object that we're inserting into the sparse array as a value so that we can use
2664e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee        // the sparse array as a set to check for duplicates
26755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
2684e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee        final Object dummy = new Object();
26955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
270d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang        // Track the length of {@link #mContactEntries} and compare to {@link #TILES_SOFT_LIMIT}.
271d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang        int counter = 0;
272d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang
2734e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee        while (cursor.moveToNext()) {
2744e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee
2754e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            final int starred = cursor.getInt(mStarredIndex);
2764e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            final long id;
27755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
27855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            if (starred > 0) {
2794e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                id = cursor.getLong(mIdIndex);
280d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang            } else if (counter >= TILES_SOFT_LIMIT) {
281d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang                break;
28255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            } else {
28355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                // The contact id for frequent contacts is stored in the .contact_id field rather
28455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                // than the _id field
2854e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                id = cursor.getLong(mContactIdForFrequentIndex);
28655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            }
2874e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee
2884e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            if (duplicates.get(id) == null) {
2894e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                duplicates.put(id, dummy);
2904e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            } else {
2914e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                continue;
2924e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            }
2934e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee
2944e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            final String photoUri = cursor.getString(mPhotoUriIndex);
2954e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            final String lookupKey = cursor.getString(mLookupIndex);
2964e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            final int pinned = cursor.getInt(mPinnedIndex);
2974e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            final String name = cursor.getString(mNameIndex);
2984e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee
2994e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            final ContactEntry contact = new ContactEntry();
3004e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee
3014e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            contact.id = id;
3024e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            contact.name = (!TextUtils.isEmpty(name)) ? name :
3034e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                    mResources.getString(R.string.missing_name);
304443d758d53d1f7ebb12e8ed39fc6195c9d5ed18dYorke Lee            contact.photoUri = (photoUri != null ? Uri.parse(photoUri) : null);
305443d758d53d1f7ebb12e8ed39fc6195c9d5ed18dYorke Lee            contact.lookupKey = ContentUris.withAppendedId(
306443d758d53d1f7ebb12e8ed39fc6195c9d5ed18dYorke Lee                    Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey), id);
307443d758d53d1f7ebb12e8ed39fc6195c9d5ed18dYorke Lee
3084e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee
3094e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            // TODO krelease: These columns are temporarily unused for now so that
3104e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            // the contact tiles will be treated like favorites since they don't have a phone
3114e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            // number. Depending on how the final UX goes we will either remove or enable
3124e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            // them again.
3134e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee
3144e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            /*
3154e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                // Set phone number, label and status
3164e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                final int phoneNumberType = cursor.getInt(mPhoneNumberTypeIndex);
3174e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                final String phoneNumberCustomLabel = cursor.getString(mPhoneNumberLabelIndex);
3184e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                contact.phoneLabel = (String) Phone.getTypeLabel(mResources, phoneNumberType,
3194e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                        phoneNumberCustomLabel);
3204e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                contact.phoneNumber = cursor.getString(mPhoneNumberIndex);
3214e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee                contact.status = cursor.getString(mStatusIndex);
3224e741192ec40278d9189b46cf2ae6bc547d9071dYorke Lee            */
32355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
32455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            contact.pinned = pinned;
325443d758d53d1f7ebb12e8ed39fc6195c9d5ed18dYorke Lee            mContactEntries.add(contact);
326d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang
327d954cd76c610e8fafd91aec857f4232ab49ef2cdHongwei Wang            counter++;
328316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        }
32955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
3305a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee        mAwaitingRemove = false;
3315a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee
33255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        arrangeContactsByPinnedPosition(mContactEntries);
33355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
33455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        notifyDataSetChanged();
335dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
336dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
337dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    /**
338dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Iterates over the {@link Cursor}
339dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Returns position of the first NON Starred Contact
340dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Returns -1 if {@link DisplayType#STARRED_ONLY}
341dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Returns 0 if {@link DisplayType#FREQUENT_ONLY}
342dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     */
3436a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee    protected int getNumStarredContacts(Cursor cursor) {
344dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        cursor.moveToPosition(-1);
345dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        while (cursor.moveToNext()) {
346dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            if (cursor.getInt(mStarredIndex) == 0) {
347dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                return cursor.getPosition();
348dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
349dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
350dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
351dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        // There are not NON Starred contacts in cursor
352dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        // Set divider positon to end
353dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        return cursor.getCount();
354dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
355dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
356316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /**
357316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * Loads a contact from the cached list.
358316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     *
359316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * @param position Position of the Contact.
360316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * @return Contact at the requested position.
361316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     */
362316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    protected ContactEntry getContactEntryFromCache(int position) {
363316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        if (mContactEntries.size() <= position) return null;
364316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        return mContactEntries.get(position);
365dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
366dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
367dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    /**
368dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Returns the number of frequents that will be displayed in the list.
369dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     */
370dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public int getNumFrequents() {
371dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        return mNumFrequents;
372dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
373dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
37418e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee    private boolean isEmptyView(int position) {
37518e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee        return position == 0 && (mContactEntries == null || mContactEntries.isEmpty());
37618e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee    }
37718e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee
378dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
379dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public int getCount() {
380c1e769c16792a880ac803b912851d6d258ed4550Yorke Lee        if (mContactEntries == null || mContactEntries.isEmpty()) {
38118e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee            // empty view
38218e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee            return 1;
383dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
384dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
38563b92b5f2cc4d4099aed4c6d10cd24e1389e7a44Yorke Lee        int total = mContactEntries.size();
386c1e769c16792a880ac803b912851d6d258ed4550Yorke Lee        // The number of contacts that don't show up as tiles
387c1e769c16792a880ac803b912851d6d258ed4550Yorke Lee        final int nonTiledRows = Math.max(0, total - getMaxContactsInTiles());
388c1e769c16792a880ac803b912851d6d258ed4550Yorke Lee        // The number of tiled rows
389c1e769c16792a880ac803b912851d6d258ed4550Yorke Lee        final int tiledRows = getRowCount(total - nonTiledRows);
390c1e769c16792a880ac803b912851d6d258ed4550Yorke Lee        return nonTiledRows + tiledRows;
391dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
392dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
3935fe874fbdfe0010b5f2439cf6c13eaee46d406eaYorke Lee    public int getMaxTiledRows() {
3945fe874fbdfe0010b5f2439cf6c13eaee46d406eaYorke Lee        return mMaxTiledRows;
3955fe874fbdfe0010b5f2439cf6c13eaee46d406eaYorke Lee    }
3965fe874fbdfe0010b5f2439cf6c13eaee46d406eaYorke Lee
397dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    /**
398dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Returns the number of rows required to show the provided number of entries
399dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * with the current number of columns.
400dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     */
401dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected int getRowCount(int entryCount) {
402dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        if (entryCount == 0) return 0;
403dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        final int nonLimitedRows = ((entryCount - 1) / mColumnCount) + 1;
4046a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee        return Math.min(mMaxTiledRows, nonLimitedRows);
4056a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee    }
4066a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee
4076a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee    private int getMaxContactsInTiles() {
4086a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee        return mColumnCount * mMaxTiledRows;
409dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
410dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
411316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    protected int getRowIndex(int entryIndex) {
412316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        if (entryIndex < mMaxTiledRows * mColumnCount) {
413316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            return entryIndex / mColumnCount;
414316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        } else {
415316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            return entryIndex - mMaxTiledRows * mColumnCount + mMaxTiledRows;
416316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        }
417316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
418316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
419dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public int getColumnCount() {
420dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        return mColumnCount;
421dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
422dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
423dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    /**
424dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Returns an ArrayList of the {@link ContactEntry}s that are to appear
425dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * on the row for the given position.
426dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     */
427dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
428dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public ArrayList<ContactEntry> getItem(int position) {
429dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        ArrayList<ContactEntry> resultList = new ArrayList<ContactEntry>(mColumnCount);
43011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
43111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        final int entryIndex = getFirstContactEntryIndexForPosition(position);
43211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
43311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        final int viewType = getItemViewType(position);
43411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
43511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        final int columnCount;
43611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        if (viewType == ViewTypes.TOP) {
43711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            columnCount = mColumnCount;
43811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        } else {
43911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            columnCount = 1;
44011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
44111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
44211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        for (int i = 0; i < columnCount; i++) {
44311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            final ContactEntry entry = getContactEntryFromCache(entryIndex + i);
44411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            if (entry == null) break; // less than mColumnCount contacts
44511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            resultList.add(entry);
44611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
44711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
44811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        return resultList;
44911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    }
45011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
45111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    /*
45211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee     * Given a position in the adapter, returns the index of the first contact entry that is to be
45311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee     * in that row.
45411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee     */
45511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    private int getFirstContactEntryIndexForPosition(int position) {
4566a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee        final int maxContactsInTiles = getMaxContactsInTiles();
4576a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee        if (position < getRowCount(maxContactsInTiles)) {
4586a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee            // Contacts that appear as tiles
45911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            return position * mColumnCount;
460dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        } else {
4616a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee            // Contacts that appear as rows
4626a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee            // The actual position of the contact in the cursor is simply total the number of
4636a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee            // tiled contacts + the given position
46411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            return maxContactsInTiles + position - 1;
465dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
466dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
467dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
468c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    /**
469c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee     * For the top row of tiled contacts, the item id is the position of the row of
470c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee     * contacts.
471c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee     * For frequent contacts, the item id is the maximum number of rows of tiled contacts +
472c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee     * the actual contact id. Since contact ids are always greater than 0, this guarantees that
473c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee     * all items within this adapter will always have unique ids.
474c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee     */
475dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
476dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public long getItemId(int position) {
477c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee        if (getItemViewType(position) == ViewTypes.FREQUENT) {
478c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            return getAdjustedItemId(getItem(position).get(0).id);
479c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee        } else {
480c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            return position;
481c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee        }
482c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    }
483c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee
484c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    /**
485c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee     * Calculates the stable itemId for a particular entry based on its contactID
486c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee     */
487c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    public long getAdjustedItemId(long id) {
488c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee        return mMaxTiledRows + id;
489c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    }
490c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee
491c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    @Override
492c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee    public boolean hasStableIds() {
493c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee        return true;
494dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
495dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
496dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
497c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee
498dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public boolean areAllItemsEnabled() {
499dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        // No dividers, so all items are enabled.
500dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        return true;
501dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
502dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
503dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
504dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public boolean isEnabled(int position) {
50518e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee        return !isEmptyView(position);
506dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
507dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
508dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
509316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    public void notifyDataSetChanged() {
510316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        if (DEBUG) {
51155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            Log.v(TAG, "notifyDataSetChanged");
512316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        }
513316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        super.notifyDataSetChanged();
514316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
515316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
516316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    @Override
517dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public View getView(int position, View convertView, ViewGroup parent) {
51855d3bb4a7f70d4a4359c31b3240f5740b788e643Yorke Lee        if (DEBUG) {
51955d3bb4a7f70d4a4359c31b3240f5740b788e643Yorke Lee            Log.v(TAG, "get view for " + String.valueOf(position));
52055d3bb4a7f70d4a4359c31b3240f5740b788e643Yorke Lee        }
52118e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee
522dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        int itemViewType = getItemViewType(position);
523dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
52418e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee        if (itemViewType == ViewTypes.EMPTY) {
52518e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee            return mEmptyView;
52618e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee        }
52718e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee
5286091473941d277ed3746143c1ca9bffdfbe2bd94Christine Chen        ContactTileRow contactTileRowView  = (ContactTileRow) convertView;
5296091473941d277ed3746143c1ca9bffdfbe2bd94Christine Chen
530dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        ArrayList<ContactEntry> contactList = getItem(position);
531dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
532dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        if (contactTileRowView == null) {
533dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            // Creating new row if needed
534316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            contactTileRowView = new ContactTileRow(mContext, itemViewType, position);
535dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
536dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
537316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        contactTileRowView.configureRow(contactList, position, position == getCount() - 1);
538316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
539dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        return contactTileRowView;
540dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
541dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
542dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    private int getLayoutResourceId(int viewType) {
543dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        switch (viewType) {
544dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            case ViewTypes.FREQUENT:
545dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                return R.layout.phone_favorite_regular_row_view;
546d99993213962df64c9d76005df86c2fb83fca074Yorke Lee            case ViewTypes.TOP:
547dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                return R.layout.phone_favorite_tile_view;
548dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            default:
549dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                throw new IllegalArgumentException("Unrecognized viewType " + viewType);
550dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
551dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
552dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
553dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public int getViewTypeCount() {
554dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        return ViewTypes.COUNT;
555dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
556dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
557dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    @Override
558dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    public int getItemViewType(int position) {
55918e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee        if (isEmptyView(position)) return ViewTypes.EMPTY;
5606a8402939f667ab70bd0043ed0859f8afbc012ecYorke Lee        if (position < getRowCount(getMaxContactsInTiles())) {
561d99993213962df64c9d76005df86c2fb83fca074Yorke Lee            return ViewTypes.TOP;
562dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        } else {
563dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            return ViewTypes.FREQUENT;
564dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
565dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
566dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
567dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    /**
568316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * Temporarily removes a contact from the list for UI refresh. Stores data for this contact
569316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * in the back-up variable.
570316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     *
571316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * @param index Position of the contact to be removed.
572316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     */
573316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    public void popContactEntry(int index) {
57472418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        if (isIndexInBound(index)) {
575316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            mDraggedEntry = mContactEntries.get(index);
576316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            mDraggedEntryIndex = index;
57772418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            mDragEnteredEntryIndex = index;
57872418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            markDropArea(mDragEnteredEntryIndex);
57972418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        }
58072418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    }
58172418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang
58272418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    /**
58372418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang     * @param itemIndex Position of the contact in {@link #mContactEntries}.
58472418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang     * @return True if the given index is valid for {@link #mContactEntries}.
58572418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang     */
58672418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    private boolean isIndexInBound(int itemIndex) {
58772418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        return itemIndex >= 0 && itemIndex < mContactEntries.size();
58872418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    }
58972418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang
59072418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    /**
59172418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang     * Mark the tile as drop area by given the item index in {@link #mContactEntries}.
59272418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang     *
59372418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang     * @param itemIndex Position of the contact in {@link #mContactEntries}.
59472418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang     */
59572418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    private void markDropArea(int itemIndex) {
59672418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        if (isIndexInBound(mDragEnteredEntryIndex) && isIndexInBound(itemIndex)) {
59772418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            // Remove the old placeholder item and place the new placeholder item.
59872418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            mContactEntries.remove(mDragEnteredEntryIndex);
59972418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            mDragEnteredEntryIndex = itemIndex;
60072418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            mContactEntries.add(mDragEnteredEntryIndex, ContactEntry.BLANK_ENTRY);
60172418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            ContactEntry.BLANK_ENTRY.id = mDraggedEntry.id;
602316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            notifyDataSetChanged();
603316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        }
604316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
605316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
606316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /**
607316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * Drops the temporarily removed contact to the desired location in the list.
608316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     */
60972418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    public void handleDrop() {
61055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        boolean changed = false;
611316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        if (mDraggedEntry != null) {
61272418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            if (isIndexInBound(mDragEnteredEntryIndex)) {
61355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                // Don't add the ContactEntry here (to prevent a double animation from occuring).
61455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                // When we receive a new cursor the list of contact entries will automatically be
61555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                // populated with the dragged ContactEntry at the correct spot.
61672418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                mDropEntryIndex = mDragEnteredEntryIndex;
61772418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                mContactEntries.set(mDropEntryIndex, mDraggedEntry);
618c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee                mIdToKeepInPlace = getAdjustedItemId(mDraggedEntry.id);
619c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee                mDataSetChangedListener.cacheOffsetsForDatasetChange();
62055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                changed = true;
62172418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            } else if (isIndexInBound(mDraggedEntryIndex)) {
62272418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                // If {@link #mDragEnteredEntryIndex} is invalid,
62372418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                // falls back to the original position of the contact.
62472418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                mContactEntries.remove(mDragEnteredEntryIndex);
62572418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                mContactEntries.add(mDraggedEntryIndex, mDraggedEntry);
626316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                mDropEntryIndex = mDraggedEntryIndex;
62755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                notifyDataSetChanged();
62855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            }
62955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
63055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            if (changed && mDropEntryIndex < PIN_LIMIT) {
63155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                final ContentValues cv = getReflowedPinnedPositions(mContactEntries, mDraggedEntry,
63255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                        mDraggedEntryIndex, mDropEntryIndex);
63355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                final Uri pinUri = PinnedPositions.UPDATE_URI.buildUpon().appendQueryParameter(
63472418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                        PinnedPositions.STAR_WHEN_PINNING, "true").build();
63555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                // update the database here with the new pinned positions
63655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                mContext.getContentResolver().update(pinUri, cv, null, null);
637316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            }
638316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            mDraggedEntry = null;
639316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        }
640316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
641316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
642316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /**
64301f15ad65dfbc72d7c8f165bda8e602ba287b434Christine Chen     * Invoked when the dragged item is dropped to unsupported location. We will then move the
64401f15ad65dfbc72d7c8f165bda8e602ba287b434Christine Chen     * contact back to where it was dragged from.
64501f15ad65dfbc72d7c8f165bda8e602ba287b434Christine Chen     */
64601f15ad65dfbc72d7c8f165bda8e602ba287b434Christine Chen    public void dropToUnsupportedView() {
64772418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        if (isIndexInBound(mDragEnteredEntryIndex)) {
64872418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            mContactEntries.remove(mDragEnteredEntryIndex);
64972418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            mContactEntries.add(mDraggedEntryIndex, mDraggedEntry);
65072418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            notifyDataSetChanged();
65172418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        }
65201f15ad65dfbc72d7c8f165bda8e602ba287b434Christine Chen    }
65301f15ad65dfbc72d7c8f165bda8e602ba287b434Christine Chen
65401f15ad65dfbc72d7c8f165bda8e602ba287b434Christine Chen    /**
655316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * Sets an item to for pending removal. If the user does not click the undo button, the item
656316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * will be removed at the next interaction.
657316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     *
658316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * @param index Index of the item to be removed.
659316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     */
660316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    public void setPotentialRemoveEntryIndex(int index) {
661316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        mPotentialRemoveEntryIndex = index;
662316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
663316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
664316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /**
66555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Removes a contact entry from the list.
666316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     *
667316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * @return True is an item is removed. False is there is no item to be removed.
668316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     */
66911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    public boolean removePendingContactEntry() {
67011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        boolean removed = false;
67172418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        if (isIndexInBound(mPotentialRemoveEntryIndex)) {
672c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            final ContactEntry entry = mContactEntries.get(mPotentialRemoveEntryIndex);
67355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            unstarAndUnpinContact(entry.lookupKey);
67411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            removed = true;
6755a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee            mAwaitingRemove = true;
676316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        }
67711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        cleanTempVariables();
67811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        return removed;
679316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
680316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
681316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /**
682316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * Resets the item for pending removal.
683316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     */
684316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    public void undoPotentialRemoveEntryIndex() {
685316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        mPotentialRemoveEntryIndex = -1;
686316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
687316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
688316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /**
689316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     * Clears all temporary variables at a new interaction.
690316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen     */
691316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    public void cleanTempVariables() {
692316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        mDraggedEntryIndex = -1;
693316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        mDropEntryIndex = -1;
69472418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        mDragEnteredEntryIndex = -1;
695316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        mDraggedEntry = null;
696316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        mPotentialRemoveEntryIndex = -1;
697316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    }
698316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
699316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen    /**
700dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     * Acts as a row item composed of {@link ContactTileView}
701dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     *
702dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee     */
70311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    public class ContactTileRow extends FrameLayout implements SwipeHelperCallback {
70411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public static final int CONTACT_ENTRY_INDEX_TAG = R.id.contact_entry_index_tag;
70511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
706dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        private int mItemViewType;
707dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        private int mLayoutResId;
708d99993213962df64c9d76005df86c2fb83fca074Yorke Lee        private final int mRowPaddingStart;
709d99993213962df64c9d76005df86c2fb83fca074Yorke Lee        private final int mRowPaddingEnd;
710d99993213962df64c9d76005df86c2fb83fca074Yorke Lee        private final int mRowPaddingTop;
711d99993213962df64c9d76005df86c2fb83fca074Yorke Lee        private final int mRowPaddingBottom;
712316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        private int mPosition;
71311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        private SwipeHelper mSwipeHelper;
71411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        private OnItemGestureListener mOnItemSwipeListener;
715dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
716316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        public ContactTileRow(Context context, int itemViewType, int position) {
717dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            super(context);
718dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            mItemViewType = itemViewType;
719dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            mLayoutResId = getLayoutResourceId(mItemViewType);
720316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            mPosition = position;
721dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
722d99993213962df64c9d76005df86c2fb83fca074Yorke Lee            final Resources resources = mContext.getResources();
723ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee
724ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee            if (mItemViewType == ViewTypes.TOP) {
725ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                // For tiled views, we still want padding to be set on the ContactTileRow.
726ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                // Otherwise the padding would be set around each of the tiles, which we don't want
727ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                mRowPaddingTop = resources.getDimensionPixelSize(
728ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                        R.dimen.favorites_row_top_padding);
729ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                mRowPaddingBottom = resources.getDimensionPixelSize(
730ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                        R.dimen.favorites_row_bottom_padding);
731ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                mRowPaddingStart = resources.getDimensionPixelSize(
732ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                        R.dimen.favorites_row_start_padding);
733ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                mRowPaddingEnd = resources.getDimensionPixelSize(
734ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                        R.dimen.favorites_row_end_padding);
735a2bdac8d96eaed2dacea274c55036ca9c1a77b4dYorke Lee
736a2bdac8d96eaed2dacea274c55036ca9c1a77b4dYorke Lee                setBackgroundResource(R.drawable.bottom_border_background);
737ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee            } else {
738ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                // For row views, padding is set on the view itself.
739ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                mRowPaddingTop = 0;
740ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                mRowPaddingBottom = 0;
741ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                mRowPaddingStart = 0;
742ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee                mRowPaddingEnd = 0;
743ba6b3366f21c47454b3ba5189e94bb6bf58fd11dYorke Lee            }
744d99993213962df64c9d76005df86c2fb83fca074Yorke Lee
745d99993213962df64c9d76005df86c2fb83fca074Yorke Lee            setPaddingRelative(mRowPaddingStart, mRowPaddingTop, mRowPaddingEnd,
746d99993213962df64c9d76005df86c2fb83fca074Yorke Lee                    mRowPaddingBottom);
747d99993213962df64c9d76005df86c2fb83fca074Yorke Lee
748dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            // Remove row (but not children) from accessibility node tree.
749dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO);
75011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
75111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            if (mItemViewType == ViewTypes.FREQUENT) {
75211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                // ListView handles swiping for this item
75311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                SwipeHelper.setSwipeable(this, true);
75411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            } else if (mItemViewType == ViewTypes.TOP) {
75511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                // The contact tile row has its own swipe helpers, that makes each individual
75611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                // tile swipeable.
75711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                final float densityScale = getResources().getDisplayMetrics().density;
75811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                final float pagingTouchSlop = ViewConfiguration.get(context)
75911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                        .getScaledPagingTouchSlop();
76011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                mSwipeHelper = new SwipeHelper(context, SwipeHelper.X, this, densityScale,
76111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                        pagingTouchSlop);
762362c4441b65f63dfc3cf933d569aba6e00dc168aHongwei Wang                // Increase swipe thresholds for square tiles since they are relatively small.
763362c4441b65f63dfc3cf933d569aba6e00dc168aHongwei Wang                mSwipeHelper.setChildSwipedFarEnoughFactor(0.9f);
764362c4441b65f63dfc3cf933d569aba6e00dc168aHongwei Wang                mSwipeHelper.setChildSwipedFastEnoughFactor(0.1f);
76511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                mOnItemSwipeListener = PhoneFavoritesTileAdapter.this;
76611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            }
767dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
768dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
769dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        /**
770dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee         * Configures the row to add {@link ContactEntry}s information to the views
771dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee         */
772316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        public void configureRow(ArrayList<ContactEntry> list, int position, boolean isLastRow) {
773dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            int columnCount = mItemViewType == ViewTypes.FREQUENT ? 1 : mColumnCount;
774316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            mPosition = position;
775dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
776dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            // Adding tiles to row and filling in contact information
777dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            for (int columnCounter = 0; columnCounter < columnCount; columnCounter++) {
778dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                ContactEntry entry =
779dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                        columnCounter < list.size() ? list.get(columnCounter) : null;
780dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                addTileFromEntry(entry, columnCounter, isLastRow);
781dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
782c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            if (columnCount == 1) {
783c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee                if (list.get(0) == ContactEntry.BLANK_ENTRY) {
784c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee                    setVisibility(View.INVISIBLE);
785c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee                } else {
786c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee                    setVisibility(View.VISIBLE);
787c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee                }
788c36684277aa45085999284bfe71cb8be71b3a464Yorke Lee            }
7896b7bbff7aaae1e51d57c255c21a0ef1c4faf7655Christine Chen            setPressed(false);
790dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
791dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
792dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        private void addTileFromEntry(ContactEntry entry, int childIndex, boolean isLastRow) {
793316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            final PhoneFavoriteTileView contactTile;
794dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
795dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            if (getChildCount() <= childIndex) {
796dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
797316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                contactTile = (PhoneFavoriteTileView) inflate(mContext, mLayoutResId, null);
798dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                // Note: the layoutparam set here is only actually used for FREQUENT.
799dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                // We override onMeasure() for STARRED and we don't care the layout param there.
800d99993213962df64c9d76005df86c2fb83fca074Yorke Lee                final Resources resources = mContext.getResources();
801dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
802dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                        ViewGroup.LayoutParams.WRAP_CONTENT,
803dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                        ViewGroup.LayoutParams.WRAP_CONTENT);
804dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
805dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                params.setMargins(
806316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                        resources.getDimensionPixelSize(R.dimen.detail_item_side_margin), 0,
807316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                        resources.getDimensionPixelSize(R.dimen.detail_item_side_margin), 0);
808dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                contactTile.setLayoutParams(params);
809dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                contactTile.setPhotoManager(mPhotoManager);
810dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                contactTile.setListener(mListener);
811dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                addView(contactTile);
812dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            } else {
813316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                contactTile = (PhoneFavoriteTileView) getChildAt(childIndex);
814dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
815dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            contactTile.loadFromContact(entry);
81611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
81711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            int entryIndex = -1;
818dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            switch (mItemViewType) {
819d99993213962df64c9d76005df86c2fb83fca074Yorke Lee                case ViewTypes.TOP:
820dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    // Setting divider visibilities
821dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    contactTile.setPaddingRelative(0, 0,
822d99993213962df64c9d76005df86c2fb83fca074Yorke Lee                            childIndex >= mColumnCount - 1 ? 0 : mPaddingInPixels, 0);
82311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                    entryIndex = getFirstContactEntryIndexForPosition(mPosition) + childIndex;
82411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                    SwipeHelper.setSwipeable(contactTile, true);
825dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    break;
826dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                case ViewTypes.FREQUENT:
827dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    contactTile.setHorizontalDividerVisibility(
828dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                            isLastRow ? View.GONE : View.VISIBLE);
82911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                    entryIndex = getFirstContactEntryIndexForPosition(mPosition);
83011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                    SwipeHelper.setSwipeable(this, true);
831dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    break;
832dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                default:
833dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    break;
834dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
83511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            // tag the tile with the index of the contact entry it is associated with
83611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            if (entryIndex != -1) {
83711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                contactTile.setTag(CONTACT_ENTRY_INDEX_TAG, entryIndex);
83811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            }
839316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            contactTile.setupFavoriteContactCard();
840dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
841dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
842dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        @Override
843dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
844dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            switch (mItemViewType) {
845d99993213962df64c9d76005df86c2fb83fca074Yorke Lee                case ViewTypes.TOP:
846dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    onLayoutForTiles();
847dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    return;
848dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                default:
849dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    super.onLayout(changed, left, top, right, bottom);
850dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    return;
851dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
852dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
853dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
854dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        private void onLayoutForTiles() {
855dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            final int count = getChildCount();
856dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
857dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            // Just line up children horizontally.
858d99993213962df64c9d76005df86c2fb83fca074Yorke Lee            int childLeft = getPaddingStart();
859dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            for (int i = 0; i < count; i++) {
860dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                final View child = getChildAt(i);
861dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
862dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                // Note MeasuredWidth includes the padding.
863dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                final int childWidth = child.getMeasuredWidth();
864dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight());
865dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                childLeft += childWidth;
866dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
867dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
868dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
869dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        @Override
870dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
871dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            switch (mItemViewType) {
872d99993213962df64c9d76005df86c2fb83fca074Yorke Lee                case ViewTypes.TOP:
873dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    onMeasureForTiles(widthMeasureSpec);
874dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    return;
875dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                default:
876dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
877dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                    return;
878dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
879dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
880dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
881dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        private void onMeasureForTiles(int widthMeasureSpec) {
882dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            final int width = MeasureSpec.getSize(widthMeasureSpec);
883dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
884dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            final int childCount = getChildCount();
885dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            if (childCount == 0) {
886dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                // Just in case...
887dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                setMeasuredDimension(width, 0);
888dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                return;
889dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
890dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
891dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            // 1. Calculate image size.
892dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            //      = ([total width] - [total padding]) / [child count]
893dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            //
894dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            // 2. Set it to width/height of each children.
895dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            //    If we have a remainder, some tiles will have 1 pixel larger width than its height.
896dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            //
897dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            // 3. Set the dimensions of itself.
898dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            //    Let width = given width.
899dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            //    Let height = image size + bottom paddding.
900dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
901d99993213962df64c9d76005df86c2fb83fca074Yorke Lee            final int totalPaddingsInPixels = (mColumnCount - 1) * mPaddingInPixels
902d99993213962df64c9d76005df86c2fb83fca074Yorke Lee                    + mRowPaddingStart + mRowPaddingEnd;
903dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
904dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            // Preferred width / height for images (excluding the padding).
905dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            // The actual width may be 1 pixel larger than this if we have a remainder.
906dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            final int imageSize = (width - totalPaddingsInPixels) / mColumnCount;
907dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            final int remainder = width - (imageSize * mColumnCount) - totalPaddingsInPixels;
908dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
909dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            for (int i = 0; i < childCount; i++) {
910dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                final View child = getChildAt(i);
911dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                final int childWidth = imageSize + child.getPaddingRight()
912dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                        // Compensate for the remainder
913dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                        + (i < remainder ? 1 : 0);
914d99993213962df64c9d76005df86c2fb83fca074Yorke Lee                final int childHeight = imageSize;
915dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                child.measure(
916dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                        MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY),
917dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                        MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY)
918dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee                        );
919dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee            }
920d99993213962df64c9d76005df86c2fb83fca074Yorke Lee            setMeasuredDimension(width, imageSize + getPaddingTop() + getPaddingBottom());
921dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee        }
922316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
923316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        /**
924316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen         * Gets the index of the item at the specified coordinates.
925316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen         *
926316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen         * @param itemX X-coordinate of the selected item.
927316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen         * @param itemY Y-coordinate of the selected item.
928316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen         * @return Index of the selected item in the cached array.
929316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen         */
930316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        public int getItemIndex(float itemX, float itemY) {
931316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            if (mPosition < mMaxTiledRows) {
932316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                final Rect childRect = new Rect();
933316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                if (DEBUG) {
934316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                    Log.v(TAG, String.valueOf(itemX) + " " + String.valueOf(itemY));
935316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                }
936316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                for (int i = 0; i < getChildCount(); ++i) {
937316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                    /** If the row contains multiple tiles, checks each tile to see if the point
938316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                     * is contained in the tile. */
939316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                    getChildAt(i).getHitRect(childRect);
940316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                    if (DEBUG) {
941316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                        Log.v(TAG, childRect.toString());
942316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                    }
943316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                    if (childRect.contains((int)itemX, (int)itemY)) {
944316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                        /** If the point is contained in the rectangle, computes the index of the
945316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                         * item in the cached array. */
946316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                        return i + (mPosition) * mColumnCount;
947316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                    }
948316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                }
949316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            } else {
950316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen                /** If the selected item is one of the rows, compute the index. */
95172418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                return getRegularRowItemIndex();
952316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            }
953316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            return -1;
954316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        }
955316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
95672418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        /**
95772418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang         * Gets the index of the regular row item.
95872418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang         *
95972418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang         * @return Index of the selected item in the cached array.
96072418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang         */
96172418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        public int getRegularRowItemIndex() {
96272418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            return (mPosition - mMaxTiledRows) + mColumnCount * mMaxTiledRows;
96372418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        }
96472418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang
965316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        public PhoneFavoritesTileAdapter getTileAdapter() {
966316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            return PhoneFavoritesTileAdapter.this;
967316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        }
968316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen
969316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        public int getPosition() {
970316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen            return mPosition;
971316b4713b2f8f26f393ecc4bb4760512a4a9f096Christine Chen        }
97211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
97311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        @Override
97411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public View getChildAtPosition(MotionEvent ev) {
97511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            // find the view under the pointer, accounting for GONE views
97611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            final int count = getChildCount();
97711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            final int touchX = (int) ev.getX();
97811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            View slidingChild;
97911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            for (int childIdx = 0; childIdx < count; childIdx++) {
98011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                slidingChild = getChildAt(childIdx);
98111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                if (slidingChild.getVisibility() == GONE) {
98211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                    continue;
98311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                }
98411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                if (touchX >= slidingChild.getLeft() && touchX <= slidingChild.getRight()) {
98511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                    if (SwipeHelper.isSwipeable(slidingChild)) {
98611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                        // If this view is swipable, then return it. If not, because the removal
98711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                        // dialog is currently showing, then return a null view, which will simply
98811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                        // be ignored by the swipe helper.
98911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                        return slidingChild;
99011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                    } else {
99111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                        return null;
99211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                    }
99311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                }
99411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            }
99511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            return null;
99611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
99711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
99811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        @Override
99911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public View getChildContentView(View v) {
100011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            return v.findViewById(R.id.contact_favorite_card);
100111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
100211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
100311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        @Override
100411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public void onScroll() {}
100511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
100611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        @Override
100711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public boolean canChildBeDismissed(View v) {
100811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            return true;
100911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
101011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
101111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        @Override
101211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public void onBeginDrag(View v) {
101311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            removePendingContactEntry();
101411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            final int index = indexOfChild(v);
1015a2bdac8d96eaed2dacea274c55036ca9c1a77b4dYorke Lee
1016a2bdac8d96eaed2dacea274c55036ca9c1a77b4dYorke Lee            /*
101711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            if (index > 0) {
101811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                detachViewFromParent(index);
101911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                attachViewToParent(v, 0, v.getLayoutParams());
1020a2bdac8d96eaed2dacea274c55036ca9c1a77b4dYorke Lee            }*/
102111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
102211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            // We do this so the underlying ScrollView knows that it won't get
102311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            // the chance to intercept events anymore
102411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            requestDisallowInterceptTouchEvent(true);
102511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
102611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
102711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        @Override
102811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public void onChildDismissed(View v) {
102911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            if (v != null) {
103011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                if (mOnItemSwipeListener != null) {
103111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                    mOnItemSwipeListener.onSwipe(v);
103211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                }
103311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            }
103411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
103511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
103611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        @Override
103711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public void onDragCancelled(View v) {}
103811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
103911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        @Override
104011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public boolean onInterceptTouchEvent(MotionEvent ev) {
10415a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee            if (mSwipeHelper != null && isSwipeEnabled()) {
104211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                return mSwipeHelper.onInterceptTouchEvent(ev) || super.onInterceptTouchEvent(ev);
104311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            } else {
104411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                return super.onInterceptTouchEvent(ev);
104511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            }
104611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
104711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
104811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        @Override
104911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public boolean onTouchEvent(MotionEvent ev) {
10505a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee            if (mSwipeHelper != null && isSwipeEnabled()) {
105111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                return mSwipeHelper.onTouchEvent(ev) || super.onTouchEvent(ev);
105211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            } else {
105311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                return super.onTouchEvent(ev);
105411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            }
105511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
105611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
105711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public int getItemViewType() {
105811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            return mItemViewType;
105911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
106011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
106111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        public void setOnItemSwipeListener(OnItemGestureListener listener) {
106211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee            mOnItemSwipeListener = listener;
106311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        }
1064dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
1065dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee
106655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    /**
106755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Used when a contact is swiped away. This will both unstar and set pinned position of the
106855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * contact to PinnedPosition.DEMOTED so that it doesn't show up anymore in the favorites list.
106955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     */
107055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    private void unstarAndUnpinContact(Uri contactUri) {
107155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        final ContentValues values = new ContentValues(2);
107255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        values.put(Contacts.STARRED, false);
107355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        values.put(Contacts.PINNED, PinnedPositions.DEMOTED);
107455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        mContext.getContentResolver().update(contactUri, values, null, null);
107555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    }
107655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
107755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    /**
107855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Given a list of contacts that each have pinned positions, rearrange the list (destructive)
107955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * such that all pinned contacts are in their defined pinned positions, and unpinned contacts
108055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * take the spaces between those pinned contacts. Demoted contacts should not appear in the
108155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * resulting list.
108255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     *
108355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * This method also updates the pinned positions of pinned contacts so that they are all
108455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * unique positive integers within range from 0 to toArrange.size() - 1. This is because
108555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * when the contact entries are read from the database, it is possible for them to have
108655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * overlapping pin positions due to sync or modifications by third party apps.
108755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     */
108855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    @VisibleForTesting
108955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    /* package */ void arrangeContactsByPinnedPosition(ArrayList<ContactEntry> toArrange) {
109055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        final PriorityQueue<ContactEntry> pinnedQueue =
109155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                new PriorityQueue<ContactEntry>(PIN_LIMIT, mContactEntryComparator);
109255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
109355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        final List<ContactEntry> unpinnedContacts = new LinkedList<ContactEntry>();
109455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
109555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        final int length = toArrange.size();
109655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        for (int i = 0; i < length; i++) {
109755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            final ContactEntry contact = toArrange.get(i);
109855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            // Decide whether the contact is hidden(demoted), pinned, or unpinned
109955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            if (contact.pinned > PIN_LIMIT) {
110055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                unpinnedContacts.add(contact);
110155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            } else if (contact.pinned > PinnedPositions.DEMOTED) {
110255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                // Demoted or contacts with negative pinned positions are ignored.
110355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                // Pinned contacts go into a priority queue where they are ranked by pinned
110455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                // position. This is required because the contacts provider does not return
110555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                // contacts ordered by pinned position.
110655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                pinnedQueue.add(contact);
110755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            }
110855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        }
110955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
111055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        final int maxToPin = Math.min(PIN_LIMIT, pinnedQueue.size() + unpinnedContacts.size());
111155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
111255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        toArrange.clear();
111355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        for (int i = 0; i < maxToPin; i++) {
111455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            if (!pinnedQueue.isEmpty() && pinnedQueue.peek().pinned <= i) {
111555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                final ContactEntry toPin = pinnedQueue.poll();
111655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                toPin.pinned = i;
111755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                toArrange.add(toPin);
111855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            } else if (!unpinnedContacts.isEmpty()) {
111955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee                toArrange.add(unpinnedContacts.remove(0));
112055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            }
112155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        }
112255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
112355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        // If there are still contacts in pinnedContacts at this point, it means that the pinned
112455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        // positions of these pinned contacts exceed the actual number of contacts in the list.
112555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        // For example, the user had 10 frequents, starred and pinned one of them at the last spot,
112655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        // and then cleared frequents. Contacts in this situation should become unpinned.
112755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        while (!pinnedQueue.isEmpty()) {
112855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            final ContactEntry entry = pinnedQueue.poll();
112955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            entry.pinned = PinnedPositions.UNPINNED;
113055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            toArrange.add(entry);
113155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        }
113255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
113355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        // Any remaining unpinned contacts that weren't in the gaps between the pinned contacts
113455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        // now just get appended to the end of the list.
113555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        toArrange.addAll(unpinnedContacts);
113655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    }
113755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
113855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    /**
113955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * Given an existing list of contact entries and a single entry that is to be pinned at a
114055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * particular position, return a ContentValues object that contains new pinned positions for
114155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * all contacts that are forced to be pinned at new positions, trying as much as possible to
114255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * keep pinned contacts at their original location.
114355f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     *
114455f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * At this point in time the pinned position of each contact in the list has already been
114555f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * updated by {@link #arrangeContactsByPinnedPosition}, so we can assume that all pinned
114655f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     * positions(within {@link #PIN_LIMIT} are unique positive integers.
114755f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee     */
114855f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    @VisibleForTesting
114955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    /* package */ ContentValues getReflowedPinnedPositions(ArrayList<ContactEntry> list,
115055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee            ContactEntry entryToPin, int oldPos, int newPinPos) {
115155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
115255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        final ContentValues cv = new ContentValues();
115372418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        final int lowerBound = Math.min(oldPos, newPinPos);
115472418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        final int upperBound = Math.max(oldPos, newPinPos);
115572418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        for (int i = lowerBound; i <= upperBound; i++) {
115672418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            final ContactEntry entry = list.get(i);
115772418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            if (entry.pinned == i) continue;
115872418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            cv.put(String.valueOf(entry.id), i);
115955f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        }
116055f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee        return cv;
116155f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee    }
116255f3ec9f882b2389e498ea68f68ce8670c5d4c73Yorke Lee
1163dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    protected static class ViewTypes {
116418e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee        public static final int COUNT = 3;
1165d99993213962df64c9d76005df86c2fb83fca074Yorke Lee        public static final int FREQUENT = 0;
1166d99993213962df64c9d76005df86c2fb83fca074Yorke Lee        public static final int TOP = 1;
116718e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee        public static final int EMPTY = 2;
1168dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee    }
116911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
117011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    @Override
117111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    public void onSwipe(View view) {
117211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        final PhoneFavoriteTileView tileView = (PhoneFavoriteTileView) view.findViewById(
117311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                R.id.contact_tile);
117411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        // When the view is in the removal dialog, it should no longer be swipeable
117511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        SwipeHelper.setSwipeable(view, false);
117611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        tileView.displayRemovalDialog();
117711ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
117811ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        final Integer entryIndex = (Integer) tileView.getTag(
117911ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee                ContactTileRow.CONTACT_ENTRY_INDEX_TAG);
118011ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
118111ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        setPotentialRemoveEntryIndex(entryIndex);
118211ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    }
118311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee
118411ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    @Override
118511ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    public void onTouch() {
118611ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee        removePendingContactEntry();
11875a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee        return;
11885a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee    }
11895a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee
11905a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee    @Override
11915a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee    public boolean isSwipeEnabled() {
11925a1db1cd6a8addc0401529454bde6e1d15f215a0Yorke Lee        return !mAwaitingRemove;
119311ee58b1b8711c6d3b2ade6a71835b6c102a08a7Yorke Lee    }
119418e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee
119518e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee    public void setEmptyView(View emptyView) {
119618e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee        mEmptyView = emptyView;
119718e7fdd55f67e3636c161462cb54e7f5555a04deYorke Lee    }
119872418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang
119972418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    public void reportDragEnteredItemIndex(int itemIndex) {
120072418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        if (mInDragging &&
120172418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                mDragEnteredEntryIndex != itemIndex &&
120272418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                isIndexInBound(itemIndex) &&
120372418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang                itemIndex < PIN_LIMIT) {
120472418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang            markDropArea(itemIndex);
120572418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang        }
120672418af9f84b7a97ff8cb08b99aa68b2db9ee1a9Hongwei Wang    }
1207dfb2eee7d98f8540fd1614db66bb03e8e1f3a26aYorke Lee}
1208