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 package com.android.contacts.detail; 17 18 import android.os.Parcelable; 19 import android.support.v4.view.PagerAdapter; 20 import android.support.v4.view.ViewPager; 21 import android.view.View; 22 23 /** 24 * Adapter for the {@link ViewPager} for the contact detail page for a contact in 2 cases: 25 * 1) without social updates, and 2) with social updates. The default initial case is for 26 * the contact with social updates which uses all possible pages. 27 */ 28 public class ContactDetailViewPagerAdapter extends PagerAdapter { 29 30 public static final String ABOUT_FRAGMENT_TAG = "view-pager-about-fragment"; 31 public static final String UPDTES_FRAGMENT_TAG = "view-pager-updates-fragment"; 32 33 private static final int INDEX_ABOUT_FRAGMENT = 0; 34 private static final int INDEX_UPDATES_FRAGMENT = 1; 35 36 private static final int MAX_FRAGMENT_VIEW_COUNT = 2; 37 38 /** 39 * The initial value for the view count needs to be MAX_FRAGMENT_VIEW_COUNT, 40 * otherwise anything smaller would break screen rotation functionality for a user viewing 41 * a contact with social updates (i.e. the user was viewing the second page, rotates the 42 * device, the view pager requires the second page to exist immediately on launch). 43 */ 44 private int mFragmentViewCount = MAX_FRAGMENT_VIEW_COUNT; 45 46 private View mAboutFragmentView; 47 private View mUpdatesFragmentView; 48 49 public ContactDetailViewPagerAdapter() { 50 } 51 52 public void setAboutFragmentView(View view) { 53 mAboutFragmentView = view; 54 } 55 56 public void setUpdatesFragmentView(View view) { 57 mUpdatesFragmentView = view; 58 } 59 60 /** 61 * Enable swiping if the detail and update fragments should be showing. Otherwise diable 62 * swiping if only the detail fragment should be showing. 63 */ 64 public void enableSwipe(boolean enable) { 65 mFragmentViewCount = enable ? MAX_FRAGMENT_VIEW_COUNT : 1; 66 notifyDataSetChanged(); 67 } 68 69 @Override 70 public int getCount() { 71 return mFragmentViewCount; 72 } 73 74 /** Gets called when the number of items changes. */ 75 @Override 76 public int getItemPosition(Object object) { 77 // Always return a valid index for the about fragment view because it's always shown 78 // whether the contact has social updates or not. 79 if (object == mAboutFragmentView) { 80 return INDEX_ABOUT_FRAGMENT; 81 } 82 // Only return a valid index for the updates fragment view if our view count > 1. 83 if (object == mUpdatesFragmentView && mFragmentViewCount > 1) { 84 return INDEX_UPDATES_FRAGMENT; 85 } 86 // Otherwise the view should have no position. 87 return POSITION_NONE; 88 } 89 90 @Override 91 public void startUpdate(View container) { 92 } 93 94 @Override 95 public Object instantiateItem(View container, int position) { 96 switch (position) { 97 case INDEX_ABOUT_FRAGMENT: 98 mAboutFragmentView.setVisibility(View.VISIBLE); 99 return mAboutFragmentView; 100 case INDEX_UPDATES_FRAGMENT: 101 mUpdatesFragmentView.setVisibility(View.VISIBLE); 102 return mUpdatesFragmentView; 103 } 104 throw new IllegalArgumentException("Invalid position: " + position); 105 } 106 107 @Override 108 public void destroyItem(View container, int position, Object object) { 109 ((View) object).setVisibility(View.GONE); 110 } 111 112 @Override 113 public void finishUpdate(View container) { 114 } 115 116 @Override 117 public boolean isViewFromObject(View view, Object object) { 118 return ((View) object) == view; 119 } 120 121 @Override 122 public Parcelable saveState() { 123 return null; 124 } 125 126 @Override 127 public void restoreState(Parcelable state, ClassLoader loader) { 128 } 129 } 130