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