From 2b94c39bb8e54ae2bd78e582e8161f783c9dc516 Mon Sep 17 00:00:00 2001 From: Yorke Lee Date: Fri, 6 Feb 2015 14:50:35 -0800 Subject: Remove shortcut cards list view Remove ListView containing shortcut cards and replace it with a single ShortcutCard card. Rename ShortcutCardAdapter to ShortcutCardManager with slight refactor. Some corresponding logic changes inside ListsFragment. Reduces the number of getView calls at startup from 4 to 1. Change-Id: I11787bdb1d04cc19634c21bfd398b7533c75d339 --- res/layout/lists_fragment.xml | 9 +- src/com/android/dialer/list/ListsFragment.java | 32 ++-- .../android/dialer/list/ShortcutCardsAdapter.java | 180 --------------------- .../android/dialer/list/ShortcutCardsManager.java | 129 +++++++++++++++ .../android/dialer/list/SwipeableShortcutCard.java | 13 +- 5 files changed, 157 insertions(+), 206 deletions(-) delete mode 100644 src/com/android/dialer/list/ShortcutCardsAdapter.java create mode 100644 src/com/android/dialer/list/ShortcutCardsManager.java diff --git a/res/layout/lists_fragment.xml b/res/layout/lists_fragment.xml index face20e26..a11c89fef 100644 --- a/res/layout/lists_fragment.xml +++ b/res/layout/lists_fragment.xml @@ -21,15 +21,12 @@ android:layout_height="match_parent" android:animateLayoutChanges="true" > - + android:background="@color/actionbar_background_color" /> 0) { - final SwipeableShortcutCard v = - (SwipeableShortcutCard) mShortcutCardsListView.getChildAt(0); - v.clipCard(ratioCardHidden); + if (mShortcutCard.isShown()) { + float ratioCardHidden = (1 - slideOffset); + mShortcutCard.clipCard(ratioCardHidden); } if (mActionBar != null) { // Amount of available space that is not being hidden by the bottom pane - final int topPaneHeight = (int) (slideOffset * mShortcutCardsListView.getHeight()); + final int shortcutCardHeight = + mShortcutCard.isShown() ? mShortcutCard.getHeight() : 0; + final int topPaneHeight = (int) (slideOffset * shortcutCardHeight); final int availableActionBarHeight = Math.min(mActionBar.getHeight(), topPaneHeight); @@ -250,7 +249,6 @@ public class ListsFragment extends Fragment implements CallLogQueryHandler.Liste mCallLogAdapter = ObjectFactory.newCallLogAdapter(getActivity(), this, new ContactInfoHelper(getActivity(), currentCountryIso), null, null, false); - mMergedAdapter = new ShortcutCardsAdapter(getActivity(), this, mCallLogAdapter); Trace.endSection(); Trace.endSection(); } @@ -309,8 +307,8 @@ public class ListsFragment extends Fragment implements CallLogQueryHandler.Liste mViewPagerTabs.setViewPager(mViewPager); addOnPageChangeListener(mViewPagerTabs); - mShortcutCardsListView = (ListView) parentView.findViewById(R.id.shortcut_card_list); - mShortcutCardsListView.setAdapter(mMergedAdapter); + mShortcutCard = (SwipeableShortcutCard) parentView.findViewById(R.id.shortcut_card); + new ShortcutCardsManager(getActivity(), this, mCallLogAdapter, mShortcutCard); mRemoveView = (RemoveView) parentView.findViewById(R.id.remove_view); mRemoveViewContent = parentView.findViewById(R.id.remove_view_content); @@ -337,7 +335,6 @@ public class ListsFragment extends Fragment implements CallLogQueryHandler.Liste } mCallLogAdapter.changeCursor(cursor); - mMergedAdapter.notifyDataSetChanged(); // Refresh the overlapping pane to ensure that any changes in the shortcut card height // are appropriately reflected in the overlap position. @@ -398,11 +395,12 @@ public class ListsFragment extends Fragment implements CallLogQueryHandler.Liste mRemoveView.setAlpha(show ? 0 : 1); mRemoveView.animate().alpha(show ? 1 : 0).start(); - if (mShortcutCardsListView.getChildCount() > 0) { - View v = mShortcutCardsListView.getChildAt(0); - v.animate().withLayer() - .alpha(show ? REMOVE_VIEW_SHOWN_ALPHA : REMOVE_VIEW_HIDDEN_ALPHA) - .start(); + if (mShortcutCard.isShown()) { + final View child = mShortcutCard.getChildAt(0); + if (child != null) { + child.animate().withLayer() + .alpha(show ? REMOVE_VIEW_SHOWN_ALPHA : REMOVE_VIEW_HIDDEN_ALPHA).start(); + } } } diff --git a/src/com/android/dialer/list/ShortcutCardsAdapter.java b/src/com/android/dialer/list/ShortcutCardsAdapter.java deleted file mode 100644 index ad6a61c79..000000000 --- a/src/com/android/dialer/list/ShortcutCardsAdapter.java +++ /dev/null @@ -1,180 +0,0 @@ -/* - * Copyright (C) 2011 Google Inc. - * Licensed to The Android Open Source Project. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.android.dialer.list; - -import android.content.Context; -import android.content.res.Resources; -import android.database.Cursor; -import android.database.DataSetObserver; -import android.view.View; -import android.view.ViewGroup; -import android.widget.BaseAdapter; - -import com.android.dialer.calllog.CallLogAdapter; -import com.android.dialer.calllog.CallLogNotificationsHelper; -import com.android.dialer.calllog.CallLogQueryHandler; -import com.android.dialer.list.SwipeHelper.OnItemGestureListener; - -/** - * An adapter that displays call shortcuts from {@link com.android.dialer.calllog.CallLogAdapter} - * in the form of cards. - */ -public class ShortcutCardsAdapter extends BaseAdapter { - - private class CustomDataSetObserver extends DataSetObserver { - @Override - public void onChanged() { - notifyDataSetChanged(); - } - } - - private static final String TAG = ShortcutCardsAdapter.class.getSimpleName(); - - private final CallLogAdapter mCallLogAdapter; - - private final ListsFragment mFragment; - - private final Context mContext; - - private final DataSetObserver mObserver; - - private final CallLogQueryHandler mCallLogQueryHandler; - - private final OnItemGestureListener mCallLogOnItemSwipeListener = - new OnItemGestureListener() { - @Override - public void onSwipe(View view) { - mCallLogQueryHandler.markNewCallsAsOld(); - mCallLogQueryHandler.markNewVoicemailsAsOld(); - CallLogNotificationsHelper.removeMissedCallNotifications(mContext); - CallLogNotificationsHelper.updateVoicemailNotifications(mContext); - mFragment.dismissShortcut(view); - } - - @Override - public void onTouch() {} - - @Override - public boolean isSwipeEnabled() { - return true; - } - }; - - private final CallLogQueryHandler.Listener mCallLogQueryHandlerListener = - new CallLogQueryHandler.Listener() { - @Override - public void onVoicemailStatusFetched(Cursor statusCursor) {} - - @Override - public boolean onCallsFetched(Cursor combinedCursor) { - mCallLogAdapter.invalidateCache(); - mCallLogAdapter.changeCursor(combinedCursor); - mCallLogAdapter.notifyDataSetChanged(); - // Return true; took ownership of cursor - return true; - } - }; - - public ShortcutCardsAdapter(Context context, - ListsFragment fragment, - CallLogAdapter callLogAdapter) { - final Resources resources = context.getResources(); - mContext = context; - mFragment = fragment; - - mCallLogAdapter = callLogAdapter; - mObserver = new CustomDataSetObserver(); - mCallLogAdapter.registerDataSetObserver(mObserver); - mCallLogQueryHandler = new CallLogQueryHandler(mContext.getContentResolver(), - mCallLogQueryHandlerListener); - } - - /** - * Determines the number of items in the adapter. - * mCallLogAdapter contains the item for the most recent caller. - * mContactTileAdapter contains the starred contacts. - * The +1 is to account for the presence of the favorites menu. - * - * @return Number of items in the adapter. - */ - @Override - public int getCount() { - return mCallLogAdapter.getCount(); - } - - @Override - public Object getItem(int position) { - return mCallLogAdapter.getItem(position); - } - - @Override - public long getItemId(int position) { - return position; - } - - @Override - public boolean hasStableIds() { - return true; - } - - /** - * Determine the number of view types present. - */ - @Override - public int getViewTypeCount() { - return mCallLogAdapter.getViewTypeCount(); - } - - @Override - public int getItemViewType(int position) { - return mCallLogAdapter.getItemViewType(position); - } - - @Override - public View getView(int position, View convertView, ViewGroup parent) { - final SwipeableShortcutCard wrapper; - if (convertView == null) { - wrapper = new SwipeableShortcutCard(mContext); - wrapper.setOnItemSwipeListener(mCallLogOnItemSwipeListener); - } else { - wrapper = (SwipeableShortcutCard) convertView; - } - - // Special case wrapper view for the most recent call log item. This allows - // us to create a card-like effect for the more recent call log item in - // the PhoneFavoriteMergedAdapter, but keep the original look of the item in - // the CallLogAdapter. - final View view = mCallLogAdapter.getView(position, convertView == null ? - null : wrapper.getChildAt(0), parent - ); - wrapper.removeAllViews(); - wrapper.prepareChildView(view); - wrapper.addView(view); - wrapper.setVisibility(View.VISIBLE); - return wrapper; - } - - @Override - public boolean areAllItemsEnabled() { - return mCallLogAdapter.areAllItemsEnabled(); - } - - @Override - public boolean isEnabled(int position) { - return mCallLogAdapter.isEnabled(position); - } -} diff --git a/src/com/android/dialer/list/ShortcutCardsManager.java b/src/com/android/dialer/list/ShortcutCardsManager.java new file mode 100644 index 000000000..8b8a7ba62 --- /dev/null +++ b/src/com/android/dialer/list/ShortcutCardsManager.java @@ -0,0 +1,129 @@ +/* + * Copyright (C) 2011 Google Inc. + * Licensed to The Android Open Source Project. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.dialer.list; + +import android.content.Context; +import android.database.Cursor; +import android.database.DataSetObserver; +import android.view.View; + +import com.android.dialer.calllog.CallLogAdapter; +import com.android.dialer.calllog.CallLogNotificationsHelper; +import com.android.dialer.calllog.CallLogQueryHandler; +import com.android.dialer.list.SwipeHelper.OnItemGestureListener; + +/** + * Handles the logic that displays call shortcuts from + * {@link com.android.dialer.calllog.CallLogAdapter} in the form of cards. + */ +public class ShortcutCardsManager { + + private class CustomDataSetObserver extends DataSetObserver { + @Override + public void onChanged() { + updateShortcutCard(); + } + } + + private static final String TAG = ShortcutCardsManager.class.getSimpleName(); + + // The position of the shortcut card within the CallLogAdapter + private static final int SHORTCUT_CARD_INDEX = 0; + + private final CallLogAdapter mCallLogAdapter; + private final ListsFragment mFragment; + private final SwipeableShortcutCard mShortcutCard; + + private final Context mContext; + + private final DataSetObserver mObserver; + + private final CallLogQueryHandler mCallLogQueryHandler; + + private final OnItemGestureListener mCallLogOnItemSwipeListener = + new OnItemGestureListener() { + @Override + public void onSwipe(View view) { + mCallLogQueryHandler.markNewCallsAsOld(); + mCallLogQueryHandler.markNewVoicemailsAsOld(); + CallLogNotificationsHelper.removeMissedCallNotifications(mContext); + CallLogNotificationsHelper.updateVoicemailNotifications(mContext); + mFragment.dismissShortcut(view); + } + + @Override + public void onTouch() {} + + @Override + public boolean isSwipeEnabled() { + return true; + } + }; + + private final CallLogQueryHandler.Listener mCallLogQueryHandlerListener = + new CallLogQueryHandler.Listener() { + @Override + public void onVoicemailStatusFetched(Cursor statusCursor) {} + + @Override + public boolean onCallsFetched(Cursor combinedCursor) { + mCallLogAdapter.invalidateCache(); + mCallLogAdapter.changeCursor(combinedCursor); + mCallLogAdapter.notifyDataSetChanged(); + // Return true; took ownership of cursor + return true; + } + }; + + public ShortcutCardsManager(Context context, + ListsFragment fragment, + CallLogAdapter callLogAdapter, + SwipeableShortcutCard shortcutCard) { + mContext = context; + mFragment = fragment; + mShortcutCard = shortcutCard; + + mCallLogAdapter = callLogAdapter; + mObserver = new CustomDataSetObserver(); + mCallLogAdapter.registerDataSetObserver(mObserver); + mCallLogQueryHandler = new CallLogQueryHandler(mContext.getContentResolver(), + mCallLogQueryHandlerListener); + mShortcutCard.setOnItemSwipeListener(mCallLogOnItemSwipeListener); + } + + /** + * Updates the contents of the shortcut card with the view provided by the + * {@link CallLogAdapter}. + */ + private void updateShortcutCard() { + final int count = mCallLogAdapter.getCount(); + final View convertView = mShortcutCard.getChildAt(SHORTCUT_CARD_INDEX); + if (count <= SHORTCUT_CARD_INDEX) { + if (convertView != null) { + convertView.setVisibility(View.GONE); + } + } else { + mShortcutCard.setVisibility(View.VISIBLE); + final View view = mCallLogAdapter.getView(SHORTCUT_CARD_INDEX, convertView, + mShortcutCard); + mShortcutCard.removeAllViews(); + mShortcutCard.prepareChildView(view); + mShortcutCard.addView(view); + view.setVisibility(View.VISIBLE); + } + } +} diff --git a/src/com/android/dialer/list/SwipeableShortcutCard.java b/src/com/android/dialer/list/SwipeableShortcutCard.java index 93e9fff10..db2210a47 100644 --- a/src/com/android/dialer/list/SwipeableShortcutCard.java +++ b/src/com/android/dialer/list/SwipeableShortcutCard.java @@ -19,6 +19,7 @@ import android.content.Context; import android.content.res.Resources; import android.graphics.Rect; import android.text.TextUtils; +import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; @@ -56,7 +57,15 @@ public class SwipeableShortcutCard extends FrameLayout implements SwipeHelperCal private Rect mClipRect = new Rect(); public SwipeableShortcutCard(Context context) { - super(context); + this(context, null); + } + + public SwipeableShortcutCard(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public SwipeableShortcutCard(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); final Resources resources = getResources(); final float densityScale =resources.getDisplayMetrics().density; final float pagingTouchSlop = ViewConfiguration.get(context) @@ -204,9 +213,7 @@ public class SwipeableShortcutCard extends FrameLayout implements SwipeHelperCal if (ratioHidden > CLIP_CARD_MOSTLY_HIDDEN_RATIO) { mClipRect.set(0, 0 , 0, 0); - setVisibility(View.INVISIBLE); } else { - setVisibility(View.VISIBLE); int newTop = (int) (ratioHidden * height); mClipRect.set(0, newTop, width, height); -- cgit v1.2.3