Home | History | Annotate | Download | only in detail
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.contacts.detail;
     18 
     19 import android.content.Context;
     20 import android.util.AttributeSet;
     21 import android.view.LayoutInflater;
     22 import android.view.MotionEvent;
     23 import android.view.View;
     24 import android.view.View.OnTouchListener;
     25 import android.view.ViewPropertyAnimator;
     26 import android.widget.HorizontalScrollView;
     27 
     28 import com.android.contacts.R;
     29 import com.android.contacts.widget.FrameLayoutWithOverlay;
     30 
     31 /**
     32  * This is a horizontally scrolling carousel with 2 fragments: one to see info about the contact and
     33  * one to see updates from the contact. Depending on the scroll position and user selection of which
     34  * fragment to currently view, the touch interceptors over each fragment are configured accordingly.
     35  */
     36 public class ContactDetailFragmentCarousel extends HorizontalScrollView implements OnTouchListener {
     37 
     38     private static final String TAG = ContactDetailFragmentCarousel.class.getSimpleName();
     39 
     40     /**
     41      * Number of pixels that this view can be scrolled horizontally.
     42      */
     43     private int mAllowedHorizontalScrollLength = Integer.MIN_VALUE;
     44 
     45     /**
     46      * Minimum X scroll position that must be surpassed (if the user is on the "about" page of the
     47      * contact card), in order for this view to automatically snap to the "updates" page.
     48      */
     49     private int mLowerThreshold = Integer.MIN_VALUE;
     50 
     51     /**
     52      * Maximum X scroll position (if the user is on the "updates" page of the contact card), below
     53      * which this view will automatically snap to the "about" page.
     54      */
     55     private int mUpperThreshold = Integer.MIN_VALUE;
     56 
     57     /**
     58      * Minimum width of a fragment (if there is more than 1 fragment in the carousel, then this is
     59      * the width of one of the fragments).
     60      */
     61     private int mMinFragmentWidth = Integer.MIN_VALUE;
     62 
     63     /**
     64      * Fragment width (if there are 1+ fragments in the carousel) as defined as a fraction of the
     65      * screen width.
     66      */
     67     private static final float FRAGMENT_WIDTH_SCREEN_WIDTH_FRACTION = 0.85f;
     68 
     69     private static final int ABOUT_PAGE = 0;
     70     private static final int UPDATES_PAGE = 1;
     71 
     72     private static final int MAX_FRAGMENT_VIEW_COUNT = 2;
     73 
     74     private boolean mEnableSwipe;
     75 
     76     private int mCurrentPage = ABOUT_PAGE;
     77     private int mLastScrollPosition;
     78 
     79     private FrameLayoutWithOverlay mAboutFragment;
     80     private FrameLayoutWithOverlay mUpdatesFragment;
     81 
     82     public ContactDetailFragmentCarousel(Context context) {
     83         this(context, null);
     84     }
     85 
     86     public ContactDetailFragmentCarousel(Context context, AttributeSet attrs) {
     87         this(context, attrs, 0);
     88     }
     89 
     90     public ContactDetailFragmentCarousel(Context context, AttributeSet attrs, int defStyle) {
     91         super(context, attrs, defStyle);
     92 
     93         final LayoutInflater inflater =
     94                 (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     95         inflater.inflate(R.layout.contact_detail_fragment_carousel, this);
     96 
     97         setOnTouchListener(this);
     98     }
     99 
    100     @Override
    101     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    102         int screenWidth = MeasureSpec.getSize(widthMeasureSpec);
    103         int screenHeight = MeasureSpec.getSize(heightMeasureSpec);
    104 
    105         // Take the width of this view as the width of the screen and compute necessary thresholds.
    106         // Only do this computation 1x.
    107         if (mAllowedHorizontalScrollLength == Integer.MIN_VALUE) {
    108             mMinFragmentWidth = (int) (FRAGMENT_WIDTH_SCREEN_WIDTH_FRACTION * screenWidth);
    109             mAllowedHorizontalScrollLength = (MAX_FRAGMENT_VIEW_COUNT * mMinFragmentWidth) -
    110                     screenWidth;
    111             mLowerThreshold = (screenWidth - mMinFragmentWidth) / MAX_FRAGMENT_VIEW_COUNT;
    112             mUpperThreshold = mAllowedHorizontalScrollLength - mLowerThreshold;
    113         }
    114 
    115         if (getChildCount() > 0) {
    116             View child = getChildAt(0);
    117             // If we enable swipe, then the {@link LinearLayout} child width must be the sum of the
    118             // width of all its children fragments.
    119             // Or the current page may already be set to something other than the first.  If so,
    120             // it also means there are multiple child fragments.
    121             if (mEnableSwipe || mCurrentPage != 0) {
    122                 child.measure(MeasureSpec.makeMeasureSpec(
    123                         mMinFragmentWidth * MAX_FRAGMENT_VIEW_COUNT, MeasureSpec.EXACTLY),
    124                         MeasureSpec.makeMeasureSpec(screenHeight, MeasureSpec.EXACTLY));
    125             } else {
    126                 // Otherwise, the {@link LinearLayout} child width will just be the screen width
    127                 // because it will only have 1 child fragment.
    128                 child.measure(MeasureSpec.makeMeasureSpec(screenWidth, MeasureSpec.EXACTLY),
    129                         MeasureSpec.makeMeasureSpec(screenHeight, MeasureSpec.EXACTLY));
    130             }
    131         }
    132 
    133         setMeasuredDimension(
    134                 resolveSize(screenWidth, widthMeasureSpec),
    135                 resolveSize(screenHeight, heightMeasureSpec));
    136     }
    137 
    138     /**
    139      * Set the current page. This dims out the non-selected page but doesn't do any scrolling of
    140      * the carousel.
    141      */
    142     public void setCurrentPage(int pageIndex) {
    143         mCurrentPage = pageIndex;
    144 
    145         updateTouchInterceptors();
    146     }
    147 
    148     /**
    149      * Set the view containers for the detail and updates fragment.
    150      */
    151     public void setFragmentViews(FrameLayoutWithOverlay about, FrameLayoutWithOverlay updates) {
    152         mAboutFragment = about;
    153         mUpdatesFragment = updates;
    154 
    155         mAboutFragment.setOverlayOnClickListener(mAboutFragTouchInterceptListener);
    156         mUpdatesFragment.setOverlayOnClickListener(mUpdatesFragTouchInterceptListener);
    157     }
    158 
    159     /**
    160      * Enable swiping if the detail and update fragments should be showing. Otherwise disable
    161      * swiping if only the detail fragment should be showing.
    162      */
    163     public void enableSwipe(boolean enable) {
    164         if (mEnableSwipe != enable) {
    165             mEnableSwipe = enable;
    166             if (mUpdatesFragment != null) {
    167                 mUpdatesFragment.setVisibility(enable ? View.VISIBLE : View.GONE);
    168                 snapToEdge();
    169                 updateTouchInterceptors();
    170             }
    171         }
    172     }
    173 
    174     /**
    175      * Reset the fragment carousel to show the about page.
    176      */
    177     public void reset() {
    178         if (mCurrentPage != ABOUT_PAGE) {
    179             mCurrentPage = ABOUT_PAGE;
    180             snapToEdgeSmooth();
    181         }
    182     }
    183 
    184     public int getCurrentPage() {
    185         return mCurrentPage;
    186     }
    187 
    188     private final OnClickListener mAboutFragTouchInterceptListener = new OnClickListener() {
    189         @Override
    190         public void onClick(View v) {
    191             mCurrentPage = ABOUT_PAGE;
    192             snapToEdgeSmooth();
    193         }
    194     };
    195 
    196     private final OnClickListener mUpdatesFragTouchInterceptListener = new OnClickListener() {
    197         @Override
    198         public void onClick(View v) {
    199             mCurrentPage = UPDATES_PAGE;
    200             snapToEdgeSmooth();
    201         }
    202     };
    203 
    204     private void updateTouchInterceptors() {
    205         // Disable the touch-interceptor on the selected page, and enable it on the other.
    206         if (mAboutFragment != null) {
    207             mAboutFragment.setOverlayClickable(mCurrentPage != ABOUT_PAGE);
    208         }
    209         if (mUpdatesFragment != null) {
    210             mUpdatesFragment.setOverlayClickable(mCurrentPage != UPDATES_PAGE);
    211         }
    212     }
    213 
    214     @Override
    215     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    216         super.onScrollChanged(l, t, oldl, oldt);
    217         if (!mEnableSwipe) {
    218             return;
    219         }
    220         mLastScrollPosition = l;
    221     }
    222 
    223     /**
    224      * Used to set initial scroll offset.  Not smooth.
    225      */
    226     private void snapToEdge() {
    227         setScrollX(calculateHorizontalOffset());
    228         updateTouchInterceptors();
    229     }
    230 
    231     /**
    232      * Smooth version of snapToEdge().
    233      */
    234     private void snapToEdgeSmooth() {
    235         smoothScrollTo(calculateHorizontalOffset(), 0);
    236         updateTouchInterceptors();
    237     }
    238 
    239     private int calculateHorizontalOffset() {
    240         return mCurrentPage == ABOUT_PAGE ? 0 : mAllowedHorizontalScrollLength;
    241     }
    242 
    243     /**
    244      * Returns the desired page we should scroll to based on the current X scroll position and the
    245      * current page.
    246      */
    247     private int getDesiredPage() {
    248         switch (mCurrentPage) {
    249             case ABOUT_PAGE:
    250                 // If the user is on the "about" page, and the scroll position exceeds the lower
    251                 // threshold, then we should switch to the "updates" page.
    252                 return (mLastScrollPosition > mLowerThreshold) ? UPDATES_PAGE : ABOUT_PAGE;
    253             case UPDATES_PAGE:
    254                 // If the user is on the "updates" page, and the scroll position goes below the
    255                 // upper threshold, then we should switch to the "about" page.
    256                 return (mLastScrollPosition < mUpperThreshold) ? ABOUT_PAGE : UPDATES_PAGE;
    257         }
    258         throw new IllegalStateException("Invalid current page " + mCurrentPage);
    259     }
    260 
    261     @Override
    262     public boolean onTouch(View v, MotionEvent event) {
    263         if (!mEnableSwipe) {
    264             return false;
    265         }
    266         if (event.getAction() == MotionEvent.ACTION_UP) {
    267             mCurrentPage = getDesiredPage();
    268             snapToEdgeSmooth();
    269             return true;
    270         }
    271         return false;
    272     }
    273 
    274     /**
    275      * Starts an "appear" animation by moving in the "Updates" from the right.
    276      */
    277     public void animateAppear() {
    278         final int x = Math.round((1.0f - FRAGMENT_WIDTH_SCREEN_WIDTH_FRACTION) * getWidth());
    279         mUpdatesFragment.setTranslationX(x);
    280         final ViewPropertyAnimator animator = mUpdatesFragment.animate();
    281         animator.translationX(0.0f);
    282     }
    283 }
    284