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 with social updates. 26 */ 27 public class ContactDetailViewPagerAdapter extends PagerAdapter { 28 29 public static final String ABOUT_FRAGMENT_TAG = "view-pager-about-fragment"; 30 public static final String UPDTES_FRAGMENT_TAG = "view-pager-updates-fragment"; 31 32 private static final int INDEX_ABOUT_FRAGMENT = 0; 33 private static final int INDEX_UPDATES_FRAGMENT = 1; 34 35 private static final int MAX_FRAGMENT_VIEW_COUNT = 2; 36 37 private int mFragmentViewCount = MAX_FRAGMENT_VIEW_COUNT; 38 39 private View mAboutFragmentView; 40 private View mUpdatesFragmentView; 41 42 public ContactDetailViewPagerAdapter() { 43 } 44 45 public void setAboutFragmentView(View view) { 46 mAboutFragmentView = view; 47 } 48 49 public void setUpdatesFragmentView(View view) { 50 mUpdatesFragmentView = view; 51 } 52 53 /** 54 * Enable swiping if the detail and update fragments should be showing. Otherwise diable 55 * swiping if only the detail fragment should be showing. 56 */ 57 public void enableSwipe(boolean enable) { 58 mFragmentViewCount = enable ? MAX_FRAGMENT_VIEW_COUNT : 1; 59 notifyDataSetChanged(); 60 } 61 62 @Override 63 public int getCount() { 64 return mFragmentViewCount; 65 } 66 67 /** Gets called when the number of items changes. */ 68 @Override 69 public int getItemPosition(Object object) { 70 if (object == mAboutFragmentView) { 71 return INDEX_ABOUT_FRAGMENT; 72 } 73 if (object == mUpdatesFragmentView) { 74 return INDEX_UPDATES_FRAGMENT; 75 } 76 return POSITION_NONE; 77 } 78 79 @Override 80 public void startUpdate(View container) { 81 } 82 83 @Override 84 public Object instantiateItem(View container, int position) { 85 switch (position) { 86 case INDEX_ABOUT_FRAGMENT: 87 return mAboutFragmentView; 88 case INDEX_UPDATES_FRAGMENT: 89 return mUpdatesFragmentView; 90 } 91 throw new IllegalArgumentException("Invalid position: " + position); 92 } 93 94 @Override 95 public void destroyItem(View container, int position, Object object) { 96 } 97 98 @Override 99 public void finishUpdate(View container) { 100 } 101 102 @Override 103 public boolean isViewFromObject(View view, Object object) { 104 return ((View) object) == view; 105 } 106 107 @Override 108 public Parcelable saveState() { 109 return null; 110 } 111 112 @Override 113 public void restoreState(Parcelable state, ClassLoader loader) { 114 } 115 } 116