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             if (mEnableSwipe) {
    120                 child.measure(MeasureSpec.makeMeasureSpec(
    121                         mMinFragmentWidth * MAX_FRAGMENT_VIEW_COUNT, MeasureSpec.EXACTLY),
    122                         MeasureSpec.makeMeasureSpec(screenHeight, MeasureSpec.EXACTLY));
    123             } else {
    124                 // Otherwise, the {@link LinearLayout} child width will just be the screen width
    125                 // because it will only have 1 child fragment.
    126                 child.measure(MeasureSpec.makeMeasureSpec(screenWidth, MeasureSpec.EXACTLY),
    127                         MeasureSpec.makeMeasureSpec(screenHeight, MeasureSpec.EXACTLY));
    128             }
    129         }
    130 
    131         setMeasuredDimension(
    132                 resolveSize(screenWidth, widthMeasureSpec),
    133                 resolveSize(screenHeight, heightMeasureSpec));
    134     }
    135 
    136     /**
    137      * Set the current page. This dims out the non-selected page but doesn't do any scrolling of
    138      * the carousel.
    139      */
    140     public void setCurrentPage(int pageIndex) {
    141         mCurrentPage = pageIndex;
    142 
    143         updateTouchInterceptors();
    144     }
    145 
    146     /**
    147      * Set the view containers for the detail and updates fragment.
    148      */
    149     public void setFragmentViews(FrameLayoutWithOverlay about, FrameLayoutWithOverlay updates) {
    150         mAboutFragment = about;
    151         mUpdatesFragment = updates;
    152 
    153         mAboutFragment.setOverlayOnClickListener(mAboutFragTouchInterceptListener);
    154         mUpdatesFragment.setOverlayOnClickListener(mUpdatesFragTouchInterceptListener);
    155     }
    156 
    157     /**
    158      * Enable swiping if the detail and update fragments should be showing. Otherwise disable
    159      * swiping if only the detail fragment should be showing.
    160      */
    161     public void enableSwipe(boolean enable) {
    162         if (mEnableSwipe != enable) {
    163             mEnableSwipe = enable;
    164             if (mUpdatesFragment != null) {
    165                 mUpdatesFragment.setVisibility(enable ? View.VISIBLE : View.GONE);
    166                 if (mCurrentPage == ABOUT_PAGE) {
    167                     mAboutFragment.requestFocus();
    168                 } else {
    169                     mUpdatesFragment.requestFocus();
    170                 }
    171                 updateTouchInterceptors();
    172             }
    173         }
    174     }
    175 
    176     /**
    177      * Reset the fragment carousel to show the about page.
    178      */
    179     public void reset() {
    180         if (mCurrentPage != ABOUT_PAGE) {
    181             mCurrentPage = ABOUT_PAGE;
    182             snapToEdge();
    183         }
    184     }
    185 
    186     public int getCurrentPage() {
    187         return mCurrentPage;
    188     }
    189 
    190     private final OnClickListener mAboutFragTouchInterceptListener = new OnClickListener() {
    191         @Override
    192         public void onClick(View v) {
    193             mCurrentPage = ABOUT_PAGE;
    194             snapToEdge();
    195         }
    196     };
    197 
    198     private final OnClickListener mUpdatesFragTouchInterceptListener = new OnClickListener() {
    199         @Override
    200         public void onClick(View v) {
    201             mCurrentPage = UPDATES_PAGE;
    202             snapToEdge();
    203         }
    204     };
    205 
    206     private void updateTouchInterceptors() {
    207         // Disable the touch-interceptor on the selected page, and enable it on the other.
    208         if (mAboutFragment != null) {
    209             mAboutFragment.setOverlayClickable(mCurrentPage != ABOUT_PAGE);
    210         }
    211         if (mUpdatesFragment != null) {
    212             mUpdatesFragment.setOverlayClickable(mCurrentPage != UPDATES_PAGE);
    213         }
    214     }
    215 
    216     @Override
    217     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    218         super.onScrollChanged(l, t, oldl, oldt);
    219         if (!mEnableSwipe) {
    220             return;
    221         }
    222         mLastScrollPosition = l;
    223     }
    224 
    225     private void snapToEdge() {
    226         final int x = mCurrentPage == ABOUT_PAGE ? 0 : mAllowedHorizontalScrollLength;
    227         smoothScrollTo(x,0);
    228         updateTouchInterceptors();
    229     }
    230 
    231     /**
    232      * Returns the desired page we should scroll to based on the current X scroll position and the
    233      * current page.
    234      */
    235     private int getDesiredPage() {
    236         switch (mCurrentPage) {
    237             case ABOUT_PAGE:
    238                 // If the user is on the "about" page, and the scroll position exceeds the lower
    239                 // threshold, then we should switch to the "updates" page.
    240                 return (mLastScrollPosition > mLowerThreshold) ? UPDATES_PAGE : ABOUT_PAGE;
    241             case UPDATES_PAGE:
    242                 // If the user is on the "updates" page, and the scroll position goes below the
    243                 // upper threshold, then we should switch to the "about" page.
    244                 return (mLastScrollPosition < mUpperThreshold) ? ABOUT_PAGE : UPDATES_PAGE;
    245         }
    246         throw new IllegalStateException("Invalid current page " + mCurrentPage);
    247     }
    248 
    249     @Override
    250     public boolean onTouch(View v, MotionEvent event) {
    251         if (!mEnableSwipe) {
    252             return false;
    253         }
    254         if (event.getAction() == MotionEvent.ACTION_UP) {
    255             mCurrentPage = getDesiredPage();
    256             snapToEdge();
    257             return true;
    258         }
    259         return false;
    260     }
    261 
    262     /**
    263      * Starts an "appear" animation by moving in the "Updates" from the right.
    264      */
    265     public void animateAppear() {
    266         final int x = Math.round((1.0f - FRAGMENT_WIDTH_SCREEN_WIDTH_FRACTION) * getWidth());
    267         mUpdatesFragment.setTranslationX(x);
    268         final ViewPropertyAnimator animator = mUpdatesFragment.animate();
    269         animator.translationX(0.0f);
    270     }
    271 }
    272